import { getRangeYears, foramtToMonth, formatToString } from './utils';
import "./month-select.css";
import './month-select-sfc.css';

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('div', {
    ref: "body",
    staticClass: "van-calendar__body"
  }, _vm._l(_vm.years, function (y) {
    return _c('div', {
      key: y,
      staticClass: "van-calendar__month"
    }, [_c('div', {
      staticClass: "van-calendar__month-title"
    }, [_vm._v(_vm._s(y) + "年")]), _vm._v(" "), _c('div', {
      staticClass: "van-calendar__days"
    }, _vm._l(_vm.months, function (m) {
      return _c('div', {
        key: m,
        staticClass: "van-calendar__day ks-month__day",
        class: {
          'van-calendar__day--disabled': _vm.currentMin && +new Date(y + "/" + m + "/1") < _vm.currentMin || _vm.currentMax && +new Date(y + "/" + m + "/1") > _vm.currentMax
        },
        on: {
          "click": function click($event) {
            _vm.handleSelect(y, m, _vm.currentMin && +new Date(y + "/" + m + "/1") < _vm.currentMin || _vm.currentMax && +new Date(y + "/" + m + "/1") > _vm.currentMax);
          }
        }
      }, [_c('div', {
        class: {
          'van-calendar__selected-day': +_vm.currentMonth === +new Date(y + "/" + m + "/1")
        }
      }, [_vm._v(_vm._s(m) + "月")])]);
    }), 0)]);
  }), 0);
};

var __vue_staticRenderFns__ = [];
export default {
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: "month-select",
  props: {
    defaultValue: {
      type: [Date, String, Number],
      default: new Date()
    },
    yearRange: {
      type: Array,
      default: function _default() {
        return [new Date().getFullYear() - 5, new Date().getFullYear()];
      }
    },
    horizontal: {
      type: Boolean,
      default: false
    },
    min: [String, Number, Date],
    max: [String, Number, Date]
  },
  data: function data() {
    return {
      years: getRangeYears(this.yearRange),
      months: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      currentMonth: null,
      currentMin: null,
      currentMax: null
    };
  },
  methods: {
    init: function init() {
      this.currentMonth = foramtToMonth(this.defaultValue);
      this.currentMin = this.min ? +foramtToMonth(this.min) : null;
      this.currentMax = this.max ? +foramtToMonth(this.max) : null;
      var monthStr = formatToString(this.currentMonth, 'month');
      var arr = monthStr.split('/');
      this.handleSelect(arr[0], arr[1], false);
      this.scrollInto();
    },
    scrollInto: function scrollInto() {
      var _this = this;

      setTimeout(function () {
        if (_this.currentMonth) {
          var top = _this.$el.querySelector('.van-calendar__selected-day');

          _this.$refs.body.scrollTop = top ? top.getBoundingClientRect().top - 100 : 0;
        }
      }, 240);
    },
    handleSelect: function handleSelect(y, m, disabled) {
      if (!disabled) {
        this.currentMonth = foramtToMonth(y + "/" + m);
        this.$emit("select", {
          date: this.currentMonth,
          str: formatToString(this.currentMonth, 'month')
        });
      }
    }
  }
};