<template>
    <div class="table-page custom-table-table">
        <h3 class="main-color" style="font-size: 30px;line-height: 1;padding: 10px 0;">近30天登陆汇总</h3>
        <div style="width: 100%;height: 35%;">
            <LoginChart />
        </div>
        <div class="search-part" v-show="isSearch">
            <div class="search-cell">
                <span class="label">类型</span>
                <el-select v-model="queryParams.type" placeholder="请选择类型">
                    <el-option v-for="item in getLogType()" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </div>
            <div class="search-cell">
                <span class="label">请求时间</span>
                <el-date-picker v-model="queryParams.time" type="datetimerange" range-separator="-" start-placeholder="开始时间"
                    end-placeholder="结束时间" />
            </div>
            <el-button type="primary" icon="Search" @click="queryData(queryParams)">查询</el-button>
            <el-button icon="Refresh" @click="queryParams = {}">重置</el-button>
        </div>
        <div class="button-part" style="justify-content: space-between;">
            <el-button icon="Delete" @click="removeData()">删除</el-button>
            <TableAbility @searchBtn="isSearch = !isSearch" @refreshBtn="queryData({})" @downloadBtn="exportData('日志数据', tableData)"></TableAbility>
        </div>
        <div class="table-part">
            <el-table ref="tableRef" v-loading="loading" :data="tableData" height="100%" border show-overflow-tooltip
                @row-click="tableRowClick">
                <el-table-column type="selection" width="55" />
                <el-table-column type="index" label="#" width="55" align="center" />
                <el-table-column property="type" label="类型" width="120" align="center" />
                <el-table-column property="title" label="标题" width="120" align="center" />
                <el-table-column property="ip" label="IP地址" width="120" align="center" />
                <el-table-column property="requestMethod" label="请求方式" width="120" align="center" />
                <el-table-column label="请求时间" width="220" align="center">
                    <template #default="scope">{{ dateFormater('yyyy-MM-dd HH:mm:ss', scope.row.requestTime) }}</template>
                </el-table-column>
                <el-table-column property="operator" label="操作人" width="120" align="center" />
                <el-table-column label="操作" align="center">
                    <template #default="scope">
                        <span @click.stop>
                            <el-button link icon="EditPen" @click="viewData(scope.row)">详情</el-button>
                            <el-button link icon="Delete" @click="removeData(scope.row)">删除</el-button>
                        </span>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="pagination-part">
            <CommonPagination :total="100" @paginationChange="paginationChange" />
        </div>
    </div>
    <el-dialog v-model="isFormDialog" :title="formDialogTitle" width="40%">
        <LogForm ref="logFormRef" :type="formDialogTitle === '详情' ? 'view' : 'edit'" @close="isFormDialog = false" />
    </el-dialog>
</template>

<script lang='ts' setup>
import { onMounted, reactive, ref, toRefs, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import LoginChart from './chart/login-chart.vue'
import CommonPagination from '@/components/common-pagination.vue'
import LogForm from './form/log-form.vue'
import { tableRemoveRow, exportData } from '@/utils/table-util'
import { getLogType } from '@/static-data/core'
import { dateFormater } from '@/utils/date-util'

const router = useRouter()
const route = useRoute()

const tableRef = ref()
const logFormRef = ref()
const isSearch = ref(true)
const loading = ref(true)
const isFormDialog = ref(false)
const formDialogTitle = ref('')
const queryParams = ref({} as any)
const tableData = ref([] as any)

queryData({ type: '正常' })

function queryData(e: any) {
    loading.value = true
    tableData.value = []
    setTimeout(() => {
        while (tableData.value.length < 10) {
            tableData.value.push({
                type: e.type || '正常',
                title: e.title || '测试',
                ip: '127.0.0.1',
                requestMethod: 'post',
                requestTime: new Date(),
                operator: 'admin'
            })
        }
        loading.value = false
    }, 200);
}

const viewData = (e: any) => {
    isFormDialog.value = true
    formDialogTitle.value = '详情'
    setTimeout(() => {
        logFormRef.value.resetData()
        logFormRef.value.formData = JSON.parse(JSON.stringify(e))
    }, 0)
}
const removeData = (e?: any) => {
    const selectRow = e || tableRef.value.getSelectionRows()
    tableRemoveRow({ data: selectRow }, (res: boolean) => {
        if (res) {
            console.log('调用删除', selectRow)
        }
    })
}
const tableRowClick = (row: any) => {
    tableRef.value.toggleRowSelection(row)
}
const paginationChange = (page: number, size: number) => {

}
</script>

<style lang='scss' scoped>
.table-page.custom-table-table {
    .table-part {
        height: calc(65% - 230px);
    }
}
</style>
