<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol"
          name="NuConfigSuppliersApplyForm">
          <a-row>
            <a-col :span="24" style="border-bottom: 2px solid #f7f7f7; margin-bottom: 14px;">
              <SectionDivider :title="'基本信息'" />
            </a-col>
            <a-col :span="24">
              <a-form-item label="供应商名称" v-bind="validateInfos.suppliersName"
                id="NuConfigSuppliersApplyForm-suppliersName" name="suppliersName">
                <a-input v-model:value="formData.suppliersName" placeholder="请输入供应商名称" disabled></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="供应商性质" v-bind="validateInfos.suppliersNature_dictText"
                id="NuConfigSuppliersApplyForm-suppliersNature_dictText" name="suppliersNature_dictText">
                <a-input v-model:value="formData.suppliersNature_dictText" placeholder="请输入供应商性质" disabled></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="供应商地址" v-bind="validateInfos.suppliersAddress"
                id="NuConfigSuppliersApplyForm-suppliersAddress" name="suppliersAddress">
                <a-input v-model:value="formData.suppliersAddress" placeholder="请输入供应商地址" disabled></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="负责人" v-bind="validateInfos.personInCharge"
                id="NuConfigSuppliersApplyForm-personInCharge" name="personInCharge">
                <a-input v-model:value="formData.personInCharge" placeholder="请输入负责人" disabled></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="联系电话" v-bind="validateInfos.contactNumber"
                id="NuConfigSuppliersApplyForm-contactNumber" name="contactNumber">
                <a-input v-model:value="formData.contactNumber" placeholder="请输入联系电话" disabled></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="开户行" v-bind="validateInfos.openingBank" id="NuConfigSuppliersApplyForm-openingBank"
                name="openingBank">
                <a-input v-model:value="formData.openingBank" placeholder="请输入开户行" disabled></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="开户行账号" v-bind="validateInfos.openingBankNo"
                id="NuConfigSuppliersApplyForm-openingBankNo" name="openingBankNo">
                <a-input v-model:value="formData.openingBankNo" placeholder="请输入开户行账号" disabled></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="营业执照" v-bind="validateInfos.imgPath" id="NuConfigSuppliersApplyForm-imgPath"
                name="imgPath">
                <!-- <a-input v-model:value="formData.imgPath" placeholder="请输入营业执照" disabled ></a-input> -->
                <j-image-upload v-if="formData.imgPath" :fileMax="1" :value="opeMediaAddress + formData.imgPath"
                  disabled></j-image-upload>
              </a-form-item>
            </a-col>
            <a-col :span="24" style="border-bottom: 2px solid #f7f7f7; margin-bottom: 14px;" v-if="!disabled">
              <SectionDivider :title="'审核信息'" />
            </a-col>
            <a-col :span="24" v-if="!disabled">
              <a-form-item label="审核状态" v-bind="validateInfos.applyStatus" id="NuConfigSuppliersApplyForm-applyStatus"
                name="applyStatus">
                <!-- <a-input v-model:value="formData.applyStatus" placeholder="请输入审核状态"  ></a-input> -->
                <a-select v-model:value="formData.applyStatus" placeholder="请选择审核状态" style="width: 200px"
                  :disabled="false">
                  <a-select-option value="2">审核通过</a-select-option>
                  <a-select-option value="3">审核驳回</a-select-option>
                  <!-- <a-select-option value="4"
                    v-if="formData.applyStatus == '4' || formData.applyStatus == '5' || formData.applyStatus == '2'">待审核</a-select-option>
                  <a-select-option value="5"
                    v-if="formData.applyStatus == '4' || formData.applyStatus == '5' || formData.applyStatus == '2'">审核驳回</a-select-option> -->
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="24" v-show="formData.applyStatus == 3" v-if="!disabled">
              <a-form-item label="驳回原因" v-bind="validateInfos.applyContent" id="NuConfigSuppliersApplyForm-applyContent"
                name="applyContent">
                <a-textarea :maxlength="50" show-count  v-model:value="formData.applyContent" placeholder="请输入驳回原因" rows="4"></a-textarea>
              </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 { getValueType } from '/@/utils';
import { saveOrUpdate } from '../NuConfigSuppliersApply.api';
import { Form } from 'ant-design-vue';
import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
const opeMediaAddress = import.meta.env.VITE_OPE_MEDIA_ADDRESS
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: '',
  suppliersName: '',
  suppliersNature: '',
  suppliersAddress: '',
  personInCharge: '',
  contactNumber: '',
  supplyState: '',
  openingBank: '',
  openingBankNo: '',
  wechartId: '',
  imgPath: '',
  applyStatus: '',
  applyContent: '',
  suppliersNature_dictText: '',
  applyId: '',
  suppliersId: '',
  sourceType: '',
});
const { createMessage } = useMessage();
const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 6 } });
const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
const labelCol2 = ref<any>({ xs: { span: 24 }, sm: { span: 2 } });
const wrapperCol2 = ref<any>({ xs: { span: 24 }, sm: { span: 20 } });
const confirmLoading = ref<boolean>(false);
//表单验证
const validatorRules = reactive({
  applyStatus: [{ required: true, message: '请选择审核状态!' },],
  applyContent: [
    {
      validator: async (_rule, value) => {
        if (formData.status === 'modifyFail' && !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();
    const tmpData = {};
    Object.keys(formData).forEach((key) => {
      if (record.hasOwnProperty(key)) {
        tmpData[key] = record[key]
      }
    })
    tmpData.applyStatus = null;
    //赋值
    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(',');
      }
    }
  }
  if (model.applyStatus == '1') {
    createMessage.warning('请选择审核状态');
    confirmLoading.value = false;
    return;
  }
  if (model.applyStatus == 3 && !model.applyContent) {
    createMessage.warning('请填写驳回原因');
    confirmLoading.value = false;
    return;
  }
  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: 0px;
}
</style>
