import Button from "../button";
import Popup from "../popup";
import "./index.css"; // const getNowQuarter = () => {
//   let _date = new Date()
//   let y = _date.getFullYear()
//   let m = _date.getMonth()
//   if (m < 3) {
//     return `${y}-1`
//   } else if (m < 6) {
//     return `${y}-2`
//   } else if (m < 9) {
//     return `${y}-3`
//   } else if (m < 12) {
//     return `${y}-4`
//   }
// }

var getYears = function getYears(arr) {
  var num = arr[1];
  var years = [];

  while (num >= arr[0]) {
    years.unshift(num);
    num = num - 1;
  }

  return years;
};

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

  var _h = _vm.$createElement;

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

  return _c('Popup', {
    staticClass: "ks-quarter-popup",
    attrs: {
      "value": _vm.value,
      "position": "bottom",
      "get-container": "body",
      "close-icon-position": "top-left",
      "close-icon": "arrow-left",
      "duration": "0.2",
      "closeable": true
    },
    on: {
      "input": _vm.handleClosePopup,
      "open": _vm.handleOpenPopup
    }
  }, [_c('div', {
    class: ['ks-quarter', {
      'ks-quarter-horizontal': _vm.horizontal
    }]
  }, [_c('div', {
    ref: "body",
    staticClass: "van-calendar__body ks-quarter-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.quarters, function (q) {
      return _c('div', {
        key: q,
        staticClass: "van-calendar__day ks-quarter__day",
        on: {
          "click": function click($event) {
            return _vm.handleSelectQuarter(y, q);
          }
        }
      }, [_c('div', {
        class: {
          'select-quarter': y === _vm.numYear && q === _vm.numQuarter
        }
      }, [_vm._v("\n              " + _vm._s(_vm.cnQuarters[q]) + "\n            ")])]);
    }), 0)]);
  }), 0), _vm._v(" "), _c('div', {
    staticClass: "ks-quarter-header"
  }, [_c('span', {
    staticClass: "ks-quarter-header__title"
  }, [_vm._v("季度选择")]), _vm._v(" "), _c('span', {
    staticClass: "ks-quarter-header__daterange"
  }, [_vm._v(_vm._s(_vm.quarterStr))])]), _vm._v(" "), _vm.showConfirm ? _c('div', {
    staticClass: "van-calendar__footer van-calendar__footer--safe-area-inset-bottom"
  }, [_c('Button', {
    staticClass: "van-calendar__confirm",
    attrs: {
      "type": "primary",
      "disabled": !_vm.currentQuarter,
      "round": "",
      "block": ""
    },
    on: {
      "click": _vm.handleSelect
    }
  }, [_vm._v("\n        确定\n      ")])], 1) : _vm._e()])]);
};

var __vue_staticRenderFns__ = [];
export default {
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: "ks-quarter",
  components: {
    Button: Button,
    Popup: Popup
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    defaultQuarter: {
      type: String,
      default: ""
    },
    yearRange: {
      type: Array,
      default: function _default() {
        return [new Date().getFullYear() - 2, new Date().getFullYear()];
      }
    },
    horizontal: {
      type: Boolean,
      default: false
    },
    showConfirm: {
      type: Boolean,
      default: true
    }
  },
  data: function data() {
    return {
      quarters: [1, 2, 3, 4],
      years: getYears(this.yearRange),
      cnQuarters: {
        1: "一季度",
        2: "二季度",
        3: "三季度",
        4: "四季度"
      },
      currentQuarter: this.defaultQuarter
    };
  },
  computed: {
    numYear: function numYear() {
      return Number(this.currentQuarter.split("-")[0]);
    },
    numQuarter: function numQuarter() {
      return Number(this.currentQuarter.split("-")[1]);
    },
    quarterStr: function quarterStr() {
      return this.numYear + "\u5E74 " + this.cnQuarters[this.numQuarter];
    }
  },
  methods: {
    handleSelectQuarter: function handleSelectQuarter(y, q) {
      this.currentQuarter = y + "-" + q;
      this.$emit("select", this.currentQuarter, this.quarterStr);
    },
    handleSelect: function handleSelect() {
      this.$emit("confirm", this.currentQuarter, this.quarterStr);
    },
    handleClosePopup: function handleClosePopup(e) {
      this.$emit("input", e);
      this.scrollClear();
    },
    handleOpenPopup: function handleOpenPopup() {
      var _this = this;

      this.currentQuarter = this.defaultQuarter;
      this.$nextTick(function () {
        _this.scrollInto();
      });
    },
    scrollInto: function scrollInto() {
      var _this2 = this;

      setTimeout(function () {
        if (_this2.currentQuarter) {
          var top = _this2.$el.querySelector(".select-quarter");

          _this2.$refs.body.scrollTop = top && top.getBoundingClientRect().top - 120;
        }
      }, 240);
    },
    scrollClear: function scrollClear() {
      this.$refs.body.scrollTop = 0;
    }
  }
};