
const { Calendar } = require('ant-design-vue')
import cascaderHeader from 'ant-design-vue/es/calendar/Header';
import WEEK from './week.vue';
import PropTypes from 'ant-design-vue/es/_util/vue-types';


export default function (Vue) {
  cascaderHeader.mixins = [{
    data() {
      return {
        count:0
      }
    }
  }]
  cascaderHeader.props.mode = PropTypes.oneOf(['time', 'date', 'month', 'year', 'decade', 'week']);

  cascaderHeader.render = (function (fn) {
    return function () {
      let _this = this
      const { type, value } = this.$props;
      const { _c, _v, } = this
      let vnode = fn.apply(this, arguments);
      vnode.data.style = {
        display: 'flex',
        alignItems: 'center'
      };

      let tltleMonth = this.$parent.$parent.$parent.tltleMonth
      vnode.children.unshift(_c('div', { class: 'ant-card-head-title', style: { textAlign: 'left', paddingLeft: '15px', fontSize: '18px', fontWeight: 'bold' } }, [_v(tltleMonth)]))
      return vnode
    }
  })(cascaderHeader.render);

  cascaderHeader.methods.getTypeSwitch = function () {
    const { locale = {}, type, fullscreen } = this.$props;
    const size = fullscreen ? 'default' : 'small';
    let _this = this;
    const { _c, _v, onInternalTypeChange } = this
    return _c(
      'ARadioGroup', {
      attrs: {
        value: type
      },
      on: {
        change(event) {
          if (event.target.value == 'week') {
            _this.count = 0
          }
          onInternalTypeChange.call(this,event)

        }
      }
    }, [
        _c('ARadioButton', { attrs: { value: 'week' } }, [_v('周')]),
        _c('ARadioButton', { attrs: { value: 'month' } }, [_v(locale.month)]),

    ]
    )
  }


  cascaderHeader.methods.getMonthYearSelections = (function (fn) {
    return function (getPrefixCls) {
      let _this = this
      const { type, value } = this.$props;
      const { _c, _v } = this
      let { yearReactNode, monthReactNode } = fn.apply(this, arguments);

      if (type == 'week') {
        yearReactNode = _c(
          'ARadioGroup', {
          attrs: {
            value: this.currentWeek,
          },
          on: {
            change(e) {
              let target= e.target.value
              if (target === 0) {
                _this.count = 0
              } else if (target === 1) {
                _this.count += 1
              } else if (target === -1) {
                _this.count -= 1
              }
              _this.$emit('valueChange', value.clone(), { count:_this.count,target })
            }
          }
        }, [
          _c('ARadioButton', { attrs: { value: 1 } }, [_c('a-icon', { attrs: { type: 'left' } })]),
          _c('ARadioButton', { attrs: { value: 0 } }, [_v('本周')]),
          _c('ARadioButton', { attrs: { value: -1 } }, [_c('a-icon', { attrs: { type: 'right' } })])
        ]
        )
      }
      return { yearReactNode, monthReactNode }
    }

  })(cascaderHeader.methods.getMonthYearSelections)

  Calendar.props.mode = PropTypes.oneOf(['month', 'year','week']);
  Calendar.props.spinning = PropTypes.boolean
  Calendar.mixins.push({
    data() {
      return {
        tltleMonth: this.moment().format('YYYY年 MMM'),
        currentWeek: { count: 0, target: 0 }
      }
    }
  })
  Calendar.methods.onHeaderValueChange = function (value, currentWeek) {

    if (this.sMode === 'week') {
      const {target,count }=currentWeek
      this.currentWeek.target = target
      this.currentWeek.count = count

      this.setValue(this.moment(), 'changePanel');

    } else {
      this.setValue(value, 'changePanel');
    }

  }

  Calendar.methods.renderCalendar = (function (fn) {
    return function (locale, localeCode) {
      const { sMode, sValue, currentWeek,spinning } = this
      let _this = this
      if (sValue && localeCode) {
        sValue.locale(localeCode);
      }
      let _current = sValue.clone()
      const vNode = fn.apply(this, arguments)
      let tltleMonth = _current.format('YYYY年 MMM');

      if (sMode == 'week') {
        let { target,count} = currentWeek
        if (target === 0) {
          tltleMonth =_current.format('YYYY年 MMM');

        } else if (target === 1) {
          tltleMonth = _current.startOf('week').subtract(count, 'week').format('YYYY年 MMM');
        } else if (target === -1) {
          tltleMonth = _current.endOf('week').subtract(count, 'week').format('YYYY年 MMM');
        }
        vNode.children[1] = this._c(WEEK, {
          attrs: {
            currentWeek: count,
            value: sValue
          },
          scopedSlots: {
            dateCellRender: this.$scopedSlots.dateCellRender
          },
          on: {
            toggleWeek(momentList) {
              _this.$emit('toggleWeek', momentList)
            }
          }

        })
      }
      this.tltleMonth = String(tltleMonth)

      return this._c('a-spin', {
        attrs: {
          spinning
      }},[vNode])
    }
  })(Calendar.methods.renderCalendar)



}

