<template>
  <div class="app-container">
     <div class="divtitle"><div style="font-size:1.6rem;font-family: Adobe Heiti Std;font-weight: normal;color: #DFEEF3;">{{divtitle}}</div></div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="locationCode">
        <el-input
          v-model="queryParams.locationCode"
          placeholder="请输入编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="locationName">
        <el-input
          v-model="queryParams.locationName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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="locationList" @selection-change="handleSelectionChange" stripe
      class="my_table"
      :row-class-name="tableRowClassName"
      :header-cell-style="{background:'#24A3B3 50%',color: '#C5D0D4'}">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="locationCode" />
      <el-table-column label="名称" align="center" prop="locationName" />
      <el-table-column label="描述" align="center" prop="detail" />
      <el-table-column label="巡检设备" align="center" prop="deviceIds" />
      <el-table-column label="所属区域" align="center" prop="areaId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['qdtl:location:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['qdtl:location:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改巡检点管理对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="locationCode">
          <el-input v-model="form.locationCode" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="名称" prop="locationName">
          <el-input v-model="form.locationName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="描述" prop="detail">
          <el-input v-model="form.detail" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="经纬度" prop="lnglat">
          <el-input v-model="form.lnglat" placeholder="请输入经纬度" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->

     <el-dialog :title="title" :visible.sync="open" width="150rem" append-to-body @open="openMap" v-if="open">
      <div style="height:70rem">
      <div style="float:left;width:30rem">
        <el-form ref="form" :model="form" :rules="rules" label-width="8rem">
        <el-form-item label="编号" prop="locationCode" style="margin-top:2rem">
          <el-input v-model="form.locationCode" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="名称" prop="locationName" style="margin-top:6rem">
          <el-input v-model="form.locationName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="描述" prop="detail" style="margin-top:6rem">
          <el-input v-model="form.detail" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="所属区域" prop="areaId"  style="margin-top:6rem">
          <el-select v-model="form.areaId" placeholder="请选择所属区域" >
            <el-option
              v-for="item in areaOptions"
              :key="item.id"
              :label="item.areaName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经纬度" prop="lnglat" style="margin-top:6rem">
          <el-input v-model="form.lnglat" placeholder="请输入经纬度" />
        </el-form-item>
        <el-form-item label="备注" prop="remark" style="margin-top:6rem">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      </div>
      <div style="float:left;height:60rem;width:100rem;margin-left:10rem;margin-top:5rem">
           <mapdiv ref='mapv' :pointObj='pointArry' :isEdit='isEdit'></mapdiv>
      </div>
      </div>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button type="primary" @click="cancel">取 消</el-button>
        <el-button type="primary" @click="deleteLocation">清 除</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLocation, getLocation, delLocation, addLocation, updateLocation } from "@/api/qdtl/location";
import { listArea, getArea, delArea, addArea, updateArea } from "@/api/qdtl/area";
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',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        locationCode: null,
        locationName: null,
        areaId: null,
      },
      // 表单参数
      form: {},

      pointArry:{
        locations:'',
        type:''
      },
      // 表单校验
      rules: {
      }
    };
  },
   components:{
      mapdiv
    },
  created() {
    this.getList();
    this.getAreaList();
  },
  methods: {
    openMap(){
       var locationArry =  this.form.lnglat ;
       this.pointArry.locations = locationArry;
       this.pointArry.type = 5
       console.log(this.pointArry);
    },
    /** 查询巡检点管理列表 */
    getList() {
      this.loading = true;
      listLocation(this.queryParams).then(response => {
        this.locationList = response.rows;
        this.total = response.total;
        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");
    },
    getAreaList() {
      listArea(this.areaQueryParams).then(response => {
        this.areaOptions = response.rows;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      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;
          float: left;
       }


</style>
