import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import { Eftpos } from "./const.js";
import SettlementModal from "./SettlementModal/index.js";
import React, { useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
import { Alert, Button, Modal, Pagination, Space, Table, Typography } from "antd";
import { locales } from "@pisell/utils";
import XClose from "@pisell/icon/es/XClose";
//#region src/components/eftpos/table.tsx
const _excluded = ["connection", "setting"], _excluded2 = ["connection", "setting"], _excluded3 = ["connection", "setting"];
const { Column } = Table;
const { Title, Text } = Typography;
const App = ({ type, eftposName, tableSize, getList, onEdit, onDel, onRepair, onLog, style, useTyroLog, systemConfigStatus, onClickByTip }, ref) => {
	/**
	* @title: 设置table 列宽
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:26
	*/
	const size = useMemo(() => {
		if (Array.isArray(tableSize)) return [
			tableSize[0] || 200,
			tableSize[1] || 120,
			tableSize[2] || 120,
			tableSize[3] || 260
		];
		return [
			200,
			120,
			120,
			260
		];
	}, [tableSize]);
	useMemo(() => {
		return size.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
	}, [size]);
	const [loading, setLoading] = useState(false);
	const deviceRef = useRef("");
	const [isModalOpen, setModalOpen] = useState(false);
	const [pagination, setPagination] = useState({
		pageSize: 10,
		current: 1,
		total: 0
	});
	const [data, setData] = useState([]);
	const [settlementModalOpen, setSettlementModalOpen] = useState(false);
	const [settlementModalData, setSettlementModalData] = useState(null);
	/**
	* @title: 加载列表数据
	* @param {number} current 加载页数
	* @return {*}
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:25
	*/
	const loadData = function() {
		var _ref = _asyncToGenerator(function* (current) {
			if (typeof getList !== "function") return Promise.reject(`getList ${getList} is not function`);
			setLoading(true);
			const res = yield getList({
				type,
				num: pagination.pageSize,
				skip: typeof current === "number" ? current : pagination.current
			}).catch((res) => {
				setLoading(false);
			});
			setLoading(false);
			setPagination((p) => _objectSpread2(_objectSpread2({}, p), {}, {
				total: res.data.count,
				current: typeof current === "number" ? current : pagination.current
			}));
			setData(res.data.list);
		});
		return function loadData(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* @title:  初始化加载页面
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:25
	*/
	useEffect(() => {
		loadData(1);
	}, []);
	const onAddDevice = () => {
		onEdit();
	};
	/**
	* @title: 编辑设备
	* @param {any} device 设备信息
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:26
	*/
	const onEditDevice = (device) => {
		const { connection, setting } = device;
		onEdit(_objectSpread2(_objectSpread2(_objectSpread2({}, _objectWithoutProperties(device, _excluded)), connection || {}), setting || {}));
	};
	/**
	* @title: 重连设备
	* @param {any} device 设备信息
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:26
	*/
	const onRepairDevice = (device) => {
		const { connection, setting } = device;
		onRepair(_objectSpread2(_objectSpread2(_objectSpread2({}, _objectWithoutProperties(device, _excluded2)), connection || {}), setting || {}));
	};
	/**
	* @title: 重连设备
	* @param {any} device 设备信息
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:26
	*/
	const onLogDevice = (device) => {
		const { connection, setting } = device;
		onLog(_objectSpread2(_objectSpread2(_objectSpread2({}, _objectWithoutProperties(device, _excluded3)), connection || {}), setting || {}));
	};
	/**
	* @title: 删除设备信息
	* @param {any} device 设备信息
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:27
	*/
	const onDelDevice = (device) => {
		deviceRef.current = device.id;
		setModalOpen(true);
	};
	/**
	* @title: 分页加载
	* @param {number} num 加载页数
	* @return {*}
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:27
	*/
	const onChangeSize = (num) => {
		loadData(num);
	};
	/**
	* @title: 删除确认按钮
	* @description: 删除完成重新加载数据
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:28
	*/
	const onOk = () => {
		setModalOpen(false);
		if (deviceRef.current) onDel(deviceRef.current).then(() => {
			return loadData();
		});
	};
	useImperativeHandle(ref, () => ({ loadData }));
	const onSettlement = (device) => {
		console.log("onSettlement", device);
		setSettlementModalOpen(true);
		setSettlementModalData({
			type: "SETTLEMENT",
			card_reader_id: device.id
		});
	};
	const onSettlementEnquiry = (device) => {
		console.log("onSettlementEnquiry", device);
		setSettlementModalOpen(true);
		setSettlementModalData({
			type: "ENQUIRY",
			card_reader_id: device.id
		});
	};
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", {
		className: "pisell-lowcode__pos-table",
		style: _objectSpread2({}, style || {})
	}, type === "windcave" && !systemConfigStatus ? /* @__PURE__ */ React.createElement(Alert, {
		message: locales.getText("page.eftpos.text.setting.tip"),
		type: "error",
		style: { margin: "10px 0 0 0" },
		action: /* @__PURE__ */ React.createElement(Button, {
			size: "small",
			type: "text",
			onClick: onClickByTip
		}, locales.getText("page.eftpos.text.setting.btn"))
	}) : null, /* @__PURE__ */ React.createElement(Title, {
		level: 4,
		className: "pisell-lowcode__pos-title"
	}, eftposName, " ", locales.getText("page.eftpos.text.device")), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__pos-table-act" }, /* @__PURE__ */ React.createElement(Text, null, locales.getText("page.eftpos.text.device.list")), /* @__PURE__ */ React.createElement(Button, {
		type: "primary",
		onClick: onAddDevice,
		style: { height: "48px" }
	}, locales.getText("page.eftpos.text.device.add")())), /* @__PURE__ */ React.createElement(Table, {
		dataSource: data,
		rowKey: "id",
		pagination: false,
		loading,
		bordered: true,
		scroll: {
			y: "600px",
			x: "100%"
		},
		tableLayout: "fixed"
	}, /* @__PURE__ */ React.createElement(Column, {
		title: locales.getText("page.eftpos.text.table.name"),
		dataIndex: "name",
		key: "name",
		width: size[0],
		className: "pisell-lowcode__pos-table-col"
	}), /* @__PURE__ */ React.createElement(Column, {
		title: locales.getText("page.eftpos.text.table.status"),
		width: size[1],
		dataIndex: "status",
		key: "status",
		render: (status) => {
			return locales.getText(`page.eftpos.text.${status.toLocaleLowerCase()}`);
		},
		className: "pisell-lowcode__pos-table-col"
	}), /* @__PURE__ */ React.createElement(Column, {
		title: locales.getText("page.eftpos.text.table.surcharge"),
		dataIndex: "setting",
		key: "setting",
		width: size[2],
		render: (setting) => {
			if (!setting) return /* @__PURE__ */ React.createElement(Text, null, "-");
			if (setting.useOriginSurcharge) return /* @__PURE__ */ React.createElement(Text, null, "-");
			const array = [setting.constant_rate ? setting.constant_rate + "%" : "", setting.constant_money].filter(Boolean);
			if (array.length === 0) return /* @__PURE__ */ React.createElement(Text, null, "-");
			return /* @__PURE__ */ React.createElement(Text, null, setting.surcharge ? array.length > 1 ? array.join("+") : array[0] : "-");
		},
		className: "pisell-lowcode__pos-table-col"
	}), /* @__PURE__ */ React.createElement(Column, {
		title: locales.getText("page.eftpos.text.table.opeartion"),
		width: type === Eftpos.MX51 ? 600 : size[3],
		key: "opeartion",
		className: "pisell-lowcode__pos-table-col",
		render: (record) => /* @__PURE__ */ React.createElement(Space, { size: "middle" }, /* @__PURE__ */ React.createElement(Button, {
			type: "link",
			onClick: () => onRepairDevice(record)
		}, locales.getText("page.eftpos.text.repair")), useTyroLog && type === "tyro" ? /* @__PURE__ */ React.createElement(Button, {
			type: "link",
			onClick: () => onLogDevice(record)
		}, locales.getText("page.eftpos.text.log")) : null, /* @__PURE__ */ React.createElement(Button, {
			type: "link",
			onClick: () => onEditDevice(record)
		}, locales.getText("page.eftpos.text.edit")), /* @__PURE__ */ React.createElement(Button, {
			type: "link",
			onClick: () => onDelDevice(record)
		}, locales.getText("page.eftpos.text.delete")), type === Eftpos.MX51 ? /* @__PURE__ */ React.createElement(Space, null, /* @__PURE__ */ React.createElement(Button, {
			type: "link",
			onClick: () => onSettlement(record)
		}, locales.getText("page.eftpos.text.settlement")), /* @__PURE__ */ React.createElement(Button, {
			type: "link",
			onClick: () => onSettlementEnquiry(record)
		}, locales.getText("page.eftpos.text.settlement_enquiry"))) : null)
	})), /* @__PURE__ */ React.createElement(Pagination, {
		style: { textAlign: "right" },
		pageSize: pagination.pageSize,
		current: pagination.current,
		total: pagination.total,
		onChange: onChangeSize,
		onShowSizeChange: onChangeSize
	})), /* @__PURE__ */ React.createElement(Modal, {
		open: isModalOpen,
		width: 400,
		centered: true,
		onCancel: () => setModalOpen(false),
		closeIcon: /* @__PURE__ */ React.createElement(XClose, {
			size: 20,
			style: { fontSize: "24px" }
		}),
		footer: /* @__PURE__ */ React.createElement("div", { style: {
			display: "flex",
			gap: "12px"
		} }, /* @__PURE__ */ React.createElement(Button, {
			block: true,
			onClick: () => setModalOpen(false),
			style: {
				flex: 1,
				height: "48px",
				borderRadius: "8px"
			}
		}, locales.getText("page.eftpos.text.table.del.cancel")), /* @__PURE__ */ React.createElement(Button, {
			type: "primary",
			block: true,
			onClick: onOk,
			style: {
				flex: 1,
				height: "48px",
				borderRadius: "8px"
			}
		}, locales.getText("page.eftpos.text.table.del.delete")))
	}, /* @__PURE__ */ React.createElement("div", { style: { height: "34px" } }), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__pos-table-del-title" }, locales.getText("page.eftpos.text.table.del.title")), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__pos-table-del-text" }, locales.getText("page.eftpos.text.table.del.text"))), settlementModalOpen && /* @__PURE__ */ React.createElement(SettlementModal, {
		open: settlementModalOpen,
		onClose: () => setSettlementModalOpen(false),
		data: settlementModalData
	}));
};
var table_default = React.forwardRef(App);
//#endregion
export { table_default as default };
