"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; 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); }

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

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

var _one = require("remax/one");

var _classnames = _interopRequireDefault(require("classnames"));

var _common = require("./../common");

var _button = _interopRequireDefault(require("../button"));

var _popup = _interopRequireDefault(require("../popup"));

var _icon = _interopRequireDefault(require("../icon"));

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

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

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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var prefixCls = (0, _common.getPrefixCls)('calendar');

var Calendar = function Calendar(props) {
  var _props$confirmText = props.confirmText,
      confirmText = _props$confirmText === void 0 ? '确定' : _props$confirmText,
      show = props.show,
      _props$type = props.type,
      type = _props$type === void 0 ? "single" : _props$type,
      _props$showFoot = props.showFoot,
      showFoot = _props$showFoot === void 0 ? false : _props$showFoot,
      _props$poppable = props.poppable,
      poppable = _props$poppable === void 0 ? true : _props$poppable,
      defaultDate = props.defaultDate,
      minDate = props.minDate,
      maxDate = props.maxDate,
      _props$position = props.position,
      position = _props$position === void 0 ? 'bottom' : _props$position,
      _onTap = props.onTap,
      onChange = props.onChange,
      _onClose = props.onClose;
  var MONTH_DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  var WEEK_NAMES = ['日', '一', '二', '三', '四', '五', '六'];
  var LINES = [1, 2, 3, 4, 5, 6];

  var _useState = (0, _react.useState)(0),
      _useState2 = _slicedToArray(_useState, 2),
      year = _useState2[0],
      setLoinsYear = _useState2[1];

  var _useState3 = (0, _react.useState)(0),
      _useState4 = _slicedToArray(_useState3, 2),
      month = _useState4[0],
      seLoinstMonth = _useState4[1];

  var _useState5 = (0, _react.useState)(0),
      _useState6 = _slicedToArray(_useState5, 2),
      days = _useState6[0],
      seLoinstDays = _useState6[1];

  var _useState7 = (0, _react.useState)(new Date()),
      _useState8 = _slicedToArray(_useState7, 2),
      currentDate = _useState8[0],
      setcurrentDate = _useState8[1];

  var _useState9 = (0, _react.useState)(false),
      _useState10 = _slicedToArray(_useState9, 2),
      tag = _useState10[0],
      setTag = _useState10[1];

  var _useState11 = (0, _react.useState)([]),
      _useState12 = _slicedToArray(_useState11, 2),
      change = _useState12[0],
      setChange = _useState12[1]; //获取当前月份


  var getMonth = function getMonth(date) {
    return date.getMonth();
  }; //获取当前年份


  var getFullYear = function getFullYear(date) {
    return date.getFullYear();
  };

  var getCurrentMonthDays = function getCurrentMonthDays(month, year) {
    var _year = year + currentDate.getFullYear();

    if (_year % 100 != 0 && _year % 4 == 0 || _year % 400 == 0) {
      MONTH_DAYS = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    }

    return MONTH_DAYS[month];
  }; //当前月第一天是周几


  var getDateByYearMonth = function getDateByYearMonth(year, month) {
    var day = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
    var date = new Date();
    date.setFullYear(year);
    date.setMonth(month, day);
    return date;
  };

  var getWeeksByFirstDay = function getWeeksByFirstDay(year, month) {
    var date = getDateByYearMonth(year, month);
    return date.getDay();
  };

  var getDayText = function getDayText(line, weekIndex, weekDay, monthDays) {
    var _classNames;

    var now_date = new Date();
    var is_now = false; //今天

    var is_change = false; //选中

    var is_disable = false; //禁用

    var day_off = false; //休息日

    var number = line * 7 + weekIndex - weekDay + 1;

    if (number <= 0 || number > monthDays) {
      return /*#__PURE__*/_react.default.createElement(_one.View, {
        className: "day-c",
        key: weekIndex
      }, "\xA0");
    }

    if (number === now_date.getDate() && formatNumber(month) === formatNumber(getMonth(now_date))) {
      is_now = true;
    }

    var new_day = "".concat(currentDate.getFullYear(), "-").concat(formatNumber(month), "-").concat(number < 10 ? "0".concat(number) : number);

    if (!is_change) {
      for (var i = 0; i < change.length; i++) {
        var element = change[i];

        if (new_day === element) {
          is_change = true;
          break;
        }
      }
    }

    if (minDate || maxDate) {
      var min_date, max_date;

      var _now_date = new Date(new_day);

      if (minDate) {
        min_date = new Date(minDate);

        if (min_date > _now_date) {
          is_disable = true;
        }
      }

      if (maxDate) {
        max_date = new Date(maxDate);

        if (max_date < _now_date) {
          is_disable = true;
        }
      }
    }

    if (weekIndex === 6 || weekIndex === 0) {
      day_off = true;
    }

    return /*#__PURE__*/_react.default.createElement(_one.View, {
      className: (0, _classnames.default)("day-c", (_classNames = {}, _defineProperty(_classNames, "now", is_now), _defineProperty(_classNames, "active", is_change), _defineProperty(_classNames, "disable", is_disable), _defineProperty(_classNames, "day_off", day_off), _classNames)),
      key: weekIndex
    }, /*#__PURE__*/_react.default.createElement(_one.View, {
      className: "day",
      onTap: function onTap() {
        if (is_disable) {
          return;
        }

        if (_onTap) {
          _onTap(new_day);
        }

        if (type === 'single') {
          setChange([new_day]);
        }
      }
    }, number));
  };

  var setCurrentYearMonth = function setCurrentYearMonth(date) {
    var month = getMonth(date);
    var year = getFullYear(date);
    setLoinsYear(year);
    seLoinstMonth(month);
    setTag(false);
  };

  var monthChange = function monthChange(monthChanged) {
    if (tag) {
      return;
    } else {
      setTag(true);
    }

    var monthAfter = month + monthChanged;
    var date = getDateByYearMonth(year, monthAfter);
    setCurrentYearMonth(date);
  };

  var formatNumber = function formatNumber(num) {
    var _num = num + 1;

    return _num < 10 ? "0".concat(_num) : "".concat(_num);
  };

  var weekDay = getWeeksByFirstDay(year, month);
  var _startX = 0;

  var init = function init() {
    var new_date = new Date();

    if (defaultDate) {
      new_date = new Date(defaultDate);
    }

    var new_month = getMonth(new_date);
    var new_days = new_date.getDay();
    setcurrentDate(new_date);
    seLoinstMonth(Number(new_month));
    seLoinstDays(new_days);
  };

  (0, _react.useEffect)(function () {
    init();
  }, []);

  var renderFoot = function renderFoot() {
    if (!showFoot) {
      return null;
    }

    return /*#__PURE__*/_react.default.createElement(_one.View, {
      className: "".concat(prefixCls, "-footer")
    }, /*#__PURE__*/_react.default.createElement(_button.default, {
      block: true,
      type: "primary",
      className: "".concat(prefixCls, "-confirm"),
      onTap: function onTap() {
        if (onChange) {
          onChange(change);
        }
      }
    }, confirmText));
  };

  var renderCalendar = function renderCalendar() {
    return /*#__PURE__*/_react.default.createElement(_one.View, {
      className: prefixCls,
      onTouchEnd: function onTouchEnd(val) {
        if (_startX > val.changedTouches[0]['clientX'] + 30) {
          monthChange(1);
        }

        if (_startX < val.changedTouches[0]['clientX'] - 30) {
          monthChange(-1);
        }
      },
      onTouchStart: function onTouchStart(val) {
        _startX = val.changedTouches[0]['clientX'];
      }
    }, /*#__PURE__*/_react.default.createElement(_one.View, {
      className: "".concat(prefixCls, "-tabbar")
    }, /*#__PURE__*/_react.default.createElement(_one.View, {
      onTap: function onTap() {
        monthChange(-1);
      }
    }, /*#__PURE__*/_react.default.createElement(_icon.default, {
      type: 'back',
      size: '30',
      color: '#297AF8'
    })), /*#__PURE__*/_react.default.createElement(_one.View, {
      className: "".concat(prefixCls, "-title")
    }, year + currentDate.getFullYear(), " \u5E74 ", formatNumber(month), "\u6708"), /*#__PURE__*/_react.default.createElement(_one.View, {
      onTap: function onTap() {
        monthChange(1);
      }
    }, /*#__PURE__*/_react.default.createElement(_icon.default, {
      type: 'right',
      size: '30',
      color: '#297AF8'
    }))), /*#__PURE__*/_react.default.createElement(_one.View, {
      className: "".concat(prefixCls, "-weekdays")
    }, WEEK_NAMES.map(function (week, key) {
      return /*#__PURE__*/_react.default.createElement(_one.View, {
        className: "".concat(prefixCls, "-weekday"),
        key: key
      }, week);
    })), LINES.map(function (l, key) {
      return /*#__PURE__*/_react.default.createElement(_one.View, {
        key: key,
        className: "day-content"
      }, WEEK_NAMES.map(function (week, index) {
        return getDayText(key, index, weekDay, getCurrentMonthDays(month, year));
      }));
    }), renderFoot());
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, poppable ? /*#__PURE__*/_react.default.createElement(_popup.default, {
    overlay: show,
    position: position,
    onClose: function onClose() {
      _onClose === null || _onClose === void 0 ? void 0 : _onClose();
    },
    style: {
      height: showFoot ? '75%' : '65%'
    }
  }, renderCalendar()) : renderCalendar());
};

var _default = Calendar;
exports.default = _default;