export const rules = {
	merchantBaseInfo: {
		userType: {
			required: true,
			errorMsg: '请选择商户类型',
		},
		shortName: {
			required: true,
			errorMsg: '请填写商户简称',
		},
		mccCode: {
			required: true,
			errorMsg: '请选择行业类目',
		},
		alipayAccount: {
			required: true,
			errorMsg: '请填写支付宝账号',
		},
		'contactIdCardFrontPic.url': {
			required: true,
			errorMsg: '请上传联系人身份证正面',
		},
		'contactIdCardBackPic.url': {
			required: true,
			errorMsg: '联系人身份证反面',
		},
		contactName: {
			required: true,
			errorMsg: '请填写联系人姓名',
		},
		contactPersonId: {
			required: true,
			errorMsg: '请填写联系人身份证号',
		},
		contactPersonIdStartDate: {
			required: true,
			errorMsg: '请填写联系人身份证开始日期',
		},
		contactPersonIdEndDate: {
			required: true,
			errorMsg: '请填写联系人身份证到期日期',
		},
		contactPhone: {
			required: true,
			errorMsg: '请填写联系人电话',
		},
		contactAddr: {
			required: true,
			errorMsg: '请填写联系人通讯地址',
		},
		contactEmail: {
			required: true,
			errorMsg: '请填写联系人邮箱',
		}
	},
	legalPersonInfo: {
		'idCardHandPic.url': {
			required: true,
			errorMsg: '请上传身份证手持图片',
		},
		'idCardFrontPic.url': {
			required: true,
			errorMsg: '请上传身份证正面图片',
		},
		'idCardBackPic.url': {
			required: true,
			errorMsg: '请上传身份证反面图片',
		},
		legalPersonName: {
			required: true,
			errorMsg: '请填写法人姓名',
		},
		legalPersonId: {
			required: true,
			errorMsg: '请填写法人身份证号',
		},
		legalIdPersonStartDate: {
			required: true,
			errorMsg: '请填写法人身份证开始日期',
		},
		legalPersonIdEndDate: {
			required: true,
			errorMsg: '请填写法人身份证到期日期',
		},
		legalPersonPhone: {
			required: true,
			errorMsg: '请填写法人电话',
		},
		legalPersonEmail: {
			required: true,
			errorMsg: '请填写法人邮箱',
		},
		legalGender: {
			required: true,
			errorMsg: '请填写法人性别',
		},
		legalAddress: {
			required: true,
			errorMsg: '请填写法人地址',
		},

	},
	businessLicenceInfo: {
		'licensePic.url': {
			required: true,
			errorMsg: '请上传营业执照照片'
		},
		licenceName: {
			required: true,
			errorMsg: '请输入营业执照全称'
		},
		licenceNo: {
			required: true,
			errorMsg: '请输入营业执照号码'
		},
		licenceStartDate: {
			required: true,
			errorMsg: '请选择营业执照开始日期'
		},
		registeredAddress: {
			required: true,
			errorMsg: '请填写营业执照注册地址'
		}
	},
	storeInfo: {
		mercProvCode: {
			required: true,
			errorMsg: '请选择归属地',
		},
		mercCityCode: {
			required: true,
			errorMsg: '请选择归属地',
		},
		mercAreaCode: {
			required: true,
			errorMsg: '请选择归属地',
		},
		mercProv: {
			required: true,
			errorMsg: '请选择归属地',
		},
		mercCity: {
			required: true,
			errorMsg: '请选择归属地',
		},
		mercArea: {
			required: true,
			errorMsg: '请选择归属地',
		},
		businessAddress: {
			required: true,
			errorMsg: '请填写营业地址',
		},
		'insidePic.url': {
			required: true,
			errorMsg: '请上传经营场所内设照片',
		},
		'doorPic.url': {
			required: true,
			errorMsg: '请上传门头照',
		},
		'cashierDeskPic.url': {
			required: true,
			errorMsg: '请上传收银台照片',
		}
	},
	settlementInfo: {
		settlementType: {
			required: true,
			errorMsg: '请选择结算类型',
		},
		settlementCardType: {
			required: true,
			errorMsg: '请选择结算卡类型',
		},
		'bankCardFrontPic.url': {
			required: true,
			errorMsg: '请上传银行卡正面照片',
		},
		openAccProvinceId: {
			required: true,
			errorMsg: '请选择开户行省市区',
		},
		openAccCityId: {
			required: true,
			errorMsg: '请选择开户行省市区',
		},
		openAccAreaId: {
			required: true,
			errorMsg: '请选择开户行省市区',
		},
		openAccProvince: {
			required: true,
			errorMsg: '请选择开户行省市区',
		},
		openAccCity: {
			required: true,
			errorMsg: '请选择开户行省市区',
		},
		openAccArea: {
			required: true,
			errorMsg: '请选择开户行省市区',
		},
		bankType: {
			required: true,
			errorMsg: '请选择开户行',
		},
		bankInstId: {
			required: true,
			errorMsg: '请选择开户行',
		},
		bankName: {
			required: true,
			errorMsg: '请选择开户行',
		},
		settlementCardNo: {
			required: true,
			errorMsg: '请填写结算卡号',
		},
		settlementName: {
			required: true,
			errorMsg: '请填写开户名称',
		},
		bankMobile: {
			required: true,
			errorMsg: '请填写结算银行预留手机号',
		}
	}
}

