
import { getCardTypeEnumList } from '@/common/constants/enumsList';
import { validateCommon } from '@/common/setup/validate';
import { QueryAddUserInfoApply } from '@/services/go/ermcp/customInfo';
import { AddUserInfoApplyReq } from '@/services/go/ermcp/customInfo/interface';
import { message } from 'ant-design-vue';
import { RuleObject } from 'ant-design-vue/lib/form/interface';
import { reactive, ref, UnwrapRef } from 'vue';
import { FormState } from './interface';

/**
 * 获取证件类型
 * @returns 
 */
export function getCardType() {
    const arr = [2, 4, 18, 21];
    return getCardTypeEnumList().filter(e => arr.includes(e.enumitemname))
}

export function handleForm() {
    const formRef = ref();
    const formState: UnwrapRef<FormState> = reactive(initFormState());
    // 验证仓库类型
    async function v_cardtype(rule: RuleObject, value: number) {
        return validateCommon(value, '请选择证件类型')
    }
    const rules = {
        userinfotype: [{ required: true, message: '请选择客户类型', trigger: 'change' }],
        customername: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
        nickname: [{ required: true, message: '请输入企业简称', trigger: 'blur' }],
        cardtype: [{ required: true, validator: v_cardtype, trigger: 'change' }],
    };
    return { formRef, formState, rules }
}

/**
 * 初始化表单数据
 * @returns 
 */
export function initFormState(): FormState {
    return {
        userinfotype: '2',
        customername: '',
        nickname: '',
        cardtype: undefined,
        cardnum: '',
        legalpersonname: '',
        taxpayernum: '',
        attachment1: '',
        attachment2: '',
        attachment3: '',
        attachment4: '',
        attachment5: '',
        contactname: '',
        mobilephone: '',
        telphone: '',
        provinceid: undefined,
        cityid: undefined,
        districtid: undefined,
        cardaddress: '',
        remark: '',
        username: '',
        cardbackphotourl: '',
        cardfrontphotourl: '',
        email: '',
    }
}

export function handleApply() {
    const loading = ref<boolean>(false);
    function applyAction(param: AddUserInfoApplyReq) {
        loading.value = true;
        QueryAddUserInfoApply(param).then(res => {
        }).catch(err => {
            message.error(err)
        }).finally(() => loading.value = false)
    }
    return { loading, applyAction }
}