<template>
  <el-dialog title="选择测试内容" v-model='dialogVisible' @close="handleClose" v-bind="dialogSmall">
    <div>
      <el-form ref="dialogFormRef" :model="formContent">
        <el-form-item prop="preTest" :label-width="50">
          <el-checkbox v-model="formContent.preTest" label="预检测"/>
        </el-form-item>
        <el-form-item v-if="checkStore.plan.timeCheck===1" prop="timeTest" :label-width="50">
          <el-checkbox v-model="formContent.timeTest" label="守时检测"/>
        </el-form-item>
        <el-form-item prop="channelsTest" :label-width="50">
          <el-checkbox v-model="formContent.channelsTest" label="系数校准"/>
        </el-form-item>
        <el-form-item prop="test" :label-width="50">
          <el-checkbox v-model="formContent.test" label="正式检测"/>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleStart">确定</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup lang='tsx' name='selectTestItemPopup'>
import {dialogSmall} from "@/utils/elementBind";
import {ref} from "vue";
import {useCheckStore} from "@/stores/modules/check";
import type {CheckData} from "@/api/check/interface";
import {ElMessageBox} from "element-plus";

const emit = defineEmits(['openTestDialog'])
const checkStore = useCheckStore();

const dialogFormRef = ref()


const dialogVisible = ref(false)
const formContent = reactive<CheckData.SelectTestItem>({preTest: true, timeTest: false, channelsTest: false, test: false})

const open = async () => {
  resetFormContent()
  checkStore.setSelectTestItems(formContent)
  dialogVisible.value = true
}

// 清空表单内容
const resetFormContent = () => {
  Object.assign(formContent, {preTest: true, channelsTest: false, timeTest: false, test: false})
}

const handleStart = () => {
  let count = 0
  for (let key in formContent) {
    if (formContent[key]) {
      count++
    }
  }
  if (count === 0) {
    ElMessage.warning('请选择测试内容！')
  } else {
    if(formContent.channelsTest){
      const factorFlagArray = ref<string[]>([]) // 初始化为空数组
      for (let i = 0; i < checkStore.devices.length; i++) {
        const factorFlag = checkStore.devices[i].factorFlag
        if (factorFlag !== undefined && factorFlag !== null && factorFlag === 0) {
          factorFlagArray.value.push(checkStore.devices[i].deviceName.toString())
        }
      }
      if (factorFlagArray.value.length > 0) {
        const factorFlags = factorFlagArray.value.join(', ')
        ElMessageBox.confirm(
            factorFlags + '设备不支持系数校准，请重新选择',
            '提示',
            {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
            },
        )
        return
      }
    }
    checkStore.setCheckType(0)
    checkStore.setSelectTestItems({...formContent})
    handleClose()
    emit('openTestDialog')
  }
}

// 关闭弹窗
const handleClose = () => {
  dialogVisible.value = false
  // 清空dialogForm中的值
  resetFormContent()
  dialogFormRef.value?.resetFields()
}

defineExpose({open})

</script>


<style scoped lang="scss">

</style>