
import { message } from "antd";

export const verifySex = (idCard: string) => {
    // 检查身份证号码长度
    if (idCard.length === 18) {
        // 18位身份证号码
        const lastChar = idCard.charAt(17);
        let genderCode;
        if (lastChar === 'X'  || lastChar == "x") {
            genderCode = 10; // 将 'X' 转换为数字 10
        } else {
            genderCode = parseInt(lastChar, 10);
        }
        return genderCode % 2 === 0 ? '女' : '男';
    } else if (idCard.length === 15) {
        // 15位身份证号码
        const genderCode = parseInt(idCard.substring(13, 15), 10);
        return genderCode % 2 === 0 ? '女' : '男';
    } else {
        return '请输入有效的身份证号码';
    }
}
export const verifyIdCard = (idCard: string) => {
    let regIdCard =
        /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
    if (regIdCard.test(idCard)) {
        if (idCard.length == 18) {
            let idCardWi:any = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10,
                5, 8, 4, 2);
            let idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2);
            let idCardWiSum : any = 0;
            for (let i = 0; i < 17; i++) {
                idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
            }
            let idCardMod = idCardWiSum % 11;
            let idCardLast = idCard.substring(17);
            if (idCardMod == 2) {
                if (idCardLast == "X" || idCardLast == "x") {
                    return true;
                } else {
                    message.error('身份证号码错误');

                    return false;

                }
            } else {
                if (idCardLast == idCardY[idCardMod]) {
                    return true;
                } else {
                    message.error('身份证号码错误');

                    return false;

                }
            }
        } else {
            return true;
        }
    } else {
        message.error('请输入有效的身份证号码');
        return false;
    }
}

export const verifyPhone = (phone: string) => {
    let reg_phone = /^(\+)?(0|86|17951)?1(3\d|4[579]|5\d|6\d|7\d|8\d|9\d)\d{8}$/;
    if (reg_phone.test(phone)) {
        return true;
    } else {
        message.error('请输入正确的手机号');
        return false;
    }
}