import { ElMessageBox } from 'element-plus';
import { msg } from '@/utils/lodash/msg.js';
export const deleteData = (datas, item, id) => {
	if (
		datas.ruleForm.documentStatus === 'c' ||
		datas.ruleForm.documentStatus == 'b'
	) {
		msg('warning', '只能删除创建中的审批流!');
		return;
	}
	ElMessageBox.confirm('确定要删除该审批方式吗？', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		let length = datas.timelineList.length;
		if (length === 4) {
			msg('warning', '请至少保留一个审批方式!');
			return;
		}

		datas.timelineList.splice(item.id, 1);
		let waysIndex = item.id - 2;
		datas.ruleForm.details[datas.detailsIndex].ways.splice(waysIndex, 1);
		let targetLength = datas.timelineList.length;

		targetLength < length
			? msg('success', '删除成功')
			: msg('success', '删除失败');
		//重置id
		for (let i = 0; i < targetLength; i++) {
			if (datas.timelineList[i].title !== '结束') {
				datas.timelineList[i].id = i;
			}
			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;
				}
			});
		}
	});
};
