import React, { useEffect, useContext } from 'react';
import { useRequest } from 'ahooks';
import CurrentDay from "../components/CurrentDay";
import List from "./list";
import ProductDetailDrawer from "./ProductDetailDrawer";
import { Context } from "../model";
import { getProductList } from "./serve";
import { formatProductSchedules } from "./utils";
import "./index.less";
var Products = function Products() {
  var _useContext = useContext(Context),
    state = _useContext.state,
    dispatch = _useContext.dispatch;
  var products = state.products,
    currentSelectDayDetail = state.currentSelectDayDetail;
  var _getProducts = useRequest(getProductList, {
    manual: true
  });

  // 当获取完商品数据和日程数据时, 进行合并,方便后续处理
  useEffect(function () {
    dispatch({
      type: 'setProducts',
      payload: {
        list: formatProductSchedules(_getProducts.data || [], state.schedules.scheduleMap)
      }
    });
  }, [_getProducts.data, state.schedules.scheduleMap]);
  var _init = function _init() {
    // 获取数据前, 关闭所有的弹窗
    dispatch({
      type: 'setProducts',
      payload: {
        search: '',
        currentProduct: null,
        openProductDetailDrawer: false,
        openAddSchedulesDrawer: false
      }
    });
    // 如果选择了空的日期, 则直接设置为空值
    if (currentSelectDayDetail && (!currentSelectDayDetail.scheduleIds || !currentSelectDayDetail.scheduleIds.length)) {
      _getProducts.mutate([]);
    } else {
      _getProducts.run({
        skip: 1,
        num: 500,
        // extension_type: ["normal"],
        with: ['category'],
        status: 'published',
        open_quotation: 1,
        with_schedule: true,
        schedule: 1,
        schedule_ids: currentSelectDayDetail ? currentSelectDayDetail.scheduleIds || [] : undefined
      });
    }
  };
  useEffect(function () {
    _init();
  }, [currentSelectDayDetail]);
  var handleClose = function handleClose() {
    dispatch({
      type: 'setState',
      payload: {
        currentSelectDayDetail: null
      }
    });
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__schedules-component-products"
  }, /*#__PURE__*/React.createElement(CurrentDay, {
    onClose: handleClose,
    currentDay: currentSelectDayDetail === null || currentSelectDayDetail === void 0 ? void 0 : currentSelectDayDetail.date
  }), /*#__PURE__*/React.createElement(List, {
    loading: _getProducts.loading,
    list: products.list
  }), /*#__PURE__*/React.createElement(ProductDetailDrawer, null));
};
export default Products;