/**
 * Validate v1.0.0 通用验证
 * @description 项目中用到的表单验证规则
 */
import test from './test.js';

// 效验身份证号码
export const isValidIdCard18 = {
  rules: [
    {
      required: true,
      errorMessage: '请输入您的身份证号码',
    },
    {
      validateFunction: function (rule, value, data, callback) {
        // 身份证号码正则表达式（18位）
        var regex = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([012]\d)|3[01])\d{3}(\d|X|x)$/

        // 校验长度，类型
        if (!regex.test(value)) {
          return false
        }

        // 提取出生年月日，并验证是否为有效日期
        var year = value.substring(6, 10)
        var month = value.substring(10, 12)
        var day = value.substring(12, 14)
        var date = new Date(year, month - 1, day)

        if (date.getFullYear() !== parseInt(year, 10) ||
          date.getMonth() !== parseInt(month, 10) - 1 ||
          date.getDate() !== parseInt(day, 10)) {
          return false
        }

        // 校验码部分
        var idCardBase = value.substring(0, 17)
        var weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
        var checkCodes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
        var sum = 0
        for (var i = 0; i < idCardBase.length; i++) {
          sum += idCardBase[i] * weights[i]
        }
        var index = sum % 11
        var last = checkCodes[index]

        // return last.toUpperCase() === value.substring(17).toUpperCase()
        if (last.toUpperCase() !== value.substring(17).toUpperCase()) {
          callback('请输入正确的身份证号码');
        }
        return true;
      },
    },
  ],
};

// 手机号
export const mobile = {
  rules: [
    {
      required: true,
      errorMessage: '请输入手机号',
    },
    {
      validateFunction: function (rule, value, data, callback) {
        if (!test.mobile(value)) {
          callback('手机号码格式不正确');
        }
        return true;
      },
    },
  ],
};

// 密码
export const password = {
  rules: [
    {
      required: true,
      errorMessage: '请输入密码',
    },
    {
      validateFunction: function (rule, value, data, callback) {
        if (!/^.{8,}$/.test(value)) {
          callback('密码长度不能少于8位');
        }
        return true;
      },
    },
  ],
};

// 短信验证码
export const code = {
  rules: [
    {
      required: true,
      errorMessage: '请输入验证码',
    },
  ],
};

// 校验邮箱格式
export const emailFormat = {
  rules: [
    {
      validateFunction: function (rule, value, data, callback) {
        const emailReg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;  
        if (!emailReg.test(value)) {
          callback('邮箱格式不正确');
        }
        return true;
      },
    },
  ],
};
// 邮箱必填且校验邮箱格式
export const emailRequired = {
  rules: [
    {
      required: true,
      errorMessage: '请输入邮箱',
    },
    {
      validateFunction: function (rule, value, data, callback) {
        const emailReg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;  
        if (!emailReg.test(value)) {
          callback('邮箱格式不正确');
        }
        return true;
      },
    },
  ],
};

// 真实姓名
export const realName = {
  rules: [
    {
      required: true,
      errorMessage: '请输入姓名',
    },
    {
      validateFunction: function (rule, value, data, callback) {
        if (!test.chinese(value)) {
          callback('请输入汉字');
        }
        return true;
      },
    },
  ],
};

export const taxName = {
  rules: [
    {
      required: true,
      errorMessage: '请输入发票抬头名称',
    },
    {
      validateFunction: function (rule, value, data, callback) {
        if (!test.chinese(value)) {
          callback('请输入汉字');
        }
        return true;
      },
    },
  ],
};

// 税号
export const taxNo = {
  rules: [
    {
      required: true,
      errorMessage: '请输入税号',
    },
  ],
};

// 开户行
export const bankName = {
  rules: [
    {
      required: true,
      errorMessage: '请输入开户行',
    },
    {
      validateFunction: function (rule, value, data, callback) {
        if (!test.chinese(value)) {
          callback('请输入汉字');
        }
        return true;
      },
    },
  ],
};
// 银行卡号
export const bankCode = {
  rules: [
    {
      required: true,
      errorMessage: '请输入银行卡号',
    },
    {
      validateFunction: function (rule, value, data, callback) {
        if (!test.number(value)) {
          callback('请输入正确账号');
        }
        return true;
      },
    },
  ],
};

// 支付宝账号
export const alipayAccount = {
  rules: [
    {
      required: true,
      errorMessage: '请输入支付宝账号',
    },
    {
      validateFunction: function (rule, value, data, callback) {
        let isEmail = test.email(value);
        let isMobile = test.mobile(value);

        if (!isEmail && !isMobile) {
          callback('请输入正确账号');
        }
        return true;
      },
    },
  ],
};

export default {
  mobile,
  alipayAccount,
  bankCode,
  bankName,
  realName,
  password,
  code,
  emailFormat,
  emailRequired,
  taxNo,
  taxName,
};
