<!-- 系统运行管理-登录异常流水查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" />
        </template>
        <app-table :data="dataList" showIndex :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['system_loginfailed_export'])"
                    @click="openComponentOnClick" />
            </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="{ record, queryParams }" @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 { queryError } from '@/services/api/system'
import { useOperation } from '@/hooks/operation'
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 AppOperation from '@pc/components/base/operation/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n

const queryParams = shallowRef<Model.SystemErrorReq>()
const { filterOptons, getQueryParams } = useDataFilter<Model.SystemErrorReq>()

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'loginaccount', label: 'system.login_failed.loginaccount' },
    { field: 'operatetime', label: 'system.login_failed.operatetime', formatValue: (val) => formatDate(val) },
    { field: 'lienttype', label: 'system.login_failed.lienttype' },
    { field: 'loginip', label: 'system.login_failed.loginip' },
    { field: 'description', label: 'system.login_failed.description' }
])

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

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

// 处理请求参数
const processRequiredParams = (callback: (params: Model.SystemErrorReq) => void, clear = false) => {
    const qs = getQueryParams(clear)
    queryParams.value = qs
    callback(qs)
}

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

const openComponentOnClick = (code: string) => {
    processRequiredParams(() => openComponent(code))
}

</script>