/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
	return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
	const valid_map = ['admin', 'editor']
	return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
	const reg =
		/^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
	return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
	const reg = /^[a-z]+$/
	return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
	const reg = /^[A-Z]+$/
	return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
	const reg = /^[A-Za-z]+$/
	return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
	const reg =
		/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
	if (typeof str === 'string' || str instanceof String) {
		return true
	}
	return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
	if (typeof Array.isArray === 'undefined') {
		return Object.prototype.toString.call(arg) === '[object Array]'
	}
	return Array.isArray(arg)
}

/**
 * @param {string} idCardReg
 * @returns {Boolean}
 */
export function validateIdCard(rule, value, callback) {
	// const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
	const reg = /^[1-9]\d{5}(19|20)\d{2}(0\d|1[0-2])([0-2]\d|3[01])\d{3}[0-9Xx]$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的身份证号码'))
		} else {
			callback()
		}
	}
}
export function validIdNumber(idNumber) {
	const errors = [] // 用于存储验证错误信息
	if (!/^\d{17}(\d|x)$/i.test(idNumber)) {
		errors.push('身份证号码格式不正确')
	}
	const cityCode = {
		11: '北京',
		12: '天津',
		13: '河北',
		14: '山西',
		15: '内蒙古',
		21: '辽宁',
		22: '吉林',
		23: '黑龙江',
		31: '上海',
		32: '江苏',
		33: '浙江',
		34: '安徽',
		35: '福建',
		36: '江西',
		37: '山东',
		41: '河南',
		42: '湖北',
		43: '湖南',
		44: '广东',
		45: '广西',
		46: '海南',
		50: '重庆',
		51: '四川',
		52: '贵州',
		53: '云南',
		54: '西藏',
		61: '陕西',
		62: '甘肃',
		63: '青海',
		64: '宁夏',
		65: '新疆',
		71: '台湾',
		81: '香港',
		82: '澳门',
		91: '国外'
	}
	if (!cityCode[idNumber.substr(0, 2)]) {
		errors.push('身份证号码籍贯编码不正确')
	}
	const birthDay = idNumber.substr(6, 8)
	const year = birthDay.substr(0, 4)
	const month = birthDay.substr(4, 2)
	const day = birthDay.substr(6)
	const birthDate = new Date(`${year}-${month}-${day}`)
	if (birthDate > new Date()) {
		errors.push('身份证号码出生日期不正确')
	}
	if (`${birthDate.getFullYear()}-${month}-${day}` !== birthDay) {
		errors.push('身份证号码出生日期不正确')
	}
	let sum = 0
	const codes = '10X98765432'
	for (let i = 0; i < 17; i++) {
		sum += idNumber[i] * codes[i]
	}
	const checkCode = codes[sum % 11]
	if (checkCode !== idNumber.substr(17).toUpperCase()) {
		errors.push('身份证号码校验码不正确')
	}
	return errors.length === 0 ? callback() : callback(new Error(errors.join('; ')))
}

