<!-- 管理员管理-管理员管理 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons">
                <el-select filterable v-if="false">
                    <el-option v-for="item in selectList" :key="item.userid" :label="item.accountname"
                        :value="item.userid" />
                </el-select>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['admin_user_add'])" @click="openComponent" />
            </template>
            <!-- 创建时间 -->
            <template #modifytime="{ value }">
                {{ formatDate(value) }}
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="handleOperateButtons(row)"
                    @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="onSearch" />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ componentId, record }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/filters'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable'
import { useOperation } from '@/hooks/operation'
import { AdminStatus, getAdminStatusName, getAdminStatusList } from '@/constants/admin'
import { querySystemmanager } from '@/services/api/admin'
import { queryOrganSelect } from '@/services/api/common'
import { useUserStore, i18n } from '@/stores'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'

const { global: { t } } = i18n

const userStore = useUserStore()

// 是否显示树形下拉框
const isTreeSelect = computed(() => {
    const { releType, orgztypes } = userStore.userInfo
    const types = orgztypes.split(',')
    return ((releType === 2 && types.includes('7')) || releType === 3)
})

const { filterOptons, getQueryParams } = useDataFilter<Model.SystemmanagerReq>()

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

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

const { dataList: selectList } = useRequest(queryOrganSelect, {
    params: {
        pageSize: 10,
        usertype: isTreeSelect.value ? '2,3' : undefined
    }
})

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'autoid', label: 'ID' },
    { field: 'logincode', label: 'admin.user.logincode' },
    { field: 'username', label: 'admin.user.username' },
    { field: 'areaName', label: 'admin.user.areaName' },
    { field: 'roleName', label: 'admin.user.roleName' },
    { field: 'managerstatus', label: 'admin.user.managerstatus', formatValue: (val) => getAdminStatusName(val) },
    { field: 'modifytime', label: 'admin.user.modifytime' },
    { field: 'operate', label: 'common.operate', fixed: 'right', width: 150 }
])

filterOptons.selectList = [
    {
        label: t('admin.user.status'),
        key: 'managerStatus',
        options: () => getAdminStatusList()
    }
]

filterOptons.inputList = [
    {
        label: t('admin.user.loginCode'), keys: ['loginCode']
    }
]

filterOptons.buttonList = [
    { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
    { label: t('operation.reset'), className: 'el-button--primary', onClick: () => onSearch(true) }
]

// 处理操作按钮
const handleOperateButtons = (row: Model.SystemmanagerRsp) => {
    if (row.logincode === userStore.userInfo.logincode) return []
    switch (row.managerstatus) {
        case AdminStatus.Disable:
            return getActionButtons(['admin_user_enable'])
        case AdminStatus.Lock:
            return getActionButtons(['admin_user_unlock'])
        default:
            return getActionButtons(['admin_user_modify', 'admin_user_reset', 'admin_user_disable'])
    }
}

const onSearch = (clear = false) => {
    const qs = getQueryParams(clear)
    getSystemmanager(qs)
}
</script>