<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="电子围栏名称" prop="electronicFenceName">
        <el-input
          v-model="queryParams.electronicFenceName"
          placeholder="请输入电子围栏名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属场景" prop="sceneId">
        <el-select v-model="queryParams.sceneId" placeholder="请选择"  @input="sceneChange">
          <el-option
            v-for="(scene, index) in scenes"
            :key="index"
            :label="scene.sceneName"
            :value="scene.sceneId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="触发状态" prop="sceneId">
        <el-select v-model="queryParams.triggerStatus" placeholder="请选择触发状态" clearable>
          <el-option
            v-for="dict in dict.type.hw_fence_trigger_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>

      </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
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:electronicFence:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:electronicFence:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:electronicFence:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:electronicFence:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="electronicFenceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="电子围栏名称" align="center" prop="electronicFenceName" />
      <el-table-column label="所属场景" align="center" prop="sceneName" />
      <el-table-column label="规则类型" align="center" prop="fenceType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hw_fence_type" :value="scope.row.fenceType"/>
        </template>
      </el-table-column>
      <el-table-column label="触发状态" align="center" prop="triggerStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hw_fence_trigger_status" :value="scope.row.triggerStatus"/>
        </template>
      </el-table-column>
      <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="['business:electronicFence:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:electronicFence: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"
    />

  </div>
</template>

<script>
import { listElectronicFence, getElectronicFence, delElectronicFence, addElectronicFence, updateElectronicFence } from "@/api/business/electronicFence";
import {getScenes} from "@/api/business/device";

export default {
  name: "ElectronicFence",
  dicts: ['hw_fence_trigger_status','hw_fence_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedHwFenceTarget: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电子围栏表格数据
      electronicFenceList: [],
      // 电子围栏目标表格数据
      hwFenceTargetList: [],
      //场景列表
      scenes: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        electronicFenceName: null,
        sceneId: null,
        fenceType: null,
        effectiveTimeFlag: null,
        timeZone: null,
        effectiveTime: null,
        triggerStatus: null,
        areaShapeFlag: null,
        areaRange: null,
        fencePushFlag: null,
        fencePushContent: null,
        fenceRecoverContent: null,
        fenceField: null
      },

    };
  },
  created() {
    getScenes().then(response => {
      this.scenes = response.data;
    });
    this.getList();
  },
  activated() {
    const time = this.$route.query.t;
    if (time != null && time != this.uniqueId) {
      this.uniqueId = time;
      this.queryParams.pageNum = Number(this.$route.query.pageNum);
      this.getList();
    }
  },

  methods: {
    /** 查询电子围栏列表 */
    getList() {
      this.loading = true;
      listElectronicFence(this.queryParams).then(response => {
        this.electronicFenceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        electronicFenceId: null,
        electronicFenceName: null,
        sceneId: null,
        fenceType: null,
        effectiveTimeFlag: null,
        timeZone: null,
        effectiveTime: null,
        triggerStatus: null,
        areaShapeFlag: null,
        areaRange: null,
        fencePushFlag: null,
        fencePushContent: null,
        fenceRecoverContent: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        fenceField: null
      };
      this.hwFenceTargetList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.electronicFenceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      const params = {pageNum: this.queryParams.pageNum};
      this.$tab.openPage("添加电子围栏", '/electronicFence/fence-add/index', params);

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const electronicFenceId = row.electronicFenceId || this.ids
      let electronicFenceName = row.electronicFenceName;
      this.$tab.openPage("修改电子围栏[" + electronicFenceName + "]", '/electronicFence/fence-edit/index/' + electronicFenceId);
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const electronicFenceIds = row.electronicFenceId || this.ids;
      this.$modal.confirm('是否确认删除电子围栏编号为"' + electronicFenceIds + '"的数据项？').then(function() {
        return delElectronicFence(electronicFenceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 电子围栏目标序号 */
    rowHwFenceTargetIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 电子围栏目标添加按钮操作 */
    handleAddHwFenceTarget() {
      let obj = {};
      obj.targetType = "";
      this.hwFenceTargetList.push(obj);
    },
    /** 电子围栏目标删除按钮操作 */
    handleDeleteHwFenceTarget() {
      if (this.checkedHwFenceTarget.length == 0) {
        this.$modal.msgError("请先选择要删除的电子围栏目标数据");
      } else {
        const hwFenceTargetList = this.hwFenceTargetList;
        const checkedHwFenceTarget = this.checkedHwFenceTarget;
        this.hwFenceTargetList = hwFenceTargetList.filter(function(item) {
          return checkedHwFenceTarget.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleHwFenceTargetSelectionChange(selection) {
      this.checkedHwFenceTarget = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/electronicFence/export', {
        ...this.queryParams
      }, `electronicFence_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
