<template>
  <div class="dialog" v-bind="dialogBig">
    <div class="dialog-content">
      <div class="right-title">
        <!-- <div>系数校准表</div> -->
        <div>{{ outputDsc }}</div>
        <div>
            <span style="  font-size: 18px;font-weight: 600;">
              设备已合格 <span style="color: #91cc75">{{ qualified }}</span> 台/共 <span style="color: green">{{ total }}</span>
              台
            </span>
          <!-- <el-button type="primary" loading
            v-if="activeIndex > 0 && activeIndex < activeTotalNum">通道系数已校准3台/共3台</el-button>
          <el-button type="primary" :disabled="true" v-if="activeIndex === activeTotalNum">通道系数已校准3台/共3台</el-button> -->
        </div>
      </div>
      <div class="container">
        <div class="dialog-left">
          <el-steps direction="vertical" :active="active" :process-status="currentStepStatus" finish-status="success">
            <el-step title="开始"/>
            <el-step>
              <template #title>
                <span>大电压/电流系数下装</span><br/>
                <span class="spanStyle">源输出为：</span><br/>
                <span class="spanStyle" v-if="active > 0">{{ big_V_Download }}</span><br/>
                <span class="spanStyle" v-if="active > 0">{{ big_I_Download }}</span>
                <el-icon v-if="active === 1 " class="loading-box">
                  <el-icon-loading/>
                </el-icon>
              </template>
            </el-step>
            <el-step>
              <template #title>
                <span>小电压/电流系数下装</span><br/>
                <span class="spanStyle">源输出为：</span><br/>
                <span class="spanStyle" v-if="active > 1">{{ small_V_Download }}</span><br/>
                <span class="spanStyle" v-if="active > 1">{{ small_I_Download }}</span>
                <el-icon v-if="active === 2" class="loading-box">
                  <el-icon-loading/>
                </el-icon>
              </template>
            </el-step>
            <el-step>
              <template #title>
                <span>大电压/电流校准</span><br/>
                <span class="spanStyle">源输出为：</span><br/>
                <span class="spanStyle" v-if="active > 2">{{ big_V_Adjust }}</span><br/>
                <span class="spanStyle" v-if="active > 2">{{ big_I_Adjust }}</span>
                <el-icon v-if="active === 3" class="loading-box">
                  <el-icon-loading/>
                </el-icon>
              </template>
            </el-step>
            <el-step>
              <template #title>
                <span>小电压/电流校准</span><br/>
                <span class="spanStyle">源输出为：</span><br/>
                <span class="spanStyle" v-if="active > 3">{{ small_V_Adjust }}</span><br/>
                <span class="spanStyle" v-if="active > 3">{{ small_I_Adjust }}</span>
                <el-icon v-if="active === 4" class="loading-box">
                  <el-icon-loading/>
                </el-icon>
              </template>
            </el-step>
            <el-step title="结束"/>
          </el-steps>
        </div>
        <div class="right-content">
          <el-tabs type="border-card" v-model="editableTabsValue" :active-index="String(activeIndex)">
            <el-tab-pane v-for="(device, index) in name" :key="index" :label="device">
              <template #label>
                    <span class="custom-tabs-label">
                      <span>{{ device }}</span>
                      <el-icon v-if="errorStates[index]" class="icon-style">
                        <Failed/>
                      </el-icon>
                    </span>
              </template>
              <channelsTestTable
                  :tableData="getTableDataForChannel(index)"
                  :big_V_loading="big_V_loadingStates"
                  :curV="CurV">
              </channelsTestTable>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="tsx" setup name="FactorTest">
import {type Device} from '@/api/device/interface/device';
import {Failed} from '@element-plus/icons-vue'
import {onBeforeMount, type Ref, ref, toRef, watch} from 'vue'
import {dialogBig} from '@/utils/elementBind'
import {ElMessageBox} from 'element-plus';
import {getCoefficientCheck} from '@/api/home/channelsTest/index'
import type {ChannelsTest} from '@/api/home/interface/channelsTest';
import type {Plan} from '@/api/plan/interface';
import {useCheckStore} from "@/stores/modules/check";
import {useUserStore} from "@/stores/modules/user";

