"use strict";

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

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

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

var _iscrollProbe = _interopRequireDefault(require("iscroll/build/iscroll-probe"));

var _getUnitHtml = require("./common/getUnitHtml");

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

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

  function PerHourUnit() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, PerHourUnit);

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

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

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

    });

    _defineProperty(_assertThisInitialized(_this), "onScrollX", function () {
      var pullRightEl = document.getElementById('pull-right-refresh');
      var pullLeftEl = document.getElementById('pull-left-refresh');

      if (pullRightEl && paramScrollerDetail.x >= 47 * window.devicePixelRatio && !pullRightEl.className.match('flip')) {
        document.getElementById('pull-right-icon').className = 'pull-right-release';
        document.getElementById('pull-right-text').innerHTML = '释放加载上一页';
        pullRightEl.className = 'flip';
      } else if (pullRightEl && paramScrollerDetail.x > 0 && paramScrollerDetail.x < 42 * window.devicePixelRatio && pullRightEl.className.match('flip')) {
        pullRightEl.className = '';
        document.getElementById('pull-right-icon').className = '';
        document.getElementById('pull-right-text').innerHTML = ''; // document.getElementById('pull-right-text').innerHTML = '右拉加载上一页';
      } else if (pullLeftEl && paramScrollerDetail.x <= paramScrollerDetail.maxScrollX - 47 * window.devicePixelRatio && !pullLeftEl.className.match('flip')) {
        document.getElementById('pull-left-icon').className = 'pull-left-release';
        document.getElementById('pull-left-text').innerHTML = '释放加载下一页';
        pullLeftEl.className = 'flip';
      } else if (pullLeftEl && paramScrollerDetail.x < paramScrollerDetail.maxScrollX && paramScrollerDetail.x > paramScrollerDetail.maxScrollX - 42 * window.devicePixelRatio && pullLeftEl.className.match('flip')) {
        pullLeftEl.className = '';
        document.getElementById('pull-left-icon').className = ''; // document.getElementById('pull-left-text').innerHTML = '左拉加载下一页';

        document.getElementById('pull-left-text').innerHTML = '';
      }
    });

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

      var pullDownEl = document.getElementById('pull-down-refresh');
      var pullUpEl = document.getElementById('pull-up-refresh');
      var autoAssign = window.$$RESOURCECONTEXT.autoAssign;
      var bounceY = String(autoAssign) === '0';
      var day = new Date().getDay() > 4 ? 4 : new Date().getDay();
      var startX = 0;

      if (String(autoAssign) === '0') {
        startX = -day * document.getElementById('select-date').offsetWidth;
      }

      paramScrollerCompare = new _iscrollProbe["default"]('#scroller-week-date', {
        probeType: 3,
        eventPassthrough: true,
        scrollX: true,
        scrollY: false,
        startX: startX
      });
      paramScrollerDetail = new _iscrollProbe["default"]('#scroller-detail', {
        probeType: 3,
        eventPassthrough: true,
        scrollX: true,
        scrollY: false,
        click: iScrollClick(),
        tap: true,
        bounce: bounceY,
        bounceDown: bounceY,
        startX: startX
      });
      paramScrollerY = new _iscrollProbe["default"]('#detail', {
        probeType: 2,
        mouseWheel: true,
        scrollX: false,
        scrollY: true,
        freeScroll: true,
        click: false,
        tap: false,
        bindToWrapper: true,
        deceleration: 0.01,
        bounce: !bounceY,
        bounceDown: !bounceY
      });
      paramScrollerCompare.on('scroll', function () {
        paramScrollerDetail.scrollTo(this.x, this.y);

        if (String(autoAssign) === '0') {
          self.onScrollX();
        }
      });
      paramScrollerDetail.on('scroll', function () {
        paramScrollerCompare.scrollTo(this.x, this.y);

        if (String(autoAssign) === '0') {
          self.onScrollX();
        }
      });
      paramScrollerY.on('scroll', function () {
        if (String(autoAssign) === '1') {
          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), "prePage", function () {
      var ruleDate = _this.props.ruleDate;

      _this.setParentState({
        loadingData: true
      });

      _this.props.getData({
        ruleDate: ruleDate - 24 * 60 * 60 * 1000
      }, function () {
        _this.setParentState({
          loadingData: false
        });
      });
    });

    _defineProperty(_assertThisInitialized(_this), "nextPage", function () {
      var ruleDate = _this.props.ruleDate;

      _this.props.getData({
        ruleDate: ruleDate + 24 * 60 * 60 * 1000
      }, function () {
        _this.setParentState({
          loadingData: true
        });
      });
    });

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

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

    return _this;
  }

  _createClass(PerHourUnit, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      firstTime = 0;
      this.initComponent(this.props);
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      this.initComponent(nextProps);
      var autoAssign = window.$$RESOURCECONTEXT.autoAssign;
      var ruleDate = nextProps.ruleDate;

      if (ruleDate !== this.props.ruleDate) {
        (0, _getUnitHtml.clear)();
        var day = (0, _moment["default"])(ruleDate).format('d') > 4 ? 4 : (0, _moment["default"])(ruleDate).format('d');
        var startX = 0;

        if (String(autoAssign) === '0') {
          startX = -day * document.getElementById('select-date').offsetWidth;
        }

        paramScrollerDetail.refresh();
        paramScrollerY.refresh();
        if (paramScrollerCompare) paramScrollerCompare.scrollTo(startX, 0);
        if (paramScrollerDetail) paramScrollerDetail.scrollTo(startX, 0);
        if (paramScrollerY) paramScrollerY.scrollTo(0, 0);
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      if (firstTime === 0) {
        (0, _getUnitHtml.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,
          ruleDate = props.ruleDate;
      var autoAssign = window.$$RESOURCECONTEXT.autoAssign;
      var unitWitdh = windowWidth * 1 / 4;
      var widthValue = {
        0: 3 / 4,
        1: 3 / 4,
        2: 3 / 8,
        3: 1 / 4
      };

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

      this.setState({
        headerContent: headerContent,
        bodyArray: bodyArray,
        dataDetail: dataDetail,
        ruleDate: ruleDate,
        unitWitdh: unitWitdh
      });
    }
  }, {
    key: "onTouchEnd",
    value: function onTouchEnd() {
      var self = this;
      var autoAssign = window.$$RESOURCECONTEXT.autoAssign;
      var day = (0, _moment["default"])(this.state.ruleDate).format('d');
      var pullDownEl = document.getElementById('pull-down-refresh');
      var pullUpEl = document.getElementById('pull-up-refresh');
      var pullRightEl = document.getElementById('pull-right-refresh');
      var pullLeftEl = document.getElementById('pull-left-refresh');

      if (String(autoAssign) === '0') {
        if (pullRightEl.className.match('flip')) {
          pullRightEl.className = '';
          document.getElementById('pull-right-icon').className = ''; // document.getElementById('pull-right-text').innerHTML = '右拉加载上一页';

          document.getElementById('pull-right-text').innerHTML = '';
          this.setParentState({
            loadingData: true
          });
          self.props.getData({
            ruleDate: self.state.ruleDate - (7 + Number(day)) * 24 * 60 * 60 * 1000
          }, function () {
            self.setParentState({
              loadingData: false
            });
            self.setState({
              row: '',
              col: '',
              clickNum: 0
            });
          });
        } else if (pullLeftEl.className.match('flip')) {
          pullLeftEl.className = '';
          document.getElementById('pull-left-icon').className = ''; // document.getElementById('pull-left-text').innerHTML = '左拉加载上一页';

          document.getElementById('pull-left-text').innerHTML = '';
          this.setParentState({
            loadingData: true
          });
          self.props.getData({
            ruleDate: self.state.ruleDate + (7 - Number(day)) * 24 * 60 * 60 * 1000
          }, function () {
            self.setParentState({
              loadingData: false
            });
            self.setState({
              row: '',
              col: '',
              clickNum: 0
            });
          });
        }
      } else if (String(autoAssign) === '1') {
        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();
        }
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var self = 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;
      var _window$$$RESOURCECON = window.$$RESOURCECONTEXT,
          multiUnit = _window$$$RESOURCECON.multiUnit,
          autoAssign = _window$$$RESOURCECON.autoAssign;
      var bottomHeight = String(autoAssign) === '1' ? 108 : 110;

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

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

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

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