<template>
  <div class="app-container">
    <el-tabs v-model="activeObjType" @tab-click="handleTabClick">
<!--      <el-tab-pane label="设施模型" name="1"></el-tab-pane>-->
      <el-tab-pane label="设备模型" name="2"></el-tab-pane>
<!--      <el-tab-pane label="设备部件" name="3"></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="modelCode">
        <el-input
          v-model="queryParams.modelCode"
          placeholder="请输入模型编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模型名称" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          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="['ems:model: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="['ems:model: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="['ems:model:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="modelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="模型编码" align="center" prop="modelCode"/>
      <el-table-column label="模型名称" align="center" prop="modelName"/>
      <el-table-column label="对象类型" align="center">
        <template slot-scope="scope">
          {{ objTypeMapping[scope.row.objType] }}
        </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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ems:model:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ems:model:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @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="modelCode">
          <el-input v-model="form.modelCode" placeholder="请输入模型code"/>
        </el-form-item>
        <el-form-item label="模型名称" prop="modelName">
          <el-input v-model="form.modelName" placeholder="请输入模型名称"/>
        </el-form-item>
        <el-form-item label="能力接口" prop="abilityHandle">
        <el-input v-model="form.abilityHandler" placeholder="请输入能力接口"/>
        </el-form-item>
        <el-form-item label="事件接口" prop="eventHandle">
          <el-input v-model="form.eventHandler" placeholder="请输入事件接口"/>
        </el-form-item>
      </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>

    <!-- ”特性“对话框   -->
    <el-drawer :title=curRow.modelName size="80%" :visible.sync="showDrawer" direction="rtl">
      <div class="drawer-content" style="padding-left:50px">
        <el-tabs v-model="subKey">
          <el-tab-pane label="对象属性" name="attr">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAttrAdd"
                           v-hasPermi="['ems:attr:add']">新增
                </el-button>
              </el-col>
            </el-row>
            <el-table v-loading="loading" :data="attrList">
              <el-table-column type="selection" width="55" align="center"/>
              <el-table-column label="属性名称" align="center" prop="attrName"/>
              <el-table-column label="属性标识" align="center" prop="attrKey"/>
              <el-table-column label="属性单位" align="center" prop="attrUnit"/>
              <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="handleAttrUpdate(scope.row)"
                             v-hasPermi="['ems:attr:edit']">修改
                  </el-button>
                  <el-button size="mini" type="text" icon="el-icon-delete" class="deleteBtn"
                             @click="handleAttrDelete(scope.row)" v-hasPermi="['ems:attr:remove']">删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
<!--            <pagination v-show="total>0" :total="total" :page.sync="queryAttrParams.pageNum"-->
<!--                        :limit.sync="queryAttrParams.pageSize"-->
<!--                        @pagination="getAttrsList"/>-->

            <!-- 添加或修改属性对话框 -->
            <el-dialog :title="title" :visible.sync="attrOpen" width="650px" append-to-body>
              <el-form ref="attrForm" :model="attrForm" :rules="attrRules" label-width="150px">
                <el-form-item label="属性名称" prop="attrName">
                  <el-input v-model="attrForm.attrName" placeholder="请输入属性名称"/>
                </el-form-item>
                <el-form-item label="属性标识" prop="attrKey">
                  <el-input v-model="attrForm.attrKey" placeholder="请输入属性标识"/>
                </el-form-item>
                <el-form-item label="属性分组" prop="attrGroup">
                  <el-radio-group v-model="attrForm.attrGroup" size="medium">
                    <el-radio v-for="(item, index) in attrTypeOptions" :key="index" :label="item.value"
                              :disabled="item.disabled">{{item.label}}</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="属性单位" prop="attrUnit">
                  <el-input v-model="attrForm.attrUnit" placeholder="请输入属性单位"/>
                </el-form-item>
                <el-form-item label="属性值类型" prop="attrValueType">
                  <el-select v-model="attrForm.attrValueType" placeholder="请选择属性值类型">
                    <el-option
                      v-for="option in getAttrValueOptions()"
                      :key="option.value"
                      :label="option.label"
                      :value="option.value">
                    </el-option>
                  </el-select>
                </el-form-item>

              <!--属性值表格-->
                <el-form-item label="枚举类型值" prop="customAttrs" v-if="attrForm.attrValueType === 'Enum'">
                  <el-table class="attr-table" v-loading="loading" :data="customAttrs" max-height="280px" key="'customAttrs'">
                    <el-table-column label="枚举值" align="center" prop="attrValue">
                      <template slot-scope="scope">
                        <el-input size="mini" v-model="scope.row.attrValue" placeholder="请输入枚举值" />
                      </template>
                    </el-table-column>
                    <el-table-column label="描述" align="center" prop="attrValueName">
                      <template slot-scope="scope">
                        <el-input size="mini" v-model="scope.row.attrValueName" placeholder="请输入描述" />
                      </template>
                    </el-table-column>
                    <el-table-column align="center" label="操作">
                      <template slot="header">
                        <div class="operateBtns" @click="addCustomAttr">
                          <span>添加</span><i class="el-icon-circle-plus-outline"></i>
                        </div>
                      </template>
                      <template slot-scope="scope">
                        <i class="el-icon-delete" @click="deleteCustomAttr(scope.$index)"></i>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitAttrForm">确 定</el-button>
                <el-button @click="attrCancel">取 消</el-button>
              </div>
            </el-dialog>
          </el-tab-pane>

          <el-tab-pane label="对象事件" name="second">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleEventAdd"
                           v-hasPermi="['ems:event:add']">新增
                </el-button>
              </el-col>
            </el-row>
            <el-table v-loading="loading" :data="eventList">
              <el-table-column type="selection" width="55" align="center"/>
              <el-table-column label="事件名称" align="center" prop="eventName"/>
              <el-table-column label="事件标识" align="center" prop="eventKey"/>
              <el-table-column label="事件类型" align="center" prop="eventType">
                <template slot-scope="scope">
                  <span>{{ scope.row.eventType === 1 ? '消息上报' : '异常告警' }}</span>
                </template>
              </el-table-column>
              <el-table-column label="事件代码" align="center" prop="eventCode"/>
              <el-table-column label="外系统事件代码" align="center" prop="extEventCode"/>
              <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="handleEventUpdate(scope.row)"
                             v-hasPermi="['ems:event:edit']">修改
                  </el-button>
                  <el-button size="mini" type="text" icon="el-icon-delete" class="deleteBtn"
                             @click="handleEventDelete(scope.row)" v-hasPermi="['ems:event:remove']">删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <pagination v-show="total>0" :total="total" :page.sync="queryEventParams.pageNum"
                        :limit.sync="queryEventParams.pageSize"
                        @pagination="getEventList"/>
            <!-- 添加或修改事件对话框 -->
            <el-dialog :title="title" :visible.sync="eventOpen" width="500px" append-to-body>
              <el-form ref="eventForm" :model="eventForm" :rules="eventRules" label-width="80px">
                <el-form-item label="事件名称" prop="eventName">
                  <el-input v-model="eventForm.eventName" placeholder="请输入事件名称"/>
                </el-form-item>
                <el-form-item label="事件标识" prop="eventKey">
                  <el-input v-model="eventForm.eventKey" placeholder="请输入事件标识"/>
                </el-form-item>
                <el-form-item label="事件类型" prop="eventType">
                  <el-select v-model="eventForm.eventType" placeholder="请选择对象类型">
                    <el-option v-for="item in objEventTypeOptions"
                               :label="item.name"
                               :value="item.code"
                               :key="item.code" />
                  </el-select>
                </el-form-item>
                <el-form-item label="事件代码" prop="eventCode">
                  <el-input v-model="eventForm.eventCode" placeholder="请输入事件代码"/>
                </el-form-item>
                <el-form-item label="外系统事件代码" prop="extEventCode">
                  <el-input v-model="eventForm.extEventCode" placeholder="请输入外系统事件代码"/>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitEventForm">确 定</el-button>
                <el-button @click="eventCancel">取 消</el-button>
              </div>
            </el-dialog>
          </el-tab-pane>

          <el-tab-pane label="对象能力" name="third">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAbilityAdd"
                           v-hasPermi="['ems:ability:add']">新增
                </el-button>
              </el-col>
            </el-row>
            <el-table v-loading="loading" :data="abilityList">
              <el-table-column type="selection" width="55" align="center"/>
              <el-table-column label="能力名称" align="center" prop="abilityName"/>
              <el-table-column label="能力键" align="center" prop="abilityKey"/>
              <el-table-column label="能力参数" align="center" prop="abilityParam"/>
              <el-table-column label="能力描述" align="center" prop="abilityDesc"/>
              <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="handleAbilityUpdate(scope.row)"
                             v-hasPermi="['ems:ability:edit']">修改
                  </el-button>
                  <el-button size="mini" type="text" icon="el-icon-delete" class="deleteBtn"
                             @click="handleAbilityDelete(scope.row)" v-hasPermi="['ems:ability:remove']">删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

            <!--添加或修改能力对话框 -->
            <el-dialog :title="title" :visible.sync="abilityOpen" width="500px" append-to-body>
              <el-form ref="abilityForm" :model="abilityForm" :rules="abilityRules" label-width="80px">
                <el-form-item label="能力键" prop="abilityKey">
                  <el-input v-model="abilityForm.abilityKey" placeholder="请输入能力键"/>
                </el-form-item>
                <el-form-item label="能力名称" prop="abilityName">
                  <el-input v-model="abilityForm.abilityName" placeholder="请输入能力名称"/>
                </el-form-item>
                <el-form-item label="能力描述" prop="abilityDesc">
                  <el-input v-model="abilityForm.abilityDesc" type="textarea" placeholder="请输入内容"/>
                </el-form-item>
                <el-form-item label="能力参数" prop="abilityParam">
                  <el-input v-model="abilityForm.abilityParam" type="textarea" placeholder="请输入内容"/>
                </el-form-item>
                <el-form-item label="隐藏标记" prop="hiddenFlag">
                  <el-radio-group v-model="abilityForm.hiddenFlag">
                    <el-radio :label="0">隐藏</el-radio>
                    <el-radio :label="1">展示</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitAbilityForm">确 定</el-button>
                <el-button @click="abilityCancel">取 消</el-button>
              </div>
            </el-dialog>
          </el-tab-pane>

        </el-tabs>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listModel, getModel, delModel, addModel, updateModel, getModelByCode } from '@/api/basecfg/objModel'