const checkStore = useCheckStore()
const activeIndex = ref(0)
const activeTotalNum = ref(4)
const qualified = ref(0)
const outputDsc = ref('电压误差为：±0.1Un%； 电流误差为：±0.5%')
const total = ref(0)
const dialogVisible = ref(false)
const active = ref(0)
let timer1: NodeJS.Timeout | null = null; // 声明并初始化 timer1
let timer2: NodeJS.Timeout | null = null; // 同样声明并初始化 timer2
const name = ref<string[]>([])//系数校准所选设备名字数组
const channel = ref<number[]>([])//系数校准所选设备通道数组
const devIdArray = ref<string[]>([])//系数校准所选设备ID数组
const select_Plan = ref<Plan.ReqPlan>()
const planId = ref('')
const isButtonDisabled = ref(false);
const CurV = ref<number>()//额定电压
// 在 setup 函数中
const errorStates = ref(new Array(name.value.length).fill(false));
//const loadingStates = ref(new Array(name.value.length).fill(false)); // 初始化 loading 状态
const big_V_loadingStates = ref(false); // 初始化 大电压大电流下装loading 状态
const small_V_loadingStates = ref(false); // 初始化 小电压小电流下装loading 状态
const big_V_loadingStates2 = ref(false); // 初始化 大电压大电流校准loading 状态
const small_V_loadingStates2 = ref(false); // 初始化 小电压小电流校准loading 状态
const editableTabsValue = ref('0')
const big_V_Download = ref('')
const big_I_Download = ref('')
const small_V_Download = ref('')
const small_I_Download = ref('')
const big_V_Adjust = ref('')
const big_I_Adjust = ref('')
const small_V_Adjust = ref('')
const small_I_Adjust = ref('')
const props = defineProps({
  testStatus: {
    type: String,
    default: 'wait'
  },
  webMsgSend: {
    type: Object,
    default: () => ({})
  },
})
const userStore = useUserStore()
const testStatus = toRef(props, 'testStatus');
const tableDataMap = new Map<number, Ref<ChannelsTest.CoefficientVO[]>>([]);
const currentStepStatus = ref<'error' | 'finish' | 'wait' | 'success' | 'process'>('finish');
const webMsgSend = toRef(props, 'webMsgSend');

onBeforeMount(() => {
  // 初始化
  initData()
})
const initData = () => {
 
  CurV.value = checkStore.devices[0]?.devVolt || 57.74;
  isButtonDisabled.value = false; // 恢复按钮
  select_Plan.value = checkStore.plan
  planId.value = checkStore.devices[0]?.planId || '';
  devIdArray.value = checkStore.devices.map(item => item.deviceId);
  name.value = checkStore.devices.map(item => item.deviceName)
  channel.value = checkStore.devices.map(item => item.chnNum)
  dialogVisible.value = true;
  total.value = name.value.length


  // 初始化 loadingStates 为 false
  // loadingStates.value = new Array(selection.length).fill(false);
  errorStates.value = new Array(checkStore.devices.length).fill(false);

  for (let i = 0; i < channel.value.length; i++) {
    const currentTableData = initializeTableData(dataTemplates, i);
    tableDataMap.set(i, currentTableData)
  }
  //console.log('tableDataMap',tableDataMap);
}


watch(testStatus, function (newValue, oldValue) {
  if (newValue === 'start') {
      // 开始系数校准操作
      handleSubmit()
    }
})

