<template>
  <th-dialog
    ref="dialog"
    class="equipment-container"
    v-bind="dialogProps"
    :loading="loading"
  >
    <div class="dialog-main-box">
      <el-scrollbar class="panel-container-scrollbar">
        <div class="panels-box">
          <equipment-panel
            ref="statPanelRef"
            v-bind="statPanelProps"
            class="stat-panel"
            @btnClickEvent="statPanelBtnClickHandle"
            @tblRowClickEvent="statTblRowClickHandle"
            @handleSizeChange="statTblPageSizeChangeHandle"
            @handleCurrentChange="statTblCurPageChangeHandle"
            @changeEvent="changeHandle"
          />
          <equipment-panel
            v-bind="linePanelProps"
            @btnClickEvent="linePanelBtnClickHandle"
            @tblRowClickEvent="lineTblRowClickHandle"
            @handleSizeChange="lineTblPageSizeChangeHandle"
            @handleCurrentChange="lineTblCurPageChangeHandle"
            @changeEvent="changeHandle"
          />
          <equipment-panel
            class="tq-panel"
            v-bind="tqPanelProps"
            @btnClickEvent="tqPanelBtnClickHandle"
            @handleSelectionChange="tqPanelTblSelChangeHandle"
            @handleSizeChange="tqTblPageSizeChangeHandle"
            @handleCurrentChange="tqTblCurPageChangeHandle"
            @changeEvent="changeHandle"
          />
          <equipment-panel
            class="selected-tq-panel"
            v-bind="selectedTqPanelProps"
            @btnClickEvent="selectedTqBtnClickHandle"
            @handleSelectionChange="selectedTqPanelTblSelChangeHandle"
            @handleSizeChange="selectedTqTblPageSizeChangeHandle"
            @handleCurrentChange="selectedTqTblCurPageChangeHandle"
          />
        </div>
      </el-scrollbar>
    </div>
  </th-dialog>
</template>
<script>
import ThDialog from '../../dialog/src/main'
import EquipmentPanel from './EquipmentPanel'
import { addArrayToAnother, delArrayFromAnother } from '../../../src/utils/util'

