function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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; }
import { useRequest } from 'ahooks';
import { Collapse, Empty, Skeleton } from 'antd';
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
import CurrentDay from "../components/CurrentDay";
import Search from "../components/Search";
import { Context } from "../model";
import ResourcesAdd from "./add";
import ResourcesEdit from "./edit";
import ResourcesDetail from "./detail";
import "./index.less";
import List from "./list";
import { getFormTypes, getRelationResource } from "./serve";
import { locales } from '@pisell/utils';
var Resources = function Resources() {
  var _useContext = useContext(Context),
    state = _useContext.state,
    dispatch = _useContext.dispatch;
  var currentSelectDayDetail = state.currentSelectDayDetail,
    resources = state.resources,
    tabKey = state.tabKey;
  var cacheResource = resources === null || resources === void 0 ? void 0 : resources.cacheResource;
  var resourcesAddRef = useRef();
  var resourcesEditRef = useRef();
  var resourcesDetailRef = useRef();
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    seachText = _useState2[0],
    setSearchText = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    activeKey = _useState4[0],
    setActiveKey = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    formTypes = _useState6[0],
    setFormTypes = _useState6[1];
  var _useState7 = useState([]),
    _useState8 = _slicedToArray(_useState7, 2),
    relationResource = _useState8[0],
    setRelationResource = _useState8[1];
  var searchChange = function searchChange(e) {
    setSearchText(e);
  };
  var _getFormTypes = useRequest(getFormTypes, {
    manual: true,
    onSuccess: function onSuccess(result) {
      var _result$data;
      var list = (result === null || result === void 0 || (_result$data = result.data) === null || _result$data === void 0 ? void 0 : _result$data.list) || [];
      setFormTypes(list);
      setActiveKey(list.map(function (item) {
        return item.id;
      }));
    }
  });
  var _getRelationResource = useRequest(getRelationResource, {
    manual: true,
    onSuccess: function onSuccess(result, params) {
      var _result$data2;
      var list = (result === null || result === void 0 || (_result$data2 = result.data) === null || _result$data2 === void 0 ? void 0 : _result$data2.list) || [];
      // 解决新增数据时立即刷新，列表新增数据无title问题
      // if (params?.[1]) {
      //   const form_record_id = params?.[1]?.res?.form_record_id;
      //   const form_data = params?.[1]?.params?.form_data || [];
      //   const index = list.findIndex((item: any) => item.id === form_record_id);
      //   if (index > -1) {
      //     const room_name = form_data?.find(
      //       (item: any) => item.key === "room_name"
      //     )?.value;
      //     if (!list[index].title) {
      //       list[index].title = room_name;
      //     }
      //   }
      // }
      setRelationResource(list);
    }
  });
  var init = function init(params) {
    _getRelationResource.run({
      skip: 1,
      num: 500,
      schedule_ids: currentSelectDayDetail ? currentSelectDayDetail.scheduleIds || [] : undefined,
      exclude_form_code: ['opening_hours']
    }, params || null);
  };
  useEffect(function () {
    _getFormTypes.run({
      type: 'manual',
      num: 500,
      skip: 1
    });
  }, []);
  useEffect(function () {
    init();
  }, [currentSelectDayDetail === null || currentSelectDayDetail === void 0 ? void 0 : currentSelectDayDetail.scheduleIds]);
  useEffect(function () {
    if (cacheResource) {
      var _resourcesEditRef$cur, _resourcesEditRef$cur2;
      //   resourcesDetailRef.current?.open?.(cacheResource);
      (_resourcesEditRef$cur = resourcesEditRef.current) === null || _resourcesEditRef$cur === void 0 || (_resourcesEditRef$cur2 = _resourcesEditRef$cur.open) === null || _resourcesEditRef$cur2 === void 0 || _resourcesEditRef$cur2.call(_resourcesEditRef$cur, cacheResource);
      // 滚动到 id 对应的位置
      setTimeout(function () {
        var _el$scrollIntoView;
        var el = document.getElementById("".concat(cacheResource.id));
        console.log('el', el);
        el === null || el === void 0 || (_el$scrollIntoView = el.scrollIntoView) === null || _el$scrollIntoView === void 0 || _el$scrollIntoView.call(el, true);
      }, 100);
    }
  }, [cacheResource]);
  var items = useMemo(function () {
    var _relationResource = relationResource.filter(function (item) {
      var _item$title, _item$title$includes;
      return (_item$title = item.title) === null || _item$title === void 0 || (_item$title$includes = _item$title.includes) === null || _item$title$includes === void 0 ? void 0 : _item$title$includes.call(_item$title, seachText);
    });
    var _allData = formTypes.reduce(function (arr, item) {
      var currentResource = _relationResource.filter(function (relation) {
        return relation.form_id === item.id;
      });
      if (currentResource.length > 0) {
        arr.push(_objectSpread({
          label: item.title,
          key: item.id,
          children: currentResource
        }, item));
      }
      return arr;
    }, []);
    return _allData.map(function (item) {
      var resources = item.children || [];
      return {
        label: item.title,
        key: item.id,
        children: /*#__PURE__*/React.createElement(List, {
          list: resources.map(function (resource) {
            return {
              id: resource.id,
              title: resource.title,
              form_id: item.id
            };
          }),
          addClick: function addClick() {
            var _resourcesAddRef$curr, _resourcesAddRef$curr2;
            (_resourcesAddRef$curr = resourcesAddRef.current) === null || _resourcesAddRef$curr === void 0 || (_resourcesAddRef$curr2 = _resourcesAddRef$curr.open) === null || _resourcesAddRef$curr2 === void 0 || _resourcesAddRef$curr2.call(_resourcesAddRef$curr, item.id);
          },
          itemClick: function itemClick(resource) {
            var _resourcesEditRef$cur3, _resourcesEditRef$cur4;
            //   resourcesDetailRef.current?.open?.(resource);
            (_resourcesEditRef$cur3 = resourcesEditRef.current) === null || _resourcesEditRef$cur3 === void 0 || (_resourcesEditRef$cur4 = _resourcesEditRef$cur3.open) === null || _resourcesEditRef$cur4 === void 0 || _resourcesEditRef$cur4.call(_resourcesEditRef$cur3, resource);
          }
        })
      };
    });
  }, [formTypes, seachText, relationResource]);
  var handleClose = function handleClose() {
    dispatch({
      type: 'setState',
      payload: {
        currentSelectDayDetail: null
      }
    });
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__schedules-component-resources"
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      padding: '0 32px'
    }
  }, /*#__PURE__*/React.createElement(CurrentDay, {
    onClose: handleClose,
    currentDay: currentSelectDayDetail === null || currentSelectDayDetail === void 0 ? void 0 : currentSelectDayDetail.date
  })), /*#__PURE__*/React.createElement("div", {
    className: "resources-header"
  }, /*#__PURE__*/React.createElement(Search, {
    value: seachText,
    onChange: searchChange
  })), /*#__PURE__*/React.createElement("div", {
    className: "resources-list"
  }, /*#__PURE__*/React.createElement(Skeleton, {
    loading: _getRelationResource.loading,
    style: {
      marginTop: 12
    }
  }, items.length ? /*#__PURE__*/React.createElement(Collapse, {
    activeKey: activeKey,
    ghost: true,
    items: items,
    expandIconPosition: "end",
    onChange: function onChange(key) {
      setActiveKey(key);
    }
  }) : /*#__PURE__*/React.createElement(Empty, {
    image: Empty.PRESENTED_IMAGE_SIMPLE,
    description: locales.getText('pisell2.text.products.no-resources')
  })))), /*#__PURE__*/React.createElement(ResourcesAdd, {
    ref: resourcesAddRef,
    onSuccess: init
  }), /*#__PURE__*/React.createElement(ResourcesEdit, {
    ref: resourcesEditRef,
    onSuccess: init
  }), /*#__PURE__*/React.createElement(ResourcesDetail, {
    tabKey: tabKey,
    ref: resourcesDetailRef,
    onSuccess: function onSuccess(e) {
      if (e) {
        init();
      }
    }
  }));
};
export default Resources;