watch(webMsgSend, function (newValue, oldValue) {
  console.log('webMsgSend---code', newValue.code)
  console.log('webMsgSend---requestId', newValue.requestId)
  if (testStatus.value !== 'waiting') {
    if (newValue.code == 10520) {
      ElMessageBox.alert('报文解析异常！', '初始化失败', {
        confirmButtonText: '确定',
        type: 'error',
      })
      TableInit();
    } else if (newValue.code == 10521) {
      ElMessageBox.alert('程控源參数有误！', '初始化失败', {
        confirmButtonText: '确定',
        type: 'error',
      })
      TableInit();
    } else if (newValue.code == 10522) {
      ElMessageBox.alert('测试项解析有误！', '初始化失败', {
        confirmButtonText: '确定',
        type: 'error',
      })
      TableInit();
    } else if (newValue.code == 10523) {
      ElMessageBox.alert('源连接失败！', '初始化失败', {
        confirmButtonText: '确定',
        type: 'error',
      })
      TableInit();
    } else if (newValue.code == 10524) {
      ElMessageBox.alert('获取源控制权失败！', '初始化失败', {
        confirmButtonText: '确定',
        type: 'error',
      })
      TableInit();
    } else if (newValue.code == 10525) {
      ElMessageBox.alert('重置源失败！', '初始化失败', {
        confirmButtonText: '确定',
        type: 'error',
      })
      TableInit();
    } else if (newValue.code == 10527) {
      ElMessageBox.alert('源未进行初始化！', '初始化失败', {
        confirmButtonText: '确定',
        type: 'error',
      })
      TableInit();
    } else if (newValue.code == 10528) {
      ElMessageBox.alert('目标源有误(该用户已控制其他源，在关闭前无法操作新的源)！', '初始化失败', {
        confirmButtonText: '确定',
        type: 'error',
      })
      TableInit();
    } else if (newValue.code == 10529) {
      ElMessageBox.alert('源状态有误，无法响应报文(例如源处于输出状态，无法响应初始化报文)！', '初始化失败', {
        confirmButtonText: '确定',
        type: 'error',
      })
      TableInit();
    } else if (newValue.code == 10550) {
      ElMessageBox.alert(`${newValue.data}设备连接异常！`, '初始化失败', {
        confirmButtonText: '确定',
        type: 'error',
      })
      TableInit();
    } else if (newValue.code == 10551) {
      ElMessageBox.alert(`${newValue.data}设备触发报告异常！`, '初始化失败', {
        confirmButtonText: '确定',
        type: 'error',
      })
      TableInit();
    } else if (newValue.code == 10552) { //todo 10552之后还会发送消息吗？
      ElMessageBox.alert('存在已经初始化步骤,执行自动关闭,请重新发起检测', '初始化失败', {
        confirmButtonText: '确定',
        type: 'error',
      })
      TableInit();
    } else {
      console.log('显示东西code', newValue.code)
      console.log('显示东西requestId', newValue.requestId)
      switch (newValue.requestId) {

        case 'yjc_ytxjy':
          switch (newValue.operateCode) {
            case'INIT_GATHER':
              if (newValue.code == -1) {
                ElMessageBox.alert('源未知异常', '初始化失败', {
                  confirmButtonText: '确定',
                  type: 'error',
                })
                TableInit();
              } else if (newValue.code == 10523) {
                ElMessageBox.alert('源连接失败', '源连接失败', {
                  confirmButtonText: '确定',
                  type: 'error',
                })
                TableInit();
              }
          }
          break;
        case 'YJC_xujy':
          switch (newValue.operateCode) {
            case 'OPER_GATHER':
              if (newValue.code == 10552) {
                ElMessageBox.alert('存在已经初始化步骤,执行自动关闭,请重新发起检测', '初始化失败', {
                  confirmButtonText: '确定',
                  type: 'error',
                })
                TableInit();
              } else if (newValue.code == 10520) {
                ElMessageBox.alert('解析报文异常,执行自动关闭,请重新发起检测', '解析报文异常', {
                  confirmButtonText: '确定',
                  type: 'error',
                })
                TableInit();
              }
              break;
            case 'DATA_REQUEST$02':
              if (newValue.code == 25003) {
                ElMessageBox.alert('相序校验未通过,执行自动关闭,请重新发起检测', '相序校验未通过', {
                  confirmButtonText: '确定',
                  type: 'error',
                })
                TableInit();
              }
              break;
          }
          break;
        case 'yjc_sbtxjy':
          switch (newValue.operateCode) {
            case 'INIT_GATHER$01':
              if (newValue.code == 10550) {
                ElMessageBox.alert('设备连接异常', '设备连接异常', {
                  confirmButtonText: '确定',
                  type: 'error',
                })
                TableInit();
              } else if (newValue.code == 10551) {
                ElMessageBox.alert('设备触发报告异常', '设备触发报告异常', {
                  confirmButtonText: '确定',
                  type: 'error',
                })
                TableInit();
              } else if (newValue.code == 10552) {
                ElMessageBox.alert('存在已经初始化步骤,执行自动关闭,请重新发起检测', '初始化失败', {
                  confirmButtonText: '确定',
                  type: 'error',
                })
                TableInit();
              } else if (newValue.code == 10520) {
                ElMessageBox.alert('解析报文异常,执行自动关闭,请重新发起检测', '解析报文异常', {
                  confirmButtonText: '确定',
                  type: 'error',
                })
                TableInit();
              }

              break;
          }
          break;
        case 'yjc_xyjy':
          switch (newValue.operateCode) {
            case 'VERIFY_MAPPING$01':
              if (newValue.code == 10200) {
                let data = JSON.parse(newValue.data)
                ElMessageBox.alert(`脚本与icd校验失败！icd名称：${data['icdType']} -> 校验项：${data['dataType']}`, '初始化失败', {
                  confirmButtonText: '确定',
                  type: 'error',
                })
                TableInit();
              }
              if (newValue.code == 10500) {
                ElMessageBox.alert(`装置中未找到该icd!`, '初始化失败', {
                  confirmButtonText: '确定',
                  type: 'error',
                })
                TableInit();
              }
              break;
          }
          break;
        case 'Coefficient_Check':
          console.log("Coefficient_Checkactive", active.value);
          switch (newValue.operateCode) {
            case 'big_start'://大电压,电流下装
              big_V_Download.value = 'Ua=Ub=Uc=' + newValue.data.devVolt + 'V';
              big_I_Download.value = 'Ia=Ib=Ic=' + newValue.data.devCurr + 'A';
              break;
            case 'big_end'://大电压,电流下装
              active.value++;
              tableLoading('small', '系数下装')
              break;
          }
          switch (newValue.operateCode) {
            case 'small_start'://小电压,电流下装
              small_V_Download.value = 'Ua=Ub=Uc=' + newValue.data.devVolt + 'V';
              small_I_Download.value = 'Ia=Ib=Ic=' + newValue.data.devCurr + 'A';
              break;
            case 'small_end'://小电压,电流下装
              active.value++;
              tableLoading('big', '系数校准')
              break;
          }
          switch (newValue.operateCode) {
            case 'big_comp_start'://大电压,电流校准
              big_V_Adjust.value = 'Ua=Ub=Uc=' + newValue.data.devVolt + 'V';
              big_I_Adjust.value = 'Ia=Ib=Ic=' + newValue.data.devCurr + 'A';
              break;
            case 'big_comp_end'://大电压,电流校准
              active.value++;
              tableLoading('small', '系数校准')
              break;
          }
          switch (newValue.operateCode) {
            case 'small_comp_start'://小电压,电流校准
              small_V_Adjust.value = 'Ua=Ub=Uc=' + newValue.data.devVolt + 'V';
              small_I_Adjust.value = 'Ia=Ib=Ic=' + newValue.data.devCurr + 'A';
              break;
            case 'small_comp_end'://小电压,电流校准
              active.value++;
              active.value++;

              for (let i = 0; i < name.value.length; i++) {
                const currentDataRef = tableDataMap.get(i);
                if (currentDataRef) {
                  const currentData = currentDataRef.value;
                  // 检查当前数据中有无不合格字段
                  const hasError = checkForErrors(currentData);
                  if (hasError) {
                  } else {
                    qualified.value++;
                  }
                  updateErrorState(i, hasError);
                }
              }

              //editableTabsValue.value = (tabNumber.value).toString();//显示下一个tab
              isButtonDisabled.value = false; // 恢复按钮
              emit('update:testStatus', 'success')
              break;
          }
          switch (newValue.operateCode) {
            case 'DATA_CHNFACTOR$02'://表格
              // 输出 key 为 0 的数组中的第一条 ChannelsTest.CoefficientVO 对象
              console.log('表格', name.value)
              for (let i = 0; i < name.value.length; i++) {
                const targetArrayRef = tableDataMap.get(i);
                if (targetArrayRef) {
                  const targetArray = targetArrayRef.value;
                  if (targetArray.length > 0) {
                    const firstCoefficientVO = targetArray.find(item => item.monitorNum === newValue.data.monitorNum &&
                        item.type === newValue.data.type &&
                        item.desc === newValue.data.desc &&
                        item.devName === newValue.data.devName);
                    if (firstCoefficientVO) { // 检查 firstCoefficientVO 是否存在
                      firstCoefficientVO.aVuData = parseFloat(newValue.data.aVuData).toFixed(4);

                      if (!isNaN(parseFloat(newValue.data.aVuXi)) && isFinite(newValue.data.aVuXi)) {
                        firstCoefficientVO.aVuXi = (parseFloat(newValue.data.aVuXi) / 10000).toFixed(4);
                      } else {
                        firstCoefficientVO.aVuXi = newValue.data.aVuXi;
                      }

                      firstCoefficientVO.bVuData = parseFloat(newValue.data.bVuData).toFixed(4);

                      if (!isNaN(parseFloat(newValue.data.bVuXi)) && isFinite(newValue.data.bVuXi)) {
                        firstCoefficientVO.bVuXi = (parseFloat(newValue.data.bVuXi) / 10000).toFixed(4);
                      } else {
                        firstCoefficientVO.bVuXi = newValue.data.bVuXi;
                      }

                      firstCoefficientVO.cVuData = parseFloat(newValue.data.cVuData).toFixed(4);

                      if (!isNaN(parseFloat(newValue.data.cVuXi)) && isFinite(newValue.data.cVuXi)) {
                        firstCoefficientVO.cVuXi = (parseFloat(newValue.data.cVuXi) / 10000).toFixed(4);
                      } else {
                        firstCoefficientVO.cVuXi = newValue.data.cVuXi;
                      }

                      firstCoefficientVO.aIeData = parseFloat(newValue.data.aIeData).toFixed(4);

                      if (!isNaN(parseFloat(newValue.data.aIeXi)) && isFinite(newValue.data.aIeXi)) {
                        firstCoefficientVO.aIeXi = (parseFloat(newValue.data.aIeXi) / 10000).toFixed(4);
                      } else {
                        firstCoefficientVO.aIeXi = newValue.data.aIeXi;
                      }

                      firstCoefficientVO.bIeData = parseFloat(newValue.data.bIeData).toFixed(4);

                      if (!isNaN(parseFloat(newValue.data.bIeXi)) && isFinite(newValue.data.bIeXi)) {
                        firstCoefficientVO.bIeXi = (parseFloat(newValue.data.bIeXi) / 10000).toFixed(4);
                      } else {
                        firstCoefficientVO.bIeXi = newValue.data.bIeXi;
                      }

                      firstCoefficientVO.cIeData = parseFloat(newValue.data.cIeData).toFixed(4);

                      if (!isNaN(parseFloat(newValue.data.cIeXi)) && isFinite(newValue.data.cIeXi)) {
                        firstCoefficientVO.cIeXi = (parseFloat(newValue.data.cIeXi) / 10000).toFixed(4);
                      } else {
                        firstCoefficientVO.cIeXi = newValue.data.cIeXi;
                      }

                      firstCoefficientVO.aV = newValue.data.aV;
                      firstCoefficientVO.bV = newValue.data.bV;
                      firstCoefficientVO.cV = newValue.data.cV;
                      firstCoefficientVO.aI = newValue.data.aI;
                      firstCoefficientVO.bI = newValue.data.bI;
                      firstCoefficientVO.cI = newValue.data.cI;
                      console.log(newValue.data.devName + '对象:', firstCoefficientVO);
                      activeIndex.value++;
                    } else {
                      console.log('未找到匹配的' + newValue.data.devName + '对象');
                    }
                  } else {
                    console.log(newValue.data.devName + '数组为空');
                  }
                } else {
                  console.log('未找到' + newValue.data.devName + '对应的数组');
                }
              }
              break;
          }
          break;
        case 'socket_timeout':
          switch (newValue.operateCode) {
            case 'VOLTAGE':
              ElMessageBox.alert('连接超时！', '连接超时', {
                confirmButtonText: '确定',
                type: 'error',
              })
              TableInit();
              break;
          }
          break;
        case 'connect':
          switch (newValue.operateCode) {
            case "Source":
              ElMessageBox.alert('源服务端连接失败', '源服务端连接失败', {
                confirmButtonText: '确定',
                type: 'error',
              })
              TableInit();
              break;
            case "Dev":
              ElMessageBox.alert('设备服务端连接失败', '设备服务端连接失败', {
                confirmButtonText: '确定',
                type: 'error',
              })
              TableInit();
              break;
          }

          break;
      }
    }
  }
})

