const METHODS = ['', '会签', '或签'];
const DISNUM = [2, 3, 4];
import { EDITTIMELINEITEM, WAYSITEM } from '../constant/index';
export const detailsDataInit = (datas, detailsId) => {
	let Dlength = datas.ruleForm.details.findIndex((item) => {
		return item.id == detailsId;
	});

	// let Dlength = datas.ruleForm.details.length - 1;
	datas.detailsIndex = Dlength;
	let versionStr = datas.ruleForm.details[Dlength].versionStr;
	datas.ruleForm.versionStr = versionStr;
	datas.ruleForm.CurrentVersionDetailsId = datas.ruleForm.details[Dlength].id;
	//审批流适用范围部门回显设置
	let deptIds = [];
	// let versionNo = $route.query.versionNo;
	let items = datas.ruleForm.details.filter((item) => {
		return item.id == detailsId;
	});
	//审批适用范围部门ids
	items[0].ranges.forEach((item) => {
		deptIds.push(item.deptId);
	});
	//部门数据是异步获取，存在详情数据过来了部门数据还没过来的问题
	if (Array.isArray(datas.wayList) && datas.wayList.length) {
		datas.wayList.forEach((item) => {
			if (deptIds.includes(item.id)) {
				datas.timelineList[0].content.push(item.name);
			}
		});
	}
	//审批方式回显设置

	//获取审批方式长度
	let length = datas.ruleForm.details[Dlength].ways.length;
	let obj = {};
	let types = [];
	let waysItem = {};
	//获取审批方式类型
	for (let i = 0; i < length; i++) {
		types.push(datas.ruleForm.details[Dlength].ways[i].type);
		waysItem[i + 2] = datas.ruleForm.details[Dlength].ways[i];
	}
	//waysItem为空证明用户没有添加审批方式，直接给title
	if (JSON.stringify(waysItem) === '{}') {
		datas.timelineList[2].title = '审批方式';
		datas.timelineList[2].text = '审批流审批方式';
	}

	if (!datas.ruleForm.details[datas.detailsIndex].ways.length) {
		let item = JSON.parse(JSON.stringify(WAYSITEM));
		datas.ruleForm.details[datas.detailsIndex].ways.push(item);
	}

	//获取对应的title
	let rLength = datas.radios.length;
	for (let i = 0; i < rLength; i++) {
		let item = datas.radios[i];
		if (types.includes(Number(item.value))) {
			obj[i + 1] = item.label;
		}
	}
	//先为静态数据添加对等的审批方式 循环从1开始
	for (let i = 1; i < length; i++) {
		let item = JSON.parse(JSON.stringify(EDITTIMELINEITEM));
		let lineLength = datas.timelineList.length;
		item.id = lineLength - 1;
		datas.timelineList.splice(item.id, 0, item);
	}

	//设置对应的交互
	//设置审批方式
	let tLength = datas.timelineList.length;
	for (let i = 0; i < tLength; i++) {
		let item = datas.timelineList[i];

		//title设置和审批方式
		let type;
		if (DISNUM.includes(item.id) && JSON.stringify(waysItem) !== '{}') {
			type = waysItem[item.id].type;
			item.title = obj[type];
			let method = waysItem[item.id].method;
			item.method.status = METHODS[method];
		}

		if (i === 0 && item.content.length) {
			datas.timelineList[0].isLine = true;
		}
		if (i === 1 && item.statuss.length) {
			datas.timelineList[1].isLine = true;
		}
		if (i === 1 && item.statuss.length) {
		}

		if (i === 2 && item.title !== '审批方式') {
			datas.timelineList[2].isLine = true;
		}
		if (i === 3 && item.title !== '审批方式') {
			console.log();
			datas.timelineList[3].isLine = true;
		}
		if (i === 4 && item.title !== '审批方式') {
			datas.timelineList[4].isLine = true;
		}
		if (obj[type] === '部门负责人审批') {
			item.method.name = '部门负责人审批';
		}
		if (obj[type] === '指定成员审批') {
			let memberIds = [];
			let names = [];
			waysItem[item.id].wayApprovers.forEach((item) => {
				memberIds.push(item.approverId);
			});
			datas.memberList.forEach((it) => {
				if (memberIds.includes(it.id)) {
					names.push(it.name);
				}
			});
			item.method.name = names.join(',');
		}
		if (obj[type] === '其它部门负责人审批') {
			let depids = [];
			waysItem[item.id].wayApprovers.forEach((item) => {
				depids.push(item.deptId);
			});
			datas.wayList.forEach((dept) => {
				if (depids.includes(dept.id)) {
					item.method.name = dept.name;
				}
			});
		}
	}
	//控制对应的审批方式添加按钮显示隐藏
	// if (length == 3) {
	// 	datas.timelineList.forEach((item) => {
	// 		item.isAdd = false;
	// 	});
	// } else {
	// 	datas.timelineList[datas.timelineList.length - 1].isAdd = true;
	// }
	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;
		}
	});
	// return {
	// 	ruleForm: datas.ruleForm,
	// 	timelineList: datas.timelineList,
	// };
};