export function validatePhone(rule, value, callback) {
	const reg = /^[1][3,4,5,7,8][0-9]{9}$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的电话号码'))
		} else {
			callback()
		}
	}
}
export function validateNumberPlate(rule, value, callback) {
	// var xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
	// var creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;

	// if (value == '' || value == undefined || value == null) {
	//     callback();
	// } else {
	//     if (creg.test(value) && value.length == 7) {
	//         callback();
	//     } else {
	//         callback(new Error('请输入正确的车牌号'));
	//     }
	//     if (xreg.test(value) && value.length == 8) {
	//         callback();
	//     } else {
	//         callback(new Error('请输入正确的车牌号'));
	//     }
	// }

	//新能源车牌号
	//不带点
	var xreg1 =
		/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/
	//带点
	var xreg2 =
		/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}\.(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/

	//普通车牌号
	//不带点
	var creg1 =
		/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/
	//带点
	var creg2 =
		/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}\.[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/
	//全是字母
	var str = /^[A-Za-z]+$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (
			(creg2.test(value) &&
				value.length == 8 &&
				!str.test(value.substring(value.length - 5)) &&
				value.indexOf('.') != -1) ||
			(creg1.test(value) &&
				value.length == 7 &&
				!str.test(value.substring(value.length - 5)) &&
				value.indexOf('.') == -1) ||
			(xreg1.test(value) && value.length == 8 && value.indexOf('.') == -1) ||
			(xreg2.test(value) && value.length == 9 && value.indexOf('.') != -1)
		) {
			console.log('zhengchang ')
			callback()
		} else {
			callback(new Error('请输入正确的车牌号'))
		}
		// if (xreg.test(value) && value.length == 8) {
		//     callback();
		// } else {
		//     callback(new Error('请输入正确的车牌号'));
		// }
	}
}
// 验证是否非字母加数字
export function isNoCn(rule, value, callback) {
	if (value == '' || value == undefined || value == null) {
		callback()
	}
	setTimeout(() => {
		const re = /^[0-9a-zA-Z]+$/
		const rsCheck = re.test(value)
		if (!rsCheck) {
			callback(new Error('请输入非汉字、符号等'))
		} else {
			callback()
		}
	}, 0)
}
// 验证密码
export function isPassword(rule, value, callback) {
	if (value == '' || value == undefined || value == null) {
		callback()
	}
	setTimeout(() => {
		const re = /^[a-z_A-Z0-9-\.!@#\$%\\\^&\*\)\(\+=\{\}\[\]\/",'<>~\·`\?:;|]+$/
		const rsCheck = re.test(value)
		if (!rsCheck) {
			callback(new Error('请输入字母、数字、符号'))
		} else {
			callback()
		}
	}, 0)
}
// 验证是否汉字
export function isChinese(rule, value, callback) {
	if (value == '' || value == undefined || value == null) {
		callback()
	}
	setTimeout(() => {
		const re = /^[\u4e00-\u9fa5]+$/
		const rsCheck = re.test(value)
		if (!rsCheck) {
			callback(new Error('请输入汉字'))
		} else {
			callback()
		}
	}, 0)
}
//整数
export function isHundred(rule, value, callback) {
	if (value == '' || value == undefined || value == null) {
		callback()
	}
	setTimeout(() => {
		if (!Number(value)) {
			callback(new Error('请输入数字'))
		} else {
			const re = /^([1-9]\d{0,1}|100)$/
			const rsCheck = re.test(value)
			if (!rsCheck) {
				callback(new Error('请输入1-100'))
			} else {
				callback()
			}
		}
	}, 0)
}
//正整数
export function isInteger(rule, value, callback) {
	if (value == '' || value == undefined || value == null) {
		// callback(new Error('请输入'));
		callback()
	}
	setTimeout(() => {
		if (!Number(value)) {
			callback(new Error('请输入数字'))
		} else {
			const re = /^[0-9]*[1-9][0-9]*$/
			const rsCheck = re.test(value)
			if (!rsCheck) {
				callback(new Error('请输入正整数'))
			} else {
				callback()
			}
		}
	}, 0)
}
//整数
export function isIntegerZero(rule, value, callback) {
	if (value == '' || value == undefined || value == null) {
		// callback(new Error('请输入'));
	}
	setTimeout(() => {
		if (!Number(value) && Number(value) != 0) {
			callback(new Error('请输入数字'))
		} else {
			const re = /^[0-9]*[1-9][0-9]*$/
			const rsCheck = re.test(value)
			if (!rsCheck) {
				if (value == 0) {
					callback()
				} else {
					callback(new Error('请输入整数'))
				}
			} else {
				callback()
			}
		}
	}, 0)
}
//两位小数
export function validateTwoDecimalPlaces(rule, value, callback) {
	if (value == '' || value == undefined || value == null) {
		callback()
	}
	setTimeout(() => {
		if (!Number(value)) {
			callback(new Error('请输入数字'))
		} else {
			const re = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
			const rsCheck = re.test(value)
			if (!rsCheck) {
				callback(new Error('请输入正确格式,可保留两位小数'))
			} else {
				callback()
			}
		}
	}, 0)
}

//三位小数
export function validateThreeDecimalPlaces(rule, value, callback) {
	if (value == '' || value == undefined || value == null) {
		callback(new Error('请输入'))
	}
	setTimeout(() => {
		if (!Number(value)) {
			callback(new Error('请输入数字'))
		} else {
			const re = /(^[1-9]([0-9]+)?(\.[0-9]{1,3})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
			const rsCheck = re.test(value)
			if (!rsCheck) {
				callback(new Error('请输入正确格式,可保留三位小数'))
			} else {
				callback()
			}
		}
	}, 0)
}

//银行卡
export function validateBankCard(rule, value, callback) {
	const reg = /^([1-9]{1})(\d{10}|\d{15}|\d{18})$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的银行卡号'))
		} else {
			callback()
		}
	}
}
//邮箱
export function isEmail(rule, value, callback) {
	const reg =
		/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的邮箱'))
		} else {
			callback()
		}
	}
}
// 手机号

