<template>
  <div class="app-container">
     <headerdiv ref="headerDiv" :currentindexP = currentindexNew></headerdiv>
     <div class="divtitle">
       <img src="~@/assets/images/icon.png" style="width:1.8rem;height:1.8rem;margin-top:0.3rem"/>
       <div style="font-size:1.6rem;font-family: Adobe Heiti Std;font-weight: normal;color: #DFEEF3;margin-left:1rem">{{divtitle}}</div>
      </div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="6.8rem" style="margin-top:1rem">
      <el-form-item label="巡更人员" prop="nickName">
         <el-select v-model="queryParams.nickName" placeholder="请选择人员" style="width:25rem">
                    <el-option
                      v-for="obj in userList"
                      :key="obj.userId"
                      :label="obj.nickName"
                      :value="obj.userId"
                    ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="startDate">
            <el-date-picker
                          v-model="queryParams.startDate"
                          type="date"
                          placeholder="选择日期"
                          style="width:15rem"
                          value-format="yyyy-MM-dd">
                        </el-date-picker>
      </el-form-item>
        <el-form-item label="结束时间" prop="endDate">
            <el-date-picker
                          v-model="queryParams.endDate"
                          type="date"
                          placeholder="选择日期"
                          style="width:15rem"
                          value-format="yyyy-MM-dd">
                        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          style="width:8rem;height:3.2rem;" 
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['qdtl:location:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
           style="width:8rem;height:3.2rem;" 
          type="primary"
          plain
          icon="el-icon-edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['qdtl:location:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          style="width:8rem;height:3.2rem;" 
          type="primary"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['qdtl:location:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="scheduleList" @selection-change="handleSelectionChange" stripe
      class="my_table"
      :row-class-name="tableRowClassName"
      :header-cell-style="{background:'#24A3B3 50%',color: '#C5D0D4'}">
      <el-table-column label="生效日期" align="center" prop="effectDate" width='250'/>
      <el-table-column label="巡更点" align="center" prop="locationName" width='200'>
            <template slot-scope="scope">
                    <div v-for="item of scope.row.locations">
                         <span>  {{item}} </span>
                         </br>
                    </div>
            </template>
      </el-table-column>
      <el-table-column label="巡更时间" align="center" prop="detail" width='150' >
             <template slot-scope="scope">
                    <div v-for="item of scope.row.timeUnit">
                         <span>  {{item}} </span>
                         </br>
                    </div>
            </template>
      </el-table-column>
      <el-table-column :label="dateRange" align="center">
                    <el-table-column
                          prop="name"
                          label="姓名"
                          align="center"
                    >
                    </el-table-column>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { getSchedule } from "@/api/qdtl/data";
import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus } from "@/api/system/user";
import headerdiv from '@/components/HeaderDiv/index.vue'
import mapdiv from "@/components/map/index.vue"

export default {
  name: "Location",
  data() {
    return {
      // 遮罩层
      loading: true,

      divtitle:'排班管理',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡检点管理表格数据
      locationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      isEdit:'true',

      selectUser:[],

      userList:[],

      dateRange:'',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},

      pointArry:{
        locations:'',
        type:''
      },
      scheduleList:[],
      // 表单校验
      rules: {
      }
    };
  },
   components:{
      mapdiv,
      headerdiv
    },
  created() {
    this.getList();
    this.getUserList();
    // this.getAreaList();
  },
  methods: {
    getUserList(){
      listUser().then(response =>{
        this.userList = response.rows
         console.log(this.userList);
        // console.log(response.rows);
      })
    },
    /** 查询巡检点管理列表 */
    getList() {
      this.loading = true;
      getSchedule().then(response =>{
               console.log(response);
               this.scheduleList = response.data
               this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        locationCode: null,
        locationName: null,
        detail: null,
        deviceIds: null,
        areaId: null,
        lnglat: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log(this.queryParams);
      this.dateRange = this.queryParams.startDate + '至' +this.queryParams.endDate
      console.log(this.dateRange);
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加巡检点管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLocation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改巡检点管理";
        this.openMap();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
              if(this.$refs.mapv.locations.length > 0){
                  this.form.lnglat = this.$refs.mapv.locations.join(',');
              }
            updateLocation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
              this.form.lnglat = this.$refs.mapv.locations.join(',');
            addLocation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除巡检点管理编号为"' + ids + '"的数据项？').then(function() {
        return delLocation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qdtl/location/export', {
        ...this.queryParams
      }, `location_${new Date().getTime()}.xlsx`)
    },
        tableRowClassName({ row, rowIndex }) {
        if ((rowIndex + 1) % 2 === 0) {
             return "warning-row"; 
        } else {
             return "success-row";
        }
    },
     deleteLocation(){
      // this.$refs.mapv.draw.set
       this.$refs.mapv.vector.getSource().clear()
    }
  }
};
</script>


<style>
 .divtitle{
          background: url("~@/assets/images/title2@2x.png") no-repeat;
          background-size: 100% 100%;
          width:100%;
          height:3.9rem;
          padding-top:0.8rem;
          padding-left:3rem;
          display: inline-flex;
       }


</style>
