<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="NuBizCustomerInfoForm">
          <a-card title="基础信息">
            <a-row>
              <a-col :span="18">
                <a-row>
                  <a-col :span="8">
                    <a-form-item label="姓名" v-bind="validateInfos.customerName" id="NuBizCustomerInfoForm-customerName" name="customerName">
                      <a-input v-model:value="formData.customerName" placeholder="请输入姓名"  allow-clear ></a-input>
                    </a-form-item>
                  </a-col>
                  <a-col :span="8">
                    <a-form-item label="性别" v-bind="validateInfos.customerSex" id="NuBizCustomerInfoForm-customerSex" name="customerSex">
                      <j-dict-select-tag v-model:value="formData.customerSex" dictCode="sex" placeholder="请选择性别"  allow-clear />
                    </a-form-item>
                  </a-col>
                  <a-col :span="8">
                    <a-form-item label="身份证号码" v-bind="validateInfos.idCard" id="NuBizCustomerInfoForm-idCard" name="idCard">
                      <a-input v-model:value="formData.idCard" placeholder="请输入身份证号码"  allow-clear  @change="handleChangeIdcard"></a-input>
                    </a-form-item>
                  </a-col>
                  <a-col :span="8">
                    <a-form-item label="年龄" v-bind="validateInfos.customerAge" id="NuBizCustomerInfoForm-customerAge" name="customerAge">
                      <a-input v-model:value="formData.customerAge" placeholder="请输入年龄"  allow-clear ></a-input>
                    </a-form-item>
                  </a-col>
                  <a-col :span="8">
                    <a-form-item label="出生日期" v-bind="validateInfos.dateOfBirth" id="NuBizCustomerInfoForm-dateOfBirth" name="dateOfBirth">
                      <a-input v-model:value="formData.dateOfBirth" placeholder="请输入出生日期" disabled  allow-clear ></a-input>
                    </a-form-item>
                  </a-col>
                  <a-col :span="8">
                    <a-form-item label="民族" v-bind="validateInfos.national" id="NuBizCustomerInfoForm-national" name="national">
                      <a-input v-model:value="formData.national" placeholder="请输入民族"  allow-clear ></a-input>
                    </a-form-item>
                  </a-col>
                  <a-col :span="8">
                    <a-form-item label="自理程度" v-bind="validateInfos.reimbType" id="NuBizCustomerInfoForm-reimreimbTypeb_type" name="reimbType">
                      <j-dict-select-tag v-model:value="formData.reimbType" dictCode="reimb_type" placeholder="请选择自理程度"  allow-clear />
                    </a-form-item>
                  </a-col>
                  <a-col :span="8">
                    <a-form-item label="医保类型" v-bind="validateInfos.medicalType" id="NuBizCustomerInfoForm-medicalType" name="medicalType">
                      <j-dict-select-tag v-model:value="formData.medicalType" dictCode="medical_insurance_type" placeholder="请选择医保类型"  allow-clear />
                    </a-form-item>
                  </a-col>
                  <a-col :span="8">
                    <a-form-item label="医保卡号" v-bind="validateInfos.medicalCard" id="NuBizCustomerInfoForm-medicalCard" name="medicalCard">
                      <a-input v-model:value="formData.medicalCard" placeholder="请输入医保卡号"  allow-clear ></a-input>
                    </a-form-item>
                  </a-col>
                </a-row>
              </a-col>
              <a-col :span="6">
                <a-col :span="24">
                  <a-form-item label="头像" v-bind="validateInfos.avatarPath" id="NuBizCustomerInfoForm-avatarPath" name="avatarPath">
                    <j-image-upload :fileMax="1" :bizPath="`customerHead`"  v-model:value="formData.avatarPath" text="上传头像" ></j-image-upload>
                  </a-form-item>
                </a-col>
              </a-col>
              <a-col :span="24" hidden>
                <a-form-item label="区域" v-bind="validateInfos.nuId" id="NuBizCustomerInfoForm-nuId" name="nuId">
                  <j-dict-select-tag v-model:value="formData.nuId" dictCode="" placeholder="请选择区域"  allow-clear />
                </a-form-item>
              </a-col>
              
              <a-col :span="6">
                <a-form-item label="监护人" v-bind="validateInfos.guardian" :labelCol="labelCol2" :wrapperCol="wrapperCol2" id="NuBizCustomerInfoForm-guardian" name="guardian">
                  <a-input v-model:value="formData.guardian" placeholder="请输入监护人"  allow-clear ></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="6">
                <a-form-item label="联系电话" v-bind="validateInfos.contactNumber" :labelCol="labelCol2" :wrapperCol="wrapperCol2" id="NuBizCustomerInfoForm-contactNumber" name="contactNumber">
                  <a-input v-model:value="formData.contactNumber" placeholder="请输入联系电话"  allow-clear ></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="6">
                <a-form-item label="关系" v-bind="validateInfos.relationship" :labelCol="labelCol2" :wrapperCol="wrapperCol2" id="NuBizCustomerInfoForm-relationship" name="relationship">
                  <a-input v-model:value="formData.relationship" placeholder="请输入关系"  allow-clear ></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="6">
                <a-form-item label="宗教信仰" v-bind="validateInfos.religiousBeliefs" :labelCol="labelCol2" :wrapperCol="wrapperCol2" id="NuBizCustomerInfoForm-religiousBeliefs" name="religiousBeliefs">
                  <a-input v-model:value="formData.religiousBeliefs" placeholder="请输入宗教信仰"  allow-clear ></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="6">
                <a-form-item label="文化程度" v-bind="validateInfos.educationLevel" :labelCol="labelCol2" :wrapperCol="wrapperCol2" id="NuBizCustomerInfoForm-educationLevel" name="educationLevel">
                  <j-dict-select-tag v-model:value="formData.educationLevel" dictCode="" placeholder="请选择文化程度"  allow-clear />
                </a-form-item>
              </a-col>
              <a-col :span="6">
                <a-form-item label="婚姻状况" v-bind="validateInfos.maritalStatus" :labelCol="labelCol2" :wrapperCol="wrapperCol2" id="NuBizCustomerInfoForm-maritalStatus" name="maritalStatus">
                  <a-input v-model:value="formData.maritalStatus" placeholder="请输入婚姻状况"  allow-clear ></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="24">
                <a-form-item label="家庭住址" v-bind="validateInfos.homeAddress" :labelCol="labelCol3" :wrapperCol="wrapperCol3" id="NuBizCustomerInfoForm-homeAddress" name="homeAddress">
                  <a-input v-model:value="formData.homeAddress" placeholder="请输入家庭住址"  allow-clear ></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="24">
                <a-form-item label="备注" v-bind="validateInfos.content" :labelCol="labelCol3" :wrapperCol="wrapperCol3" id="NuBizCustomerInfoForm-content" name="content">
                  <a-textarea v-model:value="formData.content" :rows="4" placeholder="请输入备注" />
                </a-form-item>
              </a-col>
            </a-row>
          </a-card>
          <a-card title="证件信息信息" style="margin-top: 20px;">
            <a-tabs v-model:activeKey="activeKey" type="card">
              <a-tab-pane key="1" tab="身份证">
                <a-row>
                  <a-col :span="12">
                    <a-form-item label="身份证正面" v-bind="validateInfos.frontIdCard" id="NuBizCustomerInfoForm-frontIdCard" name="frontIdCard">
                      <j-image-upload :fileMax="1" :bizPath="`customerZzxx`" v-model:value="formData.frontIdCard" ></j-image-upload>
                    </a-form-item>
                  </a-col>
                  <a-col :span="12">
                    <a-form-item label="身份证反面" v-bind="validateInfos.negativeIdCard" id="NuBizCustomerInfoForm-negativeIdCard" name="negativeIdCard">
                      <j-image-upload :fileMax="1" :bizPath="`customerZzxx`" v-model:value="formData.negativeIdCard" ></j-image-upload>
                    </a-form-item>
                  </a-col>
                </a-row>
              </a-tab-pane>
              <a-tab-pane key="2" tab="户口本">
                <a-row>
                  <a-col :span="12">
                    <a-form-item label="户口本本人页" v-bind="validateInfos.accountBookHimself" id="NuBizCustomerInfoForm-accountBookHimself" name="accountBookHimself">
                      <j-image-upload :fileMax="1" :bizPath="`customerZzxx`" v-model:value="formData.accountBookHimself" ></j-image-upload>
                    </a-form-item>
                  </a-col>
                </a-row>
              </a-tab-pane>
              <a-tab-pane key="3" tab="医保卡">
                <a-row>
                  <a-col :span="12">
                    <a-form-item label="医保卡正面" v-bind="validateInfos.frontMedical" id="NuBizCustomerInfoForm-frontMedical" name="frontMedical">
                      <j-image-upload :fileMax="1" :bizPath="`customerZzxx`" v-model:value="formData.frontMedical" ></j-image-upload>
                    </a-form-item>
                  </a-col>
                  <a-col :span="12">
                    <a-form-item label="医保卡反面" v-bind="validateInfos.negaticeMedical" id="NuBizCustomerInfoForm-negaticeMedical" name="negaticeMedical">
                      <j-image-upload :fileMax="1" :bizPath="`customerZzxx`" v-model:value="formData.negaticeMedical" ></j-image-upload>
                    </a-form-item>
                  </a-col>
                </a-row>
              </a-tab-pane>
            </a-tabs>
          </a-card>
            <a-row>
						
						<!-- <a-col :span="24">
							<a-form-item label="当前状态" v-bind="validateInfos.currentState" id="NuBizCustomerInfoForm-currentState" name="currentState">
								<j-dict-select-tag v-model:value="formData.currentState" dictCode="" placeholder="请选择当前状态"  allow-clear />
							</a-form-item>
						</a-col> -->
						
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </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 JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../NuBizCustomerInfo.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
import dayjs from "dayjs";
  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 activeKey = ref('1');
  const formData = reactive<Record<string, any>>({
    id: '',
    nuId: '',   
    customerName: '',   
    customerSex: '',   
    customerAge: '',   
    idCard: '',   
    dateOfBirth: '',   
    national: '',   
    reimbType: '',   
    avatarPath: '',   
    medicalType: '',   
    medicalCard: '',   
    educationLevel: '',   
    maritalStatus: '',   
    religiousBeliefs: '',   
    frontIdCard: '',   
    negativeIdCard: '',   
    accountBookHimself: '',   
    frontMedical: '',   
    negaticeMedical: '',   
    content: '',   
    currentState: '',   
    guardian: '',   
    relationship: '',   
    contactNumber: '',   
    homeAddress: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 7 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const labelCol2 = ref<any>({ xs: { span: 24 }, sm: { span: 7 } });
  const wrapperCol2 = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const labelCol3 = ref<any>({ xs: { span: 24 }, sm: { span: 2 } });
  const wrapperCol3 = ref<any>({ xs: { span: 24 }, sm: { span: 21 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
    customerName: [{ required: true, message: '请输入姓名!'},],
    idCard: [{ required: true, message: '请输入身份证号码!'}, { pattern: '^\\d{6}(18|19|20)?\\d{2}(0[1-9]|1[012])(0[1-9]|[12]\\d|3[01])\\d{3}(\\d|[xX])$', message: '身份证号错误!' }],
    contactNumber: [{ required: true, message: '请输入联系电话!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

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

  function handleChangeIdcard(){
    const item = formData.idCard;
    if(item.length === 18){
      formData.dateOfBirth = item.substring(6, 10) + '-' + item.substring(10, 12) + '-' + item.substring(12, 14);
      var year = dayjs().format('YYYY')
      formData.customerAge = parseInt(year) - item.substring(6, 10);
      // formData.national = item.substring(14, 16);
    }
  }
  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    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 {
    padding: 14px;
  }
</style>
