const METHODS = ['', '会签', '或签'];
const DISNUM = [2, 3, 4];
import { msg } from '@/utils/lodash/msg.js';
import { WAYSITEM } from '../constant/index';

export const dialogCommitData = (
	datas,
	type,
	applicableScopeRef,
	setMethodRef
) => {
	//适用范围
	if (datas.isApplicableScope) {
		//获取适用范围数据
		let data = applicableScopeRef.value.getData();
		datas.timelineList[0].isLine = true;
		let names = [];
		data.selectedKeys.forEach((item) => {
			names.push(item.name);
		});

		datas.timelineList[0].content = names;
		datas.ruleForm.details[datas.detailsIndex].remark = data.remark;
		//重组数据
		let rangesInitArray = [];
		let length = data.ids.length || 0;
		for (let i = 0; i < length; i++) {
			let rangesItem = { id: 0, detailsId: 0, deptId: data.ids[i] };
			rangesInitArray.push(rangesItem);
		}
		datas.ruleForm.details[datas.detailsIndex].ranges = rangesInitArray;
		//回填
	}
	if (datas.isSetConditions) {
		datas.timelineList[1].statuss = [
			{ name: '单据状态 : ', status: '审核中' },
		];
		datas.timelineList[1].text = '';
		datas.timelineList[1].isLine = true;
	}
	//审批方式
	if (datas.isMethod) {
		let data = setMethodRef.value.getData();
		let timeIndex = datas.timelineItemIndex;
		let waysIndex = timeIndex - 2;
		//选中的审批方式type
		//设置Title和禁止其他的审批方式
		datas.radios.forEach((item) => {
			if (item.value == data.radioValue) {
				datas.timelineList[timeIndex].title = item.label;
			}
		});
		datas.timelineList[timeIndex].isLine = true;
		//审批类型;
		if (!datas.ruleForm.details[datas.detailsIndex].ways.length) {
			let item = JSON.parse(JSON.stringify(WAYSITEM));
			datas.ruleForm.details[datas.detailsIndex].ways.push(item);
		}
		datas.ruleForm.details[datas.detailsIndex].ways[waysIndex].type =
			Number(data.radioValue);
		//审批方式;
		datas.ruleForm.details[datas.detailsIndex].ways[waysIndex].method =
			Number(data.methodValue);

		// //指定成员审批
		if (data.radioValue == '2') {
			let length = data.memberData.length || 0;
			datas.ruleForm.details[datas.detailsIndex].ways[
				waysIndex
			].wayApprovers = [];
			if (!length) {
				msg('warning', '请选择成员!');
				return;
			}
			if (length >= 20) {
				msg('warning', '最多选择20名成员!');
				return;
			}
			for (let i = 0; i < length; i++) {
				let obj = {
					id: 0,
					wayId: 0,
					deptId: data.memberData[i].deptId,
					approverId: data.memberData[i].id,
				};

				datas.ruleForm.details[datas.detailsIndex].ways[
					waysIndex
				].wayApprovers.push(obj);
			}
			let names = [];
			data.memberData.forEach((item) => {
				names.push(item.name);
			});
			datas.timelineList[timeIndex].method.name = names.join('、');
		}
		// //指定其他部门负责人审批
		if (data.radioValue == '3') {
			let radioData;
			datas.wayList.forEach((item) => {
				if (item.id === datas.setConditionsData.deptValue) {
					radioData = item;
				}
			});
			//!data.radioData.managerId
			if (!radioData.managerId) {
				msg('warning', '请重新选择部门负责人!');
				return false;
			}
			datas.timelineList[timeIndex].method.name = radioData.label;
			let obj = {
				id: 0,
				wayId: 0,
				deptId: radioData.id || 0,
				approverId: radioData.managerId || 0,
			};
			datas.ruleForm.details[datas.detailsIndex].ways[
				waysIndex
			].wayApprovers[0] = obj;
		}
		//禁止非选择的审批方式，只能修改选择了的
		if (DISNUM.includes(timeIndex)) {
			//改变审批方式左边的文字
			let index = Number(data.radioValue) - 1;
			let label = datas.radios[index].label;
			//设置审批人
			if (label === '部门负责人审批') {
				datas.timelineList[timeIndex].method.name = label;
			}
			//设置审批方式
			datas.timelineList[timeIndex].method.status =
				METHODS[data.methodValue];
		}
	}
	//从1开始，因为结束是最后才设置的
	let sum = 1;
	datas.timelineList.forEach((item) => {
		if (item.isLine) {
			sum++;
		}
		if (sum === datas.timelineList.length) {
			item.isLine = true;
		} else {
			datas.timelineList[datas.timelineList.length - 1].isLine = false;
		}
	});
	//关闭窗口
	datas[type] = false;
};
