import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { useGetGoalShopInfo, useGetShare, useGetShopId, useIsApp, useLanguage, useModal } from "../hooks.js";
import locales_default from "../locales.js";
import SearchBar from "../components/SearchBar/index.js";
import CupActionModal from "../components/CupActionModal/index.js";
import { COLUMN_SETTING, FILTER, HEADER_RIGHT_OPTIONS, SORT_SETTINGS } from "./const.js";
import { useColumns } from "./hooks/useColumns.js";
import { useSummary } from "./hooks/useSummary.js";
import { useCupListsApi, useEcoShopListsApi, useExportApi, useShareApi, useStatisticsApi } from "./hooks/useCupApi.js";
import CustomPageHeader from "../components/PageHeader/index.js";
import dayjs from "dayjs";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { locales } from "@pisell/utils";
import { Button, DatePicker, Icon, Page, Table } from "@pisell/materials";
import { useDebounceFn, useMemoizedFn } from "ahooks";
import { CalendarOutlined } from "@ant-design/icons";
import "./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] = useState([dayjs().startOf("day"), dayjs().endOf("day")]);
	const cupActionModal = useModal("ecocup-list-action-modal");
	const { isNormalShop } = useGetGoalShopInfo();
	useGetShopId();
	const isApp = useIsApp();
	const language = useLanguage();
	const { isEcoShare } = useGetShare();
	locales.init(locales_default, language);
	const [listParams, setListParams] = useState({
		skip: 1,
		num: PAGE_SIZE
	});
	const listParamsRef = useRef(listParams);
	const { loading, data, run } = useCupListsApi({ isEcoShare });
	const { data: staticsData, getStatics } = useStatisticsApi({ isEcoShare });
	const { data: ecoShopLists, getEcoShopLists } = useEcoShopListsApi({ isEcoShare });
	const { postExportApi } = useExportApi();
	const { postShareApi } = useShareApi();
	const isPartner = 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]);
	useEffect(() => {
		listParamsRef.current = listParams;
	}, [listParams]);
	const getLists = useCallback((params) => {
		run(_objectSpread2(_objectSpread2({}, (listParamsRef === null || listParamsRef === void 0 ? void 0 : listParamsRef.current) || {}), params));
	}, [listParams]);
	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
	]);
	useEffect(() => {
		if (isNormalShop || isApp) return;
		getEcoShopLists({});
	}, [isNormalShop, isApp]);
	useEffect(() => {
		getLists();
	}, [getLists]);
	const columns = useColumns({
		onClickAction: 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,
		isEcoShare
	});
	const summary = useSummary(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 = _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) => _objectSpread2(_objectSpread2({}, prev), params));
	};
	/**
	* @description: 值变化回调防抖处理函数
	* @return {*}
	*/
	const { run: handleValuesChangeDebounce } = useDebounceFn(handleValuesChange, { wait: 200 });
	const handleChange = (value, type) => {
		if (type === "search") setListParams((prev) => _objectSpread2(_objectSpread2({}, prev), {}, {
			search: value,
			skip: 1
		}));
		else {
			setDate(value);
			setListParams((prev) => {
				var _value$, _value$2;
				return _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 = 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 = useMemoizedFn((key) => {
		if (key === "export") postExportApi({
			isPartner,
			listParams
		});
		else if (key === "share") postShareApi();
	});
	const headerRightContent = useMemo(() => {
		if (isNormalShop || isEcoShare) return null;
		return /* @__PURE__ */ React.createElement("div", { className: `${prefixCls}-header-right-content` }, HEADER_RIGHT_OPTIONS.map((button) => /* @__PURE__ */ React.createElement(Button, {
			key: button.key,
			type: button.type,
			size: "large",
			icon: /* @__PURE__ */ React.createElement(Icon, {
				type: button.icon,
				size: 22
			}),
			onClick: () => onClickButton(button.key),
			className: `${prefixCls}-header-right-content-button`
		}, locales.getText(button.languageKey))));
	}, [
		isNormalShop,
		onClickButton,
		isEcoShare
	]);
	return /* @__PURE__ */ React.createElement("div", { className: `${prefixCls}-container` }, /* @__PURE__ */ React.createElement(Page, { style: { height: "100%" } }, /* @__PURE__ */ React.createElement(Table, {
		dataSource: (data === null || data === void 0 ? void 0 : data.list) || [],
		columns,
		title: !isApp ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(CustomPageHeader, {
			title: locales.getText("pisell2.text.ecocup.title"),
			rightContent: headerRightContent,
			isReturn: !isEcoShare
		}), /* @__PURE__ */ React.createElement("div", { className: `${prefixCls}-header` }, /* @__PURE__ */ React.createElement(DatePicker.RangePicker, {
			inputReadOnly: true,
			bordered: false,
			allowClear: false,
			size: "large",
			suffixIcon: /* @__PURE__ */ React.createElement(CalendarOutlined, null),
			value: date,
			onChange: handleChange,
			format: "DD/MM/YYYY",
			className: `${prefixCls}-date-picker`
		}), /* @__PURE__ */ React.createElement(SearchBar, {
			scanListenerKey: SCAN_LISTENER_KEY,
			size: "small",
			placeholder: locales.getText("pisell2.text.ecocup.scan-or-search"),
			onSearch: (value) => handleChange(value, "search")
		}))) : null,
		align: "center",
		pagination,
		columnSetting: !isApp ? COLUMN_SETTING : null,
		sort: !isApp ? SORT_SETTINGS : null,
		filter: !isApp ? FILTER(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.createElement(CupActionModal, cupActionModal.modalProps));
};
//#endregion
export { CupList as default };