//出错系数检测初始化
const TableInit = () => {
  console.log("出错系数检测",active.value);
  emit('update:testStatus', 'test_init_fail')
  // isButtonDisabled.value = false; // 恢复按钮
  // for (let i = 0; i < channel.value.length; i++) {
  //   const currentTableData = initializeTableData(dataTemplates, i);
  //   tableDataMap.set(i, currentTableData)
  // }
  // activeIndex.value = 0
  // qualified.value = 0
  // active.value = 0

}

//按行图标转动
const tableLoading = (type: string, desc: string) => {
  console.log('转动',channel.value)
  for (let i = 0; i < channel.value.length; i++) {

    const targetArrayRef = tableDataMap.get(i);
    if (targetArrayRef) {
      const targetArray = targetArrayRef.value;
      if (targetArray.length > 0) {
        for (let j = 0; j < channel.value[i]; j++) {
          const firstCoefficientVO = targetArray.find(item => item.monitorNum === (j + 1).toString() &&
              item.type === type &&
              item.desc === desc);
          if (firstCoefficientVO) {
            firstCoefficientVO.loading = true;
          }
        }
      }
    }else{
      console.log('不转了')
    }
  }
}

const dataTemplates: ChannelsTest.CoefficientVO[] = [
  {
    monitorNum: '1',
    desc: '系数下装',
    type: 'big',
    aVuData: '—',
    aVuXi: '—',
    bVuData: '—',
    bVuXi: '—',
    cVuData: '—',
    cVuXi: '—',
    aIeData: '—',
    aIeXi: '—',
    bIeData: '—',
    bIeXi: '—',
    cIeData: '—',
    cIeXi: '—',
    loading: false,
    devName: '',
    aV: '—',
    bV: '—',
    cV: '—',
    aI: '—',
    bI: '—',
    cI: '—',
  },
  {
    monitorNum: '2',
    desc: '系数下装',
    type: 'small',
    aVuData: '—',
    aVuXi: '—',
    bVuData: '—',
    bVuXi: '—',
    cVuData: '—',
    cVuXi: '—',
    aIeData: '—',
    aIeXi: '—',
    bIeData: '—',
    bIeXi: '—',
    cIeData: '—',
    cIeXi: '—',
    loading: false,
    devName: '',
    aV: '—',
    bV: '—',
    cV: '—',
    aI: '—',
    bI: '—',
    cI: '—',
  },
  {
    monitorNum: '3',
    desc: '系数校准',
    type: 'big',
    aVuData: '—',
    aVuXi: '—',
    bVuData: '—',
    bVuXi: '—',
    cVuData: '—',
    cVuXi: '—',
    aIeData: '—',
    aIeXi: '—',
    bIeData: '—',
    bIeXi: '—',
    cIeData: '—',
    cIeXi: '—',
    loading: false,
    devName: '',
    aV: '—',
    bV: '—',
    cV: '—',
    aI: '—',
    bI: '—',
    cI: '—',
  },
  {
    monitorNum: '4',
    desc: '系数校准',
    type: 'small',
    aVuData: '—',
    aVuXi: '—',
    bVuData: '—',
    bVuXi: '—',
    cVuData: '—',
    cVuXi: '—',
    aIeData: '—',
    aIeXi: '—',
    bIeData: '—',
    bIeXi: '—',
    cIeData: '—',
    cIeXi: '—',
    loading: false,
    devName: '',
    aV: '—',
    bV: '—',
    cV: '—',
    aI: '—',
    bI: '—',
    cI: '—',
  },

];


