<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="年份" v-bind="validateInfos.year">
            <a-input v-model:value="formData.year" placeholder="请输入年份" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="门类代码" v-bind="validateInfos.categoryCode">
            <a-input v-model:value="formData.categoryCode" placeholder="请输入门类代码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="门类名称" v-bind="validateInfos.categoryName">
            <a-input v-model:value="formData.categoryName" placeholder="请输入门类名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="学科代码" v-bind="validateInfos.subjectCode">
            <a-input v-model:value="formData.subjectCode" placeholder="请输入学科代码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="学科领域" v-bind="validateInfos.disciplineField">
            <a-input v-model:value="formData.disciplineField" placeholder="请输入学科领域" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="学位性质" v-bind="validateInfos.degreeNature">
            <a-input v-model:value="formData.degreeNature" placeholder="请输入学位性质" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="专业代码" v-bind="validateInfos.majorCode">
            <a-input v-model:value="formData.majorCode" placeholder="请输入专业代码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="专业名称" v-bind="validateInfos.majorName">
            <a-input v-model:value="formData.majorName" placeholder="请输入专业名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="研究方向" v-bind="validateInfos.researchDirection">
            <a-input v-model:value="formData.researchDirection" placeholder="请输入研究方向" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="院校编码" v-bind="validateInfos.schoolMasterCode">
            <a-input v-model:value="formData.schoolMasterCode" placeholder="请输入院校编码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="院校名称" v-bind="validateInfos.schoolName">
            <a-input v-model:value="formData.schoolName" placeholder="请输入院校名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="所在地" v-bind="validateInfos.location">
            <a-input v-model:value="formData.location" placeholder="请输入所在地" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="研究生院" v-bind="validateInfos.graduateSchool">
            <a-input v-model:value="formData.graduateSchool" placeholder="请输入研究生院" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="自划线" v-bind="validateInfos.selfMarking">
            <a-input v-model:value="formData.selfMarking" placeholder="请输入自划线" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="校博士点" v-bind="validateInfos.schoolDoctoralProgram">
            <a-input v-model:value="formData.schoolDoctoralProgram" placeholder="请输入校博士点" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="博士专业" v-bind="validateInfos.doctoralMajor">
            <a-input v-model:value="formData.doctoralMajor" placeholder="请输入博士专业" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="第四轮学科评估" v-bind="validateInfos.fourthRoundSubjectEvaluation">
            <a-input v-model:value="formData.fourthRoundSubjectEvaluation" placeholder="请输入第四轮学科评估" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="双一流学科" v-bind="validateInfos.doubleFirstClassDisciplines">
            <a-input v-model:value="formData.doubleFirstClassDisciplines" placeholder="请输入双一流学科" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="考试方式" v-bind="validateInfos.examinationMethods">
            <a-input v-model:value="formData.examinationMethods" placeholder="请输入考试方式" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="院系所" v-bind="validateInfos.departmentsAndFaculties">
            <a-input v-model:value="formData.departmentsAndFaculties" placeholder="请输入院系所" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="学习方式" v-bind="validateInfos.learningStyle">
            <a-input v-model:value="formData.learningStyle" placeholder="请输入学习方式" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="指导教师" v-bind="validateInfos.teacher">
            <a-input v-model:value="formData.teacher" placeholder="请输入指导教师" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="拟招生人数" v-bind="validateInfos.intendedEnrollment">
            <a-input v-model:value="formData.intendedEnrollment" placeholder="请输入拟招生人数" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="接收退役" v-bind="validateInfos.acceptRetirement">
            <a-input v-model:value="formData.acceptRetirement" placeholder="请输入接收退役" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="详细链接" v-bind="validateInfos.detailedLink">
            <a-input v-model:value="formData.detailedLink" placeholder="请输入详细链接" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="备注" v-bind="validateInfos.detail">
            <a-input v-model:value="formData.detail" placeholder="请输入备注" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="政治" v-bind="validateInfos.politics">
            <a-input v-model:value="formData.politics" placeholder="请输入政治" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="外语" v-bind="validateInfos.foreignLanguage">
            <a-input v-model:value="formData.foreignLanguage" placeholder="请输入外语" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="业务课一" v-bind="validateInfos.businessCourse1">
            <a-input v-model:value="formData.businessCourse1" placeholder="请输入业务课一" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="业务课二" v-bind="validateInfos.businessCourse2">
            <a-input v-model:value="formData.businessCourse2" placeholder="请输入业务课二" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../YxSchoolGraduateDegree.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    year: '',   
    categoryCode: '',   
    categoryName: '',   
    subjectCode: '',   
    disciplineField: '',   
    degreeNature: '',   
    majorCode: '',   
    majorName: '',   
    researchDirection: '',   
    schoolMasterCode: '',   
    schoolId: '',   
    schoolName: '',   
    location: '',   
    graduateSchool: '',   
    selfMarking: '',   
    schoolDoctoralProgram: '',   
    doctoralMajor: '',   
    fourthRoundSubjectEvaluation: '',   
    doubleFirstClassDisciplines: '',   
    examinationMethods: '',   
    departmentsAndFaculties: '',   
    learningStyle: '',   
    teacher: '',   
    intendedEnrollment: '',   
    acceptRetirement: '',   
    detailedLink: '',   
    detail: '',   
    politics: '',   
    foreignLanguage: '',   
    businessCourse1: '',   
    businessCourse2: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
