"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 _antd = require("antd");

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

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

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

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

var _businessUtil = require("../../utils/businessUtil");

var _commonUtil = require("../../utils/commonUtil");

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

var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function (resolve) {
      resolve(value);
    });
  }

  return new (P || (P = Promise))(function (resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }

    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }

    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }

    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};

var __generator = void 0 && (void 0).__generator || function (thisArg, body) {
  var _ = {
    label: 0,
    sent: function sent() {
      if (t[0] & 1) throw t[1];
      return t[1];
    },
    trys: [],
    ops: []
  },
      f,
      y,
      t,
      g;
  return g = {
    next: verb(0),
    "throw": verb(1),
    "return": verb(2)
  }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
    return this;
  }), g;

  function verb(n) {
    return function (v) {
      return step([n, v]);
    };
  }

  function step(op) {
    if (f) throw new TypeError("Generator is already executing.");

    while (_) {
      try {
        if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
        if (y = 0, t) op = [op[0] & 2, t.value];

        switch (op[0]) {
          case 0:
          case 1:
            t = op;
            break;

          case 4:
            _.label++;
            return {
              value: op[1],
              done: false
            };

          case 5:
            _.label++;
            y = op[1];
            op = [0];
            continue;

          case 7:
            op = _.ops.pop();

            _.trys.pop();

            continue;

          default:
            if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
              _ = 0;
              continue;
            }

            if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
              _.label = op[1];
              break;
            }

            if (op[0] === 6 && _.label < t[1]) {
              _.label = t[1];
              t = op;
              break;
            }

            if (t && _.label < t[2]) {
              _.label = t[2];

              _.ops.push(op);

              break;
            }

            if (t[2]) _.ops.pop();

            _.trys.pop();

            continue;
        }

        op = body.call(thisArg, _);
      } catch (e) {
        op = [6, e];
        y = 0;
      } finally {
        f = t = 0;
      }
    }

    if (op[0] & 5) throw op[1];
    return {
      value: op[0] ? op[1] : void 0,
      done: true
    };
  }
};

var RadioGroup = _antd.Radio.Group;

