<script lang="ts" setup name="ListLog">
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 {
  delLog,
  getBizLogDetail,
  getBizLogList,
  getLogType,
} 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 = ref([
  {
    text: '日志名称',
    value: 'logName',
    width: 170,
  },
  {
    text: '用户',
    value: 'userName',
    width: 100,
  },
  {
    text: '时间',
    value: 'createTime',
    width: 180,
  },
  {
    text: '详细信息',
    value: 'message',
  },
])
const list = ref([])
const total = ref(20)
const logTypeList = ref(null)
const listLoading = ref(true)
const dialogFormVisible = ref(false)
const dialogStatus = ref('')

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

fetchData(true)

// 打开详情对话框
const detail = (row: any) => {
  getBizLogDetail(row.id).then((res) => {
    if (res.code === 200) {
      ElMessageBox.alert(res.data, '详情', {
        confirmButtonText: '确定',
      })
    }
  })
}

// 清空日志
const cleanAll = () => {
  ElMessageBox.confirm('确定要清空全部日志吗？', '确认操作', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    delLog().then((response) => {
      if (response.code === 200) {
        ElMessage({
          message: '删除成功',
          type: 'success',
        })
        list.value = []
        total.value = 0
        // this.fetchData()
      }
    })
  })
}

// 查询数据
const search = () => {
  fetchData(false)
}

// 获取日志类型
const fetchLogType = () => {
  getLogType().then((response) => {
    logTypeList.value = response.data
  })
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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-log">
    <!-- 布局 -->
    <app-container>
      <!-- 筛选条件 -->
      <search-area @search="search">
        <search-item>
          <el-input
            v-model.trim="listQuery.keywords"
            placeholder="日志名称"
            clearable
          />
        </search-item>
        <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/biz/delLog')"
            class="search-btn"
            type="info"
            :icon="Delete"
            @click="cleanAll"
          >
            清空日志
          </el-button>
        </template>
      </search-area>
      <table-container>
        <!-- 查询结果Table显示 -->
        <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 link type="primary" size="small" @click="detail(scope.row)">
                  ㅤ详情ㅤㅤ
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </table-container>
    </app-container>
  </div>
</template>

<style lang="scss">
  .el-message-box {
    overflow: auto;
  }
</style>
