import {
	isIdentityId
} from '@/utils/validate.js'

/**
 * 提取身份证的出生年月（1990-09-12）
 * @param {Object} idCard
 */
export function getIdentityIdInfo(idCard) {
	var idCard18 = idCard
	var state = isIdentityId(idCard18)

	var infoObj = {
		birthday: "",
		general: "",
		age: "",
	}
	if (state) {
		let myDate = null
		let nowDate = new Date()

		if (idCard18.length === 15) {
			idCard18 = idCard18.substr(0, 6) + 19 + idCard18.substr(6, 9);
		}

		// 出生年月(1999-01-12)
		infoObj.birthday = idCard18.substr(6, 8).replace(/(.{4})(.{2})/, "$1-$2-")

		// 性别
		infoObj.general = idCard18.substring(16, 17) % 2 === 0 ? "1" : "0"

		// 年龄
		myDate = new Date(infoObj.birthday)
		infoObj.age = nowDate.getFullYear() - myDate.getFullYear()
		if (nowDate.getMonth() < myDate.getMonth() || (nowDate.getMonth() < myDate.getMonth() && nowDate.getDate() < myDate.getDate())) {
			infoObj.age--
		}
		return infoObj
	}
}
