<template>
    <div class="default-main">
        <div v-show="flg">
            <TableHeader datePicker showExport ref="TableHeaderRef" showCustomColumn>
                <template v-slot:select>
                    <el-form :inline="true" label-width="90px" class="">
                        <el-form-item label="用户名:">
                            <el-select
                                v-model="tableStore.table.params.loginName"
                                placeholder="用户名"
                                clearable
                                filterable
                            >
                                <el-option
                                    v-for="item in userName"
                                    :key="item.id"
                                    :label="item.loginName"
                                    :value="item.loginName"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="事件类型:">
                            <el-select v-model="tableStore.table.params.type" placeholder="事件类型" clearable filterable>
                                <el-option
                                    v-for="item in eventType"
                                    :key="item.id"
                                    :label="item.label"
                                    :value="item.id"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="操作类型:">
                            <el-select v-model="tableStore.table.params.operateType" placeholder="操作类型" clearable filterable>
                                <el-option
                                    v-for="item in operationType"
                                    :key="item.value"
                                    :label="item.value"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="操作结果:">
                            <el-select v-model="tableStore.table.params.result" placeholder="操作结果" clearable filterable>
                                <el-option
                                    v-for="item in resultList"
                                    :key="item.id"
                                    :label="item.label"
                                    :value="item.id"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                </template>
                <template #operation>
                    <el-button type="primary" @click="backups" :icon="DocumentCopy">文件备份</el-button>
                    <el-button type="primary" @click="details" :icon="Tickets">统计</el-button>
                </template>
            </TableHeader>
            <Table ref="tableRef" />
        </div>
        <div v-if="!flg">
            <Statistics @back="onSubmit" ref="statistics" />
        </div>
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted, provide, nextTick } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { getAllUserList, logFileWriter, recoverLogFile, getMemoInfo, getSysConfig } from '@/api/auditManage/auditList'
import { ElMessage } from 'element-plus'
import Statistics from './statistics.vue'
import { DocumentCopy, Tickets } from '@element-plus/icons-vue'

defineOptions({
    name: 'BusinessAdministrator/Audit/Operations/Management'
})

interface UserInfo {
    id: number | string
    loginName: string
}

const userName = ref<UserInfo[]>([])
const TableHeaderRef = ref()

// 其他响应式数据
const loading = ref(false)
const statistics = ref()

const flg = ref(true)
const zoom = ref('') //图表焦点校验

const resultList = ref([
    { id: 1, label: '成功' },
    { id: 0, label: '失败' }
])

const eventType = ref([
    {
        id: 0,
        label: '业务事件类型'
    },
    {
        id: 1,
        label: '系统事件类型'
    }
]) //事件类型
const operationType = ref([
    { id: 1, value: '查询' },
    { id: 2, value: '新增' },
    { id: 3, value: '更新' },
    { id: 4, value: '删除' },
    { id: 5, value: '认证' },
    { id: 6, value: '注销' },
    { id: 7, value: '上传' },
    { id: 8, value: '下载' },
    { id: 9, value: '越权访问' }
]) //操作类型

const statisticsTotal = ref(undefined)

const tableStore: any = new TableStore({
    url: '/system-boot/audit/getAuditLog',
    method: 'POST',
    column: [
        {
            field: 'create_time',
            title: '操作时间',
            sortable: true,
            formatter: (row: any) => {
                return row.row.time
            }
        },
        { field: 'userName', title: '操作人员' },
        {
            field: 'operate_type',
            title: '操作类型',
            sortable: true,
            formatter: (row: any) => {
                return row.row.operate
            }
        },
        { field: 'describe', title: '事件描述' },
        { field: 'type', title: '事件类型', sortable: true },
        { field: 'result', title: '操作结果', sortable: true },
        { field: 'ip', title: '操作IP' ,width:'120px'},
        { field: 'level', title: '事件等级', sortable: true }
    ],
    loadCallback: () => {
        getLogSize()
    }
})

tableStore.table.params.loginName = ''
tableStore.table.params.type = ''
tableStore.table.params.operateType = ''
tableStore.table.params.result = null
tableStore.table.params.sortBy = ''
tableStore.table.params.orderBy = ''
tableStore.table.params.pageSize = 100

provide('tableStore', tableStore)

onMounted(() => {
    search()
    onSubmit()
})

//下拉框查询
const search = () => {
    getAllUserList().then(res => {
        userName.value = res.data
    })
}
// 统计
const details = () => {
    flg.value = false
    nextTick(() => {
        statistics.value.tableStore.index()
    })
}

// 查询
const onSubmit = async () => {
    flg.value = true
    await tableStore.index()
}

// 查询日志大小
const getLogSize = () => {
    getMemoInfo().then(res => {
        getSysConfig().then(re => {
            if (res.data > re.data.logSize) {
                ElMessage({
                    showClose: true,
                    duration: 0,
                    message: '日志表存储空间已超过' + re.data.logSize + '(M)请及时备份清理!',
                    type: 'warning'
                })
            }
        })
    })
}

// 备份
const backups = async () => {
    loading.value = true
    await logFileWriter({}).then(res => {})
    setTimeout(() => {
        ElMessage({
            type: 'success',
            message: '文件备份成功'
        })
        loading.value = false
    }, 0)
}

// 恢复
const restore = () => {
    loading.value = true
    recoverLogFile({}).then(res => {})
    setTimeout(() => {
        ElMessage({
            type: 'success',
            message: '文件恢复成功'
        })
        loading.value = false
    }, 50000)
}
</script>