var LinkModal = function LinkModal(props) {
  var onConfirm = props.onConfirm,
      onClose = props.onClose,
      visible = props.visible;

  var _a = (0, _react.useState)(false),
      loading = _a[0],
      setLoading = _a[1];

  var _b = (0, _react.useState)([]),
      catgList = _b[0],
      setCatgList = _b[1];

  var _c = (0, _react.useState)([]),
      cusPageList = _c[0],
      setCusPageList = _c[1];

  var _d = (0, _react.useState)([]),
      functions = _d[0],
      setFunctions = _d[1];

  var _e = (0, _react.useState)(''),
      linkUrl = _e[0],
      setLinkUrl = _e[1];

  var _f = (0, _react.useState)('Category'),
      selected = _f[0],
      setSelected = _f[1];

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

  var _g = (0, _react.useState)(0),
      total = _g[0],
      setTotal = _g[1];

  var queryCategory = function queryCategory() {
    return __awaiter(void 0, void 0, void 0, function () {
      var res;
      return __generator(this, function (_a) {
        switch (_a.label) {
          case 0:
            return [4
            /*yield*/
            , (0, _commonUtil.commonFetch)(window.magicDesign.MBaseUrl + "/offercatgs/list", {}, 'GET')];

          case 1:
            res = _a.sent();
            setCatgList((0, _businessUtil.listToTree)(res));
            setLoading(false);
            return [2
            /*return*/
            ];
        }
      });
    });
  };

  var queryCus = function queryCus(params) {
    return __awaiter(void 0, void 0, void 0, function () {
      var res;
      return __generator(this, function (_a) {
        switch (_a.label) {
          case 0:
            return [4
            /*yield*/
            , (0, _commonUtil.commonFetch)(window.magicDesign.MBaseUrl + "/pages", params, 'GET')];

          case 1:
            res = _a.sent();
            setCusPageList(res);
            setLoading(false);
            return [2
            /*return*/
            ];
        }
      });
    });
  };

  var qryFunctions = function qryFunctions() {
    return __awaiter(void 0, void 0, void 0, function () {
      var res;
      return __generator(this, function (_a) {
        switch (_a.label) {
          case 0:
            return [4
            /*yield*/
            , (0, _commonUtil.commonFetch)(window.magicDesign.MBaseUrl + "/pages/functions", {}, 'GET')];

          case 1:
            res = _a.sent();
            setFunctions(res);
            return [2
            /*return*/
            ];
        }
      });
    });
  };

  (0, _react.useEffect)(function () {
    setTotal(0);
  }, [visible]);
  (0, _react.useEffect)(function () {
    if (selected === 'Category') {
      setLoading(true);
      queryCategory();
    } else if (selected === 'Customized Page') {
      setLoading(true);
      queryCus({
        pageType: 'Customized',
        verType: 'Formal'
      });
    } else if (selected === 'Function Page') {
      setLoading(true);
      qryFunctions();
    }
  }, [selected]);
  var rowSelection = {
    onChange: function onChange(selectedRowKeys) {
      if (selected === 'Category') {
        setLinkUrl("catg-" + selectedRowKeys[0]);
      } else if (selected === 'Customized Page') {
        setLinkUrl("customized-" + selectedRowKeys[0]);
      }
    },
    type: 'radio'
  };
  var columns = [{
    title: 'Category',
    dataIndex: 'catgName',
    render: function render(catgName) {
      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 (onConfirm) {
      onConfirm(linkUrl);
    }

    if (onClose) {
      onClose();
    }
  };

  return /*#__PURE__*/_react["default"].createElement(_antd.Modal, {
    cancelText: "Cancel",
    draggable: false,
    footer: null,
    okText: "OK",
    onCancel: onClose,
    onOk: onConfirm,
    title: "URL Setup",
    visible: visible,
    width: "1000",
    wrapClassName: "linkModal"
  }, /*#__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", {
      key: index,
      className: "typeItem " + (selected === item ? 'selected' : ''),
      onClick: function onClick() {
        return setSelected(item);
      }
    }, item);
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "content"
  }, selected === 'Category' && /*#__PURE__*/_react["default"].createElement(_antd.Table, {
    bordered: false,
    columns: columns,
    dataSource: catgList,
    defaultExpandAllRows: true,
    loading: loading,
    rowKey: function rowKey(record) {
      return record && record.key;
    },
    rowSelection: rowSelection,
    scroll: {
      y: 340
    }
  }), selected === 'Customized Page' && /*#__PURE__*/_react["default"].createElement(_antd.Table, {
    bordered: false,
    columns: columnsCustomized,
    dataSource: cusPageList,
    defaultExpandAllRows: true,
    loading: loading,
    rowKey: function rowKey(record) {
      return record.bdPageId;
    },
    rowSelection: rowSelection,
    scroll: {
      y: 340
    }
  }), selected === 'Product Details Page' && /*#__PURE__*/_react["default"].createElement(_index2["default"], {
    setUrl: function setUrl(url) {
      return setLinkUrl(url);
    }
  }), selected === 'Filtered Products' && /*#__PURE__*/_react["default"].createElement(_index["default"], {
    onDataChange: function onDataChange(url, t) {
      setLinkUrl(url);
      setTotal(t);
    }
  }), 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, {
    onChange: function onChange(e) {
      setLinkUrl(e.target.value);
    },
    style: {
      width: '100%'
    }
  }, functions.length > 0 && functions.map(function (func) {
    return /*#__PURE__*/_react["default"].createElement(_antd.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);
    }
  })), /*#__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(_antd.Button, {
    className: "btn-cancel",
    onClick: function onClick() {
      return onClose && onClose();
    }
  }, "Cancel"), /*#__PURE__*/_react["default"].createElement(_antd.Button, {
    className: "btn-confirm",
    onClick: onModalSava,
    type: "primary"
  }, "OK")))));
};

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