import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import usePrefix from "../../../../hooks/usePrefix.js";
import { ModeEnum } from "../../const.js";
import { limitDecimals } from "../../helper.js";
import { updateState, useContextData, useDispatch } from "../../store/index.js";
import payLog from "../../utils/payLog.js";
import React, { useEffect, useMemo, useState } from "react";
import { Button, InputNumber } from "antd";
import { locales } from "@pisell/utils";
import { Decimal as Decimal$1 } from "decimal.js";
import { PisellToast } from "@pisell/materials";
import "./index.less";
//#region src/components/eftposPay/component/manual/index.tsx
/**
* @title: 结果标记页面
* @Author: hongbing.wang
* @Date: 2024-08-20 11:46
*/
var manual_default = ({ onChange, className = "", formatAmount }) => {
	const dispatch = useDispatch();
	const { symbol, manual, order_id, mode, device, number, pay, amount } = useContextData();
	useEffect(() => {
		payLog.log("手动标记页展示");
	}, []);
	const [money, setMoney] = useState(useMemo(() => {
		if (typeof (manual === null || manual === void 0 ? void 0 : manual.money) === "number" || typeof (manual === null || manual === void 0 ? void 0 : manual.money) === "string") return Number(manual === null || manual === void 0 ? void 0 : manual.money);
		if (typeof (pay === null || pay === void 0 ? void 0 : pay.total) === "number" || typeof (pay === null || pay === void 0 ? void 0 : pay.total) === "string") return Number(pay === null || pay === void 0 ? void 0 : pay.total);
		return amount;
	}, [
		manual === null || manual === void 0 ? void 0 : manual.money,
		pay === null || pay === void 0 ? void 0 : pay.total,
		amount
	]));
	const [disabled, setDisabled] = useState(false);
	const prefix = usePrefix();
	const [isUpdateAmount, updateAmountStatus] = useState(manual === null || manual === void 0 ? void 0 : manual.status);
	/**
	* @title: 标记成功
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:48
	*/
	const onSuccess = () => {
		if (disabled) {
			payLog.log("手动标记：点击成功被拦截");
			return;
		}
		if (mode === ModeEnum.Refund) {
			payLog.log("手动标记(退款)：点击标记为成功");
			setDisabled(true);
			onChange("success", {
				amount,
				total: amount,
				number,
				uniquePaymentNumber: number,
				device,
				order_id,
				manual_marking_flag: 1
			});
			return;
		}
		payLog.log("手动标记：点击标记为成功");
		updateAmountStatus(true);
		dispatch(updateState({ manual: _objectSpread2(_objectSpread2({}, manual), {}, { status: true }) }));
	};
	/**
	* @title: 标记失败
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:49
	*/
	const onFail = () => {
		if (disabled) return;
		payLog.log("手动标记：点击标记为失败");
		setDisabled(true);
		onChange("fail", {
			number,
			uniquePaymentNumber: number,
			device,
			order_id
		});
		PisellToast({ content: locales.getText("page.eftpos-pay.text.manual.fail.toast") });
	};
	const onSubmit = () => {
		if (disabled) return;
		payLog.log("手动标记-确认金额：点击确认", { money });
		setDisabled(true);
		if (mode === ModeEnum.Refund || mode === ModeEnum.Query) return;
		const prevMoneyDeci = new Decimal$1((pay === null || pay === void 0 ? void 0 : pay.total) || 0);
		const moneyDeci = new Decimal$1(Number(money || 0));
		let surMoneyDeci = new Decimal$1(Number((pay === null || pay === void 0 ? void 0 : pay.surMoney) || 0));
		let surchargeDeci = new Decimal$1(Number(pay === null || pay === void 0 ? void 0 : pay.surcharge));
		const diff = moneyDeci.sub(prevMoneyDeci);
		const flag = diff.comparedTo(0);
		if (flag < 0) {
			setDisabled(false);
			return;
		}
		if (flag > 0) {
			surMoneyDeci = surMoneyDeci.add(diff);
			surchargeDeci = surchargeDeci.add(diff);
		}
		onChange("success", _objectSpread2(_objectSpread2({}, pay || {}), {}, {
			surMoney: surMoneyDeci.toFixed(2, Decimal$1.ROUND_HALF_UP),
			surcharge: surchargeDeci.toFixed(2, Decimal$1.ROUND_HALF_UP),
			total: moneyDeci.toFixed(2, Decimal$1.ROUND_HALF_UP),
			totalUnit: formatAmount(moneyDeci.toFixed(2, Decimal$1.ROUND_HALF_UP), 2, symbol),
			number,
			uniquePaymentNumber: number,
			device,
			order_id,
			manual_marking_flag: 1
		}));
		PisellToast({ content: locales.getText("page.eftpos-pay.text.manual.success.toast") });
	};
	const onCancel = () => {
		payLog.log("手动标记-确认金额：点击取消");
		updateAmountStatus(false);
		setMoney(pay === null || pay === void 0 ? void 0 : pay.total);
		dispatch(updateState({ manual: _objectSpread2(_objectSpread2({}, manual), {}, {
			status: true,
			money: pay === null || pay === void 0 ? void 0 : pay.total
		}) }));
	};
	return /* @__PURE__ */ React.createElement("div", { className: `pisell-lowcode__eft-pay-manual ${className}` }, isUpdateAmount ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-manual-edit-title" }, locales.getText("page.eftpos-pay.text.manual.title")), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-manual-edit-body" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-manual-edit-text" }, locales.getText("page.eftpos-pay.text.manual.subtitle")(formatAmount(pay === null || pay === void 0 ? void 0 : pay.total, 2, symbol)), " ", /* @__PURE__ */ React.createElement("br", null), locales.getText("page.eftpos-pay.text.manual.confirm"), /* @__PURE__ */ React.createElement("br", null), locales.getText("page.eftpos-pay.text.manual.diff")), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-manual-edit-act" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-manual-edit-text" }, locales.getText("page.eftpos-pay.text.manual.tip")), /* @__PURE__ */ React.createElement(InputNumber, {
		type: "number",
		addonBefore: symbol || "$",
		min: pay === null || pay === void 0 ? void 0 : pay.total,
		precision: 2,
		style: { width: "100%" },
		formatter: limitDecimals,
		parser: limitDecimals,
		defaultValue: money,
		onChange: (event) => {
			if (event !== null && event !== void 0) {
				setMoney(event || 0);
				dispatch(updateState({ manual: _objectSpread2(_objectSpread2({}, manual), {}, { money: event || 0 }) }));
			} else {
				setMoney(0);
				dispatch(updateState({ manual: _objectSpread2(_objectSpread2({}, manual), {}, { money: 0 }) }));
			}
		}
	}))), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ React.createElement(Button, {
		className: "pisell-lowcode__eft-pay-manual-btn",
		block: true,
		onClick: onCancel
	}, locales.getText("page.eftpos-pay.text.cancel")), /* @__PURE__ */ React.createElement(Button, {
		className: "pisell-lowcode__eft-pay-manual-btn",
		block: true,
		type: "primary",
		onClick: onSubmit
	}, locales.getText("page.eftpos-pay.text.manual.btn.confirm")(formatAmount(money, 2, symbol))))) : /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-manual-title" }, manual === null || manual === void 0 ? void 0 : manual.title), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-manual-txt" }, manual === null || manual === void 0 ? void 0 : manual.text), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__eft-pay-manual-card-box" }, /* @__PURE__ */ React.createElement("div", {
		className: "pisell-lowcode__eft-pay-manual-card",
		onClick: onSuccess
	}, /* @__PURE__ */ React.createElement("div", {
		className: "pisell-lowcode__eft-pay-manual-img",
		style: { backgroundImage: `url(${prefix}/pay_success.png)` }
	}), /* @__PURE__ */ React.createElement(Button, {
		className: "pisell-lowcode__eft-pay-manual-btn",
		block: true
	}, locales.getText("page.eftpos-pay.text.successful")(mode))), /* @__PURE__ */ React.createElement("div", {
		className: "pisell-lowcode__eft-pay-manual-card",
		onClick: onFail
	}, /* @__PURE__ */ React.createElement("div", {
		className: "pisell-lowcode__eft-pay-manual-img",
		style: { backgroundImage: `url(${prefix}/pay_error.png)` }
	}), /* @__PURE__ */ React.createElement(Button, {
		className: "pisell-lowcode__eft-pay-manual-btn",
		block: true
	}, locales.getText("page.eftpos-pay.text.failed")(mode))))));
};
//#endregion
export { manual_default as default };