export function isEmptyValue(val) {
	if (val === '' || val === undefined || val === null) {
		return true
	}
	return false
}
/**
 * 解析属性路径，返回数组格式的路径片段
 * @param {string} str - 属性路径（如 'userType' 或 'idCardHandPic.url'）
 * @returns {Array<string>} 属性路径片段数组（如 ['userType'] 或 ['idCardHandPic', 'url']）
 */
export function returnKey(str) {
	// 无论是否包含'.'，都返回数组，方便后续统一处理
	return str.includes('.') ? str.split('.') : [str];
}

/**
 * 根据属性路径数组，安全获取对象的嵌套属性值
 * @param {Object} obj - 目标对象（如 form.legalPersonInfo）
 * @param {Array<string>} keyPath - 属性路径数组（如 ['idCardHandPic', 'url']）
 * @returns {*} 嵌套属性的值（若路径不存在，返回 undefined）
 */
export function getNestedValue(obj, keyPath) {
	// 边界处理：obj不是对象，直接返回undefined
	if (typeof obj !== 'object' || obj === null) {
		return undefined;
	}

	// 逐层遍历属性路径，获取最终值
	return keyPath.reduce((currentObj, key) => {
		// 中间层级不存在，直接返回undefined，避免报错
		if (currentObj === undefined || currentObj === null) {
			return undefined;
		}
		return currentObj[key];
	}, obj);
}

export function verifyValue(val, ruleItem) {
	const isEmpty = isEmptyValue(val)
	let result = {
		ispas: true,
		errorMsg: ''
	}
	if (ruleItem.required) {

		if (isEmpty) {
			result.ispas = false
			result.errorMsg = ruleItem.errorMsg
			return result
		}
	}
	return result
}

export function verifyData(data, rule) {
	// 边界处理：data不是对象，直接返回校验失败（若有必填规则）
	if (typeof data !== 'object' || data === null) {
		// 遍历规则，返回第一个必填项的错误信息
		for (let ruleKey in rule) {
			const ruleItem = rule[ruleKey];
			if (ruleItem.required) {
				return {
					ispas: false,
					errorMsg: ruleItem.errorMsg || '数据格式错误，无法校验'
				};
			}
		}
		return {
			ispas: true,
			errorMsg: ''
		};
	}

	for (let ruleKey in rule) {
		const ruleItem = rule[ruleKey];
		// 1. 获取属性路径数组（如 ['idCardHandPic', 'url']）
		const keyPath = returnKey(ruleKey);
		// 2. 安全获取嵌套属性值（核心：支持深层级属性）
		const targetValue = getNestedValue(data, keyPath);
		// 3. 校验属性值
		const result = verifyValue(targetValue, ruleItem);

		// 4. 校验失败，直接返回结果
		if (!result.ispas) {
			return result;
		}
	}

	return {
		ispas: true,
		errorMsg: ''
	}
}