import { getYearsAndWeeks } from './utils';
import "./week-select.css";
import './week-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.year,
      staticClass: "van-calendar__month"
    }, [_c('div', {
      staticClass: "van-calendar__month-title"
    }, [_vm._v(_vm._s(y.year) + "年")]), _vm._v(" "), _c('div', {
      staticClass: "van-calendar__days"
    }, _vm._l(y.weeks, function (w, i) {
      return _c('div', {
        key: i,
        staticClass: "van-calendar__day ks-week__day",
        class: {
          'ks-week__selected-day': y.year === _vm.currentYear && i + 1 === _vm.currentWeek
        },
        on: {
          "click": function click($event) {
            return _vm.handleSelect(y.year, i + 1, w);
          }
        }
      }, [_c('div', [_vm._v(_vm._s(String(i + 1).length > 1 ? i + 1 : '0' + (i + 1)) + "周(" + _vm._s(w.startDayStr) + "~" + _vm._s(w.endDayStr) + ")")])]);
    }), 0)]);
  }), 0);
};

var __vue_staticRenderFns__ = [];
export default {
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: 'week-select',
  props: {
    defaultValue: {
      type: [Date, String, Number],
      default: new Date()
    },
    yearRange: {
      type: Array,
      default: function _default() {
        return [new Date().getFullYear() - 2, new Date().getFullYear()];
      }
    },
    horizontal: {
      type: Boolean,
      default: false
    },
    firstDayOfWeek: {
      type: Number,
      default: 1
    }
  },
  data: function data() {
    return {
      years: getYearsAndWeeks(this.yearRange, this.firstDayOfWeek),
      currentYear: null,
      currentWeek: null,
      currentStart: null,
      currentEnd: null
    };
  },
  methods: {
    init: function init() {
      var _date = new Date(this.defaultValue);

      var y = _date.getFullYear();

      var _year = this.years.find(function (item) {
        return item.year === y;
      });

      var _index = _year.weeks.findIndex(function (item) {
        return +item.startDay <= +_date && +item.endDay >= +_date;
      });

      var _week = _year.weeks[_index];
      this.handleSelect(y, _index + 1, _week);
      this.scrollInto();
    },
    scrollInto: function scrollInto() {
      var _this = this;

      setTimeout(function () {
        var top = _this.$el.querySelector('.ks-week__selected-day');

        _this.$refs.body.scrollTop = top ? top.getBoundingClientRect().top - 100 : 0;
      }, 240);
    },
    handleSelect: function handleSelect(y, w, week) {
      this.currentYear = y;
      this.currentWeek = w;
      this.currentStart = week.startDay;
      this.currentEnd = week.endDay;
      this.$emit('select', {
        year: y,
        week: week,
        num: w
      });
    }
  }
};