<!-- 系统运行管理-管理员操作日志 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" />
        </template>
        <app-table :data="dataList" showIndex :columns="tableColumns" :loading="loading">
            <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="{ record }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable'
import { queryLog } from '@/services/api/system'
import { useOperation } from '@/hooks/operation'
import { useUserStore } from '@/stores'
import { formatDate } from '@/filters'
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 { i18n } from '@/stores'

const { global: { t } } = i18n

const { filterOptons, getQueryParams } = useDataFilter<Model.SystemLogReq>()
const userStore = useUserStore()

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'resourcecode', label: 'system.operation_log.resourcecode' },
    { field: 'resourcename', label: 'system.operation_log.resourcename' },
    { field: 'logincode', label: 'system.operation_log.logincode' },
    { field: 'username', label: 'system.operation_log.username' },
    { field: 'areaname', label: 'system.operation_log.areaname' },
    { field: 'content', label: 'system.operation_log.content' },
    { field: 'operatetime', label: 'system.operation_log.operatetime', formatValue: (val) => formatDate(val) }
])

filterOptons.inputList = [
    {
        label: t('system.operation_log.tips3'), placeholder: t('system.operation_log.tips1'), keys: ['userName']
    },
    {
        label: t('system.operation_log.tips4'), placeholder: t('system.operation_log.tips2'), keys: ['content']
    }
]

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

const onSearch = (clear = false) => {
    const qs = getQueryParams(clear)
    run(qs)
}

</script>