<template>
  <div class="part-box is-filter">
    <el-form inline>
      <el-form-item label="登录名">
        <el-input
          v-model.trim="searchModel.loginName"
          placeholder="请输入"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item label="用户类型">
        <el-select
          v-model="searchModel.marker"
          placeholder="请选择"
          clearable
          style="width: 120px"
        >
          <el-option label="评卷员" :value="true" />
          <el-option label="管理员" :value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="操作类型">
        <el-select
          v-model="searchModel.type"
          placeholder="请选择"
          clearable
          style="width: 120px"
        >
          <el-option
            v-for="(val, key) in LOG_TYPE"
            :key="key"
            :label="val"
            :value="key"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="toPage(1)">查询</el-button>
        <el-button @click="onExport">导出</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="part-box">
    <el-table
      class="page-table"
      :data="dataList"
      :loading="loading"
      border
      stripe
    >
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="loginName" label="登录名" min-width="120" />
      <el-table-column prop="marker" label="用户类型" width="100">
        <template #default="scope">
          {{ scope.row.marker ? '评卷员' : '管理员' }}
        </template>
      </el-table-column>
      <el-table-column prop="type" label="操作类型" width="100">
        <template #default="scope">
          {{ dictFilter.logType(scope.row.type) }}
        </template>
      </el-table-column>
      <el-table-column prop="ipAddress" label="登录IP" width="160" />
      <el-table-column prop="menu" label="功能模块" min-width="100" />
      <el-table-column prop="createTime" label="操作时间" width="180">
        <template #default="scope">
          {{ timestampFilter(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="description" label="详情" min-width="200" />
    </el-table>
    <el-pagination
      v-model:current-page="pagination.pageNumber"
      v-model:page-size="pagination.pageSize"
      :layout="pagination.layout"
      :total="pagination.total"
      @size-change="pageSizeChange"
      @current-change="toPage"
    />
  </div>
</template>

<script setup lang="ts">
  import { reactive } from 'vue';
  import { getLogList } from '@/api/log';
  import { LogItem, LogListFilter } from '@/api/types/log';
  import useTable from '@/hooks/table';
  import { LOG_TYPE } from '@/constants/enumerate';
  import { dictFilter, timestampFilter } from '@/utils/filter';
  import { downloadExport } from '@/utils/download-export';

  defineOptions({
    name: 'LogManage',
  });

  const searchModel = reactive<LogListFilter>({
    loginName: '',
    marker: undefined,
    type: undefined,
  });

  const { dataList, pagination, loading, toPage, pageSizeChange } =
    useTable<LogItem>(getLogList, searchModel, true);

  function onExport() {
    downloadExport('exportLog', searchModel);
  }
</script>
