"use strict";

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

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

var _pick = _interopRequireDefault(require("lodash/pick"));

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

var _currencyUtil = require("../../utils/currencyUtil");

var _dateUtil = require("../../utils/dateUtil");

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

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

var _CouponListModal = _interopRequireDefault(require("../components/CouponListModal"));

var _MPropertiesBaseComponent = _interopRequireDefault(require("../MPropertiesBaseComponent"));

require("./index.less");

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

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(arguments[i]) : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var MCouponListProperties = /*#__PURE__*/function (_MPropertiesBaseCompo) {
  _inherits(MCouponListProperties, _MPropertiesBaseCompo);

  var _super = _createSuper(MCouponListProperties);

  function MCouponListProperties(_props) {
    var _this;

    _classCallCheck(this, MCouponListProperties);

    _this = _super.call(this, _props);

    _defineProperty(_assertThisInitialized(_this), "onSave", function () {
      var evt = new Event('property-update', {
        bubbles: false
      });
      evt.data = _objectSpread({}, _this.props, _this.state);
      window.document.dispatchEvent(evt);
    });

    _defineProperty(_assertThisInitialized(_this), "resetProps", function (props) {
      _this.setState(_objectSpread({}, props));
    });

    _defineProperty(_assertThisInitialized(_this), "addCoupon", function () {
      _this.setState({
        visible: true
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onOk", function (coupons) {
      var couponList = coupons.map(function (coupon) {
        return (0, _pick["default"])(coupon, ['couponId', 'couponName', 'discountAmount', 'thAmount', 'discountMethod', 'getEndDate', 'getStartDate']);
      });

      _this.setState({
        visible: false,
        coupons: couponList
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onCancel", function () {
      _this.setState({
        visible: false
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onCheckChange", function (e) {
      _this.setState({
        hideUncollectible: e.target.checked
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onDelCoupon", function (_ref) {
      var couponId = _ref.couponId;

      var filterCoupons = _this.state.coupons.filter(function (cop) {
        return cop.couponId !== couponId;
      });

      _this.setState({
        coupons: filterCoupons
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getCouponCard", function (coupon) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "couponNode"
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "couponPrice"
      }, (0, _currencyUtil.getCouponDiscountDesc)(coupon)), /*#__PURE__*/_react["default"].createElement("div", {
        className: "couponName"
      }, coupon.couponName), /*#__PURE__*/_react["default"].createElement("div", {
        className: "collectTime"
      }, "Collection Time"), /*#__PURE__*/_react["default"].createElement("div", {
        className: "getStartTime"
      }, (0, _dateUtil.formatDateTime)(coupon.getStartDate)), /*#__PURE__*/_react["default"].createElement("div", {
        className: "getStartTime"
      }, (0, _dateUtil.formatDateTime)(coupon.getEndDate)), /*#__PURE__*/_react["default"].createElement("div", {
        className: "closeBtn",
        onClick: function onClick() {
          return _this.onDelCoupon(coupon);
        }
      }, "x"));
    });

    _this.state = _objectSpread({
      style: {}
    }, _props, {
      hideUncollectible: _props.hideUncollectible,
      coupons: _props.coupons || [],
      visible: false
    });
    return _this;
  }

  _createClass(MCouponListProperties, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$state = this.state,
          visible = _this$state.visible,
          coupons = _this$state.coupons,
          hideUncollectible = _this$state.hideUncollectible;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "couponList"
      }, /*#__PURE__*/_react["default"].createElement(_index2["default"], {
        data: this.state.style,
        onChange: function onChange(data) {
          _this2.state.style = data;
        }
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: "contentWrap"
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "label"
      }, "Select Coupon"), /*#__PURE__*/_react["default"].createElement("div", {
        className: "couponWrap"
      }, coupons.map(function (coupon) {
        return _this2.getCouponCard(coupon);
      }), /*#__PURE__*/_react["default"].createElement(_fdx.Button, {
        onClick: this.addCoupon,
        className: "addBtn"
      }, "Add Coupon")), /*#__PURE__*/_react["default"].createElement("div", {
        className: "checkWrap"
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Checkbox, {
        checked: hideUncollectible,
        onChange: this.onCheckChange
      }, "Hide uncollectible coupons")), visible && /*#__PURE__*/_react["default"].createElement(_CouponListModal["default"], {
        visible: visible,
        coupons: coupons,
        onCancel: this.onCancel,
        onOk: this.onOk
      })), /*#__PURE__*/_react["default"].createElement(_index["default"], {
        onSaveClick: this.onSave
      }));
    }
  }]);

  return MCouponListProperties;
}(_MPropertiesBaseComponent["default"]);

exports["default"] = MCouponListProperties;