var _excluded = ["onRowClick", "resultProps", "onAddClient", "hasMore", "onLoadMore", "state"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { forwardRef, useImperativeHandle, useRef, useState } from 'react';
import { Tag, List } from 'antd';
// @ts-ignore
import { locales } from '@pisell/utils';
import dayjs from 'dayjs';
import InfiniteScroll from "./InfiniteScroll";
import ComprehensiveSearch from "../../pro/comprehensiveSearch";
import ClientName from "../clientName";
import AddClient from "./Add";
import localeTexts from "./locales";
import useEngineContext from "../../hooks/useEngineContext";
import useTranslationOriginal from "../../hooks/useTranslationOriginal";
import "./index.less";
var ClientSearch = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _context$appHelper, _state$bookingConfig, _context$appHelper2;
  var onRowClick = _ref.onRowClick,
    resultProps = _ref.resultProps,
    onAddClient = _ref.onAddClient,
    hasMore = _ref.hasMore,
    onLoadMore = _ref.onLoadMore,
    state = _ref.state,
    props = _objectWithoutProperties(_ref, _excluded);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    modalVisible = _useState2[0],
    setModalVisible = _useState2[1];
  var comprehensiveSearchRef = useRef(null);
  var addClientRef = useRef(null);
  var scrollContainerRef = useRef(null);
  var translationOriginal = useTranslationOriginal();
  var context = useEngineContext();
  locales.init(localeTexts, ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.storage) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.get('umi_locale')) || 'en');
  var appointmentSubject = (state === null || state === void 0 || (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.basic) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.appointment_subject) || {};
  var translation = context === null || context === void 0 || (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.translationShop;
  var handleAdd = function handleAdd() {
    setModalVisible(true);
  };
  var handleModalCancel = function handleModalCancel() {
    var _addClientRef$current;
    setModalVisible(false);
    (_addClientRef$current = addClientRef.current) === null || _addClientRef$current === void 0 || _addClientRef$current.reset();
  };

  // 渲染列表头部
  var renderListHeader = function renderListHeader() {
    return /*#__PURE__*/React.createElement("div", {
      className: "client-search-list-header"
    }, /*#__PURE__*/React.createElement("div", {
      className: "header-item",
      style: {
        width: 180
      }
    }, locales.getText('pisell2.text.client-search.name')), /*#__PURE__*/React.createElement("div", {
      className: "header-item",
      style: {
        width: 110
      }
    }, locales.getText('pisell2.text.client-search.register-time')), /*#__PURE__*/React.createElement("div", {
      className: "header-item",
      style: {
        width: 125
      }
    }, locales.getText('pisell2.text.client-search.membership')), /*#__PURE__*/React.createElement("div", {
      className: "header-item",
      style: {
        width: 180
      }
    }, locales.getText('pisell2.text.client-search.contact-info')), /*#__PURE__*/React.createElement("div", {
      className: "header-item",
      style: {
        width: 200
      }
    }, translation(appointmentSubject === null || appointmentSubject === void 0 ? void 0 : appointmentSubject.form_name) || locales.getText('pisell2.text.client-search.holder')));
  };

  // 自定义结果展示组件
  var renderResults = function renderResults(searchResults) {
    return /*#__PURE__*/React.createElement(React.Fragment, null, searchResults !== null && searchResults !== void 0 && searchResults.length ? renderListHeader() : null, /*#__PURE__*/React.createElement("div", {
      ref: scrollContainerRef,
      id: "client-search_infinite-scroll",
      style: {
        height: 'calc(100% - 40px)',
        overflowY: 'auto',
        overflowX: 'hidden',
        display: 'flex'
      }
    }, /*#__PURE__*/React.createElement(InfiniteScroll, {
      dataLength: searchResults.length,
      next: function next() {
        console.log('loading more');
        onLoadMore === null || onLoadMore === void 0 || onLoadMore();
      },
      hasMore: hasMore || false,
      style: {
        width: '100%'
      },
      loader: /*#__PURE__*/React.createElement("div", {
        style: {
          textAlign: 'center',
          padding: '20px',
          color: '#999'
        }
      }, locales.getText('pisell2.text.loading')),
      endMessage: /*#__PURE__*/React.createElement("div", {
        style: {
          textAlign: 'center',
          color: '#999'
        }
      }, searchResults !== null && searchResults !== void 0 && searchResults.length ? locales.getText('pisell2.text.no-more-data') : null),
      scrollableTarget: "client-search_infinite-scroll",
      scrollThreshold: '180px'
    }, /*#__PURE__*/React.createElement(List, {
      dataSource: searchResults,
      renderItem: function renderItem(item, index) {
        var _latest_wallet_detail, _item$contacts_info, _item$contacts_info2, _item$contacts_info3, _item$form_record, _item$form_record2;
        var membershipText = (_latest_wallet_detail = item.latest_wallet_detail) === null || _latest_wallet_detail === void 0 || (_latest_wallet_detail = _latest_wallet_detail.wallet) === null || _latest_wallet_detail === void 0 ? void 0 : _latest_wallet_detail.name;
        return /*#__PURE__*/React.createElement(List.Item, {
          key: item.id,
          className: "client-search-list-item"
        }, /*#__PURE__*/React.createElement("div", {
          className: "list-item-content",
          onClick: function onClick() {
            return onRowClick === null || onRowClick === void 0 ? void 0 : onRowClick(item, index || 0);
          },
          style: {
            cursor: 'pointer'
          }
        }, /*#__PURE__*/React.createElement("div", {
          className: "item-cell",
          style: {
            width: 180
          }
        }, /*#__PURE__*/React.createElement(ClientName, {
          dataSource: item
        }), /*#__PURE__*/React.createElement("div", null, item.phone || '-'), /*#__PURE__*/React.createElement("div", {
          style: {
            overflow: 'hidden',
            textOverflow: 'ellipsis',
            whiteSpace: 'nowrap'
          }
        }, item.email || '-')), /*#__PURE__*/React.createElement("div", {
          className: "item-cell",
          style: {
            width: 110
          }
        }, dayjs(item.created_at).format('YYYY-MM-DD') || '-'), /*#__PURE__*/React.createElement("div", {
          className: "item-cell",
          style: {
            width: 125
          }
        }, membershipText && translationOriginal !== null && translationOriginal !== void 0 && translationOriginal(membershipText) ? /*#__PURE__*/React.createElement(Tag, {
          color: "gold",
          style: {
            overflow: 'hidden',
            whiteSpace: 'nowrap',
            textOverflow: 'ellipsis',
            width: 'auto',
            maxWidth: '100%',
            display: 'inline-block',
            borderRadius: 16,
            marginRight: 0
          }
        }, translationOriginal === null || translationOriginal === void 0 ? void 0 : translationOriginal(membershipText)) : '-'), /*#__PURE__*/React.createElement("div", {
          className: "item-cell",
          style: {
            width: 180
          }
        }, /*#__PURE__*/React.createElement(ClientName, {
          dataSource: item === null || item === void 0 || (_item$contacts_info = item.contacts_info) === null || _item$contacts_info === void 0 ? void 0 : _item$contacts_info.value
        }), (item === null || item === void 0 || (_item$contacts_info2 = item.contacts_info) === null || _item$contacts_info2 === void 0 || (_item$contacts_info2 = _item$contacts_info2.value) === null || _item$contacts_info2 === void 0 || (_item$contacts_info2 = _item$contacts_info2.phone) === null || _item$contacts_info2 === void 0 ? void 0 : _item$contacts_info2.phone) || '-', /*#__PURE__*/React.createElement("div", {
          style: {
            overflow: 'hidden',
            textOverflow: 'ellipsis',
            whiteSpace: 'nowrap'
          }
        }, (item === null || item === void 0 || (_item$contacts_info3 = item.contacts_info) === null || _item$contacts_info3 === void 0 || (_item$contacts_info3 = _item$contacts_info3.value) === null || _item$contacts_info3 === void 0 ? void 0 : _item$contacts_info3.email) || '-')), /*#__PURE__*/React.createElement("div", {
          className: "item-cell",
          style: {
            width: 200
          }
        }, (item === null || item === void 0 || (_item$form_record = item.form_record) === null || _item$form_record === void 0 ? void 0 : _item$form_record.length) > 0 ? item === null || item === void 0 || (_item$form_record2 = item.form_record) === null || _item$form_record2 === void 0 ? void 0 : _item$form_record2.map(function (itemForm) {
          return /*#__PURE__*/React.createElement("div", {
            key: itemForm === null || itemForm === void 0 ? void 0 : itemForm.id,
            style: {
              overflow: 'hidden',
              textOverflow: 'ellipsis',
              whiteSpace: 'nowrap'
            }
          }, (itemForm === null || itemForm === void 0 ? void 0 : itemForm.main_field) || '-');
        }) : '-')));
      }
    }))));
  };
  var handleAddClient = function handleAddClient(record, isChecked) {
    onAddClient === null || onAddClient === void 0 || onAddClient(record, isChecked);
    handleModalCancel();
  };
  useImperativeHandle(ref, function () {
    return {
      resetScrollPosition: function resetScrollPosition() {
        if (scrollContainerRef.current) {
          scrollContainerRef.current.scrollTop = 0;
        }
      }
    };
  });
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ComprehensiveSearch, _extends({}, props, {
    ref: comprehensiveSearchRef,
    renderType: "modal",
    onAdd: handleAdd,
    resultDisplay: renderResults
  })), /*#__PURE__*/React.createElement(AddClient, {
    containerProps: {
      className: 'pisell-client-search__new-client-modal',
      title: locales.getText('pisell2.text.create-new-client'),
      open: modalVisible,
      onCancel: handleModalCancel,
      width: 680
    },
    onChange: handleAddClient,
    ref: addClientRef
  }));
});
export default ClientSearch;