<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      :columns="columns"
      row-key="operId"
      :request-api="listOperlogApi"
      :init-param="initParam"
      :data-callback="dataCallback"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" v-auth="['monitor:operlog:add']" :icon="CirclePlus" @click="openDialog(1, '操作日志记录新增')">
          新增
        </el-button>
        <el-button type="primary" v-auth="['monitor:operlog:import']" :icon="Upload" plain @click="batchAdd"> 导入 </el-button>
        <el-button type="primary" v-auth="['monitor:operlog:export']" :icon="Download" plain @click="downloadFile">
          导出
        </el-button>
        <el-button
          type="danger"
          v-auth="['system:user:remove']"
          :icon="Delete"
          plain
          :disabled="!scope.isSelected"
          @click="batchDelete(scope.selectedListIds)"
        >
          批量删除
        </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button
          type="primary"
          link
          :icon="View"
          v-auth="['monitor:operlog:query']"
          @click="openDialog(3, '操作日志记录查看', scope.row)"
        >
          查看
        </el-button>
        <el-button
          type="primary"
          link
          :icon="EditPen"
          v-auth="['monitor:operlog:edit']"
          @click="openDialog(2, '操作日志记录编辑', scope.row)"
        >
          编辑
        </el-button>
        <el-button type="primary" link :icon="Delete" v-auth="['monitor:operlog:remove']" @click="deleteOperlog(scope.row)">
          删除
        </el-button>
      </template>
    </ProTable>
    <FormDialog ref="formDialogRef" />
    <ImportExcel ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="Operlog">
import { ref, reactive } from 'vue'
import { useHandleData } from '@/hooks/useHandleData'
import { useDownload } from '@/hooks/useDownload'
import { ElMessageBox } from 'element-plus'
import ProTable from '@/components/ProTable/index.vue'
import ImportExcel from '@/components/ImportExcel/index.vue'
import FormDialog from '@/components/DialogOld/form.vue'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import { Delete, EditPen, Download, Upload, View, CirclePlus } from '@element-plus/icons-vue'
import {
  listOperlogApi,
  delOperlogApi,
  addOperlogApi,
  updateOperlogApi,
  importTemplateApi,
  importDataApi,
  exportApi,
  getOperlogApi
} from '@/api/modules/monitor/operlog'

// ProTable 实例
const proTable = ref<ProTableInstance>()

// 如果表格需要初始化请求参数，直接定义传给 ProTable (之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({ type: 1 })

// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 list && total && pageNum && pageSize 这些字段，可以在这里进行处理成这些字段
const dataCallback = (data: any) => {
  return data
}

// 删除操作日志记录信息
const deleteOperlog = async (params: any) => {
  await useHandleData(delOperlogApi, { id: [params.operId] }, `删除【${params.id}】操作日志记录`)
  proTable.value?.getTableList()
}

// 批量删除操作日志记录信息
const batchDelete = async (id: string[]) => {
  await useHandleData(delOperlogApi, { id }, '删除所选操作日志记录信息')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

// 导出操作日志记录列表
const downloadFile = async () => {
  ElMessageBox.confirm('确认导出操作日志记录数据?', '温馨提示', { type: 'warning' }).then(() =>
    useDownload(exportApi, '操作日志记录列表', proTable.value?.searchParam)
  )
}

// 批量添加操作日志记录
const dialogRef = ref<InstanceType<typeof ImportExcel> | null>(null)
const batchAdd = () => {
  const params = {
    title: '操作日志记录',
    tempApi: importTemplateApi,
    importApi: importDataApi,
    getTableList: proTable.value?.getTableList
  }
  dialogRef.value?.acceptParams(params)
}

const formDialogRef = ref<InstanceType<typeof FormDialog> | null>(null)
// 打开弹框的功能
const openDialog = async (type: number, title: string, row?: any) => {
  let res = { data: {} }
  if (row?.operId) {
    res = await getOperlogApi(row?.operId || null)
  }
  // 重置表单
  setFieldList()
  const params = {
    title,
    width: 580,
    isEdit: type !== 3,
    fieldList: fieldList,
    model: type == 1 ? {} : res.data,
    api: type == 1 ? addOperlogApi : updateOperlogApi,
    getTableList: proTable.value?.getTableList
  }
  formDialogRef.value?.openDialog(params)
}

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  { prop: 'operId', label: '日志主键' },
  {
    prop: 'title',
    label: '模块标题',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'businessType',
    label: '业务类型',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'method',
    label: '方法名称',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'requestMethod',
    label: '请求方式',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'operatorType',
    label: '操作类别',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'operName',
    label: '操作人员',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'deptName',
    label: '部门名称',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'operUrl',
    label: '请求URL',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'operIp',
    label: '主机地址',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'operLocation',
    label: '操作地点',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'operParam',
    label: '请求参数',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'jsonResult',
    label: '返回参数',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'status',
    label: '操作状态',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'errorMsg',
    label: '错误消息',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'operTime',
    label: '操作时间',
    search: {
      el: 'date-picker',
      props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
    },
    width: 120
  },
  {
    prop: 'costTime',
    label: '消耗时间',
    search: {
      el: 'input'
    },
    width: 120
  },
  { prop: 'operation', label: '操作', width: 230, fixed: 'right' }
])
// 表单配置项
let fieldList: Form.FieldItem[] = []
const setFieldList = () => {
  fieldList = [
    {
      label: '模块标题',
      field: 'title',
      placeholder: '请输入模块标题'
    },
    {
      label: '业务类型',
      field: 'businessType',
      placeholder: '请输入业务类型'
    },
    {
      label: '方法名称',
      field: 'method',
      placeholder: '请输入方法名称'
    },
    {
      label: '请求方式',
      field: 'requestMethod',
      placeholder: '请输入请求方式'
    },
    {
      label: '操作类别',
      field: 'operatorType',
      placeholder: '请输入操作类别'
    },
    {
      label: '操作人员',
      field: 'operName',
      placeholder: '请输入操作人员'
    },
    {
      label: '部门名称',
      field: 'deptName',
      placeholder: '请输入部门名称'
    },
    {
      label: '请求URL',
      field: 'operUrl',
      placeholder: '请输入请求URL'
    },
    {
      label: '主机地址',
      field: 'operIp',
      placeholder: '请输入主机地址'
    },
    {
      label: '操作地点',
      field: 'operLocation',
      placeholder: '请输入操作地点'
    },
    {
      label: '请求参数',
      field: 'operParam',
      placeholder: '请输入请求参数'
    },
    {
      label: '返回参数',
      field: 'jsonResult',
      placeholder: '请输入返回参数'
    },
    {
      label: '操作状态',
      field: 'status',
      placeholder: '请输入操作状态'
    },
    {
      label: '错误消息',
      field: 'errorMsg',
      placeholder: '请输入错误消息'
    },
    {
      label: '操作时间',
      field: 'operTime',
      type: 'date-picker',
      placeholder: '请选择操作时间'
    },
    {
      label: '消耗时间',
      field: 'costTime',
      placeholder: '请输入消耗时间'
    }
  ]
}
</script>
