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