"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = _interopRequireWildcard(require("react"));
var _indexModule = _interopRequireDefault(require("./index.module.scss"));
var _base = require("../../api/base");
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 GroupBooking = function GroupBooking(_ref) {
  var ideaId = _ref.ideaId,
    endDate = _ref.endDate,
    handlerBuy = _ref.handlerBuy;
  var DEFAULT_IMG_URL = '//img.maimairen.com/low-code/components/shops/shop1.jpg';
  var ADD_ICON = '//img.maimairen.com/low-code/components/groupBooking/add_icon.png';

  // 无ideaId传入只会存在编辑器环境，此时展示模拟数据
  var initGoodsData = ideaId ? {} : {
    ideaProductData: {
      teamMaxNum: 5,
      teamPrice: 88.88,
      teamStatus: 'WAIT'
    },
    productList: [{
      productId: 0,
      title: "商品标题",
      mainImg: DEFAULT_IMG_URL,
      specs: "1份",
      salesWord: "商品描述",
      sellingPrice: 99.99,
      marketPrice: 200
    }],
    buyerList: []
  };
  var initCountDown = {
    hour: '23',
    minute: '59',
    second: '59'
  };
  var _useState = (0, _react.useState)(initGoodsData),
    goodsData = _useState[0],
    setGoodsData = _useState[1];
  var _useState2 = (0, _react.useState)(initCountDown),
    countDown = _useState2[0],
    setCountDown = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    ftFix = _useState3[0],
    setFtFix = _useState3[1];
  var _useState4 = (0, _react.useState)(0),
    bottom = _useState4[0],
    setBottom = _useState4[1];

  // 计时器
  var timer = null;
  (0, _react.useEffect)(function () {
    var pathname = window.location.pathname;
    // 预览页时底部购买按钮区固定页面底部
    if (pathname === '/preview.html' || pathname === '/preview2.html') {
      if (pathname === '/preview2.html') {
        document.querySelector('.lowcode-plugin-sample-preview').setAttribute('padding-bottom', '113px');
        setBottom(78);
      } else {
        setBottom(0);
      }
      document.body.setAttribute('padding-bottom', '113px');
      setFtFix(true);
    }
    if (ideaId) {
      fetchData();
    }
    if (endDate && +endDate > 0) {
      isCountDown();
    }
  }, [ideaId, endDate]);
  var getDefaultUserList = function getDefaultUserList(data) {
    var defaultNum = data.ideaProductData.teamMaxNum - data.buyerList.length;
    var resultArr = [];
    for (var i = 0; i < defaultNum; i++) {
      resultArr.push(i);
    }
    return resultArr;
  };

  // 获取拼团信息
  var fetchData = /*#__PURE__*/function () {
    var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var _yield$getGroupBookin, code, data;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return (0, _base.getGroupBookingData)({
              ideaId: ideaId
            });
          case 2:
            _yield$getGroupBookin = _context.sent;
            code = _yield$getGroupBookin.code;
            data = _yield$getGroupBookin.data;
            if (code === 10200 && data) {
              setGoodsData(data);
            }
          case 6:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    return function fetchData() {
      return _ref2.apply(this, arguments);
    };
  }();
  var timeFormatter = function timeFormatter(time) {
    if (!time) {
      setCountDown({
        hour: '00',
        minute: '00',
        second: '00'
      });
      return false;
    }
    ;
    var t = time;
    var ss = t % 60;
    t = (t - ss) / 60;
    if (t < 1) {
      setCountDown({
        hour: '00',
        minute: '00',
        second: ss < 10 ? "0" + ss : "" + ss
      });
      return false;
    }
    var mm = t % 60;
    t = (t - mm) / 60;
    if (t < 1) {
      setCountDown({
        hour: '00',
        minute: mm < 10 ? "0" + mm : "" + mm,
        second: ss < 10 ? "0" + ss : "" + ss
      });
      return false;
    }
    ;
    var hh = t % 24;
    t = (t - hh) / 24;
    if (t < 1) {
      setCountDown({
        hour: hh < 10 ? "0" + hh : "" + hh,
        minute: mm < 10 ? "0" + mm : "" + mm,
        second: ss < 10 ? "0" + ss : "" + ss
      });
      return false;
    } else {
      var _h = hh + t * 24;
      setCountDown({
        hour: _h < 10 ? "0" + _h : "" + _h,
        minute: mm < 10 ? "0" + mm : "" + mm,
        second: ss < 10 ? "0" + ss : "" + ss
      });
      return false;
    }
  };

  // 倒计时
  var isCountDown = function isCountDown() {
    timer && clearTimeout(timer);
    // 当前时间戳
    var curTime = new Date().getTime();
    var durationTime = Math.floor((+endDate * 1000 - curTime) / 1000);
    timeFormatter(durationTime);
    if (+endDate * 1000 > curTime) {
      timer = setTimeout(function () {
        isCountDown();
      }, 1000);
    } else {
      timer && clearTimeout(timer);
    }
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].group_booking_wrap
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].goods_info
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].goods_image_box
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].goods_image,
    style: {
      background: "url(" + goodsData.productList[0].mainImg + ") no-repeat center/contain"
    }
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].goods_title
  }, goodsData.productList[0].title), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].goods_desc
  }, goodsData.productList[0].salesWord), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].price
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].sale_price
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].sale_price_unit
  }, "\uFFE5"), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].sale_price_num
  }, goodsData.productList[0].sellingPrice), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].sale_price_unit
  }, "/", goodsData.productList[0].specs)), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].line_price
  }, "\uFFE5", goodsData.productList[0].marketPrice))), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].group_infos
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].group_head
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].head_icon
  }), /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].head_text
  }, "\u53D1\u8D77\u591A\u4EBA\u56E2"), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].arrow_icon
  }), /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].head_text
  }, "\u62FC\u6EE1\u4EBA\u6570"), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].arrow_icon
  }), /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].head_text
  }, "\u6210\u56E2\u53D1\u8D27")), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].group_content
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].count_down
  }, /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].count_down_title
  }, "\u8DDD\u79BB\u7ED3\u675F\u8FD8\u5269"), /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].count_down_num
  }, countDown.hour, ":", countDown.minute, ":", countDown.second)), /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].count_desc
  }, "\u8FD8\u5269", /*#__PURE__*/_react["default"].createElement("span", {
    className: _indexModule["default"].count_num
  }, goodsData.ideaProductData.teamMaxNum - goodsData.buyerList.length), "\u4E2A\u4F4D\u7F6E"), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].image_list
  }, initGoodsData.buyerList.map(function (item) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: _indexModule["default"].image_item + " " + _indexModule["default"].active,
      style: {
        background: "#f3f3f3 url(" + item.buyerAvatar + ")no-repeat center/contain"
      }
    });
  }), getDefaultUserList(goodsData).map(function () {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: _indexModule["default"].image_item,
      style: {
        background: "#f3f3f3 url(" + ADD_ICON + ")no-repeat center/20px"
      }
    });
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].group_fix_ft + " " + (ftFix && _indexModule["default"].fixed),
    style: {
      'bottom': bottom + "px"
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].ft_btns
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].bag_btn
  }, /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].bag_num
  }, goodsData.productList.length), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].bag_icon + " xiatu-rich xt-bag"
  }), /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].bag_text
  }, "\u8D2D\u7269\u888B")), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].buy_btn
  }, /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].buy_num
  }, "\uFFE5", goodsData.productList[0].sellingPrice), /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].buy_text,
    onClick: function onClick() {
      return handlerBuy('buy');
    }
  }, "\u76F4\u63A5\u8D2D\u4E70")), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].group_btn
  }, /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].group_num
  }, "\uFFE5", goodsData.ideaProductData.teamPrice), /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].group_text,
    onClick: function onClick() {
      return handlerBuy('group');
    }
  }, "\u62FC\u56E2"), /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].group_tips
  }, "\u66F4\u5212\u7B97"))), /*#__PURE__*/_react["default"].createElement("div", {
    className: _indexModule["default"].ft_tips
  }, /*#__PURE__*/_react["default"].createElement("p", {
    className: _indexModule["default"].tip_text
  }, "\u4E0D\u6210\u56E2\u81EA\u52A8\u9000\u6B3E")))));
};
var _default = GroupBooking;
exports["default"] = _default;