<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol"
          name="ElderInfoForm">
          <a-row class="card-class" v-show="!detailSign || detailSign == 'zz'">
            <a-col :span="24" style="border-bottom: 2px solid #f7f7f7; margin-bottom: 14px;">
              <SectionDivider :title="'长者信息'" />
            </a-col>
            <a-col :span="12">
              <a-form-item label="NUID" v-bind="validateInfos.nuId" id="ElderInfoForm-nuId" name="nuId">
                <a-input v-model:value="formData.nuId"></a-input>
                <!-- <span>{{ formData.nuId }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="护理单元">
                <a-input v-model:value="formData.nuId_dictText"></a-input>
                <!-- <j-dict-select-tag type='list' v-model:value="formData.nuId" :dictCode="`nu_base_info,nu_name,nu_id`"
                  placeholder="请选择分类标签" allowClear @upDictCode="upNuNameFunc" />-->
                <!-- <span>{{ formData.nuId_dictText }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="姓名" v-bind="validateInfos.name" id="ElderInfoForm-name" name="name">
                <a-input v-model:value="formData.name"></a-input>
                <!-- <span>{{ formData.name }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="性别" v-bind="validateInfos.sex" id="ElderInfoForm-sex" name="sex">
                <a-input v-model:value="formData.sex"></a-input>
                <!-- <span>{{ formData.sex }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="身份证号码" v-bind="validateInfos.idCard" id="ElderInfoForm-idCard" name="idCard">
                <a-input v-model:value="formData.idCard"></a-input>
                <!-- <span>{{ formData.idCard }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="出生日期" v-bind="validateInfos.dateOfBirth" id="ElderInfoForm-dateOfBirth"
                name="dateOfBirth">
                <a-date-picker v-model:value="formData.dateOfBirth" value-format="YYYY-MM-DD" format="YYYY年MM月DD日"
                  style="width: 100%" />
                <!-- <span>{{ formData.dateOfBirth }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="民族" v-bind="validateInfos.national" id="ElderInfoForm-national" name="national">
                <a-input v-model:value="formData.national"></a-input>
                <!-- <span>{{ formData.national }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="住址" v-bind="validateInfos.houseAddress" id="ElderInfoForm-houseAddress"
                name="houseAddress">
                <a-textarea v-model:value="formData.houseAddress" :autosize="{ minRows: 1 }" />
                <!-- <span>{{ formData.houseAddress }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="医保类型" v-bind="validateInfos.medicalType_dictText">
                <a-textarea v-model:value="formData.medicalType_dictText" :autosize="{ minRows: 1 }" />
                <!-- <span>{{ formData.houseAddress }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="失能等级" v-bind="validateInfos.sndjName">
                <a-textarea v-model:value="formData.sndjName" :autosize="{ minRows: 1 }" />
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="缴费状态" v-bind="validateInfos.paymentStatus_dictText">
                <a-textarea v-model:value="formData.paymentStatus_dictText" :autosize="{ minRows: 1 }" />
              </a-form-item>
            </a-col>
          </a-row>
          <a-row class="card-class" v-if="!detailSign || detailSign == 'jhr'">
            <a-col :span="24" style="border-bottom: 2px solid #f7f7f7; margin-bottom: 14px;">
              <SectionDivider :title="'监护人信息'" />
            </a-col>
            <a-col :span="24">
              <a-col :span="12">
                <a-form-item label="人像面" v-bind="validateInfos.guardianIdCardPositive"
                  id="ElderInfoForm-guardianIdCardPositive" name="guardianIdCardPositive">
                  <j-image-upload :fileMax="1"
                    :value="opeMediaAddress + formData.guardianIdCardPositive"></j-image-upload>
                </a-form-item>
              </a-col>
            </a-col>
            <a-col :span="12">
              <a-form-item label="姓名" v-bind="validateInfos.guardianName" id="ElderInfoForm-guardianName"
                name="guardianName">
                <a-input v-model:value="formData.guardianName"></a-input>
                <!-- <span>{{ formData.guardianName }}</span> -->
              </a-form-item>
            </a-col>
            <!-- <a-col :span="12">
              <a-form-item label="关系" v-bind="validateInfos.relationship" id="ElderInfoForm-relationship"
                name="relationship">
                <a-input v-model:value="formData.relationship" ></a-input>
                <span>{{ formData.relationship }}</span>
              </a-form-item>
            </a-col> -->
            <a-col :span="12">
              <a-form-item label="身份证号" v-bind="validateInfos.guardianIdCard" id="ElderInfoForm-guardianIdCard"
                name="guardianIdCard">
                <a-input v-model:value="formData.guardianIdCard"></a-input>
                <!-- <span>{{ formData.guardianIdCard }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="联系电话" v-bind="validateInfos.guardianPhone" id="ElderInfoForm-guardianPhone"
                name="guardianPhone">
                <a-input v-model:value="formData.guardianPhone"></a-input>
                <!-- <span>{{ formData.guardianPhone }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="家庭住址" v-bind="validateInfos.guardianHomeAddress"
                id="ElderInfoForm-guardianHomeAddress" name="guardianHomeAddress">
                <a-textarea v-model:value="formData.guardianHomeAddress" :autosize="{ minRows: 1 }" />
                <!-- <span>{{ formData.guardianHomeAddress }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="工作单位" v-bind="validateInfos.guardianWorkUnit" id="ElderInfoForm-guardianWorkUnit"
                name="guardianWorkUnit">
                <a-textarea v-model:value="formData.guardianWorkUnit" :autosize="{ minRows: 1 }" />
                <!-- <span>{{ formData.guardianWorkUnit }}</span> -->
              </a-form-item>
            </a-col>
          </a-row>
          <a-row class="card-class" v-show="!detailSign || detailSign == 'zz'">
            <a-col :span="24" style="border-bottom: 2px solid #f7f7f7; margin-bottom: 14px;">
              <SectionDivider :title="'长者身份证'" />
            </a-col>
            <a-col :span="12">
              <a-form-item label="人像面" v-bind="validateInfos.idCardPositive" id="ElderInfoForm-idCardPositive"
                name="idCardPositive">
                <j-image-upload :fileMax="1" :value="opeMediaAddress + formData.idCardPositive"></j-image-upload>
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="国徽面" v-bind="validateInfos.idCardNegative" id="ElderInfoForm-idCardNegative"
                name="idCardNegative">
                <j-image-upload :fileMax="1" :value="opeMediaAddress + formData.idCardNegative"></j-image-upload>
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="有效开始日期" v-bind="validateInfos.startTime" id="ElderInfoForm-startTime"
                name="startTime">
                <a-date-picker v-model:value="formData.startTime" value-format="YYYY-MM-DD" style="width: 100%" />
                <!-- <span>{{ formData.startTime }}</span> -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="有效结束日期" v-bind="validateInfos.endTime" id="ElderInfoForm-endTime" name="endTime">
                <a-input v-if="formData.endTime == '9999-12-31'" :value="'长期'"></a-input>
                <a-date-picker v-else v-model:value="formData.endTime" value-format="YYYY-MM-DD" style="width: 100%" />
                <!-- {{ formData.endTime == '9999-12-31' ? '长期' : formData.endTime }} -->
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="签发机关" v-bind="validateInfos.issuingAuthority" id="ElderInfoForm-issuingAuthority"
                name="issuingAuthority">
                <a-input v-model:value="formData.issuingAuthority"></a-input>
                <!-- <span>{{ formData.issuingAuthority }}</span> -->
              </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 JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
import { getValueType } from '/@/utils';
import { saveOrUpdate } from '../ElderInfo.api';
import { Form } from 'ant-design-vue';
import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
import { string } from 'vue-types';

const props = defineProps({
  formDisabled: { type: Boolean, default: false },
  formData: { type: Object, default: () => ({}) },
  formBpm: { type: Boolean, default: true },
  detailSign: { type: String, default: '' },
  sndjDicts: null
});
const opeMediaAddress = import.meta.env.VITE_OPE_MEDIA_ADDRESS
const formRef = ref();
const useForm = Form.useForm;
const emit = defineEmits(['register', 'ok']);
const formData = reactive<Record<string, any>>({
  id: '',
  nuId: '',
  nuId_dictText: '',
  name: '',
  sex: '',
  age: '',
  idCard: '',
  dateOfBirth: '',
  national: '',
  houseAddress: '',
  idCardPositive: '',
  idCardNegative: '',
  guardianName: '',
  relationship: '',
  guardianIdCard: '',
  guardianPhone: '',
  guardianHomeAddress: '',
  guardianWorkUnit: '',
  issuingAuthority: '',
  startTime: '',
  endTime: '',
  guardianBirthDate: '',
  guardianSex: '',
  guardianNational: '',
  guardianCardHome: '',
  guardianIdCardPositive: '',
  medicalType: '',
  medicalType_dictText: '',
  disabilityReimbursementType: '',
  sndjName: '',
  paymentStatus_dictText: '',
});
const { createMessage } = useMessage();
const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 8 } });
const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
const confirmLoading = ref<boolean>(false);
//表单验证
const validatorRules = reactive({
});
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 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);
    formData.sndjName = '无'
    if (!!record.disabilityReimbursementType) {
      if (record.medicalType == 'syb') {
        formData.sndjName = props.sndjDicts.syb_reimbursement_type.filter(item => item.value == formData.disabilityReimbursementType)[0].text
      }
      if (record.medicalType == 'zgyb') {
        formData.sndjName = props.sndjDicts.zgyb_reimbursement_type.filter(item => item.value == formData.disabilityReimbursementType)[0].text
      }
      if (record.medicalType == 'jmyb') {
        formData.sndjName = props.sndjDicts.jmyb_reimbursement_type.filter(item => item.value == formData.disabilityReimbursementType)[0].text
      }
    }
  });
}

/**
 * 提交数据
 */
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;
    });
}

const nuNameDict = ref([])
function upNuNameFunc(val_) {
  nuNameDict.value = val_
}

function nuNameTextFunc(nuId) {
  let f = nuNameDict.value.filter(item_ => item_.value == nuId)
  if (f && f[0]) {
    return f[0].text
  } else {
    return '2'
  }
}
defineExpose({
  add,
  edit,
  submitForm,
});
</script>

<style lang="less" scoped>
.antd-modal-form {
  // padding: 14px;
}

.card-class {
  padding-top: 24px;
  padding-bottom: 24px;
  padding-left: 14px;
  padding-right: 14px;
  // background-color: rgba(255, 255, 255, 0.9);
  background-color: #fcfdff;
  border-radius: 10px;
  // box-shadow: rgba(0, 0, 0, 0.1) 0px 2px 12px;
  margin-bottom: 14px;

}
</style>
