<script lang="ts" setup name="LoginLog">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Delete } from '@element-plus/icons-vue'
import type { IlistQuery } from './log-interface'
import {
  delErrorLog,
  getErrorLogDetail,
  getErrorLogList,
} from '@/api/system/log'
const { proxy } = getCurrentInstance() as any
const listQuery: IlistQuery = reactive({
  keywords: '', // 日志名称
  beginTime: '', // 开始时间
  endTime: '', // 结束时间
  logType: '异常日志', // 日志类型
  offset: 1,
  limit: 20,
  sort: '',
  order: '',
})
const columns = [
  { text: '用户', value: 'userName', width: '150' },
  { text: '时间', value: 'createTime', width: '180' },
  { text: '详细信息', value: 'message' },
] // 显示列
const list = ref([]) // 数据列表
const total = ref(0)
const listLoading = ref(true)

// 获取日志数据
const fetchData = (isNowPage: boolean) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  getErrorLogList(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData(true)

// 打开详情对话框
const detail = (row: any) => {
  getErrorLogDetail(row.id).then((res) => {
    if (res.code === 200) {
      ElMessageBox.alert(res.data, '详情', {
        confirmButtonText: '确定',
        customClass: 'log-info',
      })
    }
  })
}
// 清空日志
const cleanAll = () => {
  ElMessageBox.confirm('确定要清空全部异常日志吗？', '确认操作', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    delErrorLog().then((response) => {
      if (response.code === 200) {
        ElMessage({
          message: '删除成功',
          type: 'success',
        })
        list.value = []
        total.value = 0
      }
    })
  })
}
// 查询数据
const search = () => {
  fetchData(false)
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData(true)
}
</script>

<template>
  <div class="list-erroe-log">
    <app-container>
      <search-area @search="search">
        <search-item>
          <el-date-picker
            v-model="listQuery.beginTime"
            type="datetime"
            value-format="YYYY/MM/DD HH:mm:ss"
            placeholder="选择开始时间"
          />
        </search-item>
        <search-item>
          <el-date-picker
            v-model="listQuery.endTime"
            type="datetime"
            value-format="YYYY/MM/DD HH:mm:ss"
            placeholder="选择结束时间"
          />
        </search-item>
        <template #btns>
          <el-button
            v-if="proxy.hasPerm('/sys/log/error/delLog')"
            class="search-btn"
            type="info"
            :icon="Delete"
            @click="cleanAll"
          >
            清空日志
          </el-button>
        </template>
      </search-area>
      <table-container>
        <normal-table
          :data="list"
          :total="total"
          :columns="columns"
          :query="listQuery"
          :list-loading="listLoading"
          @change="changePage"
        >
          <template #columns>
            <el-table-column label="操作" width="70" align="center">
              <template #default="scope">
                <el-button type="text" size="small" @click="detail(scope.row)">
                  详情ㅤㅤ
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </table-container>
    </app-container>
  </div>
</template>

<style lang="scss">
.log-info {
  width: 800px;
}
</style>

