<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型" clearable>
          <el-option
            v-for="dict in dict.type.hw_device_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="设备编号" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属监控单元" prop="monitorUnitId">
        <treeselect v-model="queryParams.monitorUnitId" :options="monitorUnitTreeData" :show-count="true"
                    style="width: 240px" placeholder="请选择所属监控单元" @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="openSelectDevice"
          v-hasPermi="['business:offlineRule:add']"
        >添加设备
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="cancelAllocateDevices"
          v-hasPermi="['business:offlineRule:remove']"
        >批量取消分配
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="设备类型" prop="deviceType" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hw_device_type" :value="scope.row.deviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="设备编号" prop="deviceCode" :show-overflow-tooltip="true"/>
      <el-table-column label="设备名称" prop="deviceName" :show-overflow-tooltip="true"/>
      <el-table-column label="所属监控单元" prop="monitorUnitName" :show-overflow-tooltip="true"/>
      <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime) }}</span>
        </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-circle-close"
            @click="cancelAllocateDevice(scope.row)"
            v-hasPermi="['business:offlineRule: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"
    />
    <select-device ref="select" :offlineRuleId="queryParams.offlineRuleId" :sceneId="queryParams.sceneId" @ok="handleQuery"/>
  </div>
</template>

<script>
import {
  monitorUnitTree,
  allocatedDeviceList,
  cancelAllocateDevice,
  cancelAllocateDevices
} from "@/api/business/offlineRule";
import selectDevice from "./selectDevie.vue";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "AllocateDevice",
  dicts: ['hw_device_type'],
  components: {Treeselect, selectDevice},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中设备列表
      targetIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      //监控单元树数据
      monitorUnitTreeData: [],
      // 设备表格数据
      deviceList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        offlineRuleId: undefined,
        sceneId: undefined,
        monitorUnitId: undefined,
        deviceCode: undefined,
        deviceName: undefined
      },

      OFFLINE_TARGET_TYPE_DEVICE: '1',
    };
  },
  created() {
    const offlineRuleId = this.$route.params && this.$route.params.offlineRuleId;
    const sceneId = this.$route.params && this.$route.params.sceneId;
    if (offlineRuleId && sceneId) {
      this.queryParams.offlineRuleId = offlineRuleId;
      this.queryParams.sceneId = sceneId;
      this.monitorUnitTree(sceneId);
      this.getList();
    }
  },
  methods: {
    monitorUnitTree(sceneId) {
      monitorUnitTree({sceneId:sceneId}).then(response => {
        this.monitorUnitTreeData = response.data;
      })
    },

    /** 查询授权用户列表 */
    getList() {
      this.loading = true;
      allocatedDeviceList(this.queryParams).then(response => {
          this.deviceList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 返回按钮
    handleClose() {
      const obj = {path: "/alarm/offlineRule"};
      this.$tab.closeOpenPage(obj);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.targetIds = selection.map(item => item.deviceId)
      this.multiple = !selection.length
    },
    /** 打开设备列表弹窗 */
    openSelectDevice() {
      this.$refs.select.show();
    },
    /** 取消分配按钮操作 */
    cancelAllocateDevice(row) {
      const offlineRuleId = this.queryParams.offlineRuleId;
      const OFFLINE_TARGET_TYPE_DEVICE = this.OFFLINE_TARGET_TYPE_DEVICE;
      this.$modal.confirm('确认要取消分配该设备"' + row.deviceName + '"吗？').then(function () {
        return cancelAllocateDevice({
          offlineRuleId: offlineRuleId,
          targetType: OFFLINE_TARGET_TYPE_DEVICE,
          targetId: row.deviceId
        });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消分配成功");
      }).catch(() => {
      });
    },
    /** 批量取消分配按钮操作 */
    cancelAllocateDevices(row) {
      const offlineRuleId = this.queryParams.offlineRuleId;
      const targetIds = this.targetIds.join(",");
      const OFFLINE_TARGET_TYPE_DEVICE = this.OFFLINE_TARGET_TYPE_DEVICE;
      this.$modal.confirm('是否取消分配选中设备项？').then(function () {
        return cancelAllocateDevices({
          offlineRuleId: offlineRuleId,
          targetType: OFFLINE_TARGET_TYPE_DEVICE,
          targetIds: targetIds
        });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消分配成功");
      }).catch(() => {
      });
    }
  }
};
</script>