// 更新错误状态的方法
const updateErrorState = (index: number, hasError: boolean) => {
  errorStates.value[index] = hasError;
};


// const emit = defineEmits<{
  
//   (e: 'submitClicked', callback: (resolve: (value: boolean) => void) => void): void;
// }>();

const emit = defineEmits(['update:testStatus']);

const getTableDataForChannel = (index: number): any[] => {
  const data = tableDataMap.get(index);
  return data ? data.value : [];
}


watch(activeIndex, function (newValue, oldValue) {
  if (activeIndex.value === 1) {
    outputDsc.value = "电压误差为：±0.1Un%； 电流误差为：±0.5%";
    // 当前源输出为：Ua=Ub=Uc=57.74V Ia=Ib=Ic=1A"
  }
})

// 示例的 checkForErrors 函数，根据实际需求进行调整
const checkForErrors = (data: ChannelsTest.CoefficientVO[]): boolean => {
  // 这里假设不合格字段的标准是 status 为 '不合格' 或 isValid 为 false
  return data.some(item =>
      item.aVuXi === '不合格' ||
      item.bVuXi === '不合格' ||
      item.cVuXi === '不合格' ||
      item.aIeXi === '不合格' ||
      item.bIeXi === '不合格' ||
      item.cIeXi === '不合格'
  );
};

