<template>
    <div class="main-container">
        <el-card class="box-card !border-none" shadow="never">

            <div class="flex justify-between items-center">
                <span class="text-lg">{{pageName}}</span>
                <el-button type="primary" @click="addEvent">
                    {{ t('addRoles') }}
                </el-button>
            </div>

            <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
                <el-form :inline="true" :model="rolesTable.searchParam" ref="searchFormRef">
                    
                    <el-form-item :label="t('name')" prop="name">
                        <el-select class="w-[280px]" v-model="rolesTable.searchParam.name" clearable :placeholder="t('namePlaceholder')">
                            <el-option label="全部" value=""></el-option>
                            <el-option
                                v-for="(item, index) in nameList"
                                :key="index"
                                :label="item.name"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                    

                    <el-form-item>
                        <el-button type="primary" @click="loadRolesList()">{{ t('search') }}</el-button>
                        <el-button @click="resetForm(searchFormRef)">{{ t('reset') }}</el-button>
                    </el-form-item>
                </el-form>
            </el-card>

            <div class="mt-[10px]">
                <el-table :data="rolesTable.data" size="large" v-loading="rolesTable.loading">
                    <template #empty>
                        <span>{{ !rolesTable.loading ? t('emptyData') : '' }}</span>
                    </template>
                    <el-table-column prop="id" :label="t('id')" min-width="120" :show-overflow-tooltip="true"/>
                    
                    <el-table-column :label="t('name')" min-width="180" align="center" :show-overflow-tooltip="true">
                        <template #default="{ row }">
                            <div v-for="(item, index) in nameList">
                                <div v-if="item.value == row.name">{{ item.name }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    

                    <el-table-column :label="t('operation')" fixed="right" min-width="120">
                       <template #default="{ row }">
                           <el-button type="primary" link @click="editEvent(row)">{{ t('edit') }}</el-button>
                           <el-button type="primary" link @click="deleteEvent(row.id)">{{ t('delete') }}</el-button>
                       </template>
                    </el-table-column>

                </el-table>
                <div class="mt-[16px] flex justify-end">
                    <el-pagination v-model:current-page="rolesTable.page" v-model:page-size="rolesTable.limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="rolesTable.total"
                        @size-change="loadRolesList()" @current-change="loadRolesList" />
                </div>
            </div>

            
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { t } from '@/lang'
import { useDictionary } from '@/app/api/dict'
import { getRolesList, deleteRoles } from '@/addon/zhjw/api/roles'
import { img } from '@/utils/common'
import { ElMessageBox,FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title;

let rolesTable = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: true,
    data: [],
    searchParam:{
      "name":""
    }
})

const searchFormRef = ref<FormInstance>()

// 选中数据
const selectData = ref<any[]>([])

// 字典数据
    const nameList = ref([] as any[])
    const nameDictList = async () => {
    nameList.value = await (await useDictionary('roles_name')).data.dictionary
    }
    nameDictList();


/**
 * 获取角色管理列表
 */
const loadRolesList = (page: number = 1) => {
    rolesTable.loading = true
    rolesTable.page = page

    getRolesList({
        page: rolesTable.page,
        limit: rolesTable.limit,
         ...rolesTable.searchParam
    }).then(res => {
        rolesTable.loading = false
        rolesTable.data = res.data.data
        rolesTable.total = res.data.total
    }).catch(() => {
        rolesTable.loading = false
    })
}
loadRolesList()

const router = useRouter()

/**
 * 添加角色管理
 */
const addEvent = () => {
    router.push('/roles/roles_edit')
}

/**
 * 编辑角色管理
 * @param data
 */
const editEvent = (data: any) => {
    router.push('/roles/roles_edit?id='+data.id)
}

/**
 * 删除角色管理
 */
const deleteEvent = (id: number) => {
    ElMessageBox.confirm(t('rolesDeleteTips'), t('warning'),
        {
            confirmButtonText: t('confirm'),
            cancelButtonText: t('cancel'),
            type: 'warning',
        }
    ).then(() => {
        deleteRoles(id).then(() => {
            loadRolesList()
        }).catch(() => {
        })
    })
}

    

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    loadRolesList()
}
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
		word-break: break-all;
		text-overflow: ellipsis;
		overflow: hidden;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}
</style>
