const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("./component/alert/index.js");
const require_titlebar = require("./component/header/titlebar.js");
const require_helper = require("./helper.js");
const require_index$1 = 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 ahooks = require("ahooks");
//#region src/components/eftposPay/amount.tsx
/**
* @title: 支付金额信息配置页
* @description: 配置支付金额与手续费
* @return React.Element
* @Author: hongbing.wang
* @Date: 2024-08-12 11:35
*/
var amount_default = ({ className = "", onChange, onClose, formatAmount = (number, precision, symbol) => `${symbol}${number}`, isMobile, net, client }) => {
	const dispatch = require_index$1.useDispatch();
	const [status, setStatus] = (0, react.useState)(true);
	const { name, symbol, money, amount: lastMoney, surRate, surMoney, useOriginSurcharge } = require_helper.getAmonunt(require_index$1.useContextData());
	const [curSurRate, setSurRate] = (0, react.useState)(surRate);
	const [curSurMoney, setSurMoney] = (0, react.useState)(surMoney);
	const [currentMoney, setCurrentMoney] = (0, react.useState)(lastMoney);
	const [tempSurRate, setTempSurRate] = (0, react.useState)(curSurRate);
	const [tempSurMoney, setTempSurMoney] = (0, react.useState)(curSurMoney);
	const surRateMoney = (0, react.useMemo)(() => {
		const surRateDeci = new decimal_js.Decimal(curSurRate);
		return new decimal_js.Decimal(currentMoney).mul(surRateDeci.div(100)).toFixed(2, decimal_js.Decimal.ROUND_HALF_UP);
	}, [currentMoney, curSurRate]);
	/**
	* @title: 总手续费
	* @description: 总手续费 = 比例手续费 + 固定手续费
	* @return  总手续费
	* @Author: hongbing.wang
	* @Date: 2024-08-19 09:51
	*/
	const totalSurMoney = (0, react.useMemo)(() => {
		const surRateDeci = new decimal_js.Decimal(curSurRate);
		const moneyDeci = new decimal_js.Decimal(currentMoney);
		const surMoneyDeci = new decimal_js.Decimal(curSurMoney);
		return moneyDeci.mul(surRateDeci.div(100)).add(surMoneyDeci).toFixed(2, decimal_js.Decimal.ROUND_HALF_UP);
	}, [
		currentMoney,
		curSurRate,
		curSurMoney
	]);
	/**
	* @title: 总金额
	* @description: 总金额 = 总手续费 + 订单金额
	* @return 总金额
	* @Author: hongbing.wang
	* @Date: 2024-08-19 09:52
	*/
	const total = (0, react.useMemo)(() => {
		const totalSurDeci = new decimal_js.Decimal(totalSurMoney);
		const moneyDeci = new decimal_js.Decimal(currentMoney);
		return totalSurDeci.add(moneyDeci).toFixed(2, decimal_js.Decimal.ROUND_HALF_UP);
	}, [currentMoney, totalSurMoney]);
	const subtracted = (0, react.useMemo)(() => {
		const moneyDeci = new decimal_js.Decimal(money);
		const currentMoneyDeci = new decimal_js.Decimal(currentMoney);
		return moneyDeci.sub(currentMoneyDeci).toFixed(2, decimal_js.Decimal.ROUND_HALF_UP);
	}, [currentMoney, money]);
	(0, ahooks.useMount)(() => {
		require_payLog.default.log("金额页面展示", {
			amount: currentMoney,
			surcharge: totalSurMoney,
			total,
			surRate: curSurRate
		});
	});
	/**
	* @title:提交
	* @description: 1. 修改金额页面提交 前往下一步, 修改收费费金额 前往修改金额页
	* @return {*}
	* @Author: hongbing.wang
	* @Date: 2024-08-19 09:50
	*/
	const onSubmit = () => {
		if (!status) {
			setStatus(true);
			setSurRate(tempSurRate);
			setSurMoney(tempSurMoney);
			return;
		}
		if (!net) {
			antd.message.open({
				key: "net-warning",
				type: "warning",
				content: _pisell_utils.locales.getText("page.eftpos-pay.text.not.connected")
			});
			return;
		}
		const rate = new decimal_js.Decimal(curSurRate).div(100).toFixed(4, decimal_js.Decimal.ROUND_HALF_UP);
		const pay = {
			sourceAmount: `${money}`,
			amount: `${currentMoney}`,
			surcharge: `${totalSurMoney}`,
			total: `${total}`,
			surPercentRate: `${rate}`,
			surRate: `${curSurRate}`,
			surMoney: `${curSurMoney}`,
			surRateMoney: `${surRateMoney}`,
			totalUnit: formatAmount(total, 2, symbol)
		};
		require_payLog.default.log("金额页-确认提交", { pay });
		dispatch(require_index$1.updateState({ pay }));
		dispatch(require_index$1.updateComponent(""));
		onChange && onChange("page", "pay");
	};
	/**
	* @title: 取消
	* @description: 跳出 支付页 返回支付失败
	* @Author: hongbing.wang
	* @Date: 2024-08-19 09:52
	*/
	const onCancel = () => {
		if (!status) {
			setStatus(true);
			return;
		}
		require_payLog.default.log("金额页-点击取消");
		onClose();
	};
	/**
	* @title: 回到编辑页
	* @description:
	* @Author: hongbing.wang
	* @Date: 2024-08-19 09:53
	*/
	const onEdit = () => {
		require_payLog.default.log("金额页-点击编辑");
		setStatus(false);
		setTempSurRate(curSurRate);
		setTempSurMoney(curSurMoney);
	};
	/**
	* 1. 金额编辑页  status :true
	* 2. 手续费编辑页 status :false
	* isMobile 确认关闭按钮 出现在哪里
	*/
	return /* @__PURE__ */ react.default.createElement("div", { className: `pisell-lowcode__eft-pay-amount ${className}` }, isMobile ? null : /* @__PURE__ */ react.default.createElement(require_titlebar.default, {
		type: "left-title-close",
		onClose
	}, status ? name : _pisell_utils.locales.getText("page.eftpos-pay.text.surcharge.edit.title")), status ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-body" }, /* @__PURE__ */ react.default.createElement("div", {
		className: "pisell-lowcode__eft-pay-cash",
		"data-money": formatAmount(money, 2, symbol)
	}, _pisell_utils.locales.getText("page.eftpos-pay.text.amount.to.pay")), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-money" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-money-title" }, _pisell_utils.locales.getText("page.eftpos-pay.text.input.title")(name)), /* @__PURE__ */ react.default.createElement(antd.InputNumber, {
		type: "number",
		addonBefore: symbol || "$",
		max: money,
		precision: 2,
		min: 0,
		style: { width: "100%" },
		value: currentMoney,
		formatter: require_helper.limitDecimals,
		parser: require_helper.limitDecimals,
		onChange: (event) => {
			if (event !== null && event !== void 0) setCurrentMoney(event || 0);
			else setCurrentMoney(0);
		}
	}), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-money-tip" }, _pisell_utils.locales.getText("page.eftpos-pay.text.amount.cash.left.pay.paid")(formatAmount(`${subtracted}`, 2, symbol)))), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-surcharge" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-details" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-total" }, _pisell_utils.locales.getText("page.eftpos-pay.text.surcharge.edit.actual.pay"), ":", formatAmount(`${total}`, 2, symbol)), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-money" }, _pisell_utils.locales.getText("page.eftpos-pay.text.surcharge.edit.inc.surcharge"), ": ", formatAmount(`${totalSurMoney}`, 2, symbol), " (", _pisell_utils.locales.getText("page.eftpos-pay.text.surcharge.edit.rate"), ":", curSurRate, "% + ", formatAmount(`${curSurMoney}`, 2, symbol), ")")), /* @__PURE__ */ react.default.createElement(antd.Button, {
		type: "link",
		onClick: onEdit,
		disabled: useOriginSurcharge
	}, _pisell_utils.locales.getText("page.eftpos-pay.text.surcharge.edit.surcharge")))) : /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-body" }, /* @__PURE__ */ react.default.createElement(require_index.default, null, _pisell_utils.locales.getText("page.eftpos-pay.text.surcharge.percentage-fixed")), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-form" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-item" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-item-title" }, _pisell_utils.locales.getText("page.eftpos-pay.text.surcharge.percentage")), /* @__PURE__ */ react.default.createElement(antd.InputNumber, {
		type: "number",
		value: tempSurRate,
		min: 0,
		max: 100,
		precision: 2,
		addonAfter: "%",
		style: { width: "100%" },
		onChange: (e) => {
			setTempSurRate(e || 0);
		}
	})), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-item" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-amount-item-title" }, _pisell_utils.locales.getText("page.eftpos-pay.text.surcharge.fixed")), /* @__PURE__ */ react.default.createElement(antd.InputNumber, {
		type: "number",
		value: tempSurMoney,
		min: 0,
		max: 1e5,
		precision: 2,
		style: { width: "100%" },
		addonBefore: symbol || "$",
		onChange: (e) => {
			setTempSurMoney(e || 0);
		}
	})))), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ react.default.createElement(antd.Button, {
		block: true,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		onClick: onCancel
	}, _pisell_utils.locales.getText("page.eftpos-pay.text.cancel")), /* @__PURE__ */ react.default.createElement(antd.Button, {
		block: true,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		type: "primary",
		onClick: onSubmit,
		disabled: currentMoney <= 0 && status
	}, _pisell_utils.locales.getText("page.eftpos-pay.text.sure"))));
};
//#endregion
exports.default = amount_default;
