<template>
  <div class="__normal-page">
    <div class="__normal-content">
      <CusContent
          v-model:tableHead="tableHead"
          @handleReset="handleReset"
          @handleSearch="onSearch"
      >
        <template #fieldOut>
          <CusForm labelWidth="100px" @handleEnter="onSearch">
            <CusFormColumn
                label="操作人:"
                v-model:param="queryForm.operName"/>
            <CusFormColumn
                label="操作人单位:"
                link="select"
                v-model:param="queryForm.deptId"
                :options="$store.state.dictionary.deptList"/>
            <CusFormColumn
                label="操作模块:"
                link="select"
                v-model:param="queryForm.operModule"
                :options="$store.state.dictionary.operModuleList"/>
            <CusFormColumn
                label="操作类型:"
                link="select"
                v-model:param="queryForm.operType"
                :options="$store.state.dictionary.operTypeList"/>
<!--            <CusFormColumn-->
<!--                label="操作对象:"-->
<!--                v-model:param="queryForm.shipId"/>-->
<!--            <CusFormColumn-->
<!--                label="操作详情:"-->
<!--                v-model:param="queryForm.shipId"/>-->
            <CusFormColumn
                label="操作时间:"
                link="datetime"
                type="datetimerange"
                v-model:param="queryForm.operationDate"/>
            <CusSearchButtons
                @handleReset="handleReset"
                @handleSearch="onSearch"
            />
          </CusForm>
        </template>
        <template #buttons>
          <div class="__cus-button_submit __hover" @click="onExport">
            <SvgIcon name="export" size="16"/>导出
          </div>
        </template>
        <template #table>
          <CusTable
              v-loading="loading"
              ref="ref_cusTable"
              :tableData="queryResult.tableData"
              :tableHead="tableHead"
              :total="queryResult.total"
              :page="queryPage.pageNum"
              :pageSize="queryPage.pageSize"
              v-model:selected="selected"
              @handlePage="handlePage"
          >
            <template #operModule-column-value="{ scope }">
              {{$store.state.dictionary.operModuleMap.get(scope.row.operModule)}}
            </template>
            <template #operType-column-value="{ scope }">
              {{$store.state.dictionary.operTypeMap.get(scope.row.operType)}}
            </template>
          </CusTable>
        </template>
      </CusContent>
    </div>
  </div>
</template>

<script lang="ts">
import {
  defineComponent,
  computed,
  onMounted,
  ref,
  reactive,
  watch,
  getCurrentInstance,
  ComponentInternalInstance,
  toRefs,
  nextTick
} from 'vue'
import {useStore} from 'vuex'
import {useRouter, useRoute} from 'vue-router'
import {getOperationLogList, operLogExport} from "@/api/modules/oper-log";
import {getDeptList} from "@/api/modules/dept";
import {ElMessage} from "element-plus";
import {downLoadBlob} from "@/utils/downLoadUrl";

export default defineComponent({
  name: '',
  components: {},
  setup(props, {emit}) {
    const store = useStore();
    const router = useRouter();
    const route = useRoute();
    const that = (getCurrentInstance() as ComponentInternalInstance).appContext.config.globalProperties
    const state = reactive({
      //  加载中
      loading: false,
      //  查询分页参数
      queryPage: {
        pageNum: 1,
        pageSize: 10
      },
      //  查询结果
      queryResult: {
        total: 0,
        tableData: []
      },
      //  查询表单参数
      queryForm: <any>{},
      //  查询表单参数备份
      back_queryForm: {},
      //  表格表头
      tableHead: [
        {value: "operName", label: "操作人", show: true},
        {value: "deptName", label: "操作人单位", show: true},
        {value: "operIp", label: "设备IP", show: true},
        {value: "operModule", label: "操作模块", show: true},
        {value: "operType", label: "操作类型", show: true},
        // {value: "p1", label: "操作对象", show: true},
        // {value: "p1", label: "操作详情", show: true},
        {value: "operTime", label: "操作时间", show: true},
      ],
      selected: []
    });
    const ref_cusTable = ref()
    //  获取字典
    const initDictionary = () => {
      store.dispatch('dictionary/LOAD_DEPT')
      store.dispatch('dictionary/LOAD_DICT_LIST', 'oper_module')
      store.dispatch('dictionary/LOAD_DICT_LIST', 'oper_type')
    }
    //  查询分页参数改变处理方法
    const handlePage = ({page, pageSize}: any) => {
      state.queryPage.pageNum = page
      state.queryPage.pageSize = pageSize
      handleSearch(page, pageSize)
    }
    //  重置查询表单方法
    const handleReset = () => {
      state.queryForm = {}
      state.back_queryForm = JSON.parse(JSON.stringify(state.queryForm))
      handleSearch()
    }
    //  查询方法
    const handleSearch = (page = 1, pageSize = 10) => {
      //  添加分页参数
      const queryParams: any = {
        pageNum: page,
        pageSize: pageSize,
      }
      //  添加表单参数
      for (const [k, v] of Object.entries(state.back_queryForm)) {
        if (that.$util.isValue(v)) {
          if (k === 'operationDate') {
            queryParams['beginTime'] = v[0]
            queryParams['endTime'] = v[1]
          } else {
            queryParams[k] = v
          }
        }
      }
      state.loading = true
      that.$api.getOperationLogList(queryParams).then((res: any) => {
        if (res.code === 200) {
          state.queryResult.tableData = res.rows
          state.queryResult.total = res.total
        } else {
          ElMessage.error(res.message)
        }
        state.loading = false
      }).catch(() => {
        state.loading = false
      })
    }
    //  点击查询按钮后
    const onSearch = () => {
      ref_cusTable.value.resetFilter()
      state.back_queryForm = JSON.parse(JSON.stringify(state.queryForm))
      state.queryPage.pageNum = 1
      handleSearch()
    }
    const onExport = () => {
      const queryParams: any = {
      }
      if (state.selected.length > 0) {
        queryParams['ids'] = state.selected.map(v => v.id)
      } else {
        //  添加表单参数
        for (const [k, v] of Object.entries(state.back_queryForm)) {
          if (that.$util.isValue(v)) {
            if (k === 'operationDate') {
              queryParams['beginTime'] = v[0]
              queryParams['endTime'] = v[1]
            } else {
              queryParams[k] = v
            }
          }
        }
      }
      that.$api.operLogExport(queryParams).then(res => {
        downLoadBlob(res, '操作记录.xlsx')
        ElMessage.success('导出成功！')
      }).catch(() => {
        ElMessage.error('导出失败！')
      })
    }
    onMounted(() => {
      state.back_queryForm = JSON.parse(JSON.stringify(state.queryForm))
      initDictionary()
      handleSearch()
    })
    return {
      ref_cusTable,
      ...toRefs(state),
      handleSearch,
      handlePage,
      handleReset,
      onSearch,
      onExport
    }
  },
})
</script>

<style scoped lang="scss">
</style>