export default {
  name: 'ThEquipment',
  components: {
    ThDialog,
    EquipmentPanel
  },
  props: {
    request: {
      type: Object,
      default: () => ({})
      // required: true
    },
    userInfoObj: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      loading: false,
      dialogProps: {
        title: '台区选择',
        // width: '98%',
        // height: '90%',
        props: {
          appendToBody: true,
          fullscreen: true
        },
        isDisabledFullScreen: true,
        // 底部按钮组
        buttons: [],
        // 不需要关闭按钮
        showCancelButton: false
      },

      //变电站panel
      statPanelProps: {
        request: this.request,
        externalParam: 'cis',
        btnType: '',
        selectedInfo: {
          label: '变电站名称',
          value: ''
        },
        buttons: [
          {
            icon: 'el-icon-search',
            label: '查询',
            type: 'query',
            props: {
              loading: false
            }
          },
          {
            icon: 'el-icon-th-shandian',
            label: '营销',
            type: 'cis',
            props: {
              loading: false
            },
            checked: true
          },
          {
            icon: 'el-icon-th-shandian',
            label: 'PMS',
            type: 'pms',
            props: {
              loading: false
            },
            checked: false
          }
        ],
        form: {
          labelWidth: '60px',
          data: {
            type: '0',
            enterprise: '',
            grade: '',
            name: ''
          },
          items: [
            {
              label: '类别',
              prop: 'type',
              component: 'select',
              props: {
                clearable: true
              },
              // options: this.$enums.statTypeOpts,
              style: {
                display: 'inline-block'
                // width: '40%'
              }
            },
            {
              label: '单位',
              prop: 'enterprise',
              component: 'select',
              props: {
                clearable: true
              },
              style: {
                display: 'inline-block'
                // width: '40%'
              }
            },
            {
              label: '等级',
              prop: 'grade',
              component: 'select',
              props: {
                placeholder: '请输入...',
                clearable: true
              },
              style: {
                display: 'inline-block'
                // width: '40%'
              }
            },
            {
              label: '名称',
              prop: 'name',
              component: 'input',
              props: {
                maxlength: 200,
                placeholder: '请输入...'
              },
              style: {
                display: 'inline-block'
                // width: '40%'
              }
            }
          ],
          rules: {},
          buttons: []
        },
        tableList: {
          loading: false,
          columns: [
            {
              prop: 'bdzmc', //与tableList的data键对应
              label: '变电站名称',
              minWidth: '160',
              align: 'center'
            }
          ],
          param: {
            params:
                {
                  filter: {
                    orgType: 0,
                    org: '36401',
                    findType: 'cis',
                    sublevel: 'AC02201',
                    substation: 111
                  },
                  pageIndex: 1,
                  pageSize: 50
                }
          },
          props: {
            height: '300',
            class: 'el-table--scrollable-y',
            highlightCurrentRow: true
          },
          showSelection: false,
          handleColumn: [],
          showIndex: true,
          data: [],
          totalCount: 0,
          pageSize: 500,
          pageNo: 1,
          pageProps: {
            pageSizes: [100, 200, 250, 300, 500]
          }
        }
      },
      //线路panel
      linePanelProps: {
        externalParam: '',
        btnType: '',
        selectedInfo: {
          label: '线路名称',
          value: ''
        },
        buttons: [
          {
            icon: 'el-icon-search',
            label: '查询',
            type: 'query',
            props: {
              loading: false
            }
          }
        ],
        form: {
          labelWidth: '80px',
          data: {
            xlmc: ''
          },
          items: [
            {
              label: '线路名称',
              prop: 'xlmc',
              component: 'input',
              props: {
                maxlength: 200,
                placeholder: '请输入...'
              },
              style: {
                display: 'inline-block'
                // width: '40%'
              }
            }
          ],
          rules: {},
          buttons: []
        },
        tableList: {
          loading: false,
          columns: [
            {
              prop: 'xlmc',
              label: '线路名称',
              minWidth: '160',
              align: 'center'
            },
            {
              prop: 'txxz',
              label: '图形选择',
              minWidth: '160',
              align: 'center',
              component: 'slot'
            }
          ],
          param: {
            params:
                {
                  filter: {
                    SUBSTATIONID: '',
                    SUBSTATIONNAME: '',
                    findType: ''
                  },
                  pageIndex: 1,
                  pageSize: 50
                }
          },
          props: {
            height: '300',
            class: 'el-table--scrollable-y',
            highlightCurrentRow: true
          },
          showSelection: false,
          handleColumn: [],
          showIndex: true,
          data: [],
          totalCount: 0,
          pageSize: 500,
          pageNo: 1,
          pageProps: {
            pageSizes: [100, 200, 250, 300, 500]
          }
        }
      },
      //台区panel
      tqPanelProps: {
        externalParam: '',
        btnType: '',
        selectedInfo: {
          label: '台区名称',
          value: ''
        },
        buttons: [
          {
            icon: 'el-icon-search',
            label: '查询',
            type: 'query',
            props: {
              loading: false
            }
          },
          {
            icon: 'el-icon-th-add',
            label: '添加',
            type: 'add'
          },
          {
            icon: 'el-icon-th-add',
            label: '全线添加',
            type: 'addAll',
            props: {
              loading: false
            }
          }
        ],
        form: {
          labelWidth: '80px',
          data: {
            tqID: '',
            tqName: '',
            zcsx: ''
          },
          items: [
            {
              label: '台区ID',
              prop: 'tqID',
              component: 'input',
              props: {
                maxlength: 200,
                placeholder: '请输入...'
              },
              style: {
                display: 'inline-block'
                // width: '40%'
              }
            },
            {
              label: '台区名称',
              prop: 'tqName',
              component: 'input',
              props: {
                maxlength: 200,
                placeholder: '请输入...'
              },
              style: {
                display: 'inline-block'
                // width: '40%'
              }
            },
            {
              label: '资产属性',
              prop: 'zcsx',
              component: 'select',
              props: {
                placeholder: '请输入...',
                clearable: true
              },
              options: [
                {
                  value: '-1',
                  label: '全部'
                },
                {
                  value: '01',
                  label: '公变'
                },
                {
                  value: '02',
                  label: '专变'
                }
              ],
              style: {
                display: 'inline-block'
                // width: '40%'
              }
            }
          ],
          rules: {},
          buttons: []
        },
        tableList: {
          loading: false,
          columns: [
            {
              prop: 'tqID',
              label: '台区ID',
              minWidth: '120',
              align: 'center'
            },
            {
              prop: 'tqName',
              label: '台区名称',
              minWidth: '120',
              align: 'center'
            }
          ],
          param: {
            params:
                {
                  filter: {
                    lineId: '',
                    tgId: '',
                    tgName: '',
                    pub_priv_flag: '-1',
                    flag: '0'
                  },
                  pageIndex: 1,
                  pageSize: 50
                }
          },
          props: {
            height: '300',
            class: 'el-table--scrollable-y',
            ref: 'tqInfoTable'
            // highlightCurrentRow: true
          },
          showSelection: true,
          handleColumn: [],
          showIndex: true,
          data: [
            // {
            //   tqID: '梅庄220变电器',
            //   tqName: '10kv丁恒线06B'
            // }
          ],
          totalCount: 0,
          pageSize: 500,
          pageNo: 1,
          pageProps: {
            pageSizes: [100, 200, 250, 300, 500]
          }
        }
      },
      //已选择台区panel
      selectedTqPanelProps: {
        externalParam: '',
        selectedInfo: {
          label: '已选择台区',
          value: ''
        },
        buttons: [
          {
            icon: 'el-icon-th-shandian',
            label: '确定',
            type: 'confirm'
          },
          {
            icon: 'el-icon-delete',
            label: '删除',
            type: 'delete'
          }
          // {
          //   icon: 'search',
          //   label: '快速添加台区',
          //   type: 'quickAdd'
          // }
        ],
        form: {
          labelWidth: '60px',
          data: {},
          items: [],
          rules: {},
          buttons: []
        },
        tableList: {
          loading: false,
          columns: [
            {
              prop: 'tqName',
              label: '已选择台区名称',
              minWidth: '160',
              align: 'center'
            }
          ],
          props: {
            height: '300',
            class: 'el-table--scrollable-y'
            // highlightCurrentRow: true
          },
          showSelection: true,
          handleColumn: [],
          showIndex: true,
          data: [
            // {
            //   yxztqmc: '10kv丁恒线06B恒茂华城公变'
            // }
          ],
          totalCount: 0,
          pageSize: 500,
          pageNo: 1,
          pageProps: {
            pageSizes: [100, 200, 250, 300, 500]
          }
        }
      },

      // 台区panel的table里cheked的列表用于添加
      tqSelectedList: [],
      // 已选台区panel被选择的台区列表系最终所需要
      selectedTqSelectedList: [],
      // 已选台区panel的table里cheked的列表用于删除
      selectedTqDelList: []
    }
  },
  computed: {
    statType() {
      return this.statPanelProps.form.data.type
    },
    statEnterprise() {
      return this.statPanelProps.form.data.enterprise
    },
    statGrade() {
      return this.statPanelProps.form.data.grade
    },
    tqPanelZcsx() {
      return this.tqPanelProps.form.data.zcsx
    },

    externalParam() {
      return this.statPanelProps.externalParam
    }
  },
  watch: {
    statType() {
      this.statPanelQueryFunc()
      if (this.statPanelProps.form.data.type === '0') {
        this.fetchEnterpriseApi({ isShowCityAllOrg: '1' })
      } else if (this.statPanelProps.form.data.type === '1') {
        this.fetchEnterpriseApi({ level: '3' })
      } else {
        // this.statPanelProps.form.data.enterprise = '';
      }

    },
    statEnterprise() {
      this.statPanelQueryFunc()
    },
    statGrade() {
      this.statPanelQueryFunc()
    },
    tqPanelZcsx() {
      this.tqPanelQueryFunc()
    },

    externalParam(val) {
      if (val === 'cis') {
        this.tqPanelProps.tableList.param.params.flag = '0'
        this.$refs.statPanelRef.findType = 'cis'
        this.$set(this.statPanelProps.form.data, 'grade', '')
        // this.statPanelRef.form.data.grade = '';
        const itemObj = this.tqPanelProps.form.items.find(function(item) {
          return item.label === '资产属性'
        })
        if (!itemObj) {
          const zcsxObj = {
            label: '资产属性',
            prop: 'zcsx',
            component: 'select',
            props: {
              placeholder: '请输入...',
              clearable: true
            },
            options: [
              {
                value: '-1',
                label: '全部'
              },
              {
                value: '01',
                label: '公变'
              },
              {
                value: '02',
                label: '专变'
              }
            ],
            style: {
              display: 'inline-block'
              // width: '40%'
            }
          }
          this.tqPanelProps.form.items.push(zcsxObj)
        }
      } else {
        this.tqPanelProps.tableList.param.params.flag = '1'
        this.$refs.statPanelRef.findType = 'pms'
        this.tqPanelProps.form.data.zcsx = ''
        this.$set(this.statPanelProps.form.data, 'grade', '')
        // this.statPanelRef.form.data.grade = '';
        const itemIndex = this.tqPanelProps.form.items.findIndex(function(item) {
          return item.label === '资产属性'
        })
        if (itemIndex) {
          this.tqPanelProps.form.items.splice(itemIndex, 1)
        }

      }
    },
    userInfoObj: {
      handler(val) {
        if (val && val.sysOrgId) {
          this.statPanelProps.form.data.enterprise = val.sysCurrOrgId
        }
      },
      immediate: true
    }
  },
  mounted() {
    this.initCisPmsBtnHandle()
  },
  methods: {
    // 显示弹出框
    showDialog() {
      // this.emptyStatFormProp();
      // this.emptyLineFormProp();
      // this.emptyTqFormProp();
      // this.emptyStatTblProp();
      // this.emptyLineTblProp();
      // this.emptyTqTblProp();
      // this.emptySelectedTqTblProp();
      this.$refs.dialog.showDialog()
      this.$nextTick(() => {
        this.statPanelQueryFunc()
      })
    },

    // 关闭弹出窗
    hiddenDialog() {
      this.$refs.dialog.handleCancel()

    },

    // 台区板块表格checked改变事件处理
    tqPanelTblSelChangeHandle(selectedList) {
      this.tqSelectedList = [].concat(selectedList)
    },

    // 已选台区板块表格checked改变事件处理
    selectedTqPanelTblSelChangeHandle(selectedList) {
      this.selectedTqDelList = [].concat(selectedList)
    },

    changeHandle(val, item) {
      if (!item) {
        return
      }

      switch (item.prop) {
        case 'name':
          this.statPanelQueryFunc()
          break
        case 'xlmc':
          this.linePanelQueryFunc()
          break
        case 'tqID':
          this.tqPanelQueryFunc()
          break
        case 'tqName':
          this.tqPanelQueryFunc()
          break
        default:
          break
      }
    },

    //  变电站信息的分页页面显示条数变化事件处理
    statTblPageSizeChangeHandle(pageSize) {
      this.statPanelProps.tableList.pageSize = pageSize
      this.statPanelQueryFunc()
    },

    // 变电站信息的分页页面数变化事件处理
    statTblCurPageChangeHandle(curNo) {
      this.statPanelProps.tableList.pageNo = curNo
      this.statPanelQueryFunc()
    },

    //  线路信息的分页页面显示条数变化事件处理
    lineTblPageSizeChangeHandle(pageSize) {
      this.linePanelProps.tableList.pageSize = pageSize
      this.linePanelQueryFunc()
    },

    // 线路信息的分页页面数变化事件处理
    lineTblCurPageChangeHandle(curNo) {
      this.linePanelProps.tableList.pageNo = curNo
      this.linePanelQueryFunc()
    },

    //  台区信息的分页页面显示条数变化事件处理
    tqTblPageSizeChangeHandle(pageSize) {
      this.tqPanelProps.tableList.pageSize = pageSize
      this.tqPanelQueryFunc()
    },

    // 台区信息的分页页面数变化事件处理
    tqTblCurPageChangeHandle(curNo) {
      this.tqPanelProps.tableList.pageNo = curNo
      this.tqPanelQueryFunc()
    },

    //  已选台区信息的分页页面显示条数变化事件处理
    selectedTqTblPageSizeChangeHandle(pageSize) {
      this.selectedTqPanelProps.tableList.pageSize = pageSize
      this.structureSelectedTqPanelData(this.selectedTqPanelProps.tableList.pageSize, this.selectedTqPanelProps.tableList.pageNo, this.selectedTqSelectedList)
    },

    // 已选台区信息的分页页面数变化事件处理
    selectedTqTblCurPageChangeHandle(curNo) {
      this.selectedTqPanelProps.tableList.pageNo = curNo
      this.structureSelectedTqPanelData(this.selectedTqPanelProps.tableList.pageSize, this.selectedTqPanelProps.tableList.pageNo, this.selectedTqSelectedList)
    },

    // 清空变电站的form输入框
    emptyStatFormProp() {
      this.$set(this.statPanelProps, 'externalParam', 'cis')
      this.$set(this.statPanelProps.form.data, 'type', '')
      this.$set(this.statPanelProps.form.data, 'enterprise', '')
      this.$set(this.statPanelProps.form.data, 'grade', '')
      this.$set(this.statPanelProps.form.data, 'name', '')
    },

    // 清空线路的form输入框
    emptyLineFormProp() {
      this.$set(this.linePanelProps.form.data, 'xlmc', '')
    },

    // 清空台区的form输入框
    emptyTqFormProp() {
      this.$set(this.tqPanelProps.form.data, 'tqID', '')
      this.$set(this.tqPanelProps.form.data, 'tqName', '')
      this.$set(this.tqPanelProps.form.data, 'grade', '')
      this.$set(this.tqPanelProps.form.data, 'zcsx', '')
    },

    // 清空变电站的table的相关数据
    emptyStatTblProp() {
      this.$set(this.statPanelProps.tableList, 'data', [])
      this.$set(this.statPanelProps.tableList, 'totalCount', 0)
      this.$set(this.statPanelProps.tableList, 'pageNo', 1)
    },

    // 清空线路的table的相关数据
    emptyLineTblProp() {
      this.$set(this.linePanelProps.tableList, 'data', [])
      this.$set(this.linePanelProps.tableList, 'totalCount', 0)
      this.$set(this.linePanelProps.tableList, 'pageNo', 1)
    },

    // 清空台区的table的相关数据
    emptyTqTblProp() {
      this.$set(this.tqPanelProps.tableList, 'data', [])
      this.$set(this.tqPanelProps.tableList, 'totalCount', 0)
      this.$set(this.tqPanelProps.tableList, 'pageNo', 1)
    },

    // 清空已选择台区的table的相关数据
    emptySelectedTqTblProp() {
      this.selectedTqSelectedList = []
      this.$set(this.selectedTqPanelProps.tableList, 'data', [])
      this.$set(this.selectedTqPanelProps.tableList, 'totalCount', 0)
      this.$set(this.selectedTqPanelProps.tableList, 'pageNo', 1)
    },

    // 变电站表格数据双击事件处理
    statTblRowClickHandle(row) {
      this.linePanelProps.tableList.param.params.filter.substationId = row.bdzbh
      this.emptyLineFormProp()
      this.linePanelQueryFunc()
    },

    // 线路信息表格记录双击事件处理
    lineTblRowClickHandle(row) {
      this.tqPanelProps.tableList.param.params.filter.lineId = row.xlid
      this.emptyTqFormProp()
      this.tqPanelQueryFunc()
    },

    // 已选择台区panel的确定按钮事件处理
    selectedTqPanelConfirmHandle() {
      this.$emit('selectedTqConfirm', this.selectedTqSelectedList)
      this.hiddenDialog()
    },

    // 设置已选择台区的分页
    setSelectedTqPanelPage(dataArr) {
      if (!dataArr) {
        return false
      }
      this.$set(this.selectedTqPanelProps.tableList, 'totalCount', dataArr.length)
    },

    // 自定义分页构建表格数据
    structureSelectedTqPanelData(pageSize, pageNo, dataArr, type) {
      if ((!dataArr || !dataArr.length) && !type) {
        return false
      }

      const dataLen = dataArr.length
      let pageCount = 0, // 页面数
        rltArr = [], // 需要返回的数组段
        startNum = 0, endNum = 0 // 要显示的数组下标

      if (pageSize !== 0) {
        pageCount = Math.floor(dataLen / pageSize)
      }

      if (dataLen % pageSize > 0) {
        pageCount += 1
      }

      // 加1表示下一页的第一条  减1表示数组下标从0开始
      startNum = (pageNo - 1) * pageSize + 1 - 1

      if (pageNo < pageCount) {
        endNum = pageSize * pageNo - 1
      } else if (pageNo === pageCount) {
        endNum = dataLen - 1
      }

      rltArr = dataArr.slice(startNum, endNum + 1)
      this.selectedTqPanelProps.tableList.data = rltArr
    },

    selectedTqPanelDelHandle() {
      this.selectedTqSelectedList = delArrayFromAnother(this.selectedTqSelectedList, this.selectedTqDelList)
      // this.selectedTqPanelProps.tableList.data = this.selectedTqSelectedList;
      this.setSelectedTqPanelPage(this.selectedTqSelectedList)
      this.structureSelectedTqPanelData(this.selectedTqPanelProps.tableList.pageSize, this.selectedTqPanelProps.tableList.pageNo, this.selectedTqSelectedList, 'delete')

    },

    selectedTqPanelQuickHandle() {},

    // 台区板块的添加
    tqPanelAddHandle() {
      this.selectedTqSelectedList = addArrayToAnother(this.selectedTqSelectedList, this.tqSelectedList)
      // this.selectedTqPanelProps.tableList.data = this.selectedTqSelectedList;
      this.setSelectedTqPanelPage(this.selectedTqSelectedList)
      this.structureSelectedTqPanelData(this.selectedTqPanelProps.tableList.pageSize, this.selectedTqPanelProps.tableList.pageNo, this.selectedTqSelectedList)
    },

    // 台区板块的全线添加
    tqPanelAddAllHandle() {
      this.tqPanelProps.tableList.param.params.pageIndex = 0
      this.tqPanelProps.tableList.param.params.pageSize = 0
      this.tqPanelProps.tableList.param.params.filter.tgId = this.tqPanelProps.form.data.tqID
      this.tqPanelProps.tableList.param.params.filter.tgName = this.tqPanelProps.form.data.tqName
      this.tqPanelProps.tableList.param.params.filter.pub_priv_flag = this.tqPanelProps.form.data.zcsx

      const params = {}
      params.params = JSON.stringify({
        filter: this.$util.serialize({
          ...this.tqPanelProps.tableList.param.params.filter
        }),
        pageIndex: this.tqPanelProps.tableList.param.params.pageIndex,
        pageSize: this.tqPanelProps.tableList.param.params.pageSize
      })

      this.getTqInfoAllAddFunc(params)
    },

    // 初始化变电站的Cis 和 Pms
    initCisPmsBtnHandle() {
      if (!this.statPanelProps.buttons) {
        return
      }

      for (let i = 0, len = this.statPanelProps.buttons.length; i < len; i++) {
        if (this.statPanelProps.buttons[i].checked === undefined) {
          continue
        }
        this.statPanelProps.buttons[i].icon = this.setBtnIconHandle(this.statPanelProps.buttons[i].checked)
      }
    },

    // 设置功能键的图标
    setBtnIconHandle(isChecked) {
      let icon = ''
      if (isChecked) {
        icon = 'el-icon-th-shandian'
      } else {
        icon = 'el-icon-th-shandian'
      }
      return icon
    },

    // 变电站的Cis 和 Pms 点击事件处理
    statPanelCisPmsHandle(btnParam) {
      this.statPanelProps.externalParam = btnParam.type
      this.statPanelQueryFunc()
      if (!this.statPanelProps.buttons) {
        return
      }

      for (let i = 0, len = this.statPanelProps.buttons.length; i < len; i++) {
        if (this.statPanelProps.buttons[i].checked === undefined) {
          continue
        }
        this.statPanelProps.buttons[i].checked = false
        this.statPanelProps.buttons[i].icon = this.setBtnIconHandle(false)
      }
      btnParam.checked = true
      btnParam.icon = this.setBtnIconHandle(true)
    },

    // 变电站板块功能键点击事件处理
    statPanelBtnClickHandle(btnParam) {
      if (!btnParam) {
        return
      }

      this.statPanelProps.btnType = btnParam.type

      switch (btnParam.type) {
        case 'query':
          btnParam.props.loading = true
          this.statPanelQueryFunc()
          break
        case 'cis':
          btnParam.props.loading = true
          this.statPanelCisPmsHandle(btnParam)
          break
        case 'pms':
          btnParam.props.loading = true
          this.statPanelCisPmsHandle(btnParam)
          break
        default:
          break
      }
    },

    // 线路panel查询点击事件处理
    linePanelBtnClickHandle(btnParam) {
      this.linePanelProps.buttons[0].props.loading = true
      this.linePanelQueryFunc()
    },

    // 台区信息的功能键点击事件处理
    tqPanelBtnClickHandle(btnParam) {
      if (!btnParam) {
        return
      }

      this.tqPanelProps.btnType = btnParam.type

      switch (btnParam.type) {
        case 'query':
          this.tqPanelProps.buttons[0].props.loading = true
          this.tqPanelQueryFunc()
          break
        case 'add':
          this.tqPanelAddHandle()
          break
        case 'addAll':
          this.tqPanelProps.buttons[2].props.loading = true
          this.tqPanelAddAllHandle()
          break
        default:
          break
      }
    },

    // 已选台区功能键点击事件处理
    selectedTqBtnClickHandle(btnParam) {
      if (!btnParam) {
        return
      }

      switch (btnParam.type) {
        case 'confirm':
          this.selectedTqPanelConfirmHandle()
          break
        case 'delete':
          this.selectedTqPanelDelHandle()
          break
        case 'quickAdd':
          this.selectedTqPanelQuickHandle()
          break
        default:
          break
      }
    },

    // 变电站信息查询方法
    statPanelQueryFunc() {

      this.setStatPanelQueryParamFunc()
      const params = {}
      params.params = JSON.stringify({
        filter: this.$util.serialize({
          ...this.statPanelProps.tableList.param.params.filter
        }),
        pageIndex: this.statPanelProps.tableList.param.params.pageIndex,
        pageSize: this.statPanelProps.tableList.param.params.pageSize
      })

      this.getStatInfoFunc(params)
    },

    // 线路信息查询方法
    linePanelQueryFunc() {
      this.setLinePanelQueryParamFunc()
      const params = {}
      params.params = JSON.stringify({
        filter: this.$util.serialize({
          ...this.linePanelProps.tableList.param.params.filter
        }),
        pageIndex: this.linePanelProps.tableList.param.params.pageIndex,
        pageSize: this.linePanelProps.tableList.param.params.pageSize
      })

      this.getLineInfoFunc(params)
    },

    // 台区信息查询方法
    tqPanelQueryFunc() {

      this.setTqPanelQueryParamFunc()

      const params = {}
      params.params = JSON.stringify({
        filter: this.$util.serialize({
          ...this.tqPanelProps.tableList.param.params.filter
        }),
        pageIndex: this.tqPanelProps.tableList.param.params.pageIndex,
        pageSize: this.tqPanelProps.tableList.param.params.pageSize
      })

      this.getTqInfoFunc(params)
    },

    // 设置变电站信息的查询参数
    setStatPanelQueryParamFunc() {
      this.statPanelProps.tableList.param.params.filter.orgType = this.statPanelProps.form.data.type
      this.statPanelProps.tableList.param.params.filter.org = this.statPanelProps.form.data.enterprise
      this.statPanelProps.tableList.param.params.filter.findType = this.statPanelProps.externalParam
      this.statPanelProps.tableList.param.params.filter.sublevel = this.statPanelProps.form.data.grade
      this.statPanelProps.tableList.param.params.filter.substation = this.statPanelProps.form.data.name
      // this.statPanelProps.tableList.param.params.filter.substation = decodeURIComponent(encodeURIComponent(this.statPanelProps.form.data.name));
      this.statPanelProps.tableList.param.params.pageIndex = this.statPanelProps.tableList.pageNo
      this.statPanelProps.tableList.param.params.pageSize = this.statPanelProps.tableList.pageSize
    },

    // 设置线路信息的查询参数
    setLinePanelQueryParamFunc() {
      this.linePanelProps.tableList.param.params.filter.findType = this.statPanelProps.externalParam
      this.linePanelProps.tableList.param.params.filter.lineName = this.linePanelProps.form.data.xlmc
      this.linePanelProps.tableList.param.params.pageSize = this.linePanelProps.tableList.pageSize
      this.linePanelProps.tableList.param.params.pageIndex = this.linePanelProps.tableList.pageNo
    },

    // 设置台区信息的查询参数
    setTqPanelQueryParamFunc() {
      this.tqPanelProps.tableList.param.params.filter.tgId = this.tqPanelProps.form.data.tqID
      this.tqPanelProps.tableList.param.params.filter.tgName = this.tqPanelProps.form.data.tqName
      this.tqPanelProps.tableList.param.params.filter.pub_priv_flag = this.tqPanelProps.form.data.zcsx
      this.tqPanelProps.tableList.param.params.pageSize = this.tqPanelProps.tableList.pageSize
      this.tqPanelProps.tableList.param.params.pageIndex = this.tqPanelProps.tableList.pageNo
    },

    //变电站信息获取接口数据
    getStatInfoFunc(statInfoParam) {
      const vueObj = this
      if (!this.request.getAddBlackoutDeviceStatInfo) {
        return false
      }
      vueObj.statPanelProps.tableList.loading = true
      this.request.getAddBlackoutDeviceStatInfo(statInfoParam).then(res => {

        vueObj.statPanelProps.tableList.loading = false
        vueObj.statPanelProps.buttons[0].props.loading = false
        vueObj.statPanelProps.buttons[1].props.loading = false
        vueObj.statPanelProps.buttons[2].props.loading = false

        if (res.data.successful === true && typeof res.data.resultValue === 'object' && typeof res.data.resultValue.items === 'object') {

          const statInfoList = []
          for (let i = 0, len = res.data.resultValue.items.length; i < len; i++) {
            const statInfoObj = {}
            statInfoObj.id = res.data.resultValue.items[i].id ? res.data.resultValue.items[i].id : i
            statInfoObj.bdzmc = res.data.resultValue.items[i].SUBSTATIONNAME
            statInfoObj.bdzbh = res.data.resultValue.items[i].SUBSTATIONID
            statInfoList.push(statInfoObj)
          }
          vueObj.statPanelProps.tableList.data = statInfoList
          vueObj.statPanelProps.tableList.totalCount = res.data.resultValue.itemCount
        }
      }).catch(function() {
        vueObj.statPanelProps.tableList.loading = false
        vueObj.statPanelProps.buttons[0].props.loading = false
        vueObj.statPanelProps.tableList.data = []
        vueObj.statPanelProps.tableList.totalCount = 0
      })
    },
    // 线路信息获取接口数据
    getLineInfoFunc(lineInfoParam) {
      const vueObj = this
      if (!this.request.getAddBlackoutDeviceLineInfo) {
        return false
      }
      vueObj.linePanelProps.tableList.loading = true
      this.request.getAddBlackoutDeviceLineInfo(lineInfoParam).then(res => {
        vueObj.linePanelProps.tableList.loading = false
        vueObj.linePanelProps.buttons[0].props.loading = false
        if (res.data.successful === true && typeof res.data.resultValue === 'object' && typeof res.data.resultValue.items === 'object') {
          const lineInfoList = []
          for (let i = 0, len = res.data.resultValue.items.length; i < len; i++) {
            const lineInfoObj = {}
            lineInfoObj.id = res.data.resultValue.items[i].id ? res.data.resultValue.items[i].id : i
            lineInfoObj.xlmc = res.data.resultValue.items[i].LINENAME
            lineInfoObj.txxz = res.data.resultValue.items[i].PDZDH_ID
            lineInfoObj.xlid = res.data.resultValue.items[i].LINEID
            lineInfoList.push(lineInfoObj)
          }
          vueObj.linePanelProps.tableList.data = lineInfoList
          vueObj.linePanelProps.tableList.totalCount = res.data.resultValue.itemCount
        }
      }).catch(function() {
        vueObj.linePanelProps.tableList.loading = false
        vueObj.linePanelProps.buttons[0].props.loading = false
        vueObj.linePanelProps.tableList.data = []
        vueObj.linePanelProps.tableList.totalCount = 0
      })
    },

    // 台区信息获取接口数据
    getTqInfoFunc(tqInfoParam) {
      const vueObj = this
      if (!this.request.getAddBlackoutDeviceTqInfo) {
        return false
      }
      vueObj.tqPanelProps.tableList.loading = true

      this.request.getAddBlackoutDeviceTqInfo(tqInfoParam).then(res => {

        vueObj.tqPanelProps.tableList.loading = false
        vueObj.tqPanelProps.buttons[0].props.loading = false
        if (res.data.successful === true && typeof res.data.resultValue === 'object' && typeof res.data.resultValue.items === 'object') {

          const tqInfoList = [].concat(res.data.resultValue.items)

          for (let i = 0, len = tqInfoList.length; i < len; i++) {
            // let tqInfoObj = {};
            tqInfoList[i].id = tqInfoList[i].id ? tqInfoList[i].id : i
            tqInfoList[i].tqID = tqInfoList[i].TGID
            tqInfoList[i].tqName = tqInfoList[i].TGNAME
            // tqInfoList[i].isChecked = isChecked;
          }

          vueObj.tqPanelProps.tableList.data = tqInfoList
          vueObj.tqPanelProps.tableList.totalCount = res.data.resultValue.itemCount
        }
      }).catch(function() {
        vueObj.tqPanelProps.tableList.loading = false
        vueObj.tqPanelProps.buttons[0].props.loading = false
        vueObj.tqPanelProps.tableList.data = []
        vueObj.tqPanelProps.tableList.totalCount = 0
      })
    },

    // 台区信息全线添加获取接口数据
    getTqInfoAllAddFunc(tqInfoParam) {
      const vueObj = this
      if (!this.request.getAddBlackoutDeviceTqInfo) {
        return false
      }
      vueObj.selectedTqPanelProps.tableList.loading = true
      this.request.getAddBlackoutDeviceTqInfo(tqInfoParam).then(res => {
        vueObj.selectedTqPanelProps.tableList.loading = false
        vueObj.tqPanelProps.tableList.loading = false
        vueObj.tqPanelProps.buttons[2].props.loading = false
        if (res.data.successful === true && typeof res.data.resultValue === 'object' && typeof res.data.resultValue.items === 'object') {
          const tqInfoList = []
          for (let i = 0, len = res.data.resultValue.items.length; i < len; i++) {
            const tqInfoObj = res.data.resultValue.items[i].values
            tqInfoObj.id = tqInfoObj.id ? tqInfoObj.id.id : i
            tqInfoObj.tqID = tqInfoObj.TGID
            tqInfoObj.tqName = tqInfoObj.TGNAME
            tqInfoList.push(tqInfoObj)
          }
          vueObj.tqSelectedList = [].concat(tqInfoList)
          vueObj.tqPanelAddHandle()
        }

      }).catch(function() {
        vueObj.selectedTqPanelProps.tableList.loading = false
        vueObj.tqPanelProps.buttons[2].props.loading = false
      })
    },

    fetchEnterpriseApi(param) {
      const vueObj = this
      if (!this.request.getAddBlackoutDeviceEnterprise) {
        return false
      }
      this.request.getAddBlackoutDeviceEnterprise(param).then(res => {
        if (res.data.successful === true && typeof res.data.resultValue === 'object' && typeof res.data.resultValue.items === 'object') {
          const enterpriseList = []
          for (let i = 0, len = res.data.resultValue.items.length; i < len; i++) {
            const enterObj = {}
            enterObj.label = res.data.resultValue.items[i].JC
            enterObj.value = res.data.resultValue.items[i].CIS_ORG_NO
            enterpriseList.push(enterObj)
          }
          vueObj.$refs.statPanelRef.blackoutDeviceEnterpriseOpts = enterpriseList
          // vueObj.statPanelProps.form.data.enterprise = (!!vueObj.userInfoObj && !!vueObj.userInfoObj.sysOrgId)?vueObj.userInfoObj.sysOrgId:'';
        } else {
          vueObj.$refs.statPanelRef.blackoutDeviceEnterpriseOpts = []
          // vueObj.statPanelProps.form.data.enterprise = '';
        }

      })
    }

  }
  // destroyed() {
  //   this.emptyStatFormProp();
  //   this.emptyLineFormProp();
  //   this.emptyTqFormProp();
  //   this.emptyStatTblProp();
  //   this.emptyLineTblProp();
  //   this.emptyTqTblProp();
  //   this.emptySelectedTqTblProp();
  // },
}
</script>

