//import { useRouter } from 'vue-router';
// const $router = useRouter();
//import { getCurrentInstance } from 'vue';
import { formatDate } from '@/utils/lodash/formatDate.js';
import { msg } from '@/utils/lodash/msg.js';
import {
	approvalFlowSave,
	approvalFlowSubmit,
	approvalFlowCheck,
	approvalFlowRecheck,
} from '@/utils/api.js';
// const { proxy } = getCurrentInstance();
// const { $http } = proxy;

export const commitHttpData = (datas, val, $http, $router, $route) => {
	datas.isOperate = true;
	setTimeout(() => {
		datas.isOperate = false;
	}, 1000);
	let type = val.type;
	let ids = [];
	if ($route.query.id) {
		ids.push(Number($route.query.id));
	} else {
		ids.push(datas.ruleForm.id);
	}
	if (type === 'add') {
		$router.push({
			path: '/examine/mange/examineAdd',
			query: { type: 'add', id: '', sjtag: Math.random() * 36 },
		});
	}
	if (type === 'submit' || type === 'save') {
		if (
			datas.ruleForm.documentStatus === 'c' ||
			datas.ruleForm.documentStatus === 'b'
		) {
			msg('error', '只能保存|提交创建中的审批流！');
			return false;
		}
		//atas.timeLineList
		//过滤适用范围
		datas.ruleForm.details[datas.detailsIndex].ranges =
			datas.ruleForm.details[datas.detailsIndex].ranges.filter((item) => {
				return item.deptId;
			});

		//过滤没有选择的审批方式,因为审批方式里的数据是写死的,所以过滤一下不想去动态添加了
		datas.ruleForm.details[datas.detailsIndex].ways =
			datas.ruleForm.details[datas.detailsIndex].ways.filter((item) => {
				return item.type && item.method;
			});
		datas.ruleForm.details[datas.detailsIndex].ways.forEach(
			(item, index) => {
				item.sort = index + 1;
				item.id = 0;
				item.detailsId = 0;
			}
		);
		datas.ruleForm.details[datas.detailsIndex].ranges.forEach((item) => {
			item.id = 0;
			item.detailsId = 0;
		});
		const URLS = {
			submit: approvalFlowSubmit,
			save: approvalFlowSave,
		};
		let MSGS = {
			submit: '提交成功!',
			save: '保存成功!',
		};
		if (!datas.ruleForm.name) {
			msg('error', '请填写审批流名称');
			return;
		}
		if (!datas.ruleForm.useOrderType) {
			msg('error', '请填写应用单据');
			return;
		}
		if (type === 'submit') {
			if (
				!datas.ruleForm.details[datas.detailsIndex].ranges.length ||
				!datas.ruleForm.details[datas.detailsIndex].ranges[0].deptId
			) {
				msg('error', '请选择审批适用范围');
				return;
			}
			if (!datas.timelineList[1].isLine) {
				msg('error', '请选择审批条件！');
				return false;
			}
			if (!datas.timelineList[2].isLine) {
				msg('error', '请选择审批方式！');
				return false;
			}
			//过滤审批方式 回显
			datas.timelineList = datas.timelineList.filter((item) => {
				return item.title !== '审批方式';
			});
		}
		datas.loading = true;
		$http
			.post(URLS[type], datas.ruleForm)
			.then((res) => {
				if (res.status === 200) {
					msg('success', MSGS[type]);
					res.data.useOrderType = res.data.useOrderType + '';
					let Dlength = res.data.details.length - 1;
					res.data.date = formatDate(new Date(res.data.date)) || '';
					datas.detailsIndex = Dlength;
					datas.ruleForm = res.data;
					datas.ruleForm.versionStr =
						datas.ruleForm.details[Dlength].versionStr;
					datas.ruleForm.CurrentVersionDetailsId =
						datas.ruleForm.details[Dlength].id;

					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;
						}
					});
				}
			})
			.finally(() => {
				datas.loading = false;
			});
	}
	if (type === 'audit') {
		if (datas.ruleForm.documentStatus !== 'b') {
			msg('error', '只能审核状态为“审核中”的订单！');
			return false;
		}
		datas.loading = true;
		$http
			.post(approvalFlowCheck, { ids })
			.then((res) => {
				if (res.status === 200) {
					if (res.data[0].isSuccess) {
						datas.ruleForm.documentStatus = 'c';
						msg('success', '审核成功');
					} else {
						msg('success', '审核失败');
					}
				}
			})
			.finally(() => {
				datas.loading = false;
			});
	}
	if (type === 'noAudit') {
		if (datas.ruleForm.documentStatus !== 'c') {
			msg('error', '只能反审核状态为“已审核”的订单！');
			return false;
		}
		datas.loading = true;
		$http
			.post(approvalFlowRecheck, { ids })
			.then((res) => {
				if (res.status === 200) {
					if (res.data[0].isSuccess) {
						datas.ruleForm.documentStatus = 'd';
						msg('success', '反审核成功');
					} else {
						msg('success', '反审核失败');
					}
				}
			})
			.finally(() => {
				datas.loading = false;
			});
	}
};
