<template>
  <!-- 分配设备 -->
  <el-dialog title="选择设备" :visible.sync="visible" width="800px" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型" style="width: 180px" 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: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          style="width: 180px"
          @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: 180px" 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>
      <el-table @row-click="clickRow" ref="table" :data="deviceList" @selection-change="handleSelectionChange"
                height="260px">
        <el-table-column type="selection" width="55"></el-table-column>
        <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>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectDevice">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  monitorUnitTree,
  unallocatedDeviceList,
  batchInsertOfflineTarget,
} from "@/api/business/offlineRule";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  dicts: ['hw_device_type'],
  components: {Treeselect},
  props: {
    offlineRuleId: {
      type: [Number, String]
    },
    sceneId: {
      type: [Number, String]
    }
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      targetIds: [],
      // 总条数
      total: 0,
      //监控单元树数据
      monitorUnitTreeData: [],
      // 未分配设备表格数据
      deviceList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        offlineRuleId: undefined,
        sceneId: undefined,
        monitorUnitId: undefined,
        deviceCode: undefined,
        deviceName: undefined
      },
      OFFLINE_TARGET_TYPE_DEVICE: '1',
    };
  },
  methods: {
    monitorUnitTree(sceneId) {
      monitorUnitTree({sceneId: sceneId}).then(response => {
        this.monitorUnitTreeData = response.data;
      })
    },
    // 显示弹框
    show() {
      this.queryParams.offlineRuleId = this.offlineRuleId;
      this.queryParams.sceneId = this.sceneId;
      this.monitorUnitTree(this.sceneId);
      this.getList();
      this.visible = true;
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.targetIds = selection.map(item => item.deviceId);
    },
    // 查询表数据
    getList() {
      unallocatedDeviceList(this.queryParams).then(res => {
        this.deviceList = res.rows;
        this.total = res.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 选择分配设备操作 */
    handleSelectDevice() {
      const offlineRuleId = this.queryParams.offlineRuleId;
      const targetIds = this.targetIds.join(",");
      if (targetIds == "") {
        this.$modal.msgError("请选择要分配的设备");
        return;
      }
      batchInsertOfflineTarget({
        offlineRuleId: offlineRuleId,
        targetType: this.OFFLINE_TARGET_TYPE_DEVICE,
        targetIds: targetIds
      }).then(res => {
        this.$modal.msgSuccess(res.msg);
        if (res.code === 200) {
          this.visible = false;
          this.$emit("ok");
        }
      });
    }
  }
};
</script>
