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