<template>
  <div class="action-log-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="操作类型：">
            <el-select
              v-model="filter.operationType"
              placeholder="操作类型"
              clearable
            >
              <el-option
                v-for="(val, key) in OPERATION_TYPE"
                :key="key"
                :value="key"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="操作人账号：">
            <el-input
              v-model.trim="filter.operatorName"
              placeholder="操作人账号"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="操作时间：">
            <el-date-picker
              v-model="createTime"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="timestamp"
              align="right"
              unlink-panels
            >
            </el-date-picker>
          </el-form-item>
        </template>
        <el-form-item label-width="0px">
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="operatorName" label="操作人" width="180">
          <span slot-scope="scope">
            {{ scope.row.loginName }}（{{ scope.row.realName }}）
          </span>
        </el-table-column>
        <el-table-column prop="privilegeName" label="菜单名称" width="120">
        </el-table-column>
        <el-table-column prop="operationTypeName" label="操作类型" width="100">
        </el-table-column>
        <el-table-column prop="ip" label="IP" width="130"> </el-table-column>
        <el-table-column prop="createTime" label="操作时间" width="170">
          <span slot-scope="scope">
            {{ scope.row.createTime | timestampFilter }}
          </span>
        </el-table-column>
        <el-table-column prop="runStatus" label="结果" width="60">
        </el-table-column>
        <el-table-column prop="detail" label="日志内容"> </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="80px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toViewArgs(scope.row)"
              >参数</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      :visible.sync="modalIsShow"
      title="日志参数"
      top="10vh"
      width="650px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
    >
      <p style="height: 400px; overflow-y: auto; overflow-x: hidden">
        {{ curRow.args }}
      </p>

      <div slot="footer">
        <el-button @click="modalIsShow = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { actionLogList } from "../api";
import { getEnums } from "../../base/api";

import pickerOptions from "@/constants/datePickerOptions";

export default {
  name: "action-log-manage",
  data() {
    return {
      filter: {
        operationType: "",
        operatorName: "",
        startTime: "",
        endTime: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      OPERATION_TYPE: {},
      loading: false,
      pickerOptions,
      createTime: [],
      modalIsShow: false,
    };
  },
  async created() {
    await this.getLogTypes();
    await this.getList();
  },
  methods: {
    async getLogTypes() {
      const res = await getEnums("CUSTOMIZED_OPERATION_TYPE_ENUM");
      const data = res || [];
      this.OPERATION_TYPE = {};
      data.forEach((item) => {
        this.OPERATION_TYPE[item.code] = item.name;
      });
    },
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      if (this.createTime) {
        datas.startTime = this.createTime[0];
        datas.endTime = this.createTime[1];
      }
      const data = await actionLogList(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toViewArgs(row) {
      this.curRow = row;
      this.modalIsShow = true;
    },
  },
};
</script>
