"use strict";

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

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

var _moment = _interopRequireDefault(require("moment"));

var _ajax = _interopRequireDefault(require("../../_util/ajax"));

var _Main = _interopRequireDefault(require("./Main"));

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? 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 _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; }

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 _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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

var Months = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'];

var PerHalfDay =
/*#__PURE__*/
function (_ref) {
  _inherits(PerHalfDay, _ref);

  function PerHalfDay(props) {
    var _this;

    _classCallCheck(this, PerHalfDay);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(PerHalfDay).call(this, props));
    _this.state = {
      isLoading: true,
      ruleDate: new Date().getTime(),
      rentalSites: [],
      // 列表
      headerContent: [],
      // 头部内容
      navContent: [],
      // 左侧导航内容
      bodyArray: [],
      // 主体unit内容
      dataDetail: {},
      // 接口返回数据
      dataLoading: true,
      showErrorPage: false
    };
    return _this;
  }

  _createClass(PerHalfDay, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.initComponent();
    }
  }, {
    key: "initComponent",
    value: function initComponent() {
      var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var callback = arguments.length > 1 ? arguments[1] : undefined;
      var concatFlag = arguments.length > 2 ? arguments[2] : undefined;
      var autoAssign = window.$$RESOURCECONTEXT.autoAssign;

      if (String(autoAssign) === '0') {
        this.findRentalSiteMonthStatus(data, callback, concatFlag); // 有编号的的下拉上拉是加载更多，所以有concatFlag
      } else if (String(autoAssign) === '1') {
        this.findAutoAssignRentalSiteMonthStatus(data, callback);
      }
    }
  }, {
    key: "getHeaderContent",
    value: function getHeaderContent() {
      var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      // 头部导航
      var weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
      var headerContent = [];
      data.forEach(function (ele, index) {
        if (ele.rentalDate) {
          headerContent.push({
            title: weekDays[index],
            rentalDate: ele.rentalDate,
            date: (0, _moment["default"])(ele.rentalDate).format('MM-DD')
          });
        } else {
          headerContent.push({
            title: ele
          });
        }
      });
      return headerContent;
    } // 处理bodyArray

  }, {
    key: "processBodyArray",
    value: function processBodyArray(_ref2) {
      var _ref2$siteDays = _ref2.siteDays,
          siteDays = _ref2$siteDays === void 0 ? [] : _ref2$siteDays,
          _ref2$headerContent = _ref2.headerContent,
          headerContent = _ref2$headerContent === void 0 ? [] : _ref2$headerContent,
          _ref2$halfDayTimeInte = _ref2.halfDayTimeIntervals,
          halfDayTimeIntervals = _ref2$halfDayTimeInte === void 0 ? [] : _ref2$halfDayTimeInte;
      var _window$$$RESOURCECON = window.$$RESOURCECONTEXT.autoAssign,
          autoAssign = _window$$$RESOURCECON === void 0 ? '0' : _window$$$RESOURCECON; // const defaultSiteRules = String(rentalType) === '1' ? [{}, {}] : [{}, {}, {}]; //1是上午下午 3是由晚上

      var defaultSiteRules = [];
      halfDayTimeIntervals.map(function () {
        defaultSiteRules.push({});
      });
      var bodyArray = [];

      if (String(autoAssign) === '1') {
        // 有编号的
        siteDays.map(function (ele) {
          defaultSiteRules.map(function (value, key) {
            var obj = {};
            headerContent.map(function (e, i) {
              obj["params".concat(i)] = ele.siteNumbers[i].siteRules[key];
            });
            bodyArray.push(obj);
          });
        });
      } else {
        siteDays.map(function (ele) {
          // 无编号的
          defaultSiteRules.map(function (value, key) {
            var obj = {};
            headerContent.map(function (e, i) {
              obj["params".concat(i)] = ele.siteRules[key];
              obj.rentalDate = ele.rentalDate;
            });
            bodyArray.push(obj);
          });
        });
      }

      return [].concat(bodyArray);
    }
  }, {
    key: "processNavContent",
    value: function processNavContent(siteDays) {
      var navContent = [];
      var weekDays = {
        Su: '星期日',
        Mo: '星期一',
        Tu: '星期二',
        We: '星期三',
        Th: '星期四',
        Fr: '星期五',
        Sa: '星期六'
      };
      siteDays.map(function (ele) {
        navContent.push({
          dayTime: ele.rentalDate,
          month: (0, _moment["default"])(ele.rentalDate).format('M'),
          day: (0, _moment["default"])(ele.rentalDate).format('D'),
          dayTimeStr: weekDays[(0, _moment["default"])(ele.rentalDate).format('dd')]
        });
      });
      return {
        navContent: [].concat(navContent)
      };
    }
  }, {
    key: "findAutoAssignRentalSiteMonthStatus",
    value: function findAutoAssignRentalSiteMonthStatus() {
      var _this2 = this;

      var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var sucCallback = arguments.length > 1 ? arguments[1] : undefined;
      // 拿有编号的资源
      var _window$$$RESOURCECON2 = window.$$RESOURCECONTEXT,
          resourceType = _window$$$RESOURCECON2.resourceType,
          priceType = _window$$$RESOURCECON2.priceType,
          rentalType = _window$$$RESOURCECON2.rentalType,
          rentalSiteId = _window$$$RESOURCECON2.rentalSiteId;
      var _window$$$RESOURCECON3 = window.$$RESOURCECONTEXT,
          sceneType = _window$$$RESOURCECON3.sceneType,
          packageName = _window$$$RESOURCECON3.packageName,
          sceneToken = _window$$$RESOURCECON3.sceneToken;
      packageName = packageName ? {
        packageName: decodeURI(packageName)
      } : {};
      sceneType = sceneType ? {
        sceneType: sceneType
      } : {};
      sceneToken = sceneToken ? {
        sceneToken: sceneToken
      } : {};
      var ruleDate = data.ruleDate || this.state.ruleDate;
      (0, _ajax["default"])({
        api: '/rental/findAutoAssignRentalSiteMonthStatus',
        data: _objectSpread({
          rentalType: String(rentalType),
          siteId: rentalSiteId,
          priceType: priceType,
          resourceType: resourceType,
          ruleDate: ruleDate
        }, packageName, sceneToken, sceneType),
        success: function success(response) {
          var _response$siteDays = response.siteDays,
              siteDays = _response$siteDays === void 0 ? [] : _response$siteDays,
              _response$siteNames = response.siteNames,
              siteNames = _response$siteNames === void 0 ? [] : _response$siteNames,
              _response$halfDayTime = response.halfDayTimeIntervals,
              halfDayTimeIntervals = _response$halfDayTime === void 0 ? [] : _response$halfDayTime;

          var _this2$processNavCont = _this2.processNavContent(siteDays),
              _this2$processNavCont2 = _this2$processNavCont.navContent,
              navContent = _this2$processNavCont2 === void 0 ? [] : _this2$processNavCont2;

          var headerContent = _this2.getHeaderContent(siteNames); // 头部导航


          var bodyArray = _this2.processBodyArray({
            siteDays: siteDays,
            headerContent: headerContent,
            halfDayTimeIntervals: halfDayTimeIntervals
          });

          _this2.setState({
            headerContent: headerContent,
            navContent: navContent,
            bodyArray: bodyArray,
            dataDetail: response,
            ruleDate: ruleDate,
            halfDayTimeIntervals: halfDayTimeIntervals,
            dataLoading: false
          }, function () {
            if (sucCallback) {
              sucCallback({
                headerContent: headerContent,
                navContent: navContent,
                bodyArray: bodyArray,
                dataDetail: response,
                ruleDate: ruleDate
              });
            }
          });
        },
        error: function error() {
          _this2.setState({
            dataLoading: false,
            showErrorPage: true
          });
        }
      });
    }
  }, {
    key: "findRentalSiteMonthStatus",
    value: function findRentalSiteMonthStatus() {
      var _this3 = this;

      var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var sucCallback = arguments.length > 1 ? arguments[1] : undefined;
      var concatFlag = arguments.length > 2 ? arguments[2] : undefined;
      // 拿无编号的资源
      var _window$$$RESOURCECON4 = window.$$RESOURCECONTEXT,
          resourceType = _window$$$RESOURCECON4.resourceType,
          priceType = _window$$$RESOURCECON4.priceType,
          rentalType = _window$$$RESOURCECON4.rentalType,
          rentalSiteId = _window$$$RESOURCECON4.rentalSiteId;
      var _window$$$RESOURCECON5 = window.$$RESOURCECONTEXT,
          sceneType = _window$$$RESOURCECON5.sceneType,
          packageName = _window$$$RESOURCECON5.packageName,
          sceneToken = _window$$$RESOURCECON5.sceneToken;
      packageName = packageName ? {
        packageName: decodeURI(packageName)
      } : {};
      sceneType = sceneType ? {
        sceneType: sceneType
      } : {};
      sceneToken = sceneToken ? {
        sceneToken: sceneToken
      } : {};
      var ruleDate = data.ruleDate || this.state.ruleDate;
      (0, _ajax["default"])({
        api: '/rental/findRentalSiteMonthStatus',
        data: _objectSpread({
          rentalType: String(rentalType),
          siteId: rentalSiteId,
          priceType: priceType,
          resourceType: resourceType,
          ruleDate: ruleDate
        }, sceneType, sceneToken, packageName),
        success: function success(response) {
          var _response$siteDays2 = response.siteDays,
              siteDays = _response$siteDays2 === void 0 ? [] : _response$siteDays2,
              _response$halfDayTime2 = response.halfDayTimeIntervals,
              halfDayTimeIntervals = _response$halfDayTime2 === void 0 ? [] : _response$halfDayTime2;

          var _this3$processNavCont = _this3.processNavContent(siteDays),
              _this3$processNavCont2 = _this3$processNavCont.navContent,
              navContent = _this3$processNavCont2 === void 0 ? [] : _this3$processNavCont2;

          var headerContent = [{
            title: "".concat(Months[Number((0, _moment["default"])(ruleDate).format('M'))], "\u6708")
          }];

          var bodyArray = _this3.processBodyArray({
            siteDays: siteDays,
            headerContent: headerContent,
            halfDayTimeIntervals: halfDayTimeIntervals
          });

          var pNavContent = [];
          var pBodyArray = [];

          if (concatFlag === 'pre') {
            pNavContent = [].concat(_toConsumableArray(navContent), _toConsumableArray(_this3.state.navContent));
            pBodyArray = [].concat(_toConsumableArray(bodyArray), _toConsumableArray(_this3.state.bodyArray));
          } else if (concatFlag === 'next') {
            pNavContent = [].concat(_toConsumableArray(_this3.state.navContent), _toConsumableArray(navContent));
            pBodyArray = [].concat(_toConsumableArray(_this3.state.bodyArray), _toConsumableArray(bodyArray));
          } else {
            pNavContent = _toConsumableArray(navContent);
            pBodyArray = _toConsumableArray(bodyArray);
          }

          _this3.setState({
            headerContent: headerContent,
            navContent: pNavContent,
            bodyArray: pBodyArray,
            dataDetail: response,
            concatFlag: concatFlag,
            halfDayTimeIntervals: halfDayTimeIntervals,
            ruleDate: ruleDate,
            dataLoading: false
          }, function () {
            if (sucCallback) {
              sucCallback({
                headerContent: headerContent,
                navContent: navContent,
                bodyArray: bodyArray,
                dataDetail: response,
                ruleDate: ruleDate
              });
            }
          });
        },
        error: function error() {
          _this3.setState({
            dataLoading: false,
            showErrorPage: true
          });
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this4 = this;

      var history = this.props.history;
      var Spin = window.$$RESOURCECONTEXT.Spin;
      var _this$state = this.state,
          headerContent = _this$state.headerContent,
          dataLoading = _this$state.dataLoading,
          showErrorPage = _this$state.showErrorPage,
          bodyArray = _this$state.bodyArray,
          dataDetail = _this$state.dataDetail,
          navContent = _this$state.navContent,
          ruleDate = _this$state.ruleDate,
          concatFlag = _this$state.concatFlag,
          halfDayTimeIntervals = _this$state.halfDayTimeIntervals;
      return _react["default"].createElement("div", {
        style: {
          margin: 'auto'
        }
      }, _react["default"].createElement(Spin, {
        spinning: dataLoading,
        showError: showErrorPage
      }, _react["default"].createElement(_Main["default"], {
        history: history,
        getUnitData: function getUnitData(data, callback, concatFlag1) {
          return _this4.initComponent(data, callback, concatFlag1);
        },
        headerContent: headerContent,
        navContent: navContent,
        bodyArray: bodyArray,
        dataDetail: dataDetail,
        ruleDate: ruleDate,
        halfDayTimeIntervals: halfDayTimeIntervals,
        concatFlag: concatFlag
      })));
    }
  }]);

  return PerHalfDay;
}(_react.PureComponent || _react.Component);

var _default = PerHalfDay;
exports["default"] = _default;
module.exports = exports.default;