<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 { delLoginLog, getLoginLogList } from '@/api/system/log'

const { proxy } = getCurrentInstance() as any
const listQuery: IlistQuery = reactive({
  keywords: '',
  beginTime: '',
  endTime: '',
  offset: 1,
  limit: 20,
  sort: '',
  order: '',
})
const columns = [
  {
    text: '日志名称',
    value: 'logName',
    width: 150,
  },
  {
    text: '用户',
    value: 'userName',
  },
  {
    text: '登录ip',
    value: 'ip',
  },
  {
    text: '操作成功与否',
    value: 'succeed',
  },
  {
    text: '操作时间',
    value: 'createTime',
  },
]
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 = true) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  getLoginLogList(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}

fetchData()

// 打开详情对话框
const detail = (row: any) => {
  ElMessageBox.alert(row.message, '详情', {
    confirmButtonText: '确定',
  })
}

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

// 查询数据
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()
}
</script>

<template>
  <div class="list-login-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/loginLog/delLoginLog')"
            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"
        />
      </table-container>
    </app-container>
  </div>
</template>

