<template>
  <div class='table-box'>
    <el-tabs type="border-card">
      <el-tab-pane label="检测相关配置">
        <div>
          <el-form :model="TestConfigForm" ref='testConfigFormRef' :rules='rules'>
            <el-divider>检测配置</el-divider>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label='一键检测方式' prop='autoGenerate' :label-width="140">
                  <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="110">
<!--                  <el-input-number v-model='TestConfigForm.maxTime' :min='1' :max='999' onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"/>-->
                  <el-input number v-model.number='TestConfigForm.maxTime' placeholder="请输入检测最大次数" onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"/>
                </el-form-item>
              </el-col>
<!--              <el-col :span="8">-->
<!--                <el-form-item label='数据处理原则' prop='dataRule' :label-width="140">-->
<!--                  <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-form>
          <el-form :model="RegResForm" ref='dialogFormRef' :rules='rules'>
            <el-divider>有效数据配置</el-divider>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="录波数据有效组数" prop="waveRecord" :label-width="140">
                  <el-input v-model.number='RegResForm.waveRecord' :placeholder="placeholder" :disabled="IsMode"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="实时数据有效组数" prop="realTime" :label-width="140">
                  <el-input number v-model.number='RegResForm.realTime' placeholder="请输入实时数据有效组数" onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="统计数据有效组数" prop="statistics" :label-width="140">
                  <el-input number v-model.number='RegResForm.statistics' placeholder="请输入统计数据有效组数" onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="闪变数据有效组数" prop="flicker" :label-width="140">
                  <el-input number v-model.number='RegResForm.flicker' placeholder="请输入闪变数据有效组数" onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm()">保存配置</el-button>
        </div>
      </el-tab-pane>
      <el-tab-pane label="日志配置">
        <div>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label='调试日志配置' :label-width="140">
                <el-select 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='调试日志记录等级' :label-width="140">
                <el-select clearable placeholder="请选择日志记录等">
                  <el-option label="FATAL(致命错误)及以上" :value="0"></el-option>
                  <el-option label="ERROR(一般错误)及以上" :value="1"></el-option>
                  <el-option label="WARN(警告)及以上" :value="2"></el-option>
                  <el-option label="INFO(一般信息)及以上" :value="3"></el-option>
                  <el-option label="DEBUG(调试信息)及以上" :value="4"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label='操作日志配置' :label-width="140">
                <el-select clearable placeholder="请选择操作日志配置">
                  <el-option label="开启" :value="0"></el-option>
                  <el-option label="关闭" :value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <div class="dialog-footer">
          <el-button type="primary">保存配置</el-button>
        </div>
      </el-tab-pane>
      <el-tab-pane label="数据删除备份配置">
        <div>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label='原始数据备份频率' :label-width="140">
                <el-select clearable placeholder="请选择原始数据备份频率">
                  <el-option label="每月" :value="0"></el-option>
                  <el-option label="每三月" :value="1"></el-option>
                  <el-option label="每年" :value="1"></el-option>
                  <el-option label="从不" :value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label='原始数据备份路径' :label-width="140">
                <el-col :span="16">
                  <el-input/>
                </el-col>
                <el-col :span="8">
                  <el-button type='primary'>立即进行备份</el-button>
                </el-col>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label='原始数据备份时间' :label-width="140">
                <el-select clearable placeholder="请选择原始数据备份时间">
                  <el-option label="03:00" :value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label='原始数据删除配置' :label-width="140">
                <el-select clearable placeholder="请选择原始数据删除配置">
                  <el-option label="从不" :value="0"></el-option>
                  <el-option label="大于半年进行删除" :value="1"></el-option>
                  <el-option label="大于一年进行删除" :value="2"></el-option>
                  <el-option label="大于两年进行删除" :value="3"></el-option>
                  <el-option label="大于五年进行删除" :value="4"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label='原始数据删除时间' :label-width="140">
                <el-select clearable placeholder="请选择原始数据删除时间">
                  <el-option label="05:00" :value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label='调试日志保存策略' :label-width="140">
                <el-select clearable placeholder="请选择调试日志保存策略">
                  <el-option label="一个月" :value="0"></el-option>
                  <el-option label="三个月" :value="1"></el-option>
                  <el-option label="六个月" :value="2"></el-option>
                  <el-option label="从不删除" :value="3"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label='操作日志保存策略' :label-width="140">
                <el-select clearable placeholder="请选择操作日志保存策略">
                  <el-option label="三个月" :value="0"></el-option>
                  <el-option label="六个月或大于100万条" :value="1"></el-option>
                  <el-option label="从不" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <div class="dialog-footer">
          <el-button type="primary">保存配置</el-button>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup lang='tsx'>
