"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactPerfectScrollbar = _interopRequireDefault(require("react-perfect-scrollbar"));

var _caseLibrayHeader = _interopRequireDefault(require("./okrcases-components/caseLibray-header"));

var _caseLibraryNav = _interopRequireDefault(require("./okrcases-components/caseLibrary-nav"));

var _caseLibraryMain = _interopRequireDefault(require("./okrcases-components/caseLibrary-main"));

var _submitOkrDialog = require("./okrcases-components/submitOkr-dialog");

var _network = require("./server/network");

var _getLocale = require("../../utils/getLocale");

require("./index.css");

require("./new-ui.css");

var _BSGlobal$tenantInfo;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

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(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

// @ts-ignore
var prefix = (_BSGlobal$tenantInfo = BSGlobal.tenantInfo) !== null && _BSGlobal$tenantInfo !== void 0 && _BSGlobal$tenantInfo.isNewIndex ? "titaui-okr-cases-library" : "okr-cases-library";

function OKRCases(props) {
  var onApplyTpl = props.onApplyTpl,
      onClose = props.onClose;

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      isUnlock = _useState2[0],
      setIsUnlock = _useState2[1]; // 是否解锁


  var _useState3 = (0, _react.useState)([]),
      _useState4 = _slicedToArray(_useState3, 2),
      navList = _useState4[0],
      setNavList = _useState4[1]; // nav列表


  var _useState5 = (0, _react.useState)([]),
      _useState6 = _slicedToArray(_useState5, 2),
      cardList = _useState6[0],
      setCardList = _useState6[1]; // 案例列表


  var _useState7 = (0, _react.useState)(0),
      _useState8 = _slicedToArray(_useState7, 2),
      okrCasesNum = _useState8[0],
      setOrkCasesNum = _useState8[1]; // 案例总数


  var _useState9 = (0, _react.useState)(false),
      _useState10 = _slicedToArray(_useState9, 2),
      isfix = _useState10[0],
      setIsfix = _useState10[1]; // 首nav是否吸顶


  var _useState11 = (0, _react.useState)(false),
      _useState12 = _slicedToArray(_useState11, 2),
      isTouchBottom = _useState12[0],
      setIsTouchBottom = _useState12[1]; // 是否触底


  var _useState13 = (0, _react.useState)(false),
      _useState14 = _slicedToArray(_useState13, 2),
      isShowSubmitOkrWindow = _useState14[0],
      setIsShowSubmitOkrWindow = _useState14[1]; // 提交想要的okr窗口是否显示


  var _useState15 = (0, _react.useState)(false),
      _useState16 = _slicedToArray(_useState15, 2),
      isShowPayWindow = _useState16[0],
      setIsShowPayWindow = _useState16[1]; // 交钱弹窗是否显示


  var _useState17 = (0, _react.useState)([]),
      _useState18 = _slicedToArray(_useState17, 2),
      secondNavList = _useState18[0],
      setSecondNavList = _useState18[1]; // 二级菜单


  var _useState19 = (0, _react.useState)(0),
      _useState20 = _slicedToArray(_useState19, 2),
      activeNavId = _useState20[0],
      setActiveNavId = _useState20[1]; // 激活菜单id 0是全部


  var _useState21 = (0, _react.useState)(0),
      _useState22 = _slicedToArray(_useState21, 2),
      secondActiveNavId = _useState22[0],
      setSecondActiveNavId = _useState22[1]; // 二级菜单激活id
  // const [pageNumber, setPageNumber] = useState(1)//请求第几页


  var _useState23 = (0, _react.useState)(false),
      _useState24 = _slicedToArray(_useState23, 2),
      isLoading = _useState24[0],
      setIsLoading = _useState24[1]; // 是否正在加载
  // const [fixAsideRight, setFixAsideRight] = useState(24)


  var _useState25 = (0, _react.useState)(false),
      _useState26 = _slicedToArray(_useState25, 2),
      isNextPageEmpty = _useState26[0],
      setIsNextPageEmpty = _useState26[1]; // 下一页是否为空


  var _useState27 = (0, _react.useState)(""),
      _useState28 = _slicedToArray(_useState27, 2),
      searchValue = _useState28[0],
      setSearchValue = _useState28[1]; // 搜索框内容
  // const [fixAsideRightScroll, setFixAsideRightScroll] = useState(0)//距离右侧滚动距离


  var _useState29 = (0, _react.useState)(0),
      _useState30 = _slicedToArray(_useState29, 2),
      fixAsideTopScroll = _useState30[0],
      setFixAsideTopScroll = _useState30[1]; // 距离上侧滚动距离
  // const [isAsideFix, setIsAsideFix] = useState(false)//侧变栏是否fix


  var _useState31 = (0, _react.useState)(0),
      _useState32 = _slicedToArray(_useState31, 2),
      fixNavTopScroll = _useState32[0],
      setFixNavTopScroll = _useState32[1]; // 与数据无关的不要用state，改用ref
  // const isTouchBottomRef = useRef(false)//是否触底


  var pageNumberRef = (0, _react.useRef)(1); // 请求第几页

  var totalHeightRef = (0, _react.useRef)(); // 总高度

  var allViewAreaRef = (0, _react.useRef)(); // 视口高度

  var asideHeightRef = (0, _react.useRef)(); // 侧边栏高度
  // 处理提交想要okr类型的弹窗出现处理

  var handleShowSubmitOkrWindow = function handleShowSubmitOkrWindow(f) {
    setIsShowSubmitOkrWindow(f);
  }; // 付钱弹窗出现处理


  var handleShowPayWindow = function handleShowPayWindow(f) {
    setIsShowPayWindow(f);
  }; // 初始化数据
  // 一级菜单选全部，请求id为0


  (0, _react.useEffect)(function () {
    // 在一开始判断用户是否购买
    // @ts-ignore
    (0, _network.getIsHadBuyOKRcases)().then(function (res) {
      // @ts-ignore
      setIsUnlock(res.obj);
    }); // 获取1级菜单

    (0, _network.getLabels)().then(function (res) {
      setNavList([{
        id: 0,
        name: (0, _getLocale.getLocale)("Mod_all"),
        orderNumber: 0,
        subTotal: "",
        subLabel: []
      }].concat(_toConsumableArray(res)));
    }); // 获取第一页的案例

    (0, _network.postForCases)({
      pageNumber: 1,
      labelId: 0,
      subLabelId: 0,
      key: ""
    }).then(function (res) {
      setCardList(res);
    }); // 案例总数

    (0, _network.gettotalNumber)().then(function (res) {
      setOrkCasesNum(res);
    });
  }, []); // // 屏幕resize事件
  // const resizeChange = () => {
  //     if (window.innerWidth < 1366) {
  //         if (onClose) {
  //             setFixAsideRight(window.innerWidth - 1366 + 0)
  //         } else {
  //             setFixAsideRight(window.innerWidth - 1366 + 12)
  //         }
  //     } else {
  //         if (onClose) {
  //             if (fixAsideRight !== 0) {
  //                 setFixAsideRight(0)
  //             }
  //         } else {
  //             if (fixAsideRight !== 12) {
  //                 setFixAsideRight(12)
  //             }
  //         }
  //     }
  // }
  // useEffect(() => {
  //     window.addEventListener("resize", resizeChange)
  //     return () => {
  //         window.removeEventListener("resize", resizeChange);
  //     }
  // }, [fixAsideRight])
  // 一级菜单选择

  var handleNavItemClick1 = function handleNavItemClick1(num) {
    setActiveNavId(num);
    var work = navList[num]; // @ts-ignore

    window.titaTracker && window.titaTracker("action").record({
      actionName: work.name,
      actionGroup: "案例库-各行业",
      productName: (0, _getLocale.getLocale)("OKR_OKRex_Casebase")
    });
    setSearchValue(""); // 点击选择类型时重新请求数据

    (0, _network.postForCases)({
      pageNumber: 1,
      labelId: work.id,
      subLabelId: 0,
      key: ""
    }).then(function (res) {
      setCardList(res);
    });
    scrollToAnchor("NavAnchor");
  }; // 锚点跳转


  var scrollToAnchor = function scrollToAnchor(anchorName) {
    var anchorElement = document.getElementById(anchorName);

    if (anchorElement) {
      anchorElement.scrollIntoView();
    }
  }; // 一级菜单改变


  (0, _react.useEffect)(function () {
    pageNumberRef.current = 1;
    setIsTouchBottom(false);
    setIsNextPageEmpty(false); // 每当一级菜单改变，获取二级菜单

    navList.map(function (item) {
      if (item.orderNumber === activeNavId) {
        var navSecondList = item.subLabel;

        if (activeNavId === 0) {
          setSecondNavList(navSecondList);
        } else {
          setSecondNavList([{
            id: 0,
            // @ts-ignore
            name: "\u5168\u90E8 ".concat(item.subTotal == 0 ? "" : item.subTotal),
            orderNumber: 0,
            subTotal: ""
          }].concat(_toConsumableArray(navSecondList)));
        }
      }
    });
    setSecondActiveNavId(0);
  }, [activeNavId]); // 二级菜单选择

  var handleSecondNavItemClick = function handleSecondNavItemClick(num) {
    if (isUnlock) {
      var work = navList[activeNavId];
      setIsTouchBottom(false);
      pageNumberRef.current = 1;
      setIsNextPageEmpty(false);
      setSearchValue("");
      setSecondActiveNavId(num);
      (0, _network.postForCases)({
        pageNumber: 1,
        labelId: work.id,
        subLabelId: num,
        key: ""
      }).then(function (res) {
        setCardList(res);
      });
    } else {
      setIsShowPayWindow(true);
    }
  }; // 搜索


  var handleSearch = function handleSearch(value) {
    setActiveNavId(0);
    pageNumberRef.current = 1;
    setIsNextPageEmpty(false);
    setSearchValue(value);
    (0, _network.postSearchCases)(value).then(function (res) {
      setCardList(res.Data);
    });
  }; // 划到底时加载新的 --懒加载


  (0, _react.useEffect)(function () {
    if (isTouchBottom) {
      pageNumberRef.current += 1;

      if (!isUnlock && pageNumberRef.current > 2 || isNextPageEmpty) {
        return;
      }

      setIsLoading(true);

      if (searchValue) {
        (0, _network.postSearchCases)(searchValue, pageNumberRef.current).then(function (res) {
          if (res.Data.length) {
            setCardList([].concat(_toConsumableArray(cardList), _toConsumableArray(res.Data)));
          } else {
            setIsNextPageEmpty(true);
          }

          setIsLoading(false);
          setIsTouchBottom(false);
        });
      } else {
        var work = navList[activeNavId];
        (0, _network.postForCases)({
          labelId: work.id,
          subLabelId: secondActiveNavId,
          key: "",
          pageNumber: pageNumberRef.current
        }).then(function (res) {
          if (res.length) {
            setCardList([].concat(_toConsumableArray(cardList), _toConsumableArray(res)));
          } else {
            setIsNextPageEmpty(true);
          }

          setIsLoading(false);
          setIsTouchBottom(false);
        });
      }
    }
  }, [isTouchBottom, searchValue]); // 下划事件，定位和懒加载

  var handleScroll = function handleScroll(e) {
    var scrollHeight = e.target.scrollTop;

    if (scrollHeight > 239 && e.target.tagName !== "TEXTAREA") {
      // 顶部吸顶
      !isfix && setIsfix(true);
      var NavTopScroll = scrollHeight;
      setFixNavTopScroll(NavTopScroll); // 文章吸底

      var AsideTopScroll = activeNavId === 0 ? scrollHeight - 240 + allViewAreaRef.current.clientHeight - asideHeightRef.current.clientHeight - 89 : scrollHeight - 240 + allViewAreaRef.current.clientHeight - asideHeightRef.current.clientHeight - 131;
      setFixAsideTopScroll(AsideTopScroll);
    } else {
      isfix && setIsfix(false);
    } // //当选择全部时，距离顶部少了42
    // const offTopDiff = activeNavId === 0 ? 0 : 42
    // //判断吸底
    // if (allViewAreaRef.current.clientHeight - asideHeightRef.current.clientHeight + scrollHeight > 324 + offTopDiff) {
    //     !isAsideFix && setIsAsideFix(true)
    //     const AsideTopScroll = scrollHeight - 240 + allViewAreaRef.current.clientHeight - asideHeightRef.current.clientHeight - 79 - offTopDiff
    //     setFixAsideTopScroll(AsideTopScroll)
    // } else {
    //     isAsideFix && setIsAsideFix(false)
    // }
    // 懒加载


    if (totalHeightRef.current.clientHeight <= allViewAreaRef.current.clientHeight + scrollHeight + 200) {
      setIsTouchBottom(true);
    }
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "okrCases-container",
    ref: allViewAreaRef,
    id: "123",
    style: {
      height: "calc(100vh - 40px)"
    },
    onScroll: function onScroll(e) {
      return handleScroll(e);
    }
  }, /*#__PURE__*/_react["default"].createElement(_reactPerfectScrollbar["default"], null, /*#__PURE__*/_react["default"].createElement("div", {
    className: prefix,
    ref: totalHeightRef,
    style: {
      minWidth: onClose ? "1366px" : "1163px"
    }
  }, /*#__PURE__*/_react["default"].createElement(_caseLibrayHeader["default"], {
    onClose: onClose,
    handleSearch: handleSearch
  }), /*#__PURE__*/_react["default"].createElement(_caseLibraryNav["default"], {
    onClose: onClose,
    navList: navList,
    isfix: isfix,
    handleNavItemClick: handleNavItemClick1,
    activeNavId: activeNavId,
    handleSearch: handleSearch,
    secondActiveNavId: secondActiveNavId,
    secondNavList: secondNavList,
    handleSecondNavItemClick: handleSecondNavItemClick,
    cardList: cardList,
    fixNavTopScroll: fixNavTopScroll
  }), /*#__PURE__*/_react["default"].createElement(_caseLibraryMain["default"], {
    cardList: cardList,
    isUnlock: isUnlock,
    onApplyTpl: onApplyTpl,
    okrCasesNum: okrCasesNum,
    handleShowSubmitOkrWindow: handleShowSubmitOkrWindow,
    handleShowPayWindow: handleShowPayWindow,
    isfix: isfix,
    isLoading: isLoading,
    activeNavId: activeNavId,
    onClose: onClose,
    fixAsideTopScroll: fixAsideTopScroll,
    asideHeightRef: asideHeightRef // isAsideFix={isAsideFix}

  }), /*#__PURE__*/_react["default"].createElement(_submitOkrDialog.SubmitDialog, {
    isShowSubmitOkrWindow: isShowSubmitOkrWindow,
    handleShowSubmitOkrWindow: handleShowSubmitOkrWindow,
    allViewAreaRef: allViewAreaRef
  }), /*#__PURE__*/_react["default"].createElement(_submitOkrDialog.PayDialog, {
    isShowPayWindow: isShowPayWindow,
    handleShowPayWindow: handleShowPayWindow,
    allViewAreaRef: allViewAreaRef
  }))));
}

var _default = OKRCases;
exports["default"] = _default;