<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-select v-model="value" placeholder="" style="width: 150px">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </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="planName" label="任务名称" width="150"> </el-table-column>
      <el-table-column prop="period" label="周期" width="280">
      </el-table-column>
      <el-table-column prop="execuDate" label="执行日期" width="180">
      </el-table-column>

      <el-table-column prop="patrolTime" label="巡检时间" width="130"> </el-table-column>
      <el-table-column prop="team" label="执行团队"> </el-table-column>
      <el-table-column prop="responser" 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" width="60%">
      <el-form :model="form">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12"
            ><div class="grid-content bg-purple">
              <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-select
                  v-model="form.region"
                  placeholder=""
                  style="width: 100%"
                >
                  <el-option label="项目一" value="1"></el-option>
                  <el-option label="项目二" value="2"></el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="执行团队:" :label-width="formLabelWidth">
                <el-select
                  v-model="form.region2"
                  placeholder=""
                  style="width: 100%"
                >
                  <el-option label="团队一" value="3"></el-option>
                  <el-option label="团队二" value="4"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="巡检开始时间:" :label-width="formLabelWidth">
                <el-date-picker
                  style="width: 100%"
                  v-model="formDaliog.startTime2"
                  type="datetime"
                  placeholder="选择日期时间"
                  align="right"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :picker-options="pickerOptions3"
                ></el-date-picker>
              </el-form-item>
              <el-form-item label="巡检结束时间:" :label-width="formLabelWidth">
                <el-date-picker
                  style="width: 100%"
                  v-model="formDaliog.endTime2"
                  type="datetime"
                  placeholder="选择日期时间"
                  align="right"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :picker-options="pickerOptions4"
                >
                </el-date-picker>
              </el-form-item></div
          ></el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12"
            ><div class="grid-content bg-purple">
              <el-form-item label="周期:" :label-width="formLabelWidth">
                <el-checkbox
                  :indeterminate="isIndeterminate"
                  v-model="checkAll"
                  @change="handleCheckAllChange"
                  >全选</el-checkbox
                >
                <div style="margin: 10px 0"></div>
                <el-checkbox-group
                  v-model="checkedCities"
                  @change="handleCheckedCitiesChange"
                >
                  <el-checkbox
                    v-for="city in cities"
                    :label="city"
                    :key="city"
                    >{{ city }}</el-checkbox
                  >
                </el-checkbox-group>
              </el-form-item>

              <el-form-item
                label="执行开始日期:"
                :label-width="formLabelWidth"
                style="margin-top: 0px"
              >
                <el-date-picker
                  style="width: 100%"
                  v-model="formDaliog.startTime"
                  type="datetime"
                  placeholder="选择日期时间"
                  align="right"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :picker-options="pickerOptions1"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="执行结束日期:" :label-width="formLabelWidth">
                <el-date-picker
                  style="width: 100%"
                  v-model="formDaliog.endTime"
                  type="datetime"
                  placeholder="选择日期时间"
                  align="right"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :picker-options="pickerOptions2"
                >
                </el-date-picker>
              </el-form-item></div
          ></el-col>
        </el-row>

        <el-table
          :data="tableData2"
          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="order" label="序号" width="50"> </el-table-column>
          <el-table-column prop="cameraName" label="点位名称">
          </el-table-column>
          <el-table-column prop="company" label="点位地址">
          </el-table-column>
          <el-table-column label="点位详情">
            <template slot-scope="scope">
                  <img :src="scope.row.imageUrl" style="width: 25px" />
                </template>
          </el-table-column>
        </el-table>
      </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";

const cityOptions = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];

export default {
  name: "javascriptthree",
  data() {
    return {
      tableData: [
        {
          planName: "admin巡检计划1",
          period: "周一、周二、周三、周四、周五、周六、周日",
          execuDate: "2020-08-01 2020-08-05",
          patrolTime: "9：00-10：00",
          team: "admin巡检团队b",
          responser: "吉超博",
        },
        {
          planName: "测试数据",
          period: "周三、周四、周五",
          execuDate: "2020-08-01 2020-08-05",
          patrolTime: "9：00-10：00",
          team: "测试团队",
          responser: "吉超博",
        },
        {
          planName: "测试数据测试数据",
          period: "周一、周三、周六、周日",
          execuDate: "2020-08-01 2020-08-05",
          patrolTime: "9：00-10：00",
          team: "测试团队",
          responser: "吉超博",
        },
      ],

      tableData2: [
        {
          order: "1",
          cameraName: "维保测试点位",
          company: "上海青浦区徐泾镇徐乐路208号",
          imageUrl:require("@/assets/img.png"),
        },
        {
          order: "2",
          cameraName: "维保测试点位",
          company: "测试地址",
          imageUrl:require("@/assets/img.png"),
        },
      ],
      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",

      //视频弹框
      dialogVideoDetailVisible: false,

      // 全选
      checkAll: false,
      checkedCities: ["周一", "周五"],
      cities: cityOptions,
      isIndeterminate: true,

      //日期时间选择器
      formDaliog: {
        startTime: "",
        endTime: "",
        startTime2: "",
        endTime2: "",
      },
      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;
          }
        },
      },

      pickerOptions3: {
        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;
          }
        },
      },
      pickerOptions4: {
        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;
          }
        },
      },
    };
  },
  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;
    },

    // 全选
    handleCheckAllChange(val) {
      this.checkedCities = val ? cityOptions : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.cities.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.cities.length;
    },
  },
};
</script>
<style scoped>
.table-tit {
  background: rgb(235, 241, 255);
  border: 1px solid #d9d9d9;
  border-bottom: 0;
  font-weight: bold;
  padding: 8px 10px;
}
.el-checkbox {
  margin-right: 15px;
  line-height: 0;
}
.el-dialog {
  min-width: 736px !important;
}
</style>
<style >
</style>