<!-- 管理员管理-管理员角色管理 -->
<template>
    <app-view>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['admin_role_add'])" @click="openComponent" />
            </template>
            <!-- 创建时间 -->
            <template #modifytime="{ value }">
                {{ formatDate(value) }}
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['admin_role_modify', 'admin_role_delete'])"
                    @click="(code: string) => openComponent(code, row)" circle />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onRefresh" />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ record }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { queryRole } from '@/services/api/admin'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'

// 状态
const userstatusEnum = useEnum('userstatus')

const { componentMap, componentId, record, openComponent, closeComponent, getActionButtons } = useOperation<Model.RoleRsp>({
    onClose: () => onRefresh()
})

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(queryRole, {
    params: {
        type: 2,
        pageNum: 1,
        pageSize: 20
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'rolename', label: 'admin.role.rolename' },
    { field: 'modifiername', label: 'admin.role.modifiername' },
    { field: 'modifytime', label: 'admin.role.modifytime' },
    { field: 'rolestatus', label: 'admin.role.rolestatus', formatValue: (val) => userstatusEnum.getEnumTypeName(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const onRefresh = () => {
    run()
}
</script>