<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.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.oldSchoolName">
            <a-input v-model:value="formData.oldSchoolName" placeholder="请输入旧院校名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="官方网址" v-bind="validateInfos.schoolWebsiteAddress">
            <a-input v-model:value="formData.schoolWebsiteAddress" placeholder="请输入官方网址" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="学校电话" v-bind="validateInfos.schoolPhone">
            <a-input v-model:value="formData.schoolPhone" placeholder="请输入学校电话" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="电子邮箱" v-bind="validateInfos.schoolEmail">
            <a-input v-model:value="formData.schoolEmail" placeholder="请输入电子邮箱" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="学校图标" v-bind="validateInfos.schoolIcon">
	          <j-image-upload  v-model:value="formData.schoolIcon" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="办学日期" v-bind="validateInfos.schoolOpenDate">
            <a-input v-model:value="formData.schoolOpenDate" placeholder="请输入办学日期" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="省份" v-bind="validateInfos.province">
            <a-input v-model:value="formData.province" placeholder="请输入省份" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="城市" v-bind="validateInfos.city">
            <a-input v-model:value="formData.city" placeholder="请输入城市" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="区域" v-bind="validateInfos.area">
            <a-input v-model:value="formData.area" placeholder="请输入区域" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="详细地址" v-bind="validateInfos.street">
            <a-input v-model:value="formData.street" placeholder="请输入详细地址" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="隶属于" v-bind="validateInfos.affiliation">
            <a-input v-model:value="formData.affiliation" placeholder="请输入隶属于" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="办学性质" v-bind="validateInfos.schoolNature">
            <a-input v-model:value="formData.schoolNature" placeholder="请输入办学性质" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="院校类型" v-bind="validateInfos.institutionType">
            <a-input v-model:value="formData.institutionType" placeholder="请输入院校类型" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="办学类型" v-bind="validateInfos.schoolType">
            <a-input v-model:value="formData.schoolType" placeholder="请输入办学类型" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="学生人数" v-bind="validateInfos.studentNum">
            <a-input v-model:value="formData.studentNum" placeholder="请输入学生人数" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="基本信息" v-bind="validateInfos.baseInfo">
	          <j-editor v-model:value="formData.baseInfo" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="标签" v-bind="validateInfos.tags">
            <a-input v-model:value="formData.tags" placeholder="请输入标签" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="学校批次" v-bind="validateInfos.schoolBatch">
            <a-input v-model:value="formData.schoolBatch" placeholder="请输入学校批次" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="985工程" v-bind="validateInfos.is985">
	          <a-input-number v-model:value="formData.is985" placeholder="请输入985工程" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="211工程" v-bind="validateInfos.is211">
	          <a-input-number v-model:value="formData.is211" placeholder="请输入211工程" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="强基计划" v-bind="validateInfos.isStrengthen">
	          <a-input-number v-model:value="formData.isStrengthen" placeholder="请输入强基计划" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="一流大学" v-bind="validateInfos.yldx">
            <a-input v-model:value="formData.yldx" placeholder="请输入一流大学" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="国重/省重" v-bind="validateInfos.gzsz">
            <a-input v-model:value="formData.gzsz" placeholder="请输入国重/省重" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="保研率" v-bind="validateInfos.byl">
            <a-input v-model:value="formData.byl" placeholder="请输入保研率" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="国家特色专业" v-bind="validateInfos.gjtszy">
            <a-input v-model:value="formData.gjtszy" placeholder="请输入国家特色专业" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="省特色专业" v-bind="validateInfos.stszy">
            <a-input v-model:value="formData.stszy" placeholder="请输入省特色专业" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否国重点" v-bind="validateInfos.gzd">
            <a-input v-model:value="formData.gzd" placeholder="请输入是否国重点" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="世界一流" v-bind="validateInfos.sjyl">
            <a-input v-model:value="formData.sjyl" placeholder="请输入世界一流" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否双一流" v-bind="validateInfos.sfsyl">
            <a-input v-model:value="formData.sfsyl" placeholder="请输入是否双一流" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="占地面积(亩)" v-bind="validateInfos.are">
	          <a-input-number v-model:value="formData.are" placeholder="请输入占地面积(亩)" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="男生占比" v-bind="validateInfos.maleRatio">
	          <a-input-number v-model:value="formData.maleRatio" placeholder="请输入男生占比" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="女生占比" v-bind="validateInfos.femaleRatio">
	          <a-input-number v-model:value="formData.femaleRatio" placeholder="请输入女生占比" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否是艺术" v-bind="validateInfos.isYs">
	          <a-input-number v-model:value="formData.isYs" placeholder="请输入是否是艺术" style="width: 100%" :disabled="disabled"/>
          </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 JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import JEditor from '/@/components/Form/src/jeecg/components/JEditor.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../YxSchool.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: '',
    schoolName: '',   
    oldSchoolName: '',   
    schoolWebsiteAddress: '',   
    schoolPhone: '',   
    schoolEmail: '',   
    schoolIcon: '',   
    schoolOpenDate: '',   
    province: '',   
    city: '',   
    area: '',   
    street: '',   
    affiliation: '',   
    schoolNature: '',   
    institutionType: '',   
    schoolType: '',   
    studentNum: '',   
    baseInfo: '',   
    tags: '',   
    schoolBatch: '',   
    is985: undefined,
    is211: undefined,
    isStrengthen: undefined,
    yldx: '',   
    gzsz: '',   
    byl: '',   
    gjtszy: '',   
    stszy: '',   
    gzd: '',   
    sjyl: '',   
    sfsyl: '',   
    are: undefined,
    maleRatio: undefined,
    femaleRatio: undefined,
    isYs: undefined,
  });
  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>
