<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-left" :model="query">
        <div class="filter-item">
          巡更计划名称:
          <el-input
            v-model.trim="query.patrol_plan_name"
            placeholder=""
            style="width: 150px"
          ></el-input>
        </div>
        <div class="filter-item">
          执行设备:
          <el-select v-model="query.device_name" placeholder="" style="width: 150px" clearable >
            <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" @click="queryData"
          >搜索</el-button
        >
      </div>

      <div class="handle-button-right">
        <el-button
          icon="el-icon-plus"
          type="success"
          @click="openAdd"
          >新增</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>
    <!-- <div>{{page.total}}</div>
    <div>{{list}}</div> -->
    <el-table
      :data="list"
      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="patrolPlanName" label="巡更计划名称" width="150">
      </el-table-column>

      <el-table-column prop="deviceName" label="设备名称" width="150">
      </el-table-column>
      <el-table-column label="视频">
        <template>
          <div class="check" @click="dialogVideoDetailVisible = true">查看</div>
        </template>
      </el-table-column>
      <el-table-column label="巡更开始时间" width="200">
        <template slot-scope="{ row }">
          <div>{{ row.startDate }} {{ row.start_date }}</div>
        </template>
      </el-table-column>

      <el-table-column prop="endDate" label="巡更结束时间" width="200">
        <template slot-scope="{ row }">
          <div>{{ row.endDate }} {{ row.end_date }}</div>
        </template>
      </el-table-column>

      <el-table-column prop="patrolPlanCycle" label="巡更周期" width="100">
      </el-table-column>

      <el-table-column label="添加时间" width="200">
        <template slot-scope="{ row }">
          <span>{{ renderTime(row.creationTime) }}</span>
        </template>
      </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
      @current-change="handleCurrentChange"
      :current-page="page.pageNo"
      :page-size="1"
      layout="prev, pager, next"
      :total="page.total">
    </el-pagination>


    

    <edit :title="edit.title " :visible="edit.visible" :formData="edit.formData" :remoteClose="remoteClose"/>

    <!-- 视频详情弹框 start -->
    <el-dialog
      title="视频详情"
      :visible.sync="dialogVideoDetailVisible"
      class="routeDialog"
    >
      <div style="text-align: center">
        <img src="@/assets/video.png" alt="" width="100%" />
      </div>
    </el-dialog>
    <!-- 视频详情弹框 end -->
  </div>
</template>


<script>
import { renderTime } from "@/utils";

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

// console.log(flexible);
// console.log(renderTime);

//aa 1：引入api文件
import { getList } from "@/api/partol";

import edit from "./edit";



export default {
  components: {
    edit

  },
  name: "elect-partol",
  data() {
    return {
      page: {
        //分页对象
        pageNo: 1,
        pageSize: 1,
        total: 0,
      },
      // query: {"demo":1}, //查询条件
      query: {},
      list: [], //列表数据


      //视频弹框
      dialogVideoDetailVisible: false,

             dialogFormVisible: false,



      options: [
        {
          value: "1",
          label: "执行设备1",
        },
        {
          value: "2",
          label: "执行设备2",
        },
        {
          value: "3",
          label: "执行设备3",
        },
      ],
      value: "",

      edit:{
        title:'',
        visible:false,
        formData:{}
      }

      
    };
  },

  created() {
    //aa 2:钩子里调用查询组件
    this.fetchData();
  },

  methods: {
    //aa 3请求接口
    fetchData() {
      getList(this.query, this.page.pageNo, this.page.pageSize).then(
        (response) => {
          console.log(response);

          // 列表数据
          this.list = response.pageList;
          this.page.total = response.totalCount;
        }
      );
    },

    //当页码改变后触发到此方法  val是当前点击到的那个页码
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.fetchData();
    },

    //条件查询
    queryData(){
      //将页码 变为1，第一页
      this.page.pageNo=1 ;
      this.fetchData()

    },


    //时间格式化2021-01-21T05:53:00.000+0000
    renderTime(date) {
      var dateee = new Date(date).toJSON();
      return new Date(+new Date(dateee) + 8 * 3600 * 1000)
        .toISOString()
        .replace(/T/g, " ")
        .replace(/\.[\d]{3}Z/, "");
    },

    //定义导出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.list.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;
    },

//子组件会触发此事件方法来关闭窗口
    remoteClose(){
       this.edit.formData={}
      this.edit.visible=false
      this.fetchData()
    },
//打开新增窗口
    openAdd(){
      this.edit.visible=true ;
      this.edit.title='新增'

    }






    
  },
};
</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>