import {
	audit,
	getBillDetailList,
	QueryStock,
	save,
	submit
} from '../api/production_replenishment.js';
import modal from '../plugins/modal.js';
import {
	audioUrls,
	playVoice
} from './audio2.js';
import {
	getSaveParam,
	getSubmitParam,
	getAuditParam,
	get2SaveParam,
	getCreateParam,
	getQueryStockParam,
	getBillDetailListParam
} from './checkParams.js';


export default {
	async create(FormId, selectedFID) {
		try {
			modal.loading("创建中，请耐心等待...");
			const createParam = getCreateParam(FormId);
			console.log(createParam)
			const createRes = await this.saveData(createParam);
			if (!createRes.IsSuccess) {
				throw new Error(createRes.Errors[0].Message);
			}
			console.log(createRes)
			//查询库存
			const queryStockParam = getQueryStockParam(createRes["SuccessEntitys"][0]["Id"], selectedFID,'');
			const queryStockRes = await this.queryStock(queryStockParam);
			if (queryStockRes["success"] !== "成功") {
				throw new Error(queryStockRes["result"]);
			}
			//查询盘点单明细
			const condition = this.createQueryBillDetailListCondition(createRes["SuccessEntitys"][0]["Id"]);
			const billDetailListParam = getBillDetailListParam(condition)
			const res = await this.fetchBillDetails(billDetailListParam)
			// if (res["success"] !== "成功") {
			// 	throw new Error(res["result"]);
			// }
			return res

			// this.handleSuccess(FID, indexList);

		} catch (error) {
			// this.handleError(error);
			throw new Error(error);
		} finally {
			modal.closeLoading();
		}
	},
	//入库、出库盘点
	async create2(FormId, date) {
		try {
			modal.loading("创建中，请耐心等待...");
			const createParam = getCreateParam(FormId);
			console.log(createParam)
			const createRes = await this.saveData(createParam);
			if (!createRes.IsSuccess) {
				throw new Error(createRes.Errors[0].Message);
			}
			console.log(createRes)
			//查询库存
			const queryStockParam = getQueryStockParam(createRes["SuccessEntitys"][0]["Id"], '',date);
			const queryStockRes = await this.queryStock(queryStockParam);
			if (queryStockRes["success"] !== "成功") {
				throw new Error(queryStockRes["result"]);
			}
			//查询盘点单明细
			const condition = this.createQueryBillDetailListCondition(createRes["SuccessEntitys"][0]["Id"]);
			const billDetailListParam = getBillDetailListParam(condition)
			const res = await this.fetchBillDetails(billDetailListParam)
			// if (res["success"] !== "成功") {
			// 	throw new Error(res["result"]);
			// }
			return res

			// this.handleSuccess(FID, indexList);

		} catch (error) {
			// this.handleError(error);
			throw new Error(error);
		} finally {
			modal.closeLoading();
		}
	},

	saveData(params) {
		return save(JSON.stringify(params))
			.then(res => res.Result.ResponseStatus);
	},

	submitData(params) {
		return submit(JSON.stringify(params))
			.then(res => res.Result.ResponseStatus);
	},
	queryStock(params) {
		return QueryStock(params)
			.then(res => res);

	},
	fetchBillDetails(billDetailListParam) {
		return getBillDetailList(JSON.stringify(billDetailListParam));
	},
	createQueryBillDetailListCondition(selected) {
		return {
			FormId: uni.getStorageSync('formId'),
			selectedFID: selected,
			FBillType: uni.getStorageSync('fbillType')
		};
	},

	auditData(params) {
		return audit(JSON.stringify(params))
			.then(res => res.Result.ResponseStatus);
	},

	handleSuccess(FID, indexList) {

		// Assuming that you pass indexList back to where it's needed
		uni.showModal({
			title: "提示",
			content: "提交成功",
			showCancel: true,
			success: (res) => {
				if (res.confirm) {
					// 用户点击确定
				} else {
					// 用户点击取消
				}
			}
		});
		playVoice(audioUrls.warningSuccessUrl);
	},

	handleError(error) {
		uni.showToast({
			title: error.message || '操作失败',
			duration: 3000,
			icon: "error"
		});
		playVoice(audioUrls.successVoiceUrl)
		console.error('操作失败:', error);
	}
};