<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.fenceName"
          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>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:config:export']"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      ref="multipleTable"
      :data="fenceList"
      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.fenceDesc}}</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.longitudeLatitude}}</span>
        </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>
          <el-button
            type="success"
            class="el-icon-map-location"
            @click="handleFence(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="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="围栏名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入围栏名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="描述信息" prop="fenceDesc">
              <el-input type="textarea" v-model="form.fenceDesc" placeholder="请输入描述信息" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <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 slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>

import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {addfence, delElectronById, findElectronicByid, listFence, updfence} from "@/api/fence/fence";
import {addDept, delDept, getDept, listDept, listDeptExcludeChild, updateDept} from "@/api/system/dept";

export default {
  name: "list",
  dicts: ['sys_normal_disable','sys_fence_type','sys_notice_status'],
  data() {
    return {
      // 非单个禁用
      single: true,
      showSearch: true,
      fenceList:[],
      queryParams:{},
      // 遮罩层
      loading: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fenceType: [
          {required: true, message: "围栏类型不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "围栏名称不能为空", trigger: "blur"}
        ],
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {

    handleExport(){
      this.download('fence/fence/export', {
        ...this.queryParams
      }, `config_${new Date().getTime()}.xlsx`)

    },

    handleSelectionChange(){

    },
    //绘制电子围栏
    handleFence(row){
      this.$router.push({
        path:'map',
        query:{id:row.id,longitudeLatitude:row.longitudeLatitude,open:1}
      })
    },
    //查看电子围栏
    lookFence(row){
      if (row.longitudeLatitude==null||row.longitudeLatitude==undefined||row.longitudeLatitude==''){
        this.$modal.msgError("请先设置电子围栏");
        return;
      }
      this.$router.push({
        path:'map',
        query:{id:row.id,longitudeLatitude:row.longitudeLatitude,open:0,backPath:'list'}
      })
    },
    handleDelete(row){
      this.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项？').then(function () {
        return delElectronById(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

// 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        status: "0"
      };
      this.resetForm("form");
    },

    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.open = true;
      this.title = "添加围栏";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      findElectronicByid(row.id).then(response => {
        console.log(response)
        this.form = response.data;
        this.open = true;
        this.title = "修改围栏";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updfence(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addfence(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 查询围栏列表 */
    getList() {
      this.loading=true;
      listFence(this.queryParams).then(res => {
        console.log(res)
       this.fenceList = res.data;
        this.loading=false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>
