const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_hooks = require("../hooks.js");
const require_locales = require("../locales.js");
const require_index = require("../components/SearchBar/index.js");
const require_index$1 = require("../components/CupActionModal/index.js");
const require_const = require("./const.js");
const require_useColumns = require("./hooks/useColumns.js");
const require_useSummary = require("./hooks/useSummary.js");
const require_useCupApi = require("./hooks/useCupApi.js");
const require_index$2 = require("../components/PageHeader/index.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let ahooks = require("ahooks");
let _ant_design_icons = require("@ant-design/icons");
require("./index.less");
//#region src/components/ecocup/cupList/index.tsx
const prefixCls = "ecocup-cup-list";
const SCAN_LISTENER_KEY = "eco-cup-list-scan-listener";
const PAGE_SIZE = 10;
const FORMAT = "YYYY-MM-DD HH:mm:ss";
const CupList = (props) => {
	const [date, setDate] = (0, react.useState)([(0, dayjs.default)().startOf("day"), (0, dayjs.default)().endOf("day")]);
	const cupActionModal = require_hooks.useModal("ecocup-list-action-modal");
	const { isNormalShop } = require_hooks.useGetGoalShopInfo();
	require_hooks.useGetShopId();
	const isApp = require_hooks.useIsApp();
	const language = require_hooks.useLanguage();
	const { isEcoShare } = require_hooks.useGetShare();
	_pisell_utils.locales.init(require_locales.default, language);
	const [listParams, setListParams] = (0, react.useState)({
		skip: 1,
		num: PAGE_SIZE
	});
	const listParamsRef = (0, react.useRef)(listParams);
	const { loading, data, run } = require_useCupApi.useCupListsApi({ isEcoShare });
	const { data: staticsData, getStatics } = require_useCupApi.useStatisticsApi({ isEcoShare });
	const { data: ecoShopLists, getEcoShopLists } = require_useCupApi.useEcoShopListsApi({ isEcoShare });
	const { postExportApi } = require_useCupApi.useExportApi();
	const { postShareApi } = require_useCupApi.useShareApi();
	const isPartner = (0, react.useMemo)(() => {
		return !!(isNormalShop || (listParams === null || listParams === void 0 ? void 0 : listParams.partner_shop_id));
	}, [isNormalShop, listParams === null || listParams === void 0 ? void 0 : listParams.partner_shop_id]);
	(0, react.useEffect)(() => {
		listParamsRef.current = listParams;
	}, [listParams]);
	const getLists = (0, react.useCallback)((params) => {
		run(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (listParamsRef === null || listParamsRef === void 0 ? void 0 : listParamsRef.current) || {}), params));
	}, [listParams]);
	(0, react.useEffect)(() => {
		var _date$, _date$2;
		if (isApp) return;
		getStatics({
			started_at: date === null || date === void 0 || (_date$ = date[0]) === null || _date$ === void 0 ? void 0 : _date$.format(FORMAT),
			ended_at: date === null || date === void 0 || (_date$2 = date[1]) === null || _date$2 === void 0 ? void 0 : _date$2.format(FORMAT),
			partner_shop_id: listParams === null || listParams === void 0 ? void 0 : listParams.partner_shop_id
		});
	}, [
		date,
		isApp,
		listParams === null || listParams === void 0 ? void 0 : listParams.partner_shop_id
	]);
	(0, react.useEffect)(() => {
		if (isNormalShop || isApp) return;
		getEcoShopLists({});
	}, [isNormalShop, isApp]);
	(0, react.useEffect)(() => {
		getLists();
	}, [getLists]);
	const columns = require_useColumns.useColumns({
		onClickAction: (0, react.useCallback)((type, record) => {
			cupActionModal.open({
				data: {
					type,
					cupData: record,
					customerData: (record === null || record === void 0 ? void 0 : record.customer) || {}
				},
				onCancel: cupActionModal.close,
				onOk: (values) => {
					cupActionModal.close();
					getLists();
				}
			});
		}, [cupActionModal]),
		locales: _pisell_utils.locales,
		isEcoShare
	});
	const summary = require_useSummary.useSummary(_pisell_utils.locales, staticsData, isPartner);
	/**
	* @description: 值变化回调处理函数
	* @param {any} changedValues
	* @param {any} values
	* @return {*}
	*/
	const handleValuesChange = (changedValues, values) => {
		var _values$order_by, _values$pagination, _values$pagination2;
		const orderByTypes = values === null || values === void 0 || (_values$order_by = values.order_by) === null || _values$order_by === void 0 ? void 0 : _values$order_by.split(":");
		const params = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (values === null || values === void 0 ? void 0 : values.filters) || {}), {}, {
			order_by: orderByTypes === null || orderByTypes === void 0 ? void 0 : orderByTypes[0],
			sort: orderByTypes === null || orderByTypes === void 0 ? void 0 : orderByTypes[1],
			skip: (values === null || values === void 0 || (_values$pagination = values.pagination) === null || _values$pagination === void 0 ? void 0 : _values$pagination.page) || 1,
			num: (values === null || values === void 0 || (_values$pagination2 = values.pagination) === null || _values$pagination2 === void 0 ? void 0 : _values$pagination2.size) || PAGE_SIZE,
			partner_shop_id: (values === null || values === void 0 ? void 0 : values.partner_shop_id) || void 0,
			status: (values === null || values === void 0 ? void 0 : values.status) || void 0
		});
		setListParams((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), params));
	};
	/**
	* @description: 值变化回调防抖处理函数
	* @return {*}
	*/
	const { run: handleValuesChangeDebounce } = (0, ahooks.useDebounceFn)(handleValuesChange, { wait: 200 });
	const handleChange = (value, type) => {
		if (type === "search") setListParams((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, {
			search: value,
			skip: 1
		}));
		else {
			setDate(value);
			setListParams((prev) => {
				var _value$, _value$2;
				return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, {
					skip: 1,
					operated_at_start: value === null || value === void 0 || (_value$ = value[0]) === null || _value$ === void 0 ? void 0 : _value$.format(FORMAT),
					operated_at_end: value === null || value === void 0 || (_value$2 = value[1]) === null || _value$2 === void 0 ? void 0 : _value$2.format(FORMAT)
				});
			});
		}
	};
	const pagination = (0, react.useMemo)(() => {
		return {
			pageSize: PAGE_SIZE,
			total: (data === null || data === void 0 ? void 0 : data.count) || 0,
			size: "large",
			current: (listParams === null || listParams === void 0 ? void 0 : listParams.skip) || 1,
			localPagination: false
		};
	}, [data === null || data === void 0 ? void 0 : data.count, listParams === null || listParams === void 0 ? void 0 : listParams.skip]);
	const onClickButton = (0, ahooks.useMemoizedFn)((key) => {
		if (key === "export") postExportApi({
			isPartner,
			listParams
		});
		else if (key === "share") postShareApi();
	});
	const headerRightContent = (0, react.useMemo)(() => {
		if (isNormalShop || isEcoShare) return null;
		return /* @__PURE__ */ react.default.createElement("div", { className: `${prefixCls}-header-right-content` }, require_const.HEADER_RIGHT_OPTIONS.map((button) => /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			key: button.key,
			type: button.type,
			size: "large",
			icon: /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, {
				type: button.icon,
				size: 22
			}),
			onClick: () => onClickButton(button.key),
			className: `${prefixCls}-header-right-content-button`
		}, _pisell_utils.locales.getText(button.languageKey))));
	}, [
		isNormalShop,
		onClickButton,
		isEcoShare
	]);
	return /* @__PURE__ */ react.default.createElement("div", { className: `${prefixCls}-container` }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Page, { style: { height: "100%" } }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Table, {
		dataSource: (data === null || data === void 0 ? void 0 : data.list) || [],
		columns,
		title: !isApp ? /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(require_index$2.default, {
			title: _pisell_utils.locales.getText("pisell2.text.ecocup.title"),
			rightContent: headerRightContent,
			isReturn: !isEcoShare
		}), /* @__PURE__ */ react.default.createElement("div", { className: `${prefixCls}-header` }, /* @__PURE__ */ react.default.createElement(_pisell_materials.DatePicker.RangePicker, {
			inputReadOnly: true,
			bordered: false,
			allowClear: false,
			size: "large",
			suffixIcon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.CalendarOutlined, null),
			value: date,
			onChange: handleChange,
			format: "DD/MM/YYYY",
			className: `${prefixCls}-date-picker`
		}), /* @__PURE__ */ react.default.createElement(require_index.default, {
			scanListenerKey: SCAN_LISTENER_KEY,
			size: "small",
			placeholder: _pisell_utils.locales.getText("pisell2.text.ecocup.scan-or-search"),
			onSearch: (value) => handleChange(value, "search")
		}))) : null,
		align: "center",
		pagination,
		columnSetting: !isApp ? require_const.COLUMN_SETTING : null,
		sort: !isApp ? require_const.SORT_SETTINGS : null,
		filter: !isApp ? require_const.FILTER(_pisell_utils.locales, isNormalShop, ecoShopLists, isPartner, listParams === null || listParams === void 0 ? void 0 : listParams.skip) : null,
		summary: !isApp ? summary : null,
		className: `${prefixCls}-table`,
		rowClassName: `${prefixCls}-table-row`,
		loading,
		onValuesChange: handleValuesChangeDebounce,
		rowKey: "id",
		virtual: false,
		tableLayout: "",
		scroll: {
			scrollToFirstRowOnChange: true,
			autoCalc: true
		}
	})), cupActionModal.visible && /* @__PURE__ */ react.default.createElement(require_index$1.default, cupActionModal.modalProps));
};
//#endregion
exports.default = CupList;
