<template>
  <div class="app-container" >
    <el-row :gutter="20">
      <el-col :span="4" :xs="24" >
        <div class="head-container" >
          <el-input
            v-model="areaName"
            placeholder="请输入区域名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container" style="height: 100vh; overflow: hidden; position: relative;">
          <el-tree
            :data="areaOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
            style="height: calc(100vh - 50px); overflow-y: auto;"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="设备名称" prop="deviceName">
            <el-input
              v-model="queryParams.deviceName"
              placeholder="请输入设备名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="归属设施" prop="refFacs">
            <el-select v-model="queryParams.refFacs" >
              <el-option v-for="item in facsOptions" :label="item.facsName" :value="item.facsCode" :key="item.facsCode" />
            </el-select>
          </el-form-item>
          <el-form-item label="子系统" prop="subsystemCode">
            <el-select v-model="queryParams.subsystemCode" >
              <el-option v-for="item in subsystemOptions" :label="item.systemName" :value="item.systemCode" :key="item.systemCode" />
            </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-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
          <el-table-column label="设备名称" align="left" prop="deviceName"/>
          <el-table-column label="归属区域" align="left" prop="areaPath" width="200px"/>
          <el-table-column label="归属设施" align="center" prop="refFacsName"/>
          <el-table-column label="设备类型" align="center" prop="deviceTypeName"/>
          <el-table-column label="设备状态" align="center" prop="deviceStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_device_stat" :value="scope.row.deviceStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="子系统" align="center" prop="subsystemName"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text"  @click="handleDetail(scope.row)">详情</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="deviceName">
              <el-input disabled v-model="form.deviceName" placeholder="请输入设备名称"/>
            </el-form-item>
            <el-form-item label="设备代码" prop="deviceCode">
              <el-input disabled v-model="form.deviceCode" placeholder="请输入设备代码"/>
            </el-form-item>
            <el-form-item label="归属设施" prop="refFacs" >
              <el-input disabled v-model="form.refFacsName" placeholder="归属设施"/>
            </el-form-item>
            <el-form-item label="归属区域" prop="refArea">
              <el-input disabled v-model="form.refAreaName" placeholder="归属区域"/>
            </el-form-item>
            <el-form-item label="设备类型" prop="deviceType">
              <el-input disabled v-model="form.deviceTypeName" placeholder="设备类型"/>
            </el-form-item>
            <el-form-item label="子系统" prop="subsystemName">
              <el-input disabled v-model="form.subsystemName" placeholder="子系统"/>
            </el-form-item>
            <el-form-item label="设备状态" prop="deviceStatus">
              <el-input disabled v-model="form.deviceStatus" placeholder="设备状态"/>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancel">关闭</el-button>
          </div>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listDevRecursionByArea, getDevice, delDevice, addDevice, updateDevice } from '@/api/device/device'
import { areaTreeSelect } from "@/api/basecfg/area"
import { listAllFacs } from "@/api/basecfg/emsfacs"
import { getDevcType} from '@/api/commonApi'
import { listSubsystemAll } from '@/api/adapter/subsystem'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getBuilding } from '@/api/basecfg/building'


export default {
  name: 'Device',
  dicts: ['sys_device_stat'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 能源设备表格数据
      deviceList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 区域名称
      areaName: undefined,
      // 区域树选项
      areaOptions: undefined,
      // 设施选项
      facsOptions: undefined,
      // 设备分类
      devcTypeOptions: undefined,
      subsystemOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceCode: null,
        deviceName: null,
        deviceType: null,
        deviceStatus: null,
        areaType: null,
        refArea: null,
        refFacs: null,
        subsystemCode: null
      },
      // 表单参数
      form: {}
    }
  },
  created() {
    this.getList();
    this.getAreaTree('Zoning');
    this.getFacsOptions("");
    this.getDevcType();
    this.getSubsystem();
  },
  methods: {
    /** 查询能源设备列表 */
    getList() {
      this.loading = true
      listDevRecursionByArea(this.queryParams).then(response => {
        this.deviceList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询区域树结构 */
    getAreaTree(tier) {
      areaTreeSelect(tier).then(response => {
        this.areaOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.refArea = data.id;
      this.queryParams.areaType = data.tier;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceCode: null,
        deviceName: null,
        deviceType: null,
        deviceStatus: null,
        refArea: null,
        refFacs: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.refArea = null;
      this.queryParams.areaType = null;
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleDetail(row) {
      this.reset()
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '设备详情'
      })
    },
    handleAreaType(data) {
      this.form.areaType = data.tier;
    },
    getFacsOptions(areaCode) {
      listAllFacs(areaCode).then(response =>{
        this.facsOptions = response.data;
      })
    },
    getDevcType() {
      getDevcType().then(response =>{
        this.devcTypeOptions = response.data;
      })
    },
    getSubsystem() {
      listSubsystemAll().then(response =>{
        this.subsystemOptions = response.data;
      })
    }
  }
}
</script>