export function isMobile(rule, value, callback) {
	const reg = /^1[3-9]\d{9}$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的手机号码'))
		} else {
			callback()
		}
	}
}
//固定电话
export function isTelephone(rule, value, callback) {
	const reg = /0\d{2,3}-[1-9]\d{6,7}/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的固定电话'))
		} else {
			callback()
		}
	}
}
//QQ
export function isQQnumber(rule, value, callback) {
	const reg = /[1-9][0-9]{4,}/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的QQ号'))
		} else {
			callback()
		}
	}
}
//传真
export function isFax(rule, value, callback) {
	const reg = /^(\d{3,4}-)?\d{7,8}$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的传真号码'))
		} else {
			callback()
		}
	}
}
//组织机构代码
export function isOrganizationCode(rule, value, callback) {
	const reg = /[0-9A-HJ-NPQRTUWXY]{9}/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的组织机构代码'))
		} else {
			callback()
		}
	}
}
//统一社会信用代码 || 身份证号
export function isSocialCreditOrIdCode(rule, value, callback) {
	const reg = /^[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			const reg = /^[1-9]\d{5}(19|20)\d{2}(0\d|1[0-2])([0-2]\d|3[01])\d{3}[0-9Xx]$/
			if (!reg.test(value) && value != '') {
				callback(new Error('请输入正确的证照号'))
			} else {
				callback()
			}
		} else {
			callback()
		}
	}
}
//统一社会信用代码
export function isSocialCreditCode(rule, value, callback) {
	const reg = /^[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的统一社会信用代码'))
		} else {
			callback()
		}
	}
}
//邮政编码
export function isZipCode(rule, value, callback) {
	const reg = /^[1-9][0-9]{5}$/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的邮政编码'))
		} else {
			callback()
		}
	}
}
//经度
export function isLongitude(rule, value, callback) {
	const reg = /^-?(((\d|[1-9]\d|1[0-7]\d|0)\.\d{0,10})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,10}|180)$/i
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的经度'))
		} else {
			callback()
		}
	}
}
//纬度
export function isLatitude(rule, value, callback) {
	const reg = /^-?([0-8]?\d{1}\.\d{0,10}|90\.0{0,10}|[0-8]?\d{1}|90)$/i
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的纬度'))
		} else {
			callback()
		}
	}
}
//网址
export function isInternetUrl(rule, value, callback) {
	const reg = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/
	if (value == '' || value == undefined || value == null) {
		callback()
	} else {
		if (!reg.test(value) && value != '') {
			callback(new Error('请输入正确的网址'))
		} else {
			callback()
		}
	}
}
//数字
export function isNumber(rule, value, callback) {
	if (value == '' || value == undefined || value == null) {
		callback()
	}
	setTimeout(() => {
		if (!Number(value)) {
			callback(new Error('请输入数字'))
		} else {
			callback()
		}
	}, 0)
}

//驾驶证号
export function validateDriversLicenseNumber(rule, value, callback) {
	const reg = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/
	if (!reg.test(value)) {
		callback(new Error('请输入正确的驾驶证号'))
	} else {
		callback()
	}
}

// 正整数的正则表达式(包括0)
export function positiveInteger(rule, value, callback) {
	const reg = /^[+]{0,1}(\d+)$/
	if (!reg.test(value)) {
		callback(new Error('请输正数'))
	} else {
		callback()
	}
}
// 正整数的正则表达式(不包括0)
export function unpositiveInteger(rule, value, callback) {
	const reg = /^[+]{0,1}[1-9]\d*$|^[+]{0,1}(0\.\d*[1-9])$|^[+]{0,1}([1-9]\d*\.\d*[1-9])$/
	if (!reg.test(value)) {
		callback(new Error('请输正数'))
	} else {
		callback()
	}
}
// 表格中时间格式化 :formatter="formalDate"
export function formalDate(val) {
	/*       console.log(row,'row')
    console.log(column,'column') */
	let date = val
	if (data == undefined) {
		return ''
	}
	let formalDate = new Date(date)
	let year = formalDate.getFullYear()
	let month = formalDate.getMonth() + 1
	let day = formalDate.getDate()
	let hours = formalDate.getHours()
	let minutes = formalDate.getMinutes()
	// let seconds = formalDate.getSeconds()
	if (month < 10) {
		month = '0' + month
	}
	if (day < 10) {
		day = '0' + day
	}
	if (hours < 10) {
		hours = '0' + hours
	}
	if (minutes < 10) {
		minutes = '0' + minutes
	}
	return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes
}

//空格校验
export function spaceCheck(rule, value, callback) {
	const reg = /^[^\s]*$/
	if (!reg.test(value)) {
		callback(new Error('不能输入空格'))
	} else {
		callback()
	}
}
