<template>
  <div class="app-container">
    <el-form :model="query" ref="query" :inline="true"  >
      <el-form-item label="点位名称:" >
        <el-input
          v-model="query.ptName"
          placeholder="请输入点位名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item class="right">
        <el-button plain size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          size="mini"
          @click="getData('query')"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <!-- <el-divider></el-divider> -->
    <el-table
      v-loading="loading"
      :data="tableData"
      stripe
      border
      id="out-table"
      @selection-change="handleSelectionChange"
      style="margin-top:20px"
    >
      <el-table-column prop="ptName" label="点位名称" show-overflow-tooltip></el-table-column>
      <el-table-column prop="ptCard" label="点位钮号" show-overflow-tooltip></el-table-column>
      <el-table-column prop="ptNum" label="编号" show-overflow-tooltip></el-table-column>
      <el-table-column prop="ptType" label="类型" show-overflow-tooltip>
        <template slot-scope="scope">
          {{
            scope.row.ptType == "0"
              ? "地点"
              : scope.row.ptType == "1"
              ? "人员"
              : scope.row.ptType == "2"
              ? "时间"
              : ""
          }}
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      @current-change="handleCurrentChange"
      :page-sizes="[10, 15, 20, 30]"
      @size-change="handleSizeChange"
      :current-page="pageNo"
      :page-size="pageSize"
      layout="sizes,prev, pager, next"
      :total="totalCount"
    >
    </el-pagination>
  </div>
</template>

<script>
//引入api文件
import {
  patrollingPointPush,
  queryPatrollingPoint
} from "@/api/patrol/patrol-point";

export default {
  name: "elect-partol",

  data() {
    return {
      //点位详情弹框
      dialogPointDetailVisible: false,
      //点位标签弹框
      dialogPointLabelVisible: false,
      //点位表
      dialogPointObjectVisible: false,

      multipleSelection: [],

      //列表总数
      totalCount: 0,
      //列表每页条目数
      pageSize: 15,
      //开始页
      pageNo: 1,
      //列表数据
      tableData: [],
      //列表初始化信息存储
      cols: [],
      //input点位名称存储
      ptName_value: "",
      query: {
        ptName: undefined,
        pageNo: 1,
        pageSize: 15
      },
      form: {
        //列表每页条目数
        pageSize: 15,
        //开始页
        pageNo: 1
      },
      loading: true,
    };
  },
  mounted(){
    this.getData();
  },
  created() {
    // this.select();
    this.getData();
  },

  methods: {
    /** 重置按钮操作 */
    resetQuery() {
      this.query = {
        ptName: undefined,
        pageNo: 1,
        pageSize: 15
      },
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getData();
    },
    //初始化数据
    getData(id) {
      if (id) {
        queryPatrollingPoint(this.query).then(Response => {
          if (Response.data.pageList) {
            this.tableData = Response.data.pageList;
            this.totalCount = Response.data.totalCount
          }
          this.loading = false
        });
      } else {
        queryPatrollingPoint(this.form).then(Response => {
          if (Response.data.pageList) {
            this.tableData = Response.data.pageList;
            this.totalCount = Response.data.totalCount
          }
          this.loading = false
        });
      }
    },

    //公共查询调用
    select() {
      this.fetchData({
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        ptName: "",
        sort: ""
      });
    },
    //列表查询
    fetchData(data) {
      var that = this;
      api.queryPatrollingPoint(data).then(requset => {

        that.totalCount = requset.totalCount;
        that.pageSize = requset.pageSize;
        that.pageNo = requset.pageNo;
        that.list = requset.pageList;
        for (let i = 0; i < requset.pageList.length; i++) {
          var pageList = requset.pageList[i];
          pageList.ptType == 0
            ? (pageList.ptType = "地点")
            : pageList.ptType == 1
            ? (pageList.ptType = "人员")
            : pageList.ptType == 2
            ? (pageList.ptType = "时间")
            : "";
        }
        this.loading = false;
      });
    },
    //改变起始页
    handleCurrentChange(val) {
      this.pageNo = val;
      this.query.pageNo = this.pageNo;
      this.getData("query");
    },
    //改变每条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.query.pageSize = this.pageSize;
      this.getData("query");
    },
    //关闭窗口 （子组件会触发此事件方法来关闭窗口）
    remoteClose() {
      this.edit.formData = {};
      this.edit.visible = false;
      this.select();
    },

    //修改
    handleEdit(index, row) {
      this.edit.visible = true;
      this.edit.title = "修改";
      // row.patrolPlanCycle = row.patrolPlanCycle
      //   ? row.patrolPlanCycle.split(",")
      //   : [];
      this.edit.formData = row;
    },

    //删除
    handleDelete(id) {
      this.$confirm("确定删除该数据吗?", "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 发送删除请求
          api.deleteById({ queryJson: id }).then(response => {
            // 处理响应结果提示
            this.$message({
              type: response.success ? "success" : "error",
              message: response.Msg
            });
          });
          // 刷新列表数据
          this.select();
        })
        .catch(() => {
          // 不用理会
        });
    },

    //全部删除
    batchDelete() {
      let multData = this.multipleSelection;
      let tableData = this.list;
      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.list.splice(y, 1);
          }
        }
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //导出功能
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then(excel => {
        const tHeader = [
          "巡更计划名称",
          "设备名称",
          "执行开始日期",
          "执行结束日期",
          "巡更开始时间",
          "巡更结束时间",
          "巡更周期",
          "添加时间"
        ];
        const filterVal = [
          "deviceName",
          "patrolPlanName",
          "startDate",
          "endDate",
          "startTime",
          "endTime",
          "patrolPlanCycle",
          "creationTime"
        ];
        const data = this.formatJson(filterVal);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: "table-list"
        });
        this.downloadLoading = false;
      });
    },
    formatJson(filterVal) {
      return this.list.map(v =>
        filterVal.map(j => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    },

    //时间格式化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/, "");
    }
  }
};
</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>
