<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-left">
        <div class="filter-item">
          设备编号:
          <el-input
            v-model="input"
            placeholder=""
            style="width: 150px"
          ></el-input>
        </div>
        <div class="filter-item">
          设备名称:
         <el-input
            v-model="input"
            placeholder=""
            style="width: 150px"
          ></el-input>
        </div>
        

        <el-button type="primary" icon="el-icon-search" class="search-button"
          >搜索</el-button
        >
      </div>

      <div class="handle-button-right">
        <el-button
          icon="el-icon-plus"
          type="success"
          @click="dialogFormVisible = true"
          >新增</el-button
        >
        <el-button icon="el-icon-delete" type="danger" @click="batchDelete"
          >删除</el-button
        >
        <el-button type="warning">
          <svg-icon icon-class="export" />
          导出
        </el-button>
      </div>
    </div>

    <el-divider></el-divider>
    <el-table
      :data="tableData"
      stripe
      border
      :header-cell-style="{ background: '#EBF1FF', color: '#606266' }"
      style="width: 100%"
      id="out-table"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40"> </el-table-column>

      <el-table-column prop="deviceId" label="设备编号"> </el-table-column>
      <el-table-column prop="deviceName" label="设备名称" > </el-table-column>
      <el-table-column prop="produceType" label="产品型号"> </el-table-column>
  
      <el-table-column prop="position" label="安装位置" width="300"> </el-table-column>
      <el-table-column prop="parameter"  label="性能参数">
      </el-table-column>
     
      <el-table-column label="操作" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            style="color: #406CE5"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            icon="el-icon-delete"
            style="color: #F27979"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background layout="prev, pager, next" :total="1000">
    </el-pagination>

    <el-dialog title="新增" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="设备编号:" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="设备名称:" :label-width="formLabelWidth">
          <el-input v-model="form.name2" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="产品型号:" :label-width="formLabelWidth">
          <el-input v-model="form.name3" autocomplete="off"></el-input>
        </el-form-item>

       <el-form-item label="安装位置:" :label-width="formLabelWidth">
          <el-input v-model="form.name2" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="性能参数:" :label-width="formLabelWidth">
          <el-input v-model="form.name3" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="dialogFormVisible = false"
          style="background: #f6f6f6"
          >取 消</el-button
        >
        <el-button type="primary" @click="dialogFormVisible = false"
          >新 增</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>


<script>
// 引入导出Excel表格依赖
import FileSaver from "file-saver";
import XLSX from "xlsx";

export default {
  name: "javascriptthree",
  data() {
    return {
      tableData: [
        {
          deviceId: "123456789",
          deviceName: "脉冲电子围栏",
          produceType: "产品型号产品型号",
          position: "虹泾总部，行政楼，三楼，301",
          parameter: "性能参数性能参数",
          
        },
        {
          deviceId: "98654321",
          deviceName: "红外对射",
          produceType: "产品型号产品型号",
          position: "虹泾总部，行政楼，三楼，301",
          parameter: "性能参数性能参数",
          
        },
        {
          deviceId: "135790643",
          deviceName: "红外对射",
          produceType: "产品型号产品型号",
          position: "虹泾总部，行政楼，三楼，301",
          parameter: "性能参数性能参数",
          
        },
        {
          deviceId: "123456789",
          deviceName: "摄像头",
          produceType: "产品型号产品型号",
          position: "虹泾总部，行政楼，三楼，301",
          parameter: "性能参数性能参数",
          
        },
      ],
      multipleSelection: [],

      options: [
        {
          value: "1",
          label: "账号1",
        },
        {
          value: "2",
          label: "账号2",
        },
        {
          value: "3",
          label: "账号3",
        },
      ],
      value: "",

      options2: [
        {
          value: "1",
          label: "角色1",
        },
        {
          value: "2",
          label: "角色2",
        },
        {
          value: "3",
          label: "角色3",
        },
      ],
      value2: "",

      options3: [
        {
          value: "1",
          label: "单位名称1",
        },
        {
          value: "2",
          label: "单位名称2",
        },
        {
          value: "3",
          label: "单位名称3",
        },
      ],
      value3: "",

      input: "",

      dialogFormVisible: false,
      form: {
        name: "",
        region: "",
        region2: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      formLabelWidth: "120px",
    };
  },
  methods: {
    //定义导出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);
            console.log("aa");
          }
        }
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>