<template>
  <div class="app-container">

    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" size="small">
      <el-form-item label="围栏组名称" prop="fenceName">
        <el-input
          v-model="queryParams.groupName"
          clearable
          placeholder="请输入围栏名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="围栏状态">
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @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
          v-hasPermi="['system:notice:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:notice:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="groupList"
      style="width: 100%">
      <el-table-column
        label="id"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="围栏组名称"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.groupName}}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="围栏组状态"
        width="180">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_notice_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column
        label="围栏优先级"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.priority}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="success"
            class="el-icon-connection"
            @click="boundFence(scope.row)">查看绑定的围栏</el-button>
          <el-button
            type="success"
            class="el-icon-connection"
            @click="boundCar(scope.row)">绑定车辆</el-button>
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)">修改</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="900px">

      <el-steps :active="active" finish-status="success">
         <el-step title="步骤 1" description="围栏组信息"></el-step>
          <el-step title="步骤 2" description="绑定围栏"></el-step>
      </el-steps>


<div v-show="active==0">
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <el-row>
          <el-col :span="20">
            <el-form-item label="围栏组名称" prop="groupName">
              <el-input v-model="form.groupName" placeholder="请输入围栏组名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="优先级" prop="priority">
              <el-input-number v-model="form.priority" @change="handleChange" :min="0" :max="99" label="优先级范围在(0-99)"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="围栏组状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>


</div>
      <div v-show="active==1">
      <el-table
        ref="multipleTable"
        :data="fenceList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          label="id"
          width="120">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="电子围栏名称"
        >
        </el-table-column>
        <el-table-column
          prop="longitudeLatitude"
          label="围栏位置编码"
         >
        </el-table-column>

        <el-table-column
          label="判定方式" >
          <template slot-scope="scope">
            <el-select v-model="scope.row.way" placeholder="请选择" v-show="form.electronicFenceRespList!=null">
              <el-option
                v-for="dict in dict.type.sys_fence_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>
      </el-table>

      </div>
      <div slot="footer" class="dialog-footer">
        <el-button style="margin-top: 12px;" @click="active--" v-show="active===1">上一步</el-button>
        <el-button style="margin-top: 12px;" @click="active++" v-show="active===0">下一步</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-drawer
      size="60%"
      :title="title"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose">

      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="fenceAndWayRespList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column
          label="id"
          width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="围栏名称"
          width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="围栏位置编码"
          width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.longitudeLatitude}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="判定方式"
          width="180">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_fence_type" :value="scope.row.way"/>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="warning"
              class="el-icon-view"
              @click="lookFence(scope.row)"></el-button>
          </template>
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="handleUnbind(scope.row)">解除绑定关系</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div>
        <el-button
          type="primary"
          size="mini"
          @click="closeDrawer()">确认</el-button>
      </div>

    </el-drawer>
    <el-drawer
      size="60%"
      :title="title"
      :visible.sync="carDirawer"
      :direction="direction"
      :before-close="handleClose">

      <el-table
      ref="multipleTable"
      :data="carList"
      style="width: 100%"
      @selection-change="carSelectionChange">
<el-table-column
  type="selection"
  width="55">
</el-table-column>
        <el-table-column
          label="vin"
          width="120">
          <template slot-scope="scope">{{ scope.row.vin}}</template>
        </el-table-column>
      </el-table>
      <div>
        <el-button
          type="primary"
          size="mini"
          @click="carMidAdd()">确认</el-button>
        <el-button
          type="info"
          size="mini"
          @click="closecarDrawer()">返回</el-button>
      </div>

    </el-drawer>


  </div>
</template>

<script>

import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  addGroup, carlistselect,
  deliteGroupById,
  findFenceAndWayById,
  findGroupByid, selectCarAdd,
  selectGroupList, unbindById,
  updGroup
} from "@/api/fence/group";
import {fenceArray, findElectronicByid, listFence} from "@/api/fence/fence";
export default {
  name: "group",
  dicts: ['sys_normal_disable','sys_group_type','sys_notice_status','sys_fence_type'],
  data() {
    return {
      carBound: {
        id:undefined,
        carSelectListList:[]
      },
       carDirawer:false,
      carList:[],

      groupId:null,
      fenceAndWayRespList:[],
      testList:[],
//进度条
      active: 0,
      //抽屉
      drawer: false,
      direction: 'rtl',
      // 非单个禁用
      single: true,
      showSearch: true,
      groupList:[],
      fenceList:[],
      queryParams:{},
      // 遮罩层
      loading: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 围栏组表单参数
      form: {
        //被选择的电子围栏
        electronicFenceRespList: [],
      },
      // 表单校验
      rules: {
        fenceType: [
          {required: true, message: "围栏类型不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "围栏名称不能为空", trigger: "blur"}
        ],
      }
    }
  },

  mounted() {
    // 可以在这里添加一些初始化逻辑
    console.log('Component is mounted, checking if the table reference is available:', this.$refs.multipleTable);
  },

  created() {
    this.getList();
    this.fenceArrayList();
  },
  methods: {

    carMidAdd(){

      selectCarAdd(this.carBound).then(res=>{
        this.$modal.msgSuccess("绑定成功");
        this.carDirawer=false;
      })
    },


    carSelectionChange(val){
    this.carSelectListList=val;
    console.log(val);
    },

    boundCar(row){
this.carBound.id = row.id;
      carlistselect().then(res=>{

        this.carList = res.data;

this.carDirawer=true;

      })
    },

    closecarDrawer(){
      this.carDirawer = false;
    },


//解除绑定
    handleUnbind(row){
      unbindById(row.id,this.groupId).then(res=>{
        this.$modal.msgSuccess("解除成功");
        findFenceAndWayById(this.groupId).then(response => {
          this.fenceAndWayRespList = response.data;
        });
      })
    },

    //查看电子围栏
    lookFence(row){
      if (row.longitudeLatitude == null||row.longitudeLatitude==''){
        this.$modal.msgError("请先设置电子围栏");
        return;
      }
      this.$router.push({
        path:'map',
        query:{id:row.id,longitudeLatitude:row.longitudeLatitude,open:0,backPath:'group'}
      })
    },
    //控制抽屉
    closeDrawer(){
      this.drawer=false;
    },

    /** 查询电子围栏列表 */
    selectFence(row) {

      findFenceAndWayById(row.id).then(response => {
        this.fenceAndWayRespList = response.data;
      });

      },

    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    handleChange(value) {
      console.log(value);
    },
    handleSelectionChange(val) {
      this.form.electronicFenceRespList = val;
    },
    //打开绑定围栏的功能
    boundFence(row){
      this.groupId= row.id;
      this.fenceAndWayRespList=[];
      this.selectFence(row);
        this.title = "绑定的电子围栏";
      this.drawer = true;
    },

    handleDelete(row){
      this.$modal.confirm('是否确认删除名称为"' + row.groupName + '"的数据项？').then(function () {
        return deliteGroupById(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

// 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        status: "0"
      };
      this.resetForm("form");
    },
    fenceArrayList(){
      fenceArray().then(res => {
        console.log(res)
        this.fenceList = res.data;
      });
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.open = true;
      this.title = "添加围栏组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.fenceArrayList();
      this.title="修改围栏组";
      this.open = true;
      findGroupByid(row.id).then(response => {
        this.form = response.data;
        let rows = response.data.electronicFenceRespList;
        console.log(rows)
        if (rows!=null&&rows!=undefined&&rows!=''){
          rows.forEach(row => {
            console.log(this.fenceList[row.id-1]);
            console.log(row);
            this.$refs.multipleTable.toggleRowSelection(this.fenceList[row.id-1]);
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updGroup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.active=0;
            });
          } else {
            addGroup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.active=0;
            });
          }
        }
      });
    },
    /** 查询围栏列表 */
    getList() {
      this.loading=true;
      selectGroupList(this.queryParams).then(res => {
        console.log(res)
        this.groupList = res.data;
        this.loading=false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

  }
};
</script>
