const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_serve = require("../serve.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_materials = require("@pisell/materials");
//#region src/plus/orderList/components/BatchActionBar.tsx
const { useContainerContext } = _pisell_materials.PisellDataSourceContainer;
const OrderBatchActionBar = (props) => {
	const { selectedKeys, selectedRows, onClearSelection, locales } = props;
	const context = require_useEngineContext.default();
	const [loadingStates, setLoadingStates] = (0, react.useState)({});
	const { serverActions: { list } } = useContainerContext();
	const handleSuccess = (0, react.useCallback)((successMessage) => {
		list.refresh();
		onClearSelection();
	}, [onClearSelection]);
	/**
	* 操作失败后的通用处理
	*/
	const handleError = (0, react.useCallback)((error, defaultMessage) => {}, []);
	/**
	* 批量发货
	*/
	const handleShip = (0, react.useCallback)(function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			let checked = true;
			_pisell_materials.PisellModal.confirm({
				title: locales.getText("pisell2.orderList.batchAction.ship.title")(keys.length),
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("p", null, locales.getText("pisell2.orderList.batchAction.ship.content")), /* @__PURE__ */ react.default.createElement(antd.Checkbox, {
					defaultChecked: true,
					onChange: (e) => {
						checked = e.target.checked;
					}
				}, locales.getText("page.order.fa.check"))),
				onOk: function() {
					var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { ship: true }));
							yield require_serve.editordership({
								ids: keys,
								is_notice: checked ? 1 : 0
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { ship: false }));
						}
					});
					return function onOk() {
						return _ref.apply(this, arguments);
					};
				}()
			});
		});
		return function(_x, _x2) {
			return _ref2.apply(this, arguments);
		};
	}(), [handleSuccess, handleError]);
	/**
	* 发送发票
	*/
	const handleInvoice = (0, react.useCallback)(function() {
		var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			_pisell_materials.PisellModal.confirm({
				title: locales.getText("page.order.table.list.button.send.invoice.tip"),
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: locales.getText("pisell2.orderList.batchAction.button.ok"),
				onOk: function() {
					var _ref3 = require_asyncToGenerator._asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { invoice: true }));
							yield require_serve.postSendInvoice({ order_ids: keys });
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { invoice: false }));
						}
					});
					return function onOk() {
						return _ref3.apply(this, arguments);
					};
				}()
			});
		});
		return function(_x3, _x4) {
			return _ref4.apply(this, arguments);
		};
	}(), [handleSuccess, handleError]);
	/**
	* 打印装箱单
	*/
	const handlePrintPack = (0, react.useCallback)((keys, rows) => {
		var _JSON$parse;
		const shopId = (_JSON$parse = JSON.parse(window.app.data.storage.getStorage("core"))) === null || _JSON$parse === void 0 || (_JSON$parse = _JSON$parse.mypisell_data) === null || _JSON$parse === void 0 ? void 0 : _JSON$parse.shop_id;
		if (!shopId) {
			console.log("shopId is not found");
			return;
		}
		require_serve.printPack({
			host: "",
			shop_id: shopId,
			ids: keys
		});
	}, [context]);
	/**
	* 打印拣货单
	*/
	const handlePrintPicking = (0, react.useCallback)((keys, rows) => {
		var _JSON$parse2;
		const shopId = (_JSON$parse2 = JSON.parse(window.app.data.storage.getStorage("core"))) === null || _JSON$parse2 === void 0 || (_JSON$parse2 = _JSON$parse2.mypisell_data) === null || _JSON$parse2 === void 0 ? void 0 : _JSON$parse2.shop_id;
		if (!shopId) {
			console.log("shopId is not found");
			return;
		}
		require_serve.printPicking({
			host: "",
			shop_id: shopId,
			ids: keys
		});
	}, [context]);
	/**
	* 添加标记
	*/
	const handleAddTags = (0, react.useCallback)(function() {
		var _ref6 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			let tags = [];
			_pisell_materials.PisellModal.confirm({
				title: locales.getText("pisell2.orderList.batchAction.addTags.title")(keys.length),
				width: 520,
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: /* @__PURE__ */ react.default.createElement("div", { style: { paddingTop: 16 } }, /* @__PURE__ */ react.default.createElement("div", { style: {
					marginBottom: 8,
					color: "#666"
				} }, locales.getText("pisell2.orderList.column.tags")), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellQuickFilter, {
					hasForm: false,
					filterList: [{
						name: "tag_ids",
						type: "select",
						key: "tag_ids",
						label: locales.getText("pisell2.orderList.filter.tags.label"),
						other: {
							allowClear: true,
							mode: "multiple",
							isSearchServer: true,
							showSearch: true,
							onChange: (value, options) => {
								tags = options.map((opt) => opt.label);
							},
							style: { width: "470px" },
							placeholder: locales.getText("pisell2.orderList.filter.tags.placeholder"),
							options: (params) => require_serve.getTagSelectOptions(params).then((res) => res.map((item) => ({
								label: item.name,
								value: item.id
							})))
						}
					}]
				})),
				onOk: function() {
					var _ref5 = require_asyncToGenerator._asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { addTags: true }));
							yield require_serve.batchAddOrderTags({
								name: tags,
								ids: keys
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { addTags: false }));
						}
					});
					return function onOk() {
						return _ref5.apply(this, arguments);
					};
				}()
			});
		});
		return function(_x5, _x6) {
			return _ref6.apply(this, arguments);
		};
	}(), [handleSuccess, handleError]);
	/**
	* 删除标记
	*/
	const handleRemoveTags = (0, react.useCallback)(function() {
		var _ref8 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			let tags = [];
			_pisell_materials.PisellModal.confirm({
				title: locales.getText("pisell2.orderList.batchAction.removeTags.title")(keys.length),
				width: 520,
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: /* @__PURE__ */ react.default.createElement("div", { style: { paddingTop: 16 } }, /* @__PURE__ */ react.default.createElement("div", { style: {
					marginBottom: 8,
					color: "#666"
				} }, locales.getText("pisell2.orderList.column.tags")), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellQuickFilter, {
					hasForm: false,
					filterList: [{
						name: "tag_ids",
						type: "select",
						key: "tag_ids",
						label: locales.getText("pisell2.orderList.filter.tags.label"),
						other: {
							allowClear: true,
							mode: "multiple",
							isSearchServer: true,
							showSearch: true,
							onChange: (value, options) => {
								tags = options.map((opt) => opt.label);
							},
							style: { width: "470px" },
							placeholder: locales.getText("pisell2.orderList.filter.tags.placeholder"),
							options: (params) => require_serve.getTagSelectOptions(params).then((res) => res.map((item) => ({
								label: item.name,
								value: item.id
							})))
						}
					}]
				})),
				onOk: function() {
					var _ref7 = require_asyncToGenerator._asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { removeTags: true }));
							yield require_serve.batchDeleteOrderTags({
								name: tags,
								ids: keys
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { removeTags: false }));
						}
					});
					return function onOk() {
						return _ref7.apply(this, arguments);
					};
				}()
			});
		});
		return function(_x7, _x8) {
			return _ref8.apply(this, arguments);
		};
	}(), [handleSuccess, handleError]);
	/**
	* 标记为未完成（open）
	*/
	const handleMarkAsOpen = (0, react.useCallback)(function() {
		var _ref10 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			const open = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "open").length;
			const completed = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "completed").length;
			const cancelled = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "cancelled").length;
			_pisell_materials.PisellModal.confirm({
				title: locales.getText("page.order.cancel.title")(locales.getText("pisell2.orderList.orderStatus.open")),
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: locales.getText("page.order.lists.cancel.content")(open, completed, cancelled, locales.getText("pisell2.orderList.orderStatus.completed"), true, "void"),
				onOk: function() {
					var _ref9 = require_asyncToGenerator._asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { markAsOpen: true }));
							yield require_serve.batchPutOrderStatus({
								order_ids: keys,
								status: "open"
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { markAsOpen: false }));
						}
					});
					return function onOk() {
						return _ref9.apply(this, arguments);
					};
				}()
			});
		});
		return function(_x9, _x10) {
			return _ref10.apply(this, arguments);
		};
	}(), [handleSuccess, handleError]);
	/**
	* 完成订单（completed）
	*/
	const handleMarkAsCompleted = (0, react.useCallback)(function() {
		var _ref12 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			const open = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "open").length;
			const completed = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "completed").length;
			const cancelled = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "cancelled").length;
			_pisell_materials.PisellModal.confirm({
				title: locales.getText("page.order.cancel.title")(locales.getText("pisell2.orderList.orderStatus.completed")),
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				okText: locales.getText("pisell2.orderList.batchAction.button.ok"),
				content: locales.getText("page.order.lists.cancel.content")(open, completed, cancelled, locales.getText("pisell2.orderList.orderStatus.open"), true, ""),
				onOk: function() {
					var _ref11 = require_asyncToGenerator._asyncToGenerator(function* () {
						try {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { markAsCompleted: true }));
							yield require_serve.batchPutOrderStatus({
								order_ids: keys,
								status: "completed"
							});
							handleSuccess();
						} catch (error) {
							handleError();
						} finally {
							setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { markAsCompleted: false }));
						}
					});
					return function onOk() {
						return _ref11.apply(this, arguments);
					};
				}()
			});
		});
		return function(_x11, _x12) {
			return _ref12.apply(this, arguments);
		};
	}(), [handleSuccess, handleError]);
	/**
	* 取消订单（cancelled）
	*/
	const handleMarkAsCancelled = (0, react.useCallback)(function() {
		var _ref14 = require_asyncToGenerator._asyncToGenerator(function* (keys, rows) {
			const open = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "open").length;
			const completed = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "completed").length;
			const cancelled = rows === null || rows === void 0 ? void 0 : rows.filter((item) => item.status === "cancelled").length;
			/**
			* 执行取消订单操作
			* @param isVoid 是否作废 0-仅取消 1-取消并作废
			*/
			const executeCancelOrder = function() {
				var _ref13 = require_asyncToGenerator._asyncToGenerator(function* (isVoid) {
					try {
						setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { markAsCancelled: true }));
						yield require_serve.batchPutOrderStatus(require_objectSpread2._objectSpread2({
							order_ids: keys,
							status: "cancelled"
						}, isVoid === 1 && { is_void: isVoid }));
						handleSuccess();
						modalInstance.destroy();
					} catch (error) {
						handleError();
					} finally {
						setLoadingStates((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { markAsCancelled: false }));
					}
				});
				return function executeCancelOrder(_x13) {
					return _ref13.apply(this, arguments);
				};
			}();
			const modalInstance = _pisell_materials.PisellModal.confirm({
				title: locales.getText("page.order.cancel.title")(locales.getText("pisell2.orderList.orderStatus.cancelled")),
				content: locales.getText("page.order.lists.cancel.content")(open, completed, cancelled, locales.getText("pisell2.orderList.orderStatus.cancelled"), true, ""),
				cancelText: locales.getText("pisell2.orderList.batchAction.button.cancel"),
				footer: () => /* @__PURE__ */ react.default.createElement("div", { style: {
					display: "flex",
					gap: 10,
					justifyContent: "flex-end",
					width: "100%"
				} }, /* @__PURE__ */ react.default.createElement(antd.Button, {
					key: "cancel",
					onClick: () => modalInstance.destroy()
				}, locales.getText("pisell2.orderList.batchAction.button.cancel")), /* @__PURE__ */ react.default.createElement(antd.Button, {
					key: "only-cancel",
					onClick: () => executeCancelOrder(0),
					loading: loadingStates.markAsCancelled
				}, locales.getText("pisell2.orderList.batchAction.button.onlyCancel")), /* @__PURE__ */ react.default.createElement(antd.Button, {
					key: "cancel-void",
					type: "primary",
					onClick: () => executeCancelOrder(1),
					loading: loadingStates.markAsCancelled
				}, locales.getText("pisell2.orderList.batchAction.button.cancelAndVoid")))
			});
		});
		return function(_x14, _x15) {
			return _ref14.apply(this, arguments);
		};
	}(), [
		handleSuccess,
		handleError,
		loadingStates.markAsCancelled
	]);
	/**
	* 作废订单（voided）
	*/
	/**
	* 批量操作按钮配置
	*/
	const actions = (0, react.useMemo)(() => [
		{
			key: "ship",
			label: locales.getText("page.order.table.shipping_status.mark.fulfilled"),
			icon: "pisell2-check-circle-broken",
			loading: loadingStates.ship,
			onClick: handleShip
		},
		{
			key: "invoice",
			label: locales.getText("page.order.table.list.button.send.invoice"),
			icon: "pisell2-sticker-square",
			loading: loadingStates.invoice,
			onClick: handleInvoice
		},
		{
			key: "print-pack",
			label: locales.getText("page.order.table.list.printpack"),
			icon: "pisell2-cube-01",
			onClick: handlePrintPack
		},
		{
			key: "print-picking",
			label: locales.getText("page.order.table.list.printpink"),
			icon: "pisell2-package",
			onClick: handlePrintPicking
		},
		{
			key: "add-tags",
			label: locales.getText("page.order.text.addtags.button"),
			icon: "pisell2-tag-01",
			loading: loadingStates.addTags,
			onClick: handleAddTags
		},
		{
			key: "remove-tags",
			label: locales.getText("page.order.table.list.deletetag"),
			icon: "pisell2-tag-01",
			loading: loadingStates.removeTags,
			onClick: handleRemoveTags
		},
		{
			key: "mark-as-open",
			label: locales.getText("page.order.open"),
			icon: "pisell2-clipboard",
			loading: loadingStates.markAsOpen,
			onClick: handleMarkAsOpen
		},
		{
			key: "mark-as-completed",
			label: locales.getText("page.order.completeds"),
			icon: "pisell2-clipboard-check",
			loading: loadingStates.markAsCompleted,
			onClick: handleMarkAsCompleted
		},
		{
			key: "mark-as-cancelled",
			label: locales.getText("page.order.cancelleds"),
			icon: "pisell2-slash-circle-01",
			loading: loadingStates.markAsCancelled,
			onClick: handleMarkAsCancelled
		}
	], [
		loadingStates.ship,
		loadingStates.invoice,
		loadingStates.addTags,
		loadingStates.removeTags,
		loadingStates.markAsOpen,
		loadingStates.markAsCompleted,
		loadingStates.markAsCancelled,
		loadingStates.markAsVoid,
		handleShip,
		handleInvoice,
		handlePrintPack,
		handlePrintPicking,
		handleAddTags,
		handleRemoveTags,
		handleMarkAsOpen,
		handleMarkAsCompleted,
		handleMarkAsCancelled,
		locales
	]);
	if (selectedKeys.length === 0) return null;
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellBatchActionBar, {
		selectedKeys,
		selectedRows,
		position: "float",
		offset: 24,
		overflowThreshold: 2,
		defaultActions: { showDefaultActions: false },
		actions
	});
};
//#endregion
exports.default = OrderBatchActionBar;