import {useDictStore} from '@/stores/modules/dict'
import {computed, onMounted, Ref, 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, FormItemRule} from 'element-plus'
import {useModeStore} from '@/stores/modules/mode'; // 引入模式 store
defineOptions({
  name: 'base'
})

const modeStore = useModeStore();
const dictStore = useDictStore()
const testConfigFormRef = ref()
const dialogFormRef = ref()
const mode = ref()
const IsMode = ref(false)
const placeholder = ref()
const TestConfigList = ref<Base.ResTestConfig>()
const RegResList = ref<VersionRegister.ResSys_Reg_Res>()


// 初始化时获取
onMounted(async () => {
  mode.value = modeStore.currentMode;//pinia中获取当前是那个模块进来的
  const response = await getTestConfig()
  TestConfigForm.value = response.data as unknown as Base.ResTestConfig
  const patternId = dictStore.getDictData('Pattern').find(item => item.name === mode.value)?.id//获取数据字典中对应的id
  RegResForm.value.type = patternId || '';
  const response2 = await getRegRes(RegResForm.value)
  RegResForm.value = response2.data as unknown as VersionRegister.ResSys_Reg_Res
  //只有比对式有录波
  if (mode.value == '比对式') {
    placeholder.value = '请输入录波数据有效组数'
    IsMode.value = false
  } else {
    placeholder.value = '/'
    IsMode.value = true
  }
})

const TestConfigForm = ref<Base.ResTestConfig>({
  id: '',
  autoGenerate: 0,
  maxTime: 3,
  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>({
  id: '',
  waveRecord: 0,
  realTime: 20,
  statistics: 5,
  flicker: 1,
})

// 定义弹出组件元信息
const rules = computed(() => {
  const baseRules: Ref<Record<string, Array<FormItemRule>>> = ref({
    maxTime: [
      {required: true, trigger: 'blur', message: '检测最大次数必填!'},
      {pattern: /^(?:[1-9]|[1-9][0-9]|[1-9][0-9]{2})$/, message: '检测最大次数为1~999的正整数', trigger: 'blur'}
    ],
    realTime: [
      {required: true, trigger: 'blur', message: '实时数据有效组数必填!'},
      {pattern: /^(?:[1-9]|[1-9][0-9]|[1-9][0-9]{2})$/, message: '实时数据有效组数为1~999的正整数', trigger: 'blur'}
    ],
    statistics: [
      {required: true, trigger: 'blur', message: '统计数据有效组数必填!'},
      {pattern: /^(?:[1-9]|[1-9][0-9]|[1-9][0-9]{2})$/, message: '统计数据有效组数为1~999的正整数', trigger: 'blur'}
    ],
    flicker: [
      {required: true, trigger: 'blur', message: '闪变数据有效组数必填!'},
      {pattern: /^(?:[1-9]|[1-9][0-9]|[1-9][0-9]{2})$/, message: '闪变数据有效组数为1~999的正整数', trigger: 'blur'}
    ],
  });


  if (mode.value == '比对式') {
    baseRules.value.waveRecord = [
      {required: true, trigger: 'blur', message: '录波数据有效组数必填!'},
      {pattern: /^(?:[1-9]|[1-9][0-9]|[1-9][0-9]{2})$/, message: '录波数据有效组数为1~999的正整数', trigger: 'blur'}
    ];
  }

  return baseRules;
})


const submitForm = async () => {
  try {
    testConfigFormRef.value?.validate(async (valid: boolean) => {
      if (valid) {
        await updateTestConfig(TestConfigForm.value);

        dialogFormRef.value?.validate(async (valid: boolean) => {
          if (valid) {
            //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>