
import { getCardTypeEnumList } from '@/common/constants/enumsList';
import { queryResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
import { validateCommon } from '@/common/setup/validate';
import { QueryAddUserInfoApply } from '@/services/go/ermcp/customInfo';
import { AddUserInfoApplyReq } from '@/services/go/ermcp/customInfo/interface';
import { queryParentAreaList } from '@/services/go/ermcp/qhj';
import { QhjParentAreaList } from '@/services/go/ermcp/qhj/interface';
import { message } from 'ant-design-vue';
import { RuleObject } from 'ant-design-vue/lib/form/interface';
import { reactive, Ref, 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: '请选择客户类型', type: 'number' }],
        username: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        nickname: [{ required: true, message: '请输入企业简称', trigger: 'blur' }],
        mobilephone: [{ required: true, message: '请输入手机号码', trigger: 'blur' },
        { message: '请输入正确的手机号', pattern: /^[1][3,4,5,7,8,9][0-9]{9}$/ }],
        cardtype: [{ required: true, validator: v_cardtype, trigger: 'change' }],
        areaid: [{ required: true, message: '请选择所属机构', }],
        // logincode: [{ required: true, message: '请选择登录账号', }], #96146
        loginpwd: [{ required: true, message: '请选择登录密码', }],
    };
    return { formRef, formState, rules }
}

/**
 * 初始化表单数据
 * @returns
 */
export function initFormState(): FormState {
    return {
        areaid: undefined,
        logincode: '',
        loginpwd: '',
        userinfotype: 2,
        customername: '',
        nickname: '',
        cardtype: undefined,
        cardnum: '',
        legalpersonname: '',
        taxpayernum: '',
        attachment1: '',
        attachment2: '',
        attachment3: '',
        attachment4: '',
        attachment5: '',
        contactname: '',
        mobilephone: '',
        telphone: '',
        provinceid: undefined,
        cityid: undefined,
        districtid: undefined,
        ipaddress: '',
        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 }
}
// 查询所属机构列表
export function handleAreaList() {
    const areaList = ref<QhjParentAreaList[]>([])
    function getAreaList(loading: Ref<boolean>) {
        queryResultLoadingAndInfo(queryParentAreaList, loading).then(res => {
            areaList.value = res
        })
    }
    return { areaList, getAreaList }
}