<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-left">
        <div class="filter-item">操作内容：
          <el-input v-model="operation" placeholder="请输入操作内容" style="width: 140px"></el-input>
        </div>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList()">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <!-- <el-divider></el-divider> -->
    <el-table
      :data="tableData"
      stripe
      style="width: 100%;margin-top:10px;"
      id="out-table"
      @selection-change="handleSelectionChange"
      :height="tableHeight"
    >
      <el-table-column prop="id" label="序号" show-overflow-tooltip></el-table-column>
      <el-table-column prop="operation" label="操作内容" show-overflow-tooltip> </el-table-column>
      <el-table-column prop="ip" label="请求ip" show-overflow-tooltip> </el-table-column>
      <el-table-column prop="userName" label="操作人员" show-overflow-tooltip> </el-table-column>
      <el-table-column prop="creatDate" label="操作时间" show-overflow-tooltip></el-table-column>

    </el-table>
    <el-pagination
      background
      @current-change="handleCurrentChange"
      :page-sizes="[10, 15, 20, 30]"
      @size-change="handleSizeChange"
      :current-page="current"
      :page-size="size"
      layout="sizes,prev, pager, next"
      :total="totalCount"
    >
    </el-pagination>
  </div>
</template>
<script>
// 引入导出Excel表格依赖
import FileSaver from "file-saver"
import XLSX from "xlsx"
import { logList } from '@/api/system/log'
export default {
  name:"javascriptthree",
  data() {
    return {
      tableData: [

      ],
       //存储列表总数
      totalCount: 0,
      //存储开始页
      current: 1,
      //存储每页几条
      size: 15,
      operation:undefined,
      multipleSelection: [],

      options: [
        {
          value: "1",
          label: "操作人1",
        },
        {
          value: "2",
          label: "操作人2",
        },
        {
          value: "3",
          label: "操作人3",
        },
      ],
      value: "",
      input:"",

      dialogFormVisible: false,
      form: {
        name: "",
        region: "",
        region2: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      formLabelWidth: "120px",
      userName:undefined,
      //表格自适应高度
      tableHeight:undefined,

     //日期时间选择器
      formDaliog: {
        startTime: "",
        endTime: "",
      },
      pickerOptions1: {
        disabledDate: (time) => {
          if (this.formDaliog.endTime) {
            return (
              time.getTime() < Date.now() - 8.64e7 ||
              time.getTime() > new Date(this.formDaliog.endTime).getTime()
            );
          } else {
            return time.getTime() < Date.now() - 8.64e7;
          }
        },
      },
      pickerOptions2: {
        disabledDate: (time) => {
          if (this.formDaliog.startTime) {
            return (
              time.getTime() < Date.now() - 8.64e7 ||
              time.getTime() < new Date(this.formDaliog.startTime).getTime()
            );
          } else {
            return time.getTime() < Date.now() - 8.64e7;
          }
        },
      },
    };
  },
  created() {
    this.getList();
  },
  mounted(){
    // table高度
    if(window.innerWidth <1920){
      this.tableHeight = window.innerHeight - 300
    }
    // 监听窗口大小变化
    let self = this;
    window.onresize = function() {
      self.tableHeight = window.innerHeight - 300
    }
  },
  methods: {
    /** 重置按钮操作 */
    resetQuery() {
      this.operation = undefined;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    getList(){
       //获取tableData数据
      logList({
        current: this.current,
        size: this.size,
        name:this.operation}).then(Response =>{
        if(Response.status == "SUCCESS"){
          if(Response.data.pageList){
            this.tableData = Response.data.pageList
          }
        }
      })
    },
    //分页查询
    handleCurrentChange(val) {
      this.current = val;
      this.getData();
    },
    //改变每条数
    handleSizeChange(val) {
      this.size = val;
      this.getData();
    },
    //定义导出Excel表格事件
    exportExcel() {
      /* 从表生成工作簿对象 */
      var wb = XLSX.utils.table_to_book(document.querySelector("#out-table"));
      /* 获取二进制字符串作为输出 */
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array",
      });
      try {
        FileSaver.saveAs(
          //Blob 对象表示一个不可变、原始数据的类文件对象。
          //Blob 表示的不一定是JavaScript原生格式的数据。
          //File 接口基于Blob，继承了 blob 的功能并将其扩展使其支持用户系统上的文件。
          //返回一个新创建的 Blob 对象，其内容由参数中给定的数组串联组成。
          new Blob([wbout], { type: "application/octet-stream" }),
          //设置导出文件名称
          "sheetjs.xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },
    handleEdit(index, row) {
      alert(index, row);
    },
    handleDelete(index, row) {
      this.$confirm("确定删除该单位吗?", "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.tableData.splice(index, 1);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    batchDelete() {
      let multData = this.multipleSelection;
      let tableData = this.tableData;
      let multDataLen = multData.length;
      let tableDataLen = tableData.length;

      for (let i = 0; i < multDataLen; i++) {
        for (let y = 0; y < tableDataLen; y++) {
          if (JSON.stringify(tableData[y]) === JSON.stringify(multData[i])) {
            // 判断是否相等，相等就删除
            this.tableData.splice(y, 1);

          }
        }
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
