<template>
  <!-- 用户操作日志 -->
  <div class="leakage-cable">
    <div class="leakage-top">
      <div style="color: #666666"></div>
      <div class="operate-btn">
        <el-button type="primary" @click="refresh">刷新</el-button>
        <el-button type="primary" @click="isQuery = !isQuery">查询</el-button>
        <el-button type="primary" @click="exportLog">导出</el-button>
      </div>
    </div>
    <div v-if="isQuery">
      <search @search="search" ref="reset" />
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      class="statistics-table"
      :row-class-name="tableRowClassName"
      :row-style="{ height: '50px' }"
      :header-cell-style="{ background: '#EAF1FE', color: '#666666' }"
    >
      <el-table-column prop="userName" label="用户名" align="center" />
      <el-table-column prop="operateTypeName" label="操作类别" align="center" />
      <el-table-column prop="operateObjName" label="操作对象" align="center" />
      <el-table-column prop="objectName" label="对象名称" align="center" />
      <el-table-column prop="operateContent" label="操作内容" align="center" />
      <el-table-column
        prop="operateResultName"
        label="操作结果"
        align="center"
      />
      <el-table-column prop="creationTime" label="操作时间" align="center" />
    </el-table>
    <Pagination
      :limit="params.size"
      :page="params.current"
      :total="total"
      class="pagination"
      @pagination="handlePageChange"
    />
  </div>
</template>
<script>
import { logList } from "../api";
import search from "./components/search.vue";
import { exportLog } from "@/api/export";
import download from "@/utils/download";
import { successAlert, warningAlert } from "@/utils/alert";

export default {
  data() {
    return {
      params: {
        current: 1,
        size: 10,
      },
      total: 10,
      tableData: [],
      isQuery: false,
      istrue: 0,
      searchOption: {},
      exids: [],
    };
  },
  components: {
    search,
  },
  methods: {
    handlePageChange(pageData) {
      this.params.size = pageData.size;
      this.params.current = pageData.page;
      this.getTableData();
    },
    refresh() {
      this.$refs.reset != undefined
        ? this.$refs.reset.reset()
        : this.getTableData();
    },
    search(option) {
      this.params.current = 1;
      this.istrue = 1;
      this.searchOption = option;
      this.getTableData();
    },
    getTableData() {

      let params = {
        ...this.params,
        ...this.searchOption,
      };
      logList(params).then((res) => {
        let list = res.records || [];
        this.tableData = list;
        this.total = res.total;
        this.exids = list.map((i) => i.id);
        if (this.istrue == 1) {
          if (this.tableData.length != 0) {
            successAlert("操作成功");
          } else {
            warningAlert("查询结果为空");
          }
          this.istrue = 0;
        }
      });
    },
    tableRowClassName({ row, rowIndex }) {
      return rowIndex % 2 === 0 ? "" : "single-row";
    },
    exportLog() {
      if (this.exids.length == 0) {
        this.$message.warning("暂无数据");
        return false;
      } else {
        exportLog({ ids: this.exids }).then((res) => {
          download(res, "vnd.ms-excel", `用户日志.xls`);
        });
      }
    },
  },
  mounted() {
    this.getTableData();
  },
};
</script>
<style lang="scss" scoped>
.leakage-cable {
  .leakage-top {
    margin-bottom: 20px;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }
  & ::v-deep .cell {
    color: #333333;
  }
  .page {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 20px 0;
    .current {
      margin: 0 20px;
    }
  }
}
</style>
<style lang="scss">
.statistics-table {
  .single-row {
    background: #f1f6ff;
  }
  td {
    padding: 5px !important;
  }
}
</style>