import { getEvent, delEvent, addEvent, updateEvent } from '@/api/basecfg/objEvent'
import { getAttr, delAttr, addAttr, updateAttr } from '@/api/basecfg/objAttribute'
import { getAbility, delAbility, addAbility, updateAbility } from '@/api/basecfg/objAbility'
import {getAttrList,delAttrEnum} from '@/api/basecfg/emum'

export default {
  name: 'Model',
  data() {
    return {
      // 用于存储属性表格
      customAttrs: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      attrTotal:0,
      eventTotal:0,
      abilityTotal:0,
      // 能源对象属性表格数据
      modelList: [],
      filteredModelList:[],
      activeObjType: '2',
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      eventOpen: false,
      attrOpen: false,
      abilityOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelCode: null,
        modelName: null,
        objType: null
      },
      objTypeMapping: {
        1: '设施',
        2: '设备',
        3: '设备部件'
      },
      objTypeOptions: [
        { code: 1, name: '设施' },
        { code: 2, name: '设备' },
        { code: 3, name: '设备部件' }
      ],
      objEventTypeOptions: [
        { code: 1, name: '信息上报' },
        { code: 2, name: '异常警告' },
      ],
      attrTypeOptions: [
        { "label": "基础信息", "value":'Base'},
        { "label": "状态信息", "value": 'State' },
        { "label": "计量信息", "value": 'Measure'},
        { "label": "协议信息", "value": 'Protocol'}

      ],
      // 查询参数
      queryEventParams: {
        pageNum: 1,
        pageSize: 10
      },
      queryAttrParams: {
        pageNum: 1,
        pageSize: 10
      },
      queryAbilityParams: {
        pageNum: 1,
        pageSize: 10
      },
      curRow: {},
      // 表单参数
      form: {
        id: null,
        modelCode: null,
        modelName: null,
        // 这里将由标签页控制
        objType: null,
        abilityHandler: null,
        eventHandler: null
      },
      eventForm: {},
      attrForm: {},
      abilityForm: {},
      //  表单校验
      rules: {
        modelCode: [
          { required: true, message: '模型code不能为空', trigger: 'blur' }
        ],
        modelName: [
          { required: true, message: '模型名称不能为空', trigger: 'blur' }
        ],
        objType: [
          { required: true, message: '对象类型不能为空', trigger: 'change' }
        ]
      },
      eventRules: {
        modelCode: [
          { required: true, message: '模型code不能为空', trigger: 'blur' }
        ],
        eventKey: [
          { required: true, message: '事件名称不能为空', trigger: 'blur' }
        ],
        eventType: [
          { required: true, message: '事件类型不能为空', trigger: 'change' }
        ],
        eventName: [
          { required: true, message: '事件名称不能为空', trigger: 'blur' }
        ]
      },
      attrRules: {
        modelCode: [
          { required: true, message: '模型code不能为空', trigger: 'blur' }
        ],
        attrKey: [
          { required: true, message: '属性标识不能为空', trigger: 'blur' }
        ],
        attrName: [
          { required: true, message: '属性名称不能为空', trigger: 'blur' }
        ]
      },
      abilityRules: {
        modelCode: [
          { required: true, message: '模型code不能为空', trigger: 'blur' }
        ],
        abilityKey: [
          { required: true, message: '能力键不能为空', trigger: 'blur' }
        ],
        abilityName: [
          { required: true, message: '能力名称不能为空', trigger: 'blur' }
        ]
      },
      // 能源对象事件
      eventList: [],
      // 能源对象属性
      attrList: [],
      // 能源对象能力
      abilityList: [],
      showDrawer: false,
      subDialogShow: false,
      subKey: 'attr'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleTabClick(tab) {
      this.activeObjType = tab.name;
      this.form.objType = null;
      this.getList();
    },
    getObjAttrList(modelCode, attrKey) {
      this.loading = true;
      getAttrList(modelCode, attrKey).then((response) => {
        this.customAttrs = response.data || [];
          this.loading = false;
        })

    },
    // 添加自定义属性的方法
    addCustomAttr() {
      this.customAttrs.push({
        attrValue: null,
        attrValueName: null,
      });
    },
    //删除自定义属性的方法
    deleteCustomAttr(index) {
      const customAttr = this.customAttrs[index];
      const attrEnumId = customAttr.id;
      this.$modal.confirm('是否确认删除枚举值编号为"' + attrEnumId + '"的数据项？').then(() => {
        return delAttrEnum(attrEnumId);
      }).then(() => {
        this.customAttrs.splice(index, 1);
        this.$modal.msgSuccess('删除成功');
      })
    },
    // 设备模型·特性
    handleDetail(row) {
      this.showDrawer = true
      this.curRow = row
      getModel(this.curRow.id).then(response => {
        const data = response.data
        console.log('data', data)
        this.attrList = data.attrList
        this.eventList = data.eventList
        this.abilityList = data.abilityList
      })
    },
    /** 新增按钮操作 */
    handleAttrAdd() {
      this.attrReset()
      this.attrOpen = true
      this.title = '添加能源对象属性'
      this.attrForm.modelCode= this.curRow.modelCode
      console.log("新增123",this.attrForm)
      this.getObjAttrList(this.attrForm.modelCode, this.attrForm.attrKey);
    },
    handleEventAdd() {
      this.eventReset()
      this.eventOpen = true
      this.title = '添加能源对象事件'
      this.eventForm.modelCode= this.curRow.modelCode
    },
    handleAbilityAdd() {
      this.abilityReset()
      this.abilityOpen = true
      this.title = '添加能源对象事件'
      this.abilityForm.modelCode= this.curRow.modelCode
    },
    /**属性值类型*/
    getAttrValueOptions() {
      return [
        { label: "数值", value: "Value" },
        { label: "字符串", value: "String" },
        { label: "枚举", value: "Enum" }
      ];
    },
    /** 查询设备模型列表 */
    getList() {
      this.loading = true
      listModel({ objType: this.activeObjType }).then(response => {
        this.modelList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getEventList(modelcode) {
      this.loading = true
      getModelByCode(modelcode).then(response => {
        const data = response.data
        this.attrList = data.attrList
        this.eventList = data.eventList
        this.abilityList = data.abilityList
      })
      this.loading = false

    },
    /** 查询能源对象属性列表 */
    getAttrsList(modelcode) {
      this.loading = true
      getModelByCode(modelcode).then(response => {
        const data = response.data
        this.attrList = data.attrList
        this.eventList = data.eventList
        this.abilityList = data.abilityList
      })
      this.loading = false
    },
    /** 查询能源对象能力列表 */
    getAbilityList(modelcode) {
      this.loading = true
      getModelByCode(modelcode).then(response => {
        const data = response.data
        this.attrList = data.attrList
        this.eventList = data.eventList
        this.abilityList = data.abilityList

      })
      this.loading = false
    },

    // 表单重置
    attrReset() {
      this.attrForm = {
        id: null,
        modelCode:  this.curRow.modelCode || '',
        attrKey: null,
        attrGroup: null,
        attrName: null,
        attrValue: null,
        attrUnit: null,
        attrValueType:null
      }
      this.resetForm('attrForm')
    },
    eventReset() {
      this.eventForm = {
        id: null,
        modelCode:  this.curRow.modelCode || '',
        eventKey: null,
        eventType: null,
        eventName: null,
        eventDesc: null,
        eventCode: null,
        extEventCode: null
      }
      this.resetForm('eventForm')
    },
    abilityReset() {
      this.abilityForm = {
        id: null,
        modelCode:  this.curRow.modelCode || '',
        abilityKey: null,
        abilityName: null,
        abilityDesc: null,
        abilityParam: null,
        hiddenFlag:null,
      }
      this.resetForm('abilityForm')
    },

    /** 修改按钮操作 */
    handleAttrUpdate(row) {
      this.attrReset()
      const id = row.id || this.ids
      getAttr(id).then(response => {
        this.attrForm = response.data
        this.attrOpen = true
        this.title = '修改能源对象属性'
        this.attrForm.modelCode= this.curRow.modelCode
        this.getObjAttrList(this.attrForm.modelCode, this.attrForm.attrKey);

      })
    },
    handleEventUpdate(row) {
      this.eventReset()
      const id = row.id || this.ids
      getEvent(id).then(response => {
        this.eventForm = response.data
        this.eventOpen = true
        this.title = '修改能源对象事件'
        this.attrForm.modelCode= this.curRow.modelCode

      })
    },
    handleAbilityUpdate(row) {
      this.abilityReset()
      const id = row.id || this.ids
      getAbility(id).then(response => {
        this.abilityForm = response.data
        this.abilityOpen = true
        this.title = '修改能源对象能力'
        this.attrForm.modelCode= this.curRow.modelCode

      })
    },

    /** 删除按钮操作 */
    handleAttrDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除能源对象属性编号为"' + ids + '"的数据项？').then(function() {
        return delAttr(ids)
      }).then(() => {
        this.getAttrsList(row.modelCode)
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    handleEventDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除能源对象事件编号为"' + ids + '"的数据项？').then(function() {
        return delEvent(ids)
      }).then(() => {
        this.getEventList(row.modelCode)
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    handleAbilityDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除能源对象能力编号为"' + ids + '"的数据项？').then(function() {
        return delAbility(ids)
      }).then(() => {
        this.getAbilityList(row.modelCode)
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },

    /** 提交按钮 */
    submitAttrForm() {
      this.$refs['attrForm'].validate(valid => {
        if (valid) {
          const formData = {
            ...this.attrForm,
            valueEnums: this.customAttrs.map(item => ({
              modelCode: this.attrForm.modelCode,
              attrKey: this.attrForm.attrKey,
              attrValue: item.attrValue,
              attrValueName: item.attrValueName
            }))
          };

          if (this.attrForm.id != null) {
            updateAttr(formData).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.attrOpen = false;
              this.getAttrsList(this.attrForm.modelCode);  // 刷新属性列表
            });
          } else {
            addAttr(formData).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.attrOpen = false;
              this.getAttrsList(this.attrForm.modelCode);
            });
          }
        }
      });
    },
    submitEventForm() {
      this.$refs['eventForm'].validate(valid => {
        if (valid) {
          if (this.eventForm.id != null) {
            updateEvent(this.eventForm).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.eventOpen = false
              this.getEventList(this.eventForm.modelCode)
            })
          } else {
            addEvent(this.eventForm).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.eventOpen = false
              this.getEventList(this.eventForm.modelCode)
            })
          }
        }
      })
    },
    submitAbilityForm() {
      this.$refs['abilityForm'].validate(valid => {
        if (valid) {
          if (this.abilityForm.id != null) {
            updateAbility(this.abilityForm).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.abilityOpen = false
              this.getAbilityList(this.abilityForm.modelCode)
            })
          } else {
            addAbility(this.abilityForm).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.abilityOpen = false
              this.getAbilityList(this.abilityForm.modelCode)
            })
          }
        }
      })
    },
    // 取消按钮
    attrCancel() {
      this.attrOpen = false
      this.attrReset()
    },
    eventCancel() {
      this.eventOpen = false
      this.eventReset()
    },
    // 取消按钮
    abilityCancel() {
      this.abilityOpen = false
      this.abilityReset()
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        modelCode: null,
        modelName: null,
        objType: null,

      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加能源对象属性'
      this.form.objType = this.activeObjType; // 设置默认对象类型
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getModel(id).then(response => {
        this.form = response.data
        this.form.objType = this.activeObjType;
        this.open = true
        this.title = '修改能源对象属性'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateModel(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addModel(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除能源对象属性编号为"' + ids + '"的数据项？').then(function() {
        return delModel(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
