"use strict";

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

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

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

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

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

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 _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 PerHour =
/*#__PURE__*/
function (_ref) {
  _inherits(PerHour, _ref);

  function PerHour(props) {
    var _this;

    _classCallCheck(this, PerHour);

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

  _createClass(PerHour, [{
    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 autoAssign = window.$$RESOURCECONTEXT.autoAssign;

      if (String(autoAssign) === '0') {
        this.findRentalSiteWeekStatus(data, callback);
      } else if (String(autoAssign) === '1') {
        this.findAutoAssignRentalSiteDayStatus(data, callback);
      }
    } // 返回processDayTimes :[01:00,02:00]  navContent:[{dayTime:1000,dayTimeStr:"1:00"}]左侧导航
    // bodyArray 只是为了构造navContent同等长度的数组，主体的内容应该跟左侧导航保持长度一样

  }, {
    key: "processDayTimes",
    value: function processDayTimes(_ref2) {
      var dayTimes = _ref2.dayTimes;
      var processDayTimes = [];
      var navContent = [];
      var bodyArray = []; // dayTimes 如果是多个时间段的时候，是断开的，所以这里自己拼接

      dayTimes.forEach(function (e) {
        var dayTimeStr = (0, _moment["default"])(e).format('HH:mm');
        var obj = {
          dayTime: e,
          dayTimeStr: dayTimeStr
        };
        processDayTimes.push(dayTimeStr);
        bodyArray.push(obj);
        navContent.push(obj);
      });
      return {
        processDayTimes: processDayTimes,
        bodyArray: bodyArray,
        navContent: navContent
      };
    }
  }, {
    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， validArray指后台接口返回的存单元格有效数组

  }, {
    key: "processBodyArray",
    value: function processBodyArray() {
      var bodyArray = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      var headerContent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
      var dayTimes = arguments.length > 2 ? arguments[2] : undefined;
      var processDayTimes = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
      var validArray = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
      var theFirstTime = dayTimes[0];
      var siteRules = []; // 为了确认 有多少个时间段

      validArray.forEach(function (ele) {
        if (ele.siteRules.length !== 0) {
          siteRules = ele.siteRules;
        }
      });
      siteRules.forEach(function (e, i) {
        var obj = {};
        headerContent.forEach(function (ele, index) {
          obj["params".concat(index)] = validArray[index].siteRules[i] || {};
        });
        var key = processDayTimes.indexOf((0, _moment["default"])(e.beginTime).format('HH:mm'));

        if (key !== i) {
          bodyArray[key] = obj;
        } else {
          bodyArray[i] = obj;
        }
      });
      bodyArray.unshift({
        dayTime: theFirstTime,
        dayTimeStr: (0, _moment["default"])(theFirstTime).format('HH:mm')
      });
      return bodyArray;
    }
  }, {
    key: "processNavContent",
    value: function processNavContent(navContent, dayTimes) {
      var dayTimesLength = dayTimes.length;
      var theLastTime = dayTimes[dayTimesLength - 1];
      navContent.push({
        dayTime: theLastTime,
        dayTimeStr: (0, _moment["default"])(theLastTime).format('HH:mm')
      });
      return navContent;
    }
  }, {
    key: "findAutoAssignRentalSiteDayStatus",
    value: function findAutoAssignRentalSiteDayStatus() {
      var _this2 = this;

      var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var sucCallback = arguments.length > 1 ? arguments[1] : undefined;
      var _window$$$RESOURCECON = window.$$RESOURCECONTEXT,
          priceType = _window$$$RESOURCECON.priceType,
          packageName = _window$$$RESOURCECON.packageName,
          _window$$$RESOURCECON2 = _window$$$RESOURCECON.rentalType,
          rentalType = _window$$$RESOURCECON2 === void 0 ? '0' : _window$$$RESOURCECON2,
          _window$$$RESOURCECON3 = _window$$$RESOURCECON.rentalSiteId,
          rentalSiteId = _window$$$RESOURCECON3 === void 0 ? '' : _window$$$RESOURCECON3;
      var sceneType = this.props.sceneType;
      (0, _ajax["default"])({
        api: '/rental/findAutoAssignRentalSiteDayStatus',
        data: {
          rentalType: String(rentalType),
          priceType: priceType,
          packageName: packageName ? decodeURI(packageName) : '',
          siteId: rentalSiteId,
          ruleDate: data.ruleDate || this.state.ruleDate,
          sceneType: sceneType
        },
        success: function success(response) {
          var _response$siteNames = response.siteNames,
              siteNames = _response$siteNames === void 0 ? [] : _response$siteNames,
              _response$dayTimes = response.dayTimes,
              dayTimes = _response$dayTimes === void 0 ? [] : _response$dayTimes,
              _response$siteNumbers = response.siteNumbers,
              siteNumbers = _response$siteNumbers === void 0 ? [] : _response$siteNumbers;

          var _this2$processDayTime = _this2.processDayTimes({
            dayTimes: dayTimes
          }),
              _this2$processDayTime2 = _this2$processDayTime.navContent,
              navContent = _this2$processDayTime2 === void 0 ? [] : _this2$processDayTime2,
              _this2$processDayTime3 = _this2$processDayTime.processDayTimes,
              processDayTimes = _this2$processDayTime3 === void 0 ? [] : _this2$processDayTime3,
              _this2$processDayTime4 = _this2$processDayTime.bodyArray,
              bodyArray = _this2$processDayTime4 === void 0 ? [] : _this2$processDayTime4;

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


          bodyArray = _this2.processBodyArray(bodyArray, headerContent, dayTimes, processDayTimes, siteNumbers);
          navContent = _this2.processNavContent(navContent, dayTimes);

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

      var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var sucCallback = arguments.length > 1 ? arguments[1] : undefined;
      var _window$$$RESOURCECON4 = window.$$RESOURCECONTEXT,
          priceType = _window$$$RESOURCECON4.priceType,
          packageName = _window$$$RESOURCECON4.packageName,
          _window$$$RESOURCECON5 = _window$$$RESOURCECON4.rentalType,
          rentalType = _window$$$RESOURCECON5 === void 0 ? '0' : _window$$$RESOURCECON5,
          _window$$$RESOURCECON6 = _window$$$RESOURCECON4.rentalSiteId,
          rentalSiteId = _window$$$RESOURCECON6 === void 0 ? '' : _window$$$RESOURCECON6;
      var sceneType = this.props.sceneType;
      (0, _ajax["default"])({
        api: '/rental/findRentalSiteWeekStatus',
        data: {
          siteId: rentalSiteId,
          priceType: priceType,
          packageName: packageName ? decodeURI(packageName) : '',
          ruleDate: data.ruleDate || this.state.ruleDate,
          rentalType: String(rentalType),
          sceneType: sceneType
        },
        success: function success(response) {
          var _response$siteDays = response.siteDays,
              siteDays = _response$siteDays === void 0 ? [] : _response$siteDays,
              _response$dayTimes2 = response.dayTimes,
              dayTimes = _response$dayTimes2 === void 0 ? [] : _response$dayTimes2;

          var _this3$processDayTime = _this3.processDayTimes(response),
              _this3$processDayTime2 = _this3$processDayTime.navContent,
              navContent = _this3$processDayTime2 === void 0 ? [] : _this3$processDayTime2,
              _this3$processDayTime3 = _this3$processDayTime.processDayTimes,
              processDayTimes = _this3$processDayTime3 === void 0 ? [] : _this3$processDayTime3,
              _this3$processDayTime4 = _this3$processDayTime.bodyArray,
              bodyArray = _this3$processDayTime4 === void 0 ? [] : _this3$processDayTime4;

          var headerContent = _this3.getHeaderContent(siteDays); // 头部导航


          bodyArray = _this3.processBodyArray(bodyArray, headerContent, dayTimes, processDayTimes, siteDays, '');
          navContent = _this3.processNavContent(navContent, dayTimes);

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

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

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

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