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

import React, { Component, PureComponent } from 'react';
import moment from 'moment';
import callApi from '../../_util/ajax';
import Main from './Main';

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

  function PerMonth(props) {
    var _this;

    _classCallCheck(this, PerMonth);

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

  _createClass(PerMonth, [{
    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.findRentalSiteYearStatus(data, callback, concatFlag);
      } else if (String(autoAssign) === '1') {
        this.findAutoAssignRentalSiteYearStatus(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: moment(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;
      var _window$$$RESOURCECON = window.$$RESOURCECONTEXT.autoAssign,
          autoAssign = _window$$$RESOURCECON === void 0 ? '0' : _window$$$RESOURCECON;
      var bodyArray = [];

      if (String(autoAssign) === '1') {
        siteDays.map(function (ele) {
          var obj = {};
          headerContent.map(function (e, i) {
            obj["params".concat(i)] = ele.siteNumbers[i].siteRules[0];
          });
          bodyArray.push(obj);
        });
      } else {
        siteDays.map(function (ele) {
          var obj = {};
          headerContent.map(function (e, i) {
            obj["params".concat(i)] = ele.siteRules[0];
            obj.rentalDate = ele.rentalDate;
          });
          bodyArray.push(obj);
        });
      }

      return bodyArray;
    }
  }, {
    key: "processNavContent",
    value: function processNavContent(siteDays) {
      var navContent = [];
      siteDays.map(function (ele) {
        navContent.push({
          dayTime: ele.rentalDate,
          month: moment(ele.rentalDate).format('YYYY'),
          day: moment(ele.rentalDate).format('M月'),
          dayTimeStr: moment(ele.rentalDate).format('YYYY')
        });
      });
      return {
        navContent: navContent
      };
    }
  }, {
    key: "findAutoAssignRentalSiteYearStatus",
    value: function findAutoAssignRentalSiteYearStatus() {
      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,
          priceType = _window$$$RESOURCECON2.priceType,
          packageName = _window$$$RESOURCECON2.packageName,
          _window$$$RESOURCECON3 = _window$$$RESOURCECON2.rentalType,
          rentalType = _window$$$RESOURCECON3 === void 0 ? '4' : _window$$$RESOURCECON3,
          _window$$$RESOURCECON4 = _window$$$RESOURCECON2.rentalSiteId,
          rentalSiteId = _window$$$RESOURCECON4 === void 0 ? '' : _window$$$RESOURCECON4;
      var sceneType = window.$$context.sceneType;
      var ruleDate = data.ruleDate || this.state.ruleDate;
      callApi({
        api: '/rental/findAutoAssignRentalSiteYearStatus',
        data: {
          rentalType: String(rentalType),
          siteId: rentalSiteId,
          priceType: priceType,
          packageName: packageName ? decodeURI(packageName) : '',
          ruleDate: ruleDate,
          sceneType: 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;

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

          _this2.setState({
            headerContent: headerContent,
            navContent: navContent,
            bodyArray: bodyArray,
            dataDetail: response,
            ruleDate: ruleDate,
            dataLoading: false
          }, function () {
            if (sucCallback) {
              sucCallback({
                headerContent: headerContent,
                navContent: navContent,
                bodyArray: bodyArray,
                dataDetail: response,
                ruleDate: ruleDate
              });
            }
          });
        }
      });
    }
  }, {
    key: "findRentalSiteYearStatus",
    value: function findRentalSiteYearStatus() {
      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$$$RESOURCECON5 = window.$$RESOURCECONTEXT,
          priceType = _window$$$RESOURCECON5.priceType,
          packageName = _window$$$RESOURCECON5.packageName,
          _window$$$RESOURCECON6 = _window$$$RESOURCECON5.rentalType,
          rentalType = _window$$$RESOURCECON6 === void 0 ? '4' : _window$$$RESOURCECON6,
          _window$$$RESOURCECON7 = _window$$$RESOURCECON5.rentalSiteId,
          rentalSiteId = _window$$$RESOURCECON7 === void 0 ? '' : _window$$$RESOURCECON7;
      var sceneType = window.$$context.sceneType;
      var ruleDate = data.ruleDate || this.state.ruleDate;
      callApi({
        api: '/rental/findRentalSiteYearStatus',
        data: {
          rentalType: String(rentalType),
          siteId: rentalSiteId,
          priceType: priceType,
          packageName: packageName ? decodeURI(packageName) : '',
          ruleDate: ruleDate,
          sceneType: sceneType
        },
        success: function success(response) {
          var _response$siteDays2 = response.siteDays,
              siteDays = _response$siteDays2 === void 0 ? [] : _response$siteDays2;

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

          var headerContent = [{
            title: Number(moment(ruleDate).format('YYYY'))
          }];

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

          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,
            ruleDate: ruleDate,
            dataLoading: false,
            concatFlag: concatFlag
          }, function () {
            if (sucCallback) {
              sucCallback({
                headerContent: headerContent,
                navContent: navContent,
                bodyArray: bodyArray,
                dataDetail: response,
                ruleDate: ruleDate
              });
            }
          });
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this4 = this;

      var history = this.props.history;
      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;
      var Spin = window.$$RESOURCECONTEXT.Spin;
      return React.createElement("div", {
        style: {
          margin: 'auto'
        }
      }, React.createElement(Spin, {
        spinning: dataLoading,
        showError: showErrorPage
      }, React.createElement(Main, {
        history: history,
        getUnitData: function getUnitData(data, callback, concatFlag1) {
          return _this4.initComponent(data, callback, concatFlag1);
        },
        headerContent: headerContent,
        navContent: navContent,
        bodyArray: bodyArray,
        dataDetail: dataDetail,
        ruleDate: ruleDate,
        concatFlag: concatFlag
      })));
    }
  }]);

  return PerMonth;
}(PureComponent || Component);

export default PerMonth;