<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.routeName"
            placeholder=""
            style="width: 150px"
          ></el-input>
        </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" @click="handleDownload">
          <svg-icon icon-class="export" />
          导出
        </el-button>
      </div>
    </div>

    <el-divider></el-divider>

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

      <el-table-column prop="routeName" label="线路名称"> </el-table-column>

      <el-table-column label="路线详情">
        <template slot-scope="{ row }">
          <div class="check" @click="dialogRouteDetailVisible = true">
            {{ row.routeUrl }}
          </div>
        </template>
      </el-table-column>

      <el-table-column prop="remarks" label="备注"> </el-table-column>

      <el-table-column label="状态">
        <template slot-scope="scope">
          {{ scope.row.state == 1 ? "显示" : "不显示" }}
        </template>
      </el-table-column>

      <el-table-column prop="user" label="添加人"> </el-table-column>

      <el-table-column label="添加时间" width="200" sortable="custom">
        <template slot-scope="{ row }">
          <span>{{ renderTime(row.creationTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="对应点位">
        <template>
          <div class="check" @click="dialogPointVisible = true">查看</div>
        </template>
      </el-table-column>

      <!-- <el-table-column label="标签" width="120">
        <template slot-scope="scope">
          {{ scope.row.label == 1 ? "团队责任人" : "团队普通成员" }}
        </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.row.id)"
            >删除</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>

    <!-- 路线详情弹框 start -->
    <el-dialog
      title="路线详情"
      :visible.sync="dialogRouteDetailVisible"
      class="routeDialog"
    >
      <div style="text-align: center">
        <img src="@/assets/route.png" alt="" width="100%" />
        <p style="margin: 20px auto">虹泾总部园巡检路线1</p>
      </div>
    </el-dialog>
    <!-- 路线详情弹框 end -->

    <!-- 点位弹框 start -->
    <el-dialog title="对应点位" :visible.sync="dialogPointVisible">
      <el-form :model="form">
        <el-table
          :data="tableDataPoint"
          stripe
          border
          :header-cell-style="{ background: '#EBF1FF', color: '#606266' }"
          style="width: 100%"
          id="out-table"
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="order" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="pointName" label="点位名称"> </el-table-column>
          <el-table-column prop="pointAddress" label="点位地址">
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="dialogPointVisible = false"
          style="background: #f6f6f6"
          >关 闭</el-button
        >
        <el-button type="primary" @click="dialogPointVisible = false"
          >保 存</el-button
        >
      </div>
    </el-dialog>
    <!-- 点位弹框 end -->

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


<script>
//引入api文件
import api from "@/api/patrolRoute";

//引入局部组件
import edit from "./edit";
// import deviceList from "./deviceList";

export default {
  //组件注册
  components: {
    edit,
    // deviceList,
  },
  name: "elect-partol",

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

      // deviceList: [], //设备列表

      edit: {
        title: "",
        visible: false,
        formData: {
          // patrolPlanCycle: [],
        },
      },

      //对应点位弹框
      dialogPointVisible: false,
      form: {
        name: "",
        region: "",
        region2: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      tableDataPoint: [
        {
          order: "1",
          pointName: "867725032779518",
          pointAddress: "青浦区徐泾镇徐乐路208号虹泾1楼",
        },
        {
          order: "2",
          pointName: "867725032779518",
          pointAddress: "青浦区徐泾镇徐乐路208号虹泾2楼",
        },
        {
          order: "3",
          pointName: "867725032779518",
          pointAddress: "青浦区徐泾镇徐乐路208号虹泾3楼",
        },
      ],

      //路线详情弹框
      dialogRouteDetailVisible: false,

      multipleSelection: [],
    };
  },

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

  methods: {
    // // 从后台获取数据,重新排序
    // changeSort(val) {
    //   // this.fetchData()
    //   console.log(val); // column: {…} order: "ascending" prop: "date"
    //   // 根据当前排序重新获取后台数据,一般后台会需要一个排序的参数
    // },


    changeSort(val) {
      const { prop, order } = val
    
        if (order === 'ascending') {
        this.query.sort = 'ASC'
      } else {
        this.query.sort = 'DESC'
      }
      this.fetchData()
      
    },
  


    //列表查询

    fetchData() {
      api
        .getList(this.query, this.page.pageNo, this.page.pageSize)
        .then((response) => {
          console.log("response", response);
          // 列表数据
          this.list = response.pageList;
          this.page.total = response.totalPages;
          // this.sort=response.sort
        });
    },

    //设备下拉列表查询
    fetchPersonData() {
      api.getPersonList().then((response) => {
        console.log("response2");
        console.log(response);
        this.deviceList = response.pageList;
        // this.page.total = response.totalPages;
      });
    },

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

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

    //打开新增窗口
    openAdd() {
      this.edit.visible = true;
      this.edit.title = "新增";
    },

    //关闭窗口 （子组件会触发此事件方法来关闭窗口）
    remoteClose() {
      console.log("============");
      console.log(this.edit.formData);
      this.edit.formData = {};
      this.edit.visible = false;
      this.fetchData();
    },

    //修改
    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.fetchData();
        })
        .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);
            console.log("aa");
          }
        }
      }
    },
    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>