<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-tabs v-model="queryParams.deviceCategory" @tab-click="deviceCategoryChange">
          <el-tab-pane label="产能设备" name="E"></el-tab-pane>
          <el-tab-pane label="储能设备" name="C"></el-tab-pane>
          <el-tab-pane label="输能设备" name="W"></el-tab-pane>
          <el-tab-pane label="用能设备" name="Z"></el-tab-pane>
        </el-tabs>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="设备分类" prop="deviceSubCategory">
            <el-select v-model="queryParams.deviceSubCategory" >
              <el-option v-for="item in subCategoryOptions" placeholder="设备分类" :label="item.name" :value="item.code"
                         :key="item.code" />
            </el-select>
          </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" >
          <el-table-column label="设备名称" align="left" prop="deviceName"/>
          <el-table-column label="归属区域" align="left" prop="areaPath" width="280px"/>
          <el-table-column label="归属设施" align="center" prop="refFacsName"/>
          <el-table-column label="设备类型" align="center" prop="deviceTypeName"/>
          <el-table-column label="子系统" align="center" prop="subsystemName"/>
<!--          <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="deviceStatus">
            <template slot-scope="scope">
                <span
                  :style="{
                        padding: '6px 12px',
                        borderRadius: '4px',
                        display: 'inline-block',
                        textAlign: 'center',
                        cursor: 'pointer',
                        color: getDeviceStatusTextColor(scope.row.deviceStatus),
                        backgroundColor: getDeviceStatusBgColor(scope.row.deviceStatus),
                        minWidth: '70px',
                        height: '35px',
                        textAlign: 'center'
                 }"
                >
                  {{ getDeviceStatusText(scope.row.deviceStatus) }}
                   </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"  @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 :visible.sync="open" title="设备状态详情" custom-class="detail-dialog">
          <div v-if="curRow">
            <!-- 设施基本信息 -->
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span class="section-title">设备状态基本信息</span>
              </div>
              <div>
                <p><span class="bold">设备名称：</span>{{ curRow.deviceName }}</p>
                <p><span class="bold">设备代码：</span>{{ curRow.deviceCode }}</p>
                <p><span class="bold">归属设施：</span>{{ curRow.refFacs }}</p>
                <p><span class="bold">归属区域：</span>{{ curRow.refArea }}</p>
                <p><span class="bold">子系统：</span>{{ curRow.subsystemName }}</p>
                <p><span class="bold">设备状态：</span>{{ curRow.deviceStatus }}</p>
              </div>
            </el-card>

            <!-- 属性信息 -->
            <el-card class="box-card" v-if="attrData.length > 0">
              <div slot="header" class="clearfix">
                <span class="section-title">属性定义</span>
              </div>
              <div v-for="(item, index) in attrData" :key="index">
                <p><span class="bold">属性名称：</span>{{ item.attrName }}</p>
                <p><span class="bold">属性标识：</span>{{ item.attrKey }}</p>
                <p><span class="bold">属性值：</span>{{ item.attrValue }}</p>
                <p><span class="bold">属性单位：</span>{{ item.attrUnit }}</p>
                <!-- 在每个条目之后添加横线，除了最后一个条目 -->
                <div v-if="index < attrData.length - 1" class="divider"></div>
              </div>
            </el-card>
            <el-card class="box-card" v-else>
              <div slot="header" class="clearfix">
                <span class="section-title">属性定义</span>
              </div>
              <div>
                <p><span class="bold">属性名称：</span>暂无数据</p>
                <p><span class="bold">属性标识：</span>暂无数据</p>
                <p><span class="bold">属性值：</span>暂无数据</p>
                <p><span class="bold">属性单位：</span>暂无数据</p>
              </div>
            </el-card>

            <!-- 能力信息 -->
            <el-card class="box-card" v-if="abilityData.length > 0">
              <div slot="header" class="clearfix">
                <span class="section-title">能力定义</span>
              </div>
              <div v-for="(item, index) in abilityData" :key="index">
                <p><span class="bold">能力名称：</span>{{ item.abilityName }}</p>
                <p><span class="bold">能力键：</span>{{ item.abilityKey }}</p>
                <p><span class="bold">能力参数：</span>{{ item.abilityParam }}</p>
                <p><span class="bold">能力描述：</span>{{ item.abilityDesc }}</p>
                <!-- 在每个条目之后添加横线，除了最后一个条目 -->
                <div v-if="index < abilityData.length - 1" class="divider"></div>
              </div>
            </el-card>
            <el-card class="box-card" v-else>
              <div slot="header" class="clearfix">
                <span class="section-title">能力定义</span>
              </div>
              <div>
                <p><span class="bold">能力名称：</span>暂无数据</p>
                <p><span class="bold">能力键：</span>暂无数据</p>
                <p><span class="bold">能力参数：</span>暂无数据</p>
                <p><span class="bold">能力描述：</span>暂无数据</p>
              </div>
            </el-card>

            <!-- 事件信息 -->
            <el-card class="box-card" v-if="eventData.length > 0">
              <div slot="header" class="clearfix">
                <span class="section-title">事件定义</span>
              </div>
              <div v-for="(item, index) in eventData" :key="index">
                <p><span class="bold">事件名称：</span>{{ item.eventKey }}</p>
                <p><span class="bold">事件类型：</span>{{ item.eventType === 1 ? '消息上报' : '异常告警' }}</p>
                <p><span class="bold">事件代码：</span>{{ item.eventCode }}</p>
                <p><span class="bold">外部事件代码：</span>{{ item.extEventCode }}</p>
                <!-- 在每个条目之后添加横线，除了最后一个条目 -->
                <div v-if="index < eventData.length - 1" class="divider"></div>
              </div>
            </el-card>
            <el-card class="box-card" v-else>
              <div slot="header" class="clearfix">
                <span class="section-title">事件定义</span>
              </div>
              <div>
                <p><span class="bold">事件名称：</span>暂无数据</p>
                <p><span class="bold">事件类型：</span>暂无数据</p>
                <p><span class="bold">事件代码：</span>暂无数据</p>
                <p><span class="bold">外部事件代码：</span>暂无数据</p>
              </div>
            </el-card>
          </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 { getFacsCategorygetByCode, 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'
import {getModelByCode} from "@/api/basecfg/objModel";


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"
      },
      curRow: {},
      attrData: [],
      abilityData: [],
      eventData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceCode: null,
        deviceName: null,
        deviceSubCategory: null,
        deviceCategory: 'E',
        deviceStatus: null,
        areaType: null,
        refArea: null,
        refFacs: null,
        subsystemCode: null
      },
      // 表单参数
      form: {}
    }
  },
  created() {
    this.getList();
    this.getAreaTree('Area');
    this.getFacsOptions("");
    this.getSubsystem();
  },
  methods: {
    getDeviceStatusText(status) {
      const statusMap = {
        0: '运行',
        1: '故障',
        2: '未运行'
      };
      return statusMap[status] || '';
    },
    getDeviceStatusTextColor(status) {
      if (status === 0) return 'rgb(232,244,255)';
      if (status === 1) return '#FFFFFF';
      if (status === 2) return 'rgb(255,235,235)';
      return '#000000'; // 默认黑色文字
    },
    getDeviceStatusBgColor(status) {
      if (status === 0) return 'rgb(117,186,255)';
      if (status === 1) return 'rgb(242,197,92)';
      if (status === 2) return 'rgb(255,159,159)';
      return 'transparent'; // 默认无背景色
    },
    /** 查询能源设备列表 */
    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,
        deviceSubCategory: null,
        deviceCategory: 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()
      this.curRow= row
      console.log("设备data",this.curRow)
      getModelByCode(this.curRow.deviceModel).then(response => {
        this.open = true
        const code=response.data
        console.log("code",code)
        this.attrData=response.data.attrList
        this.eventData=response.data.eventList
        this.abilityData=response.data.abilityList
      })
    },
    getFacsOptions(areaCode) {
      listAllFacs(areaCode).then(response =>{
        this.facsOptions = response.data;
      })
    },
    getSubsystem() {
      listSubsystemAll().then(response =>{
        this.subsystemOptions = response.data;
      })
    },
    getSubCategorygetByCode () {
      getFacsCategorygetByCode(this.queryParams.deviceCategory).then(response => {
        this.subCategoryOptions = response.data.subtypeList||[];
      })
    },
    deviceCategoryChange () {
      this.queryParams.deviceSubCategory = ''
      if (this.queryParams.deviceCategory === 'E' || this.queryParams.deviceCategory === 'W'
        || this.queryParams.deviceCategory === 'T'|| this.queryParams.deviceCategory === 'C') {
        this.getAreaTree("Area")
      } else if (this.queryParams.deviceCategory === 'Z') {
        this.getAreaTree("Zoning")
      }
      this.getSubCategorygetByCode()
      this.handleQuery()
    }
  }
}
</script>

<style lang="scss" scoped>
.detail-dialog .el-dialog {
  width: 80%; /* 调整弹框宽度 */
}

.section-title {
  font-size: 18px; /* 标题字体大小 */
  font-weight: bold; /* 字体加粗 */
  margin-top: 20px; /* 标题顶部间距 */
  margin-bottom: 10px; /* 标题底部间距 */
}



</style>
