import { watch,computed } from "vue";
import { ElMessage } from "element-plus";
export const researcherActivityHook=({form,companyInfo,formRef,researcherList})=>{
	console.log({form,companyInfo,formRef});
	const rules={
		// 表单校验规则
		activityType: [
			{ required: true, message: "请选择活动类型", trigger: "change" },
		],
		roadshowType: [
			{ required: true, message: "请选择路演形式", trigger: "change" },
		],
		roadshowPlatform: [
			{ required: true, message: "请选择路演平台", trigger: "blur" },
		],
		roadshowCity: [
			{ required: true, message: "请选择路演城市", trigger: "blur" },
		],
		companyName: [
			{ required: true, message: "请选择客户", trigger: "blur" },
		],
		meetingType: [
			{ required: true, message: "请选择会议形式", trigger: "blur" },
		],
		meetingPlatform: [
			{ required: true, message: "请选择会议平台", trigger: "blur" },
		],
		meetingCity: [
			{ required: true, message: "请选择会议城市", trigger: "blur" },
		],
		meetingTheme: [
			{ required: true, message: "请输入会议主题", trigger: "blur" },
		],
		partnersName: [
			{ required: true, message: "请输入合作方名称", trigger: "blur" },
		],
		activityClass: [
			{ required: true, message: "请选择活动类别", trigger: "blur" },
		],
	}
	const activityClassProps={
		value:"ActivityTypeName",
		label:"ActivityTypeName",
		children:"ChildList"
	}
	//ficc研究员列表
	const ficcReasearcherList=computed(()=>{
		return (researcherList.value.find(i=>i.label==='ficc')||{}).ResearcherList||[]
	})
	//rai研究员列表
	const raiReasercherList=computed(()=>{
		return (researcherList.value.find(i=>i.label==='权益')||{}).ResearcherList||[]
	})
	/* 添加完成后 拼接动态内容 */
	const setDynamicLink=(type)=>{
		switch (type) {
			case '内部会议' : return {
				text: '添加成功，已加入研究员日历',
				content: `可前往【我的日历】-【${type}】查看`,
				query: {
					act_tab: 3
				},
			}
			case '报告电话会' : return {
				text: '添加成功，已加入研究员日历',
				content: `可前往【我的日历】-【${type}】查看`,
				query: {
					act_tab: 4
				},
			}
			case '路演' : return {
				text: '活动申请已提交，请等待研究员处理',
				content: `可前往【我的日历】-【待处理申请】查看`,
				query: {
					act_tab: 1
				},
			}
			case '公开会议' : return {
				text: '活动申请已提交，请等待研究员处理',
				content: `可前往【我的日历】-【待处理申请】查看`,
				query: {
					act_tab: 1
				},
			}
		}
	}

	// 清除客户信息
	const companyClear=()=>{
		form.formData.companyId = '';
		form.formData.companyName = "";
		companyInfo.value = null;
	}

	//添加多个研究员选择时 校验是否重复
	const addUserCheckHandler=(val)=>{
		let arr = form.formData.selectResearchers.filter(item => item.researcherId === val);
		if(arr.length > 1) return ElMessage.warning('研究员已重复添加');
	}

	// 改变路演城市 重置城市校验
	const onChangeCity=(val, type)=>{
		console.log(val)
		if (type === "roadshow") {
			form.formData.roadshowCity = [val.province.value,val.city.value];
			formRef.value.clearValidate(['roadshowCity'])
		} else if (type === "meeting") {
			form.formData.meetingCity = [val.province.value, val.city.value];
			formRef.value.clearValidate(['meetingCity'])
		}
	}

	 	
	const findResearcherById=(id,type='ficc')=>{
		const list = type==='ficc'?ficcReasearcherList.value:raiReasercherList.value
		const flatList = list.map(item=>item.ResearcherList||[]).flat(Infinity)
		return flatList.find(user=>user.AdminId===id)
	}
	
	const findResearcherName=(id)=>{
		const isFicc = findResearcherById(id,'ficc')
		const isRai = findResearcherById(id,'rai')
		return isFicc?isFicc.RealName:isRai?isRai.RealName:''
	}

	watch(()=>form.formData.companyName,(val)=>{
		if(!val) {
			companyClear();
		}
	})
	watch(()=>form.formData.roadshowType,(val)=>{
		if(!val) {
			formRef.value && formRef.value.clearValidate(['roadshowPlatform','roadshowCity'])
		}
	})
	watch(()=>form.formData.meetingType,(val)=>{
		if(!val) {
			formRef.value && formRef.value.clearValidate(['meetingPlatform','meetingCity'])
		}
	})
	return {
		rules,
		activityClassProps,
		setDynamicLink,
		companyClear,
		addUserCheckHandler,
		onChangeCity,
		findResearcherName
	}
}
