<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol"
          name="OrgApplyInfoForm">

          <a-row>
            <a-col :span="24">
              <SectionDivider :title="'机构信息'" />
            </a-col>
            <a-col :span="12">
              <a-form-item label="营业执照照片" v-bind="validateInfos.comBusinessLicense"
                id="OrgApplyInfoForm-comBusinessLicense" name="comBusinessLicense">
                <JImageUpload :fileMax="1" v-model:value="formData.comBusinessLicense" :disabled="true"></JImageUpload>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="12">
              <a-form-item label="企业名称" v-bind="validateInfos.comName" id="OrgApplyInfoForm-comName" name="comName">
                {{formData.comName}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="企业注册地址" v-bind="validateInfos.comRegisterAddress"
                id="OrgApplyInfoForm-comRegisterAddress" name="comRegisterAddress">
                {{formData.comRegisterAddress}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="企业信用代码" v-bind="validateInfos.comCreditCode" id="OrgApplyInfoForm-comCreditCode"
                name="comCreditCode">
                {{formData.comCreditCode}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="企业法人" v-bind="validateInfos.comLegalPerson" id="OrgApplyInfoForm-comLegalPerson"
                name="comLegalPerson">
                {{formData.comLegalPerson}}
              </a-form-item>
            </a-col>
            
          </a-row>

          <a-row>

            <a-col :span="12">
              <a-form-item label="机构负责人" v-bind="validateInfos.orgLeader" id="OrgApplyInfoForm-orgLeader"
                name="orgLeader">
                {{formData.orgLeader}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="机构负责人电话" v-bind="validateInfos.orgLeaderPhone" id="OrgApplyInfoForm-orgLeaderPhone"
                name="orgLeaderPhone">
                {{formData.orgLeaderPhone}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="机构房屋性质" v-bind="validateInfos.orgPropertyType" id="OrgApplyInfoForm-orgPropertyType"
                name="orgPropertyType">
                {{formData.orgPropertyType}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="机构建筑面积" v-bind="validateInfos.orgBuildingArea" id="OrgApplyInfoForm-orgBuildingArea"
                name="orgBuildingArea">
                {{formData.orgBuildingArea}} ㎡
              </a-form-item>
            </a-col>
            <!-- <a-col :span="12">
              <a-form-item label="机构所在地">
                {{cityViewValue}}
              </a-form-item>
            </a-col> -->
            <a-col :span="12">
              <a-form-item label="机构地址" v-bind="validateInfos.orgAddress" id="OrgApplyInfoForm-orgAddress"
                name="orgAddress">
                {{formData.orgAddress}}
              </a-form-item>
            </a-col>
          </a-row>
          
          <a-row>
            <a-col :span="24">
              <SectionDivider :title="'申请人身份证'" />
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="12">
              <a-form-item label="身份证正面" v-bind="validateInfos.cardZmPath" id="OrgApplyInfoForm-cardZmPath"
                name="cardZmPath">
                <JImageUpload :fileMax="1" v-model:value="formData.cardZmPath" :disabled="true"></JImageUpload>
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="身份证反面" v-bind="validateInfos.cardFmPath" id="OrgApplyInfoForm-cardFmPath"
                name="cardFmPath">
                <JImageUpload :fileMax="1" v-model:value="formData.cardFmPath" :disabled="true"></JImageUpload>
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="姓名" v-bind="validateInfos.name" id="OrgApplyInfoForm-name" name="name">
                {{formData.name}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="性别" v-bind="validateInfos.sex" id="OrgApplyInfoForm-sex" name="sex">
                {{formData.sex}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="身份证号" v-bind="validateInfos.idCard" id="OrgApplyInfoForm-idCard" name="idCard">
                {{formData.idCard}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="出生日期" v-bind="validateInfos.birthDate" id="OrgApplyInfoForm-birthDate"
                name="birthDate">
                {{formData.birthDate?formData.birthDate.substring(0,10):""}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="有效开始日期" v-bind="validateInfos.startTime" id="OrgApplyInfoForm-startTime"
                name="startTime">
                {{formData.startTime?formData.startTime.substring(0,10):""}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="有效结束日期" v-bind="validateInfos.endTime" id="OrgApplyInfoForm-endTime" name="endTime">
                {{formData.endTime?formData.endTime.substring(0,10):""}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="民族" v-bind="validateInfos.national" id="OrgApplyInfoForm-national" name="national">
                {{formData.national}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="住址" v-bind="validateInfos.idCardAddress" id="OrgApplyInfoForm-idCardAddress"
                name="idCardAddress">
                {{formData.idCardAddress}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="签发机关" v-bind="validateInfos.issuingAuthority" id="OrgApplyInfoForm-issuingAuthority"
                name="issuingAuthority">
                {{formData.issuingAuthority}}
              </a-form-item>
            </a-col>
          </a-row>
          
          <a-row>
            <a-col :span="24">
              <SectionDivider :title="'加盟审核'" />
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="12">
              <a-form-item label="审批意见" v-bind="validateInfos.status" id="OrgApplyInfoForm-status" name="status">
                <a-select v-model:value="formData.status" style="width: 200px" placeholder="请选择审批意见" :disabled="sfsh!='1'">
                  <a-select-option value="2">审核通过</a-select-option>
                  <a-select-option value="3">审核驳回</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="12" v-show="formData.status == '3'">
              <a-form-item label="驳回原因" v-bind="validateInfos.content" id="OrgApplyInfoForm-content" name="content">
                <a-textarea :autosize="{ minRows: 3 }" maxlength="50" show-count v-model:value="formData.content"
                  allow-clear placeholder="请输入驳回原因"></a-textarea>
              </a-form-item>
            </a-col>
            <a-col :span="12" v-show="formData.buildStatus == '3'">
              <a-form-item label="加盟合同" v-bind="validateInfos.contract" id="OrgApplyInfoForm-contract">
                <JUpload v-model:value="formData.contract" :maxCount="1"></JUpload>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row v-show="formData.workOrderStatus != '0'">
            <a-col :span="24">
              <SectionDivider :title="'工单回执'" />
            </a-col>
            <a-col :span="12">
              <a-form-item label="工单状态" >
                {{formData.workOrderStatus_dictText}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="处理人" >
                {{formData.handleBy}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="回复附件" >
                <JUpload v-model:value="formData.replyFile" :maxCount="1" disabled></JUpload>
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="回复时间" >
                {{formData.replyTime?formData.replyTime.substring(0,10):""}}
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item label="回复内容" >
                {{formData.replyContent}}
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
    <!-- <a-col :span="24" v-if="!!formData.orgCoordinateLa && !!formData.orgCoordinateLo">
      <TencentMap :latitude="formData.orgCoordinateLa" :longitude="formData.orgCoordinateLo" />
    </a-col> -->
  </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 { getValueType } from '/@/utils';
import { saveOrUpdate } from '../OrgApplyInfo.api';
import { Form } from 'ant-design-vue';
import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
import TencentMap from '/@/components/TencentMap/TencentMap.vue';
import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
import { t } from '/@/hooks/web/useI18n';

const cityViewValue = ref('')
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: '',
  openId: '',
  wechatName: '',
  tel: '',
  status: '',
  buildStatus: '',
  content: '',
  createTime: '',
  updateTime: '',
  izEntry: '',
  name: '',
  sex: '',
  national: '',
  birthDate: '',
  idCardAddress: '',
  idCard: '',
  issuingAuthority: '',
  startTime: '',
  endTime: '',
  cardZmPath: '',
  cardFmPath: '',
  comBusinessLicense: '',
  comName: '',
  comRegisterAddress: '',
  comCreditCode: '',
  comLegalPerson: '',
  orgAddress: '',
  orgCoordinateLo: '',
  orgCoordinateLa: '',
  orgLeader: '',
  orgLeaderPhone: '',
  orgBuildingNumber: '',
  orgPropertyType: '',
  orgBuildingArea: undefined,
  orgProvince: '',
  orgCity: '',
  orgDistrict: '',
  orgProvince_dictText: '',
  orgCity_dictText: '',
  orgDistrict_dictText: '',
  franchiseTime: null,
  contract:null,
  replyContent: '',
  replyFile: '',
  replyTime: '',
  handleBy: '',
  workOrderStatus: '',
  workOrderStatus_dictText: '',

});
const tempNullVal = ref('');
const sfsh = ref<string>('0');
const { createMessage } = useMessage();
const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 17 } });
const confirmLoading = ref<boolean>(false);
//表单验证
const validatorRules = reactive({
  status: [{ required: true, message: '请选择审批结果!' },],
  content: [
    {
      validator: async (_rule, value) => {
        if (formData.status === '3' && !value) {
          return Promise.reject('请输入驳回原因!');
        }
        return Promise.resolve();
      },
    },
  ],
});
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();
    let tmpData = {};
    Object.keys(formData).forEach((key) => {
      if (record.hasOwnProperty(key)) {
        tmpData[key] = record[key]
      }
    })
    var lsbl = tmpData.status;
    if (tmpData.status != '2' && tmpData.status != '3') {
      tmpData.status = null
    }
    cityViewValue.value = record.orgProvince_dictText
      + (!!record.orgCity_dictText ? record.orgCity_dictText : '')
      + (!!record.orgDistrict_dictText ? record.orgDistrict_dictText : '')
    //赋值
    Object.assign(formData, tmpData);
    if(lsbl == '1'){
      sfsh.value = '1'
    }else{
      sfsh.value = '0'
    }
  });
}

/**
 * 提交数据
 */
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(',');
      }
    }
  }
  if(model.status == '2'){
    model.buildStatus = '3'
  }
  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>
