<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="所属场景，关联hw_scene表的scene_id字段" prop="sceneId">
        <el-input
          v-model="queryParams.sceneId"
          placeholder="请输入所属场景，关联hw_scene表的scene_id字段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生效时间标识" prop="effectiveTimeFlag">
        <el-input
          v-model="queryParams.effectiveTimeFlag"
          placeholder="请输入生效时间标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时区" prop="timeZone">
        <el-input
          v-model="queryParams.timeZone"
          placeholder="请输入时区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="格式：开始时间_结束时间_状态，多个用|隔开" prop="effectiveTime">
        <el-input
          v-model="queryParams.effectiveTime"
          placeholder="请输入格式：开始时间_结束时间_状态，多个用|隔开"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域形状" prop="areaShapeFlag">
        <el-input
          v-model="queryParams.areaShapeFlag"
          placeholder="请输入区域形状"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警推送标识(1、是，0、否）" prop="fencePushFlag">
        <el-input
          v-model="queryParams.fencePushFlag"
          placeholder="请输入报警推送标识(1、是，0、否）"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段" prop="fenceField">
        <el-input
          v-model="queryParams.fenceField"
          placeholder="请输入预留字段"
          clearable
          @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
          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="电子围栏ID" align="center" prop="electronicFenceId" />
      <el-table-column label="电子围栏名称" align="center" prop="electronicFenceName" />
      <el-table-column label="所属场景，关联hw_scene表的scene_id字段" align="center" prop="sceneId" />
      <el-table-column label="规则类型" align="center" prop="fenceType" />
      <el-table-column label="生效时间标识" align="center" prop="effectiveTimeFlag" />
      <el-table-column label="时区" align="center" prop="timeZone" />
      <el-table-column label="格式：开始时间_结束时间_状态，多个用|隔开" align="center" prop="effectiveTime" />
      <el-table-column label="触发状态" align="center" prop="triggerStatus" />
      <el-table-column label="区域形状" align="center" prop="areaShapeFlag" />
      <el-table-column label="区域范围：为多边形时保存格式，经度_纬度，多个以|隔开；为圆形时的中心点和半径，下划线隔开" align="center" prop="areaRange" />
      <el-table-column label="报警推送标识(1、是，0、否）" align="center" prop="fencePushFlag" />
      <el-table-column label="报警推送内容" align="center" prop="fencePushContent" />
      <el-table-column label="恢复正常推送内容" align="center" prop="fenceRecoverContent" />
      <el-table-column label="描述" align="center" prop="remark" />
      <el-table-column label="预留字段" align="center" prop="fenceField" />
      <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"
    />

    <!-- 添加或修改电子围栏对话框 -->
    <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="electronicFenceName">
          <el-input v-model="form.electronicFenceName" placeholder="请输入电子围栏名称" />
        </el-form-item>
        <el-form-item label="所属场景，关联hw_scene表的scene_id字段" prop="sceneId">
          <el-input v-model="form.sceneId" placeholder="请输入所属场景，关联hw_scene表的scene_id字段" />
        </el-form-item>
        <el-form-item label="生效时间标识" prop="effectiveTimeFlag">
          <el-input v-model="form.effectiveTimeFlag" placeholder="请输入生效时间标识" />
        </el-form-item>
        <el-form-item label="时区" prop="timeZone">
          <el-input v-model="form.timeZone" placeholder="请输入时区" />
        </el-form-item>
        <el-form-item label="格式：开始时间_结束时间_状态，多个用|隔开" prop="effectiveTime">
          <el-input v-model="form.effectiveTime" placeholder="请输入格式：开始时间_结束时间_状态，多个用|隔开" />
        </el-form-item>
        <el-form-item label="区域形状" prop="areaShapeFlag">
          <el-input v-model="form.areaShapeFlag" placeholder="请输入区域形状" />
        </el-form-item>
        <el-form-item label="区域范围：为多边形时保存格式，经度_纬度，多个以|隔开；为圆形时的中心点和半径，下划线隔开" prop="areaRange">
          <el-input v-model="form.areaRange" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="报警推送标识(1、是，0、否）" prop="fencePushFlag">
          <el-input v-model="form.fencePushFlag" placeholder="请输入报警推送标识(1、是，0、否）" />
        </el-form-item>
        <el-form-item label="报警推送内容">
          <editor v-model="form.fencePushContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="恢复正常推送内容">
          <editor v-model="form.fenceRecoverContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预留字段" prop="fenceField">
          <el-input v-model="form.fenceField" placeholder="请输入预留字段" />
        </el-form-item>
        <el-divider content-position="center">电子围栏目标信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddHwFenceTarget">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteHwFenceTarget">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="hwFenceTargetList" :row-class-name="rowHwFenceTargetIndex" @selection-change="handleHwFenceTargetSelectionChange" ref="hwFenceTarget">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="目标类型" prop="targetType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.targetType" placeholder="请选择目标类型">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </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 { listElectronicFence, getElectronicFence, delElectronicFence, addElectronicFence, updateElectronicFence } from "@/api/business/electronicFence";

export default {
  name: "ElectronicFence",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedHwFenceTarget: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电子围栏表格数据
      electronicFenceList: [],
      // 电子围栏目标表格数据
      hwFenceTargetList: [],
      // 弹出层标题
      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
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        electronicFenceName: [
          { required: true, message: "电子围栏名称不能为空", trigger: "blur" }
        ],
        sceneId: [
          { required: true, message: "所属场景，关联hw_scene表的scene_id字段不能为空", trigger: "blur" }
        ],
        fenceType: [
          { required: true, message: "规则类型不能为空", trigger: "change" }
        ],
        effectiveTimeFlag: [
          { required: true, message: "生效时间标识不能为空", trigger: "blur" }
        ],
        effectiveTime: [
          { required: true, message: "格式：开始时间_结束时间_状态，多个用|隔开不能为空", trigger: "blur" }
        ],
        triggerStatus: [
          { required: true, message: "触发状态不能为空", trigger: "change" }
        ],
        areaShapeFlag: [
          { required: true, message: "区域形状不能为空", trigger: "blur" }
        ],
        fencePushFlag: [
          { required: true, message: "报警推送标识(1、是，0、否）不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    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() {
      this.reset();
      this.open = true;
      this.title = "添加电子围栏";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const electronicFenceId = row.electronicFenceId || this.ids
      getElectronicFence(electronicFenceId).then(response => {
        this.form = response.data;
        this.hwFenceTargetList = response.data.hwFenceTargetList;
        this.open = true;
        this.title = "修改电子围栏";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.hwFenceTargetList = this.hwFenceTargetList;
          if (this.form.electronicFenceId != null) {
            updateElectronicFence(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addElectronicFence(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    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>
