// import request from "@/utils/request";
import i18n from "@/lang";
import _ from "lodash"

const $t = function() {
	return i18n.t.apply(i18n, arguments);
};
// console.log(i18n.t("valid.required"))

function swop(fun) {
	return (value, rule, ...rest) => {
		let v = value, r = rule
		if(_.isObject(value) && !_.isObject(rule)) {
			r = value
			v = rule	
		}
		
		return fun.call(this,v, r, ...rest)
	}
}

let validateForm = {
	// 手机验证 （element Form规则)
	mobile: swop((value, rule) => {
		if (!value) {
			return true
		}
		var reg = /^[1][0-9][0-9]{9}$/;
		if (value.length < 11 || !reg.test(value)) {
			return false
		}
		return true
	}),
	// 手机验证 （element Form规则)
	email: swop((value, rule,callback) => {
		if (!value) {
			return true
		}
		var reg = /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/;
		if (!reg.test(value)) {
			return false
		}
		return true
	}),
    idCard: swop((value, rule, callback) => {
        let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
        if (!value) {
			return true
        }
        if (!reg.test(value)) {
			return false
        }
        return true
    }),
    chinese: swop((value, rule, callback) => {
        let reg = /^[\u0391-\uFFE5]+$/
        if (!reg.test(value)) {
			callback(new Error($t("valid.formatError")));
        }
        callback(rule, value, callback);
    }),
    numberAndLetter: swop((value, rule, callback) => {
        let reg = /^[0-9a-zA-Z]*$/
        if (!reg.test(value)) {
			callback(new Error($t("valid.formatError")));
        }
        callback();
	}),
	digit: swop((value, rule, callback) => {
        let reg = /^[0-9]*$/
        if (!reg.test(value)) {
			return false
		}
		return true
	}),
	oneDecimal: swop((value, rule, callback) => {
        let reg = /^[0-9]+([.]{1}[0-9]{0,1})?$/
        if (!reg.test(value)) {
			return false
		}
		return true
	}),
};

export default validateForm;
