<template>
  <div class="app-container">
    <el-form  :inline="true" >
      <el-form-item label="操作内容:">
        <el-input v-model="operation" placeholder="请输入操作内容" clearable ></el-input>
      </el-form-item>
      <el-form-item label="请求IP:">
        <el-input v-model="ip" placeholder="请输入请求IP" clearable ></el-input>
      </el-form-item>
      <el-form-item label="操作人员:">
        <el-input v-model="userName" placeholder="请输入操作人员" clearable ></el-input>
      </el-form-item>
      <el-form-item label="操作时间:" style="margin-top:-2px">
          <el-date-picker style="width:350px;margin:0px 0 0 0px !important;"
            v-model="dateRange"
            size="small"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
      </el-form-item>
      </el-form-item>
      <el-form-item class="right">
        <el-button plain size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" size="mini" @click="getList()">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      stripe
      style="margin-top:20px;"
      id="out-table"
      @selection-change="handleSelectionChange"
      border
    >
      <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: [

      ],
      // 日期范围
      dateRange: [],
       //存储列表总数
      totalCount: 0,
      //存储开始页
      current: 1,
      //存储每页几条
      size: 15,
      operation:undefined,
      creatDate:undefined,
      ip:undefined,
      userName: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",
     //日期时间选择器
      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();
  },
  methods: {
    /** 重置按钮操作 */
    resetQuery() {
      this.operation = undefined;
      this.current = 1
      this.size = 15
      this.operation = undefined
      this.ip = undefined
      this.userName = undefined
      this.startTime = undefined
      this.endTime = undefined
      this.dateRange = []
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    getList() {
       //获取tableData数据
       if(this.dateRange.length>0){
        this.startTime = this.dateRange[0]
        this.endTime = this.dateRange[1].split(" ")[0] + " 23:59:59"
       }
      logList({
        current: this.current,
        size: this.size,
        name:this.operation,
        ip:this.ip,
        userName:this.userName,
        startTime:this.startTime,
        endTime:this.endTime,
      }).then(Response =>{
        if(Response.status == "SUCCESS"){
          if(Response.data.pageList){
            this.tableData = Response.data.pageList
            this.totalCount = Response.data.totalCount
          }
        }
      })
    },
    //分页查询
    handleCurrentChange(val) {
      this.current = val;
      this.getList();
    },
    //改变每条数
    handleSizeChange(val) {
      this.size = val;
      this.getList();
    },
    //定义导出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>
