<template>
    <div class='table-box' >
      <el-tabs type="border-card">
            <el-tab-pane label="检测相关配置">
                <div class="form-grid">
                  <el-divider >检测配置</el-divider>
                        <el-row  :gutter="24" >
                            <el-col :span="8">
                              <el-form-item label='一键检测方式'  prop='autoGenerate' :label-width="100">
                                    <el-select v-model="TestConfigForm.autoGenerate"  clearable placeholder="请选择一键检测方式" >
                                      <el-option label="只检测,报告后续手动生成" :value="0"></el-option>
                                      <el-option label="检测和生成报告同时进行" :value="1"></el-option>
                                    </el-select>
                                </el-form-item>          
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label='复检最大次数'  prop='maxTime' :label-width="100">
                                  <el-input-number  v-model='TestConfigForm.maxTime' :min='1' :max='999' />
                                </el-form-item>           
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label='数据处理原则'  prop='dataRule' :label-width="100">
                                    <el-select v-model="TestConfigForm.dataRule"  clearable placeholder="请选择数据处理原则" >
                                    <el-option 
                                    v-for="item in dictStore.getDictData('Data_Rule')"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                                    />
                                </el-select>
                                </el-form-item>           
                            </el-col>
                            
                        </el-row>   
                        <el-divider >有效数据配置</el-divider>
                        <el-row  :gutter="24" >
                            <el-col :span="8">
                                <el-form-item label="录波数据有效组数" prop="waveRecord" :label-width="125">
                                    <el-input  v-model.number='RegResForm.waveRecord' placeholder="/" disabled/>
                                </el-form-item>           
                            </el-col>
                            <el-col :span="8">
                              <el-form-item label="实时数据有效组数" prop="realTime" :label-width="125">
                                    <el-input number v-model.number='RegResForm.realTime' placeholder="请输入实时数据有效组数"/>
                                </el-form-item>   
                            </el-col>
                            <el-col :span="8">
                              <el-form-item label="统计数据有效组数" prop="statistics" :label-width="125">
                                    <el-input number v-model.number='RegResForm.statistics' placeholder="请输入统计数据有效组数"/>
                                </el-form-item>      
                            </el-col>
                            <el-col :span="8">
                              <el-form-item label="闪变数据有效组数" prop="flicker" :label-width="125">
                                    <el-input number v-model.number='RegResForm.flicker' placeholder="请输入闪变数据有效组数"/>
                                </el-form-item>      
                            </el-col>
                        </el-row>   
                        
                </div>
                <div class="dialog-footer">
                  <el-button type="primary"  @click="submitForm()">保存配置</el-button>  
                </div>
            </el-tab-pane>
            <el-tab-pane label="日志配置">

            </el-tab-pane>
            <el-tab-pane label="数据删除备份配置">

            </el-tab-pane>
      </el-tabs>
    </div>
  </template>
  
  <script setup lang='tsx''>
  import { type Dict } from '@/api/system/dictionary/interface'
  import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
  import PqPopup from '@/views/system/dictionary/dictPq/components/pqPopup.vue'
  import { useDictStore } from '@/stores/modules/dict'
  import { useHandleData } from '@/hooks/useHandleData'
  import { onMounted, reactive, ref } from 'vue'
  import {type  Base } from '@/api/system/base/interface'
  import {type VersionRegister } from '@/api/system/versionRegister/interface'
  import {getTestConfig,updateTestConfig } from '@/api/system/base/index'
  import {getRegRes,updateRegRes } from '@/api/system/versionRegister/index'
import { ElMessage } from 'element-plus'
import { el } from 'element-plus/es/locale'
  const dictStore = useDictStore()
  const TestConfigForm = ref<Base.ResTestConfig>({
    id: '',
    autoGenerate: 0,
    maxTime: 3,
    dataRule: '',
    state: 1, //状态
  })

  const RegResForm = ref<VersionRegister.ResSys_Reg_Res>({
        id: '',     //版本注册表Id
        type: '', //版本类型（模拟式、数字式、比对式）
        code: '', //注册码
        licenseKey: '', //密钥
        expireDate: '', //到期时间
        waveRecord: 0,
        realTime: 20,
        statistics: 5,
        flicker: 1,
        state: 1, //状态
})

const RegResForm2 = ref<VersionRegister.Sys_Reg_Res>({
        waveRecord: 0,
        realTime: 20,
        statistics: 5,
        flicker: 1,
})



  const TestConfigList = ref<Base.ResTestConfig>()
  const RegResList = ref<VersionRegister.ResSys_Reg_Res>()
  // 初始化时获取
  onMounted(async () => {
    const response = await getTestConfig()
    TestConfigForm.value = response.data as unknown as Base.ResTestConfig

    const patternId  = dictStore.getDictData('Pattern').find(item=>item.name==='模拟式')?.id
    RegResForm.value.type = patternId || '';
    const response2 = await getRegRes(RegResForm.value)
    RegResForm.value = response2.data as unknown as VersionRegister.ResSys_Reg_Res

  })

  const submitForm = async () => {
    try {
          if (TestConfigForm.value.id) {
              await updateTestConfig(TestConfigForm.value);

               // 提取并传递 4 个参数
              const { id,waveRecord, realTime, statistics, flicker } = RegResForm.value
              RegResForm2.value = { id,waveRecord, realTime, statistics, flicker }

              if(RegResForm.value.waveRecord == null){
                 RegResForm2.value.waveRecord = -1
              }
              await updateRegRes(RegResForm2.value);
              
              ElMessage.success({ message: `保存配置成功！` })
            } 
        } catch (err) {
          console.error('验证过程中出现错误', err)
        }
  }

  </script>
    <style scoped>
    .dialog-footer {
      float: right;
    }
    </style>