"use strict";

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

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

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

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

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

require("./index.less");

var _Upload = _interopRequireDefault(require("./Upload"));

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

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 _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 _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 _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 _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.push.apply(ownKeys, Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

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); Object.defineProperty(Constructor, "prototype", { writable: false }); 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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); 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; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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 MZeroSaleProperties = /*#__PURE__*/function (_MPropertiesBaseCompo) {
  _inherits(MZeroSaleProperties, _MPropertiesBaseCompo);

  var _super = _createSuper(MZeroSaleProperties);

  function MZeroSaleProperties(_props) {
    var _this;

    _classCallCheck(this, MZeroSaleProperties);

    _this = _super.call(this, _props);

    _defineProperty(_assertThisInitialized(_this), "campaignList", []);

    _defineProperty(_assertThisInitialized(_this), "getInit", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return (0, _commonUtil.fishFetch)("".concat(window.MParams.MBaseUrl, "/h5/winner/specials/list"));

            case 2:
              _this.campaignList = _context.sent;

              _this.setState({});

            case 4:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));

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

    _defineProperty(_assertThisInitialized(_this), "addNewItem", function () {
      _this.setState({
        imgList: [].concat(_toConsumableArray(_this.state.imgList), [{}])
      });
    });

    _defineProperty(_assertThisInitialized(_this), "uploadImgUrl", function (index, url) {
      var arr = JSON.parse(JSON.stringify(_this.state.imgList));
      arr[index].imgUrl = url;

      _this.setState({
        imgList: arr
      });
    });

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

    _defineProperty(_assertThisInitialized(_this), "setImgLink", function (index, value) {
      var arr = JSON.parse(JSON.stringify(_this.state.imgList));
      arr[index].link = value;

      _this.setState({
        imgList: arr
      });
    });

    _this.state = _objectSpread({
      title: '',
      posterList: []
    }, _props);
    return _this;
  }

  _createClass(MZeroSaleProperties, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.getInit();
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$state = this.state,
          title = _this$state.title,
          posterList = _this$state.posterList;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "m-zero-sale-wrap"
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Form, {
        layout: "vertical"
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Form.Item, {
        label: "Title"
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Input, {
        value: title,
        onChange: function onChange(e) {
          return _this2.setState({
            title: e.target.value
          });
        },
        placeholder: "please input"
      })), /*#__PURE__*/_react["default"].createElement(_fdx.Form.Item, {
        label: "Poster"
      }, posterList.map(function (poster, index) {
        return /*#__PURE__*/_react["default"].createElement("div", {
          className: "poster-wrap",
          key: "p".concat(index)
        }, /*#__PURE__*/_react["default"].createElement("div", {
          className: "title-wrap"
        }, /*#__PURE__*/_react["default"].createElement("span", null, "Poster", index + 1), /*#__PURE__*/_react["default"].createElement(_fdx.Icon, {
          className: "del-icon",
          onClick: function onClick() {
            posterList.splice(index, 1);

            _this2.setState({});
          },
          type: "delete"
        })), /*#__PURE__*/_react["default"].createElement(_Upload["default"], {
          value: poster.postImage,
          onChange: function onChange(v) {
            poster.postImage = v;

            _this2.setState({});
          }
        }), /*#__PURE__*/_react["default"].createElement(_fdx.Select, {
          placeholder: "Select URL",
          className: "mt-10",
          allowClear: true,
          style: {
            width: '100%'
          },
          onChange: function onChange(value) {
            poster.campaignId = value;

            _this2.setState({});
          },
          value: poster.campaignId
        }, _this2.campaignList.map(function (c) {
          return /*#__PURE__*/_react["default"].createElement(_fdx.Select.Option, {
            value: c.campaignId
          }, c.campaignName);
        })), /*#__PURE__*/_react["default"].createElement("div", {
          className: "mt-15 mb-10"
        }, "Image ", poster.imageList.filter(function (v) {
          return v;
        }).length, "/", poster.imageList.length), /*#__PURE__*/_react["default"].createElement("div", {
          className: "image-list"
        }, poster.imageList.map(function (image, i) {
          return /*#__PURE__*/_react["default"].createElement(_Upload["default"], {
            key: i,
            value: image,
            onChange: function onChange(v) {
              poster.imageList[i] = v;

              _this2.setState({});
            }
          });
        })));
      }), /*#__PURE__*/_react["default"].createElement(_fdx.Button, {
        className: "btn-wrap",
        onClick: function onClick() {
          posterList.push({
            postImage: '',
            campaignId: null,
            imageList: ['', '', '']
          });

          _this2.setState({});
        }
      }, "Add Poster"))), /*#__PURE__*/_react["default"].createElement(_BottomButton["default"], {
        onSaveClick: this.onSave
      }));
    }
  }]);

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

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