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

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

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

import React, { Component, PureComponent } from 'react';
import moment from 'moment';
import IScroll from 'iscroll/build/iscroll-probe';
import { getUnitHtml, clear } from './common/getUnitHtml';
import { getDaysInMonth } from './common/getMonthDay';

var iScrollClick = function iScrollClick() {
  if (/iPhone|iPad|iPod|Macintosh/i.test(navigator.userAgent)) return false;
  if (/Chrome/i.test(navigator.userAgent)) return /Android/i.test(navigator.userAgent);
  if (/Silk/i.test(navigator.userAgent)) return false;

  if (/Android/i.test(navigator.userAgent)) {
    var s = navigator.userAgent.substr(navigator.userAgent.indexOf('Android') + 8, 3);
    return !(parseFloat(s[0] + s[3]) < 44);
  }

  return false;
};

var firstTime = '';
var paramScrollerCompare;
var paramScrollerDetail;
var paramScrollerY;
var unitNum = {
  1: 89,
  2: 44.5,
  3: 133.5,
  4: 44.5,
  5: 133.5
};
var Months = ['', '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
var timeout = null;
var startTime = Date.parse(new Date()); // 开始时间

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

  function UnitWithoutHour() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, UnitWithoutHour);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(UnitWithoutHour)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "state", {
      unitWidth: window.innerWidth * 2 / 3,
      ruleDate: new Date().getTime(),
      // 当前时间
      scrollLeft: '',
      // 初始默认在当前星期几
      dataDetail: {},
      headerContent: [],
      bodyArray: [] // 表格主题数据

    });

    _defineProperty(_assertThisInitialized(_this), "getUnitHeight", function () {
      var _this$props$halfDayTi = _this.props.halfDayTimeIntervals,
          halfDayTimeIntervals = _this$props$halfDayTi === void 0 ? [] : _this$props$halfDayTi;
      var rentalType = window.$$RESOURCECONTEXT.rentalType;
      var unitHeight = '';

      if (String(rentalType) === '1' || String(rentalType) === '3') {
        unitHeight = halfDayTimeIntervals.length < 3 ? 89 : 133.5;
      } else {
        unitHeight = unitNum[String(rentalType)];
      }

      return unitHeight;
    });

    _defineProperty(_assertThisInitialized(_this), "getNavPot", function (props) {
      var navContent = props.navContent;
      var rentalType = window.$$RESOURCECONTEXT.rentalType;
      var months = [];
      var dayTimes = [];
      var weekDayTime = {};
      navContent.map(function (ele) {
        weekDayTime[ele.month] = weekDayTime[ele.month] + 1 || 1;

        if (months.indexOf(ele.month) === -1) {
          months.push(ele.month);
          dayTimes.push(rentalType === '4' ? 12 : getDaysInMonth(moment(ele.dayTime).format('YYYY'), moment(ele.dayTime).format('MM')));
        }
      });

      if (rentalType === '5') {
        dayTimes = [];

        for (var k in weekDayTime) {
          dayTimes.push(weekDayTime[k]);
        }
      }

      var pDayTimes = [];
      dayTimes.map(function (ele, index) {
        if (index === 0) {
          pDayTimes[index] = 0;
        } else if (index > 1) {
          pDayTimes[index] = pDayTimes[index - 1] + dayTimes[index - 1] * (_this.getUnitHeight() * window.devicePixelRatio);
        } else if (index === 1) {
          pDayTimes[index] = dayTimes[index - 1] * (_this.getUnitHeight() * window.devicePixelRatio);
        }
      });
      return {
        pots: pDayTimes,
        months: months
      };
    });

    _defineProperty(_assertThisInitialized(_this), "changeHeaderNav", function (scroll) {
      var autoAssign = window.$$RESOURCECONTEXT.autoAssign;

      var pPots = _toConsumableArray(_this.pots).reverse();

      var pMonth = _toConsumableArray(_this.months).reverse();

      var processPots = function processPots(index) {
        if (Math.abs(scroll.y) > pPots[index]) {
          if (String(autoAssign) !== '1' && document.getElementById('week-num').innerHTML !== Months[Number(pMonth[index])]) {
            document.getElementById('week-num').innerHTML = Months[Number(pMonth[index])] || pMonth[index];
          }

          return true;
        }

        return false;
      };

      for (var i = 0; i < pPots.length; i++) {
        if (processPots(i)) {
          break;
        }
      }
    });

    _defineProperty(_assertThisInitialized(_this), "initScroll", function () {
      var self = _assertThisInitialized(_this);

      var ruleDate = _this.props.ruleDate;
      var rentalType = window.$$RESOURCECONTEXT.rentalType;
      var day = moment(ruleDate).format('D');
      var pullDownEl = document.getElementById('pull-down-refresh');
      var pullUpEl = document.getElementById('pull-up-refresh');
      var scrollEl = document.getElementById('scroller-detail');
      var bodyEl = document.getElementById('detail'); // 当按月和按周的时候不用指向默认当前日期

      var paramScrollerStartY;

      if (String(rentalType) === '4') {
        paramScrollerStartY = 0;
      } else {
        var initScrollerStartY = _this.getUnitHeight() * Number(day - 1) * window.devicePixelRatio;

        if (scrollEl.offsetHeight - initScrollerStartY <= bodyEl.offsetHeight) {
          paramScrollerStartY = -(scrollEl.offsetHeight - bodyEl.offsetHeight);
        } else {
          paramScrollerStartY = -initScrollerStartY;
        }
      }

      paramScrollerCompare = new IScroll('#scroller-week-date', {
        // 头
        probeType: 3,
        eventPassthrough: true,
        scrollX: true,
        scrollY: false,
        disablePointer: true,
        disableTouch: false,
        disableMouse: false,
        deceleration: 0.005
      });
      paramScrollerDetail = new IScroll('#scroller-detail', {
        // 右侧body
        probeType: 3,
        eventPassthrough: true,
        scrollX: true,
        scrollY: false,
        click: iScrollClick(),
        tap: true,
        disablePointer: true,
        disableTouch: false,
        disableMouse: false,
        deceleration: 0.005,
        bounce: false
      });
      paramScrollerY = new IScroll('#detail', {
        // 整个body
        probeType: 2,
        mouseWheel: true,
        scrollX: false,
        scrollY: true,
        freeScroll: true,
        click: false,
        tap: false,
        bindToWrapper: true,
        disablePointer: true,
        disableTouch: false,
        disableMouse: false,
        deceleration: 0.005,
        startY: paramScrollerStartY
      });
      paramScrollerCompare.on('scroll', function () {
        paramScrollerDetail.scrollTo(this.x, this.y);
      });
      paramScrollerDetail.on('scroll', function () {
        paramScrollerCompare.scrollTo(this.x, this.y);
      }); // if (String(rentalType) !== '4') {

      paramScrollerY.on('scroll', function () {
        var _this2 = this;

        if (self.pots.length > 1) {
          if (timeout !== null) clearTimeout(timeout);
          var curTime = Date.parse(new Date()); // 当前时间

          if (curTime - startTime >= 1000) {
            // 时间差>=1秒直接执行
            self.changeHeaderNav(this);
            startTime = curTime;
          } else {
            // 否则延时执行，像滚动了一下，差值<1秒的那种也要执行
            timeout = setTimeout(function () {
              self.changeHeaderNav(_this2);
            }, 100);
          }
        }

        if (this.y >= 60 * window.devicePixelRatio && !pullDownEl.className.match('flip')) {
          document.getElementById('pull-down-icon').className = 'pull-down-release';
          document.getElementById('pull-down-text').innerHTML = '释放加载上一页';
          pullDownEl.className = 'flip';
        } else if (this.y > 0 && this.y < 55 * window.devicePixelRatio && pullDownEl.className.match('flip')) {
          pullDownEl.className = '';
          document.getElementById('pull-down-icon').className = '';
          document.getElementById('pull-down-text').innerHTML = '下拉加载更多';
        } else if (this.y <= paramScrollerY.maxScrollY - 60 * window.devicePixelRatio && !pullUpEl.className.match('flip')) {
          document.getElementById('pull-up-icon').className = 'pull-up-release';
          document.getElementById('pull-up-text').innerHTML = '释放加载下一页';
          pullUpEl.className = 'flip';
        } else if (this.y < paramScrollerY.maxScrollY && this.y > paramScrollerY.maxScrollY - 55 * window.devicePixelRatio && pullUpEl.className.match('flip')) {
          pullUpEl.className = '';
          document.getElementById('pull-up-icon').className = '';
          document.getElementById('pull-up-text').innerHTML = '上拉加载更多';
        }
      }); // }
    });

    _defineProperty(_assertThisInitialized(_this), "touchEnd", function () {
      var pullDownEl = document.getElementById('pull-down-refresh');
      var pullUpEl = document.getElementById('pull-up-refresh');

      if (pullDownEl.className.match('flip')) {
        pullDownEl.className = '';
        document.getElementById('pull-down-icon').className = '';
        document.getElementById('pull-down-text').innerHTML = '下拉加载更多';

        _this.prePage();
      } else if (pullUpEl.className.match('flip')) {
        pullUpEl.className = '';
        document.getElementById('pull-up-icon').className = '';
        document.getElementById('pull-up-text').innerHTML = '上拉加载更多';

        _this.nextPage();
      }
    });

    _defineProperty(_assertThisInitialized(_this), "prePage", function () {
      var bodyArray = _this.props.bodyArray;
      var _window$$$RESOURCECON = window.$$RESOURCECONTEXT,
          autoAssign = _window$$$RESOURCECON.autoAssign,
          rentalType = _window$$$RESOURCECON.rentalType;
      var ruleDate;
      var currentMonth = Number(moment(ruleDate).format('MM'));
      var day = getDaysInMonth(currentMonth === 1 ? moment(ruleDate).format('YYYY') : moment(ruleDate).format('YYYY') - 1, currentMonth === 1 ? 12 : moment(ruleDate).format('MM') - 1);
      var nextMonthDate = Number(moment("".concat(moment(ruleDate - day * 24 * 60 * 60 * 1000).format('YYYY-MM'), "-01")).format('x'));

      if (String(autoAssign) === '1') {
        ruleDate = _this.props.ruleDate;
        nextMonthDate = Number(moment("".concat(moment(ruleDate - day * 24 * 60 * 60 * 1000).format('YYYY-MM'), "-01")).format('x'));

        if (String(rentalType) === '4') {
          day = currentMonth === 1 ? 365 : 366;
          nextMonthDate = Number(ruleDate - day * 24 * 60 * 60 * 1000);
        }
      } else if (String(rentalType) === '4') {
        ruleDate = bodyArray[0].rentalDate;
        day = 31;
        nextMonthDate = Number(ruleDate - day * 24 * 60 * 60 * 1000);
      } else if (String(rentalType) === '5') {
        ruleDate = bodyArray[0].rentalDate;
        day = 7;
        nextMonthDate = Number(ruleDate - day * 24 * 60 * 60 * 1000);
      } else {
        ruleDate = bodyArray[0].rentalDate;
        day = 1;
        nextMonthDate = Number(ruleDate - day * 24 * 60 * 60 * 1000);
      }

      _this.setParentState({
        loadingData: true
      });

      _this.props.getData({
        ruleDate: nextMonthDate
      }, function () {
        _this.setParentState({
          loadingData: false
        });
      }, String(autoAssign) === '1' ? '' : 'pre');
    });

    _defineProperty(_assertThisInitialized(_this), "nextPage", function () {
      var bodyArray = _this.props.bodyArray;
      var _window$$$RESOURCECON2 = window.$$RESOURCECONTEXT,
          autoAssign = _window$$$RESOURCECON2.autoAssign,
          rentalType = _window$$$RESOURCECON2.rentalType;
      var ruleDate;
      var currentMonth = Number(moment(ruleDate).format('MM'));
      var day = getDaysInMonth(currentMonth === 12 ? moment(ruleDate).format('YYYY') + 1 : moment(ruleDate).format('YYYY'), currentMonth === 12 ? 1 : moment(ruleDate).format('MM') + 1);
      var nextMonthDate = Number(moment("".concat(moment(ruleDate + day * 24 * 60 * 60 * 1000).format('YYYY-MM'), "-01")).format('x'));

      if (String(autoAssign) === '1') {
        ruleDate = _this.props.ruleDate;
        nextMonthDate = Number(moment("".concat(moment(ruleDate + day * 24 * 60 * 60 * 1000).format('YYYY-MM'), "-01")).format('x'));

        if (String(rentalType) === '4') {
          day = currentMonth === 12 ? 365 : 366;
          nextMonthDate = Number(ruleDate + day * 24 * 60 * 60 * 1000);
        }
      } else if (String(rentalType) === '4') {
        ruleDate = bodyArray[bodyArray.length - 1].rentalDate;
        day = 31;
        nextMonthDate = Number(ruleDate + day * 24 * 60 * 60 * 1000);
      } else if (String(rentalType) === '5') {
        ruleDate = bodyArray[bodyArray.length - 1].rentalDate;
        day = 7;
        nextMonthDate = Number(ruleDate + day * 24 * 60 * 60 * 1000);
      } else {
        ruleDate = bodyArray[bodyArray.length - 1].rentalDate;
        day = 1;
        nextMonthDate = Number(ruleDate + day * 24 * 60 * 60 * 1000);
      }

      _this.setParentState({
        loadingData: true
      });

      _this.props.getData({
        ruleDate: nextMonthDate
      }, function () {
        _this.setParentState({
          loadingData: false
        });
      }, String(autoAssign) === '1' ? '' : 'next');
    });

    _defineProperty(_assertThisInitialized(_this), "configState", function (state) {
      _this.setState(state);
    });

    _defineProperty(_assertThisInitialized(_this), "setParentState", function (state) {
      _this.props.setParentState(state);
    });

    return _this;
  }

  _createClass(UnitWithoutHour, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      firstTime = 0;
      this.initComponent(this.props);
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      this.initComponent(nextProps);
      var ruleDate = nextProps.ruleDate,
          concatFlag = nextProps.concatFlag,
          rentalType = nextProps.rentalType;
      var day = moment(ruleDate).format('D');

      if (ruleDate !== this.props.ruleDate) {
        clear();
        var y = -(this.getUnitHeight() * Number(day - 1) * window.devicePixelRatio);

        if (String(rentalType) === '5') {
          var d = window.parseInt(day / 7) + 1;
          y = -(this.getUnitHeight() * Number(d - 1) * window.devicePixelRatio);
        }

        paramScrollerDetail.refresh();
        setTimeout(function () {
          paramScrollerY.refresh();
        }, 500);
        if (paramScrollerCompare) paramScrollerCompare.scrollTo(0, 0); // 头清0

        if (y <= paramScrollerY.maxScrollY) {
          y = paramScrollerY.maxScrollY;
        }

        if (paramScrollerY) {
          this.changeHeaderNav(paramScrollerY);
        }

        if (!concatFlag) {
          // concatFlag当时上下滑的时候，滚动条不刷新位置
          if (paramScrollerDetail) paramScrollerDetail.scrollTo(0, 0);
          if (paramScrollerY) paramScrollerY.scrollTo(paramScrollerY.x, y);
        } else if (String(concatFlag) === 'pre') {
          if (paramScrollerDetail) paramScrollerDetail.scrollTo(0, 0);
          if (paramScrollerY) paramScrollerY.scrollTo(paramScrollerY.x, y);
        } else if (String(concatFlag) === 'next') {
          if (paramScrollerDetail) paramScrollerDetail.scrollTo(0, 0);
          if (paramScrollerY) paramScrollerY.scrollTo(paramScrollerY.x, paramScrollerY.y + this.getUnitHeight() * window.devicePixelRatio - parseFloat(document.getElementById('scroller-detail').style.height));
        }
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      if (firstTime === 0) {
        clear();
        this.initScroll();
        firstTime += 1;
      }
    }
  }, {
    key: "initComponent",
    value: function initComponent(props) {
      var windowWidth = window.innerWidth;
      var _props$headerContent = props.headerContent,
          headerContent = _props$headerContent === void 0 ? [] : _props$headerContent,
          _props$bodyArray = props.bodyArray,
          bodyArray = _props$bodyArray === void 0 ? [] : _props$bodyArray,
          _props$dataDetail = props.dataDetail,
          dataDetail = _props$dataDetail === void 0 ? {} : _props$dataDetail,
          autoAssign = props.autoAssign,
          ruleDate = props.ruleDate;
      var unitWitdh = "".concat(windowWidth * 2 / 3, "px");
      var widthValue = {
        0: 2 / 3,
        1: 2 / 3,
        2: 1 / 3
      };

      if (String(autoAssign) === '1') {
        unitWitdh = widthValue[String(headerContent.length)] ? windowWidth * widthValue[String(headerContent.length)] : '2rem';
      }

      var _this$getNavPot = this.getNavPot(props),
          _this$getNavPot$pots = _this$getNavPot.pots,
          pots = _this$getNavPot$pots === void 0 ? [] : _this$getNavPot$pots,
          _this$getNavPot$month = _this$getNavPot.months,
          months = _this$getNavPot$month === void 0 ? [] : _this$getNavPot$month;

      this.pots = pots;
      this.months = months;
      this.setState({
        headerContent: headerContent,
        bodyArray: bodyArray,
        dataDetail: dataDetail,
        ruleDate: ruleDate,
        unitWitdh: unitWitdh
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var windowHeight = window.innerHeight;
      var _this$state = this.state,
          headerContent = _this$state.headerContent,
          bodyArray = _this$state.bodyArray,
          unitWitdh = _this$state.unitWitdh,
          dataDetail = _this$state.dataDetail,
          ruleDate = _this$state.ruleDate,
          multiUnit = _this$state.multiUnit,
          autoAssign = _this$state.autoAssign,
          notice = _this$state.notice;
      var bottomHeight = 110;

      if (String(multiUnit) === '1' && String(autoAssign) === '0') {
        bottomHeight += 45;
      }

      if (String(notice) && String(notice) !== 'undefined') {
        bottomHeight += 30;
      }

      return React.createElement("div", {
        className: "main scroller-detail",
        id: "scroller-detail",
        onTouchEnd: function onTouchEnd() {
          return _this3.touchEnd();
        },
        style: {
          height: "".concat(windowHeight - bottomHeight * window.devicePixelRatio, "px")
        }
      }, React.createElement("div", {
        id: "slide",
        className: "slide"
      }, React.createElement("div", {
        className: "data"
      }, React.createElement("div", {
        className: "group"
      }, bodyArray.map(function (ele, index) {
        return React.createElement("div", {
          key: "".concat(String(ruleDate), "-").concat(String(index)),
          className: "item scroller-detail-column"
        }, headerContent.map(function (e, i) {
          return getUnitHtml({
            ele: ele["params".concat(String(i))] || {},
            row: index,
            col: i,
            unitWitdh: unitWitdh,
            data: {
              bodyArray: bodyArray,
              headerContent: headerContent,
              dataDetail: dataDetail,
              ruleDate: ruleDate
            }
          }, {
            setParentState: _this3.setParentState,
            configState: _this3.configState
          });
        }));
      })))));
    }
  }]);

  return UnitWithoutHour;
}(PureComponent || Component);

export default UnitWithoutHour;