"use strict";

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

var _fdx = require("@whalecloud/fdx");

var _fishx = require("fishx");

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

var _Coupons = _interopRequireDefault(require("../Coupons"));

var _index = _interopRequireDefault(require("../FilteredProductModal/index"));

var _index2 = _interopRequireDefault(require("../ProductDetailPage/index"));

var _BundleCampaign = _interopRequireDefault(require("../BundleCampaign"));

require("./index.less");

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 _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; }

var RadioGroup = _fdx.Radio.Group;

var LinkModal = function LinkModal(props) {
  var handleModalSave = props.handleModalSave,
      closeModal = props.closeModal,
      visible = props.visible,
      renderer = props.renderer;

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      loading = _useState2[0],
      setLoading = _useState2[1];

  var _useState3 = (0, _react.useState)([]),
      _useState4 = _slicedToArray(_useState3, 2),
      catgList = _useState4[0],
      setCatgList = _useState4[1];

  var _useState5 = (0, _react.useState)([]),
      _useState6 = _slicedToArray(_useState5, 2),
      cusPageList = _useState6[0],
      setCusPageList = _useState6[1];

  var _useState7 = (0, _react.useState)([]),
      _useState8 = _slicedToArray(_useState7, 2),
      functions = _useState8[0],
      setFunctions = _useState8[1]; // const [selectedCatg, setSelectedCatg] = useState('');


  var _useState9 = (0, _react.useState)(''),
      _useState10 = _slicedToArray(_useState9, 2),
      linkUrl = _useState10[0],
      setLinkUrl = _useState10[1];

  var _useState11 = (0, _react.useState)('Category'),
      _useState12 = _slicedToArray(_useState11, 2),
      selected = _useState12[0],
      setSelected = _useState12[1];

  var list = ['Category', 'Customized Page', 'Product Details Page', 'Filtered Products', 'Function Page', 'Coupons', 'Bundle Campaign'];

  var _useState13 = (0, _react.useState)(0),
      _useState14 = _slicedToArray(_useState13, 2),
      total = _useState14[0],
      setTotal = _useState14[1]; // const queryCategory = useFetch(params => ({ url: `${window.MParams.MBaseUrl}/offerdiscatgs/tree?terminalType=${renderer}`, method: 'get' }), {


  var queryCategory = (0, _fishx.useFetch)(function (params) {
    return {
      url: "".concat(window.MParams.MBaseUrl, "/offercatgs/list"),
      method: 'get'
    };
  }, {
    manual: true,
    onSuccess: function onSuccess(res) {
      console.log(listToTree(res));
      setCatgList(listToTree(res)); // setCatgList(res);

      setLoading(false);
    }
  });

  var listToTree = function listToTree(list) {
    var map = {};
    var node;
    var tree = [];
    var i;
    var nList = [];

    for (i = 0; i < list.length; i++) {
      map[list[i].catgId] = list[i];
      list[i].children = list[i].isLeaf == 0 ? null : [];
    }

    for (i = 0; i < list.length; i += 1) {
      node = {
        catgName: list[i].catgName,
        value: list[i].catgId,
        key: list[i].catgId,
        children: list[i].children
      };

      if (list[i].parentCatgId !== null) {
        map[list[i].parentCatgId] && map[list[i].parentCatgId].children.push(node);
      } else {
        tree.push(node);
      }
    }

    return tree;
  };

  var queryCus = (0, _fishx.useFetch)(function (params) {
    return {
      url: "".concat(window.MParams.MBaseUrl, "/pages"),
      method: 'get',
      params: params
    };
  }, {
    manual: true,
    onSuccess: function onSuccess(res) {
      setCusPageList(res);
      setLoading(false);
    }
  });

  var _useFetch = (0, _fishx.useFetch)(function () {
    return {
      url: "".concat(window.MParams.MBaseUrl, "/pages/functions"),
      method: 'get'
    };
  }, {
    manual: true,
    onSuccess: function onSuccess(res) {
      setFunctions(res);
    }
  }),
      qryFunctions = _useFetch.run;

  (0, _react.useEffect)(function () {
    setTotal(0);
  }, [visible]);
  (0, _react.useEffect)(function () {
    if (selected === 'Category') {
      setLoading(true);
      queryCategory.run();
    } else if (selected === 'Customized Page') {
      setLoading(true);
      queryCus.run({
        pageType: 'Customized',
        verType: 'Formal'
      });
    } else if (selected === 'Function Page') {
      setLoading(true);
      qryFunctions();
    }
  }, [selected]);
  var rowSelection = {
    onChange: function onChange(selectedRowKeys) {
      if (selected === 'Category') {
        // setSelectedCatg(selectedRowKeys[0]);
        setLinkUrl("catg-".concat(selectedRowKeys[0]));
      } else if (selected === 'Customized Page') {
        setLinkUrl("customized-".concat(selectedRowKeys[0]));
      }
    },
    getCheckboxProps: function getCheckboxProps(record) {// if (selected === 'Category') {
      //   return {
      //     disabled: record.children !== null,
      //   };
      // }
    },
    type: 'radio'
  };
  var columns = [{
    title: 'Category',
    dataIndex: 'catgName',
    render: function render(catgName) {
      console.log(catgName, 778);
      return /*#__PURE__*/_react["default"].createElement("span", {
        className: "productName"
      }, catgName);
    }
  }];
  var columnsCustomized = [{
    title: 'page Name',
    dataIndex: 'pageName',
    render: function render(pageName) {
      return /*#__PURE__*/_react["default"].createElement("span", {
        className: "productName"
      }, pageName);
    }
  }];

  var onModalSava = function onModalSava() {
    if (handleModalSave) {
      handleModalSave(linkUrl);
    }

    if (closeModal) {
      closeModal();
    }
  };

  return /*#__PURE__*/_react["default"].createElement(_fdx.Modal, {
    title: "URL Setup",
    okText: "OK",
    cancelText: "Cancel",
    width: "1000",
    wrapClassName: "linkModal",
    visible: visible,
    onOk: handleModalSave,
    draggable: false,
    onCancel: closeModal,
    footer: null
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "modalWrap"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "linkType"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "typeTitle"
  }, "Link Type:"), list.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      onClick: function onClick() {
        return setSelected(item);
      },
      className: "typeItem ".concat(selected === item ? 'selected' : ''),
      key: index
    }, item);
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "content"
  }, selected === 'Category' && /*#__PURE__*/_react["default"].createElement(_fdx.Table, {
    scroll: {
      y: 340
    },
    defaultExpandAllRows: true,
    loading: loading,
    bordered: false,
    rowKey: function rowKey(record) {
      return record && record.key;
    },
    rowSelection: rowSelection,
    columns: columns,
    dataSource: catgList
  }), selected === 'Customized Page' && /*#__PURE__*/_react["default"].createElement(_fdx.Table, {
    scroll: {
      y: 340
    },
    defaultExpandAllRows: true,
    loading: loading,
    bordered: false,
    rowKey: function rowKey(record) {
      return record.bdPageId;
    },
    rowSelection: rowSelection,
    columns: columnsCustomized,
    dataSource: cusPageList
  }), selected === 'Filtered Products' && /*#__PURE__*/_react["default"].createElement(_index["default"], {
    onDataChange: function onDataChange(url, t) {
      console.log(t, 33);
      setLinkUrl(url);
      setTotal(t);
    }
  }), selected === 'Product Details Page' && /*#__PURE__*/_react["default"].createElement(_index2["default"], {
    setUrl: function setUrl(url) {
      return setLinkUrl(url);
    }
  }), selected === 'Function Page' && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "cotTitle"
  }, "Page Name"), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(RadioGroup, {
    style: {
      width: '100%'
    },
    onChange: function onChange(e) {
      setLinkUrl(e.target.value);
    }
  }, functions.length > 0 && functions.map(function (func) {
    return /*#__PURE__*/_react["default"].createElement(_fdx.Radio, {
      className: "cotItem",
      value: func.url
    }, /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        paddingLeft: 30,
        display: 'inline-block'
      }
    }, func.urlName));
  })))), selected === 'Coupons' && /*#__PURE__*/_react["default"].createElement(_Coupons["default"], {
    onChange: function onChange(url) {
      setLinkUrl(url);
    }
  }), selected === 'Bundle Campaign' && /*#__PURE__*/_react["default"].createElement(_BundleCampaign["default"], {
    setUrl: function setUrl(url) {
      return setLinkUrl(url);
    }
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "footer"
  }, selected === 'Filtered Products' ? /*#__PURE__*/_react["default"].createElement("div", {
    className: "total"
  }, total, " Products In Total") : /*#__PURE__*/_react["default"].createElement("div", null), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_fdx.Button, {
    onClick: onModalSava,
    type: "primary",
    style: {
      marginRight: 10
    }
  }, "OK"), /*#__PURE__*/_react["default"].createElement(_fdx.Button, {
    onClick: function onClick() {
      return closeModal && closeModal();
    }
  }, "Cancel")))));
};

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