<template>
  <div class="app-container">
     <div class="divtitle"><div style="font-size:1.6rem;font-family: Adobe Heiti Std;font-weight: normal;color: #DFEEF3;">{{title}}</div></div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="6.8rem">
      <el-form-item label="编号" prop="areaCode">
        <el-input
          v-model="queryParams.areaCode"
          placeholder="请输入编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域类型" prop="areaType">
        <el-select v-model="queryParams.areaType" placeholder="请选择区域类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.tl_area_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" 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:area: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:area: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:area:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="areaList" @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="areaCode" />
      <el-table-column label="名称" align="center" prop="areaName" />
      <el-table-column label="区域类型" align="center" prop="areaType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tl_area_type" :value="scope.row.areaType"/>
        </template>
      </el-table-column>
      <el-table-column label="区域设备" align="center" prop="deviceCount" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="getDevice(scope.row)"
          >{{scope.row.deviceCount}}</el-button>
          </template>
      </el-table-column>
      <el-table-column label="区域巡检点" align="center" prop="locationCount">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="getLocation(scope.row)"
          >{{scope.row.locationCount}}</el-button>
          </template>
      </el-table-column>
      <el-table-column label="区域描述" align="center" prop="detail" />
      <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:area:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['qdtl:area:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      style=" position:fixed;top:90rem;right:5rem;"
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :layout="layout"
      @pagination="getList"
    />

    <!-- 添加或修改区域管理对话框 -->
    <el-dialog :title="addtitle" :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="areaCode" style="margin-top:2rem">
          <el-input v-model="form.areaCode" placeholder="请输入编号" style="width:17rem"/>
        </el-form-item>
        <el-form-item label="区域名称" prop="areaName" style="margin-top:6rem">
          <el-input v-model="form.areaName" placeholder="请输入名称" style="width:17rem"/>
        </el-form-item>
        <el-form-item label="区域类型" prop="areaType" style="margin-top:6rem">
          <el-select v-model="form.areaType" placeholder="请选择区域类型" style="width:17rem" @change="areaTypeChange">
            <el-option
              v-for="dict in dict.type.tl_area_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区域描述" prop="detail" style="margin-top:6rem">
          <el-input v-model="form.detail" type="textarea" placeholder="请输入内容" style="width:17rem"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark" style="margin-top:6rem">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" style="width:17rem"/>
        </el-form-item>
        <el-form-item label="电子围栏" prop="fence" style="margin-top:6rem">
          <el-input v-model="form.fence" type="textarea" placeholder="请输入内容" style="width:17rem"/>
        </el-form-item>
        <el-form-item label="经纬度" prop="lnglat" style="margin-top:6rem">
          <el-input v-model="form.lnglat" placeholder="请输入经纬度" style="width:17rem"/>
        </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>

     <el-dialog title="巡检设备列表" :visible.sync="openDevice" width="150rem" append-to-body>
      <div style="height:70rem">
            <el-table v-loading="loading" :data="deviceList"  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="deviceCode" />
                  <el-table-column label="设备名称" align="center" prop="deviceName" />
                  <el-table-column label="设备类型" align="center" prop="deviceType">
                    <template slot-scope="scope">
                      <dict-tag :options="dict.type.tl_device_type" :value="scope.row.deviceType"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="经纬度" align="center" prop="lnglat" />
                  <el-table-column label="备注" align="center" prop="remark" />
          </el-table>    
      </div>
    </el-dialog>

     <el-dialog title="巡检点列表" :visible.sync="openLocaton" width="150rem" append-to-body>
      <div style="height:70rem">
            <el-table :data="locationList" 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>  
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listArea, getArea, delArea, addArea, updateArea } from "@/api/qdtl/area";
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/qdtl/device";
import { listLocation, getLocation, delLocation, addLocation, updateLocation } from "@/api/qdtl/location";
import mapdiv from "@/components/map/index.vue"

export default {
  name: "Area",
  dicts: ['tl_area_type','tl_device_type'],
  // dicts: ['tl_device_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 区域管理表格数据
      areaList: [],

      deviceList:[],

      locationList:[],
      // 弹出层标题
      title: "区域管理",
      // 是否显示弹出层
      open: false,

      openDevice: false,

      openLocaton:false,
      //分页器类型
      layout:"total, prev, pager, next,jumper",

      addtitle:"新增区域",
      //地图受否可以编辑打点
      isEdit:'true',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaCode: null,
        areaName: null,
        areaType: null,
      },
      pointArry:{
        locations:'',
        type:''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        areaCode: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        areaName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        areaType: [
          { required: true, message: "区域类型不能为空", trigger: "change" }
        ],
      }
    };
  },
    components:{
      mapdiv
    },
  created() {
    this.getList();
  },
  methods: {
    // getDeviceList() {
    //   listDevice(this.queryParams).then(response => {
    //     this.deviceList = response.rows;
    //     this.total = response.total;
    //   });
    // },
    openMap(){
       var locationArry =  this.form.fence ;
       this.pointArry.locations = locationArry;
       this.pointArry.type = this.form.areaType
       console.log(this.pointArry);
    },
//     addPoint(){
// this.$refs.mapv.addpoint(locationArry)
//     },
    areaTypeChange(){
      console.log(this.form.areaType);
      // if(this.form.areaType == 1){
      //     this.$refs.mapv.typeSelect="Polygon";
      //     this.$refs.mapv.changeAddInteractions();
      // }else{
      //     this.$refs.mapv.typeSelect="Point";
      //     this.$refs.mapv.changeAddInteractions();
      // } 
            // this.$refs.mapv.typeSelect="";
    },
    tableRowClassName({ row, rowIndex }) {
        if ((rowIndex + 1) % 2 === 0) {
             return "warning-row"; 
        } else {
             return "success-row";
        }
    },
    /** 查询区域管理列表 */
    getList() {
      this.loading = true;
      listArea(this.queryParams).then(response => {
        this.areaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        areaCode: null,
        areaName: null,
        areaType: null,
        detail: null,
        remark: null,
        fence: null,
        lnglat: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      if(this.pointArry){
           this.pointArry = {locations:'',
      type:''};
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    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.addtitle = "新增区域";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      const that = this;
      getArea(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.addtitle = "修改区域管理";
         this.areaTypeChange();
         this.openMap();
        // that.$refs.mapv.addInteractions();
      });
    },
     /** 打开设备列表弹窗 */
     getDevice(row) {
      const id = row.id || this.ids
      const that = this;
      listDevice({areaId:id}).then(response => {
             this.openDevice = true;
             this.deviceList = response.rows;
              console.log( this.deviceList);
      });
    },
     getLocation(row) {
      const id = row.id || this.ids
      const that = this;
      listLocation({areaId:id}).then(response => {
             this.openLocaton = true;
             this.locationList = response.rows;
              // console.log( this.locationList);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArea(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if(this.form.areaType == 1){
                  // this.form.fence = this.$refs.mapv.locations.join(';');
                  this.form.fence =JSON.stringify(this.$refs.mapv.locations);
                  // this.form.lnglat = null;
            }else{
                  this.form.fence = this.$refs.mapv.locations.join(',');
                  // this.form.fence = null;
            }
            console.log(this.form.fence);
            console.log(this.form.lnglat);
            addArea(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 delArea(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qdtl/area/export', {
        ...this.queryParams
      }, `area_${new Date().getTime()}.xlsx`)
    },
    //清除绘制内容
    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>