const handleSubmit = async () => {
  // 创建一个 Promise 来等待父组件的回调
  // const response = await new Promise<boolean>((resolve) => {
  //   emit('submitClicked', resolve);
  // });

  // if (!response) {
  //   return;
  // }()

  isButtonDisabled.value = true; // 禁用按钮
  tableLoading('big', '系数下装')
/*  await getCoefficientCheck({
    userPageId: "cdf",
    devIds: devIdArray.value,
    planId: planId.value,
    errorSysId: select_Plan.value?.errorSysId,
    scriptId: select_Plan.value?.scriptId,
    operateType: '0', // '0'为预检测、‘1‘为正式检测
    userId:userStore.userInfo.id
  })*/
  active.value++;  

  console.log('开始检测active.value', active.value)
};

// 提取初始化并填充 currentTableData 的函数
const initializeTableData = (templates: ChannelsTest.CoefficientVO[], index: number): Ref<ChannelsTest.CoefficientVO[]> => {
  const currentTableData = ref<ChannelsTest.CoefficientVO[]>([]);
  for (let j = 0; j < channel.value[index]; j++) {
    templates.forEach((template) => {
      // 使用解构赋值排除 id 和 MonitorIdx 属性
      const {devName, monitorNum: __, ...rest} = template;
      currentTableData.value.push({
        monitorNum: (j + 1).toString(),
        devName: name.value[index],
        ...rest,
      });
    });
  }
  return currentTableData;
};

</script>

<style scoped lang="scss">
/* 确保 el-icon-loading 的动画效果没有被覆盖 */
.loading-box {
  animation: rotate 2s linear infinite;
  font-size: 30px; /* 增大图标的大小 */
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.right-title {
  display: flex;
  flex-direction: row;
  /* 横向排列 */
  justify-content: space-between;
  margin-bottom: 10px;
}

.custom-tabs-label .el-icon {
  vertical-align: middle;
}

.custom-tabs-label span {
  vertical-align: middle;
  margin-left: 4px;
}

.dialog-content {
  height: 510px;
}

.el-tabs--border-card {
  height: 470px;
}

/* .el-icon svg {
  color: #ff7171;
} */

.icon-style {
  color: #ff7171;
}

.container {
  display: flex;
}

.dialog-left {
  flex: 1;
}

.right-content {
  flex: 6;
}

.spanStyle {
  font-size: 14px;
}

</style>