<template>
    <div :class="`${prefix}-date-picker__main`">
        <div :class="`${prefix}-date-picker__title`">
            <div class="prev">
                <i :class="`${prefix}-icon-dprev`"
                   @click="handleClick('prevYear')"></i>
                <i :class="`${prefix}-icon-prev`"
                   @click="handleClick('prevMonth')"></i>
            </div>
            <slot name="title"></slot>
            <div class="next">
                <i :class="`${prefix}-icon-next`"
                   @click="handleClick('nextMonth')"></i>
                <i :class="`${prefix}-icon-dnext`"
                   @click="handleClick('nextYear')"></i>
            </div>
        </div>
        <div :class="`${prefix}-date-picker__body`">
            <slot></slot>
        </div>
    </div>
</template>

<script>
import dayjs from 'dayjs';
import pickerMixins from './mixins';
import { prefix, prefixMixins } from 'jdd-design/utils';
export default {
    name: `${prefix}-picker`,
    mixins: [prefixMixins, pickerMixins],
    data() {
        return {
            viewDate: null
        };
    },
    props: {
        value: Number
    },
    watch: {
        value: function() {
            this.viewDate = this.value;
        }
    },
    methods: {
        handleClick(type) {
            switch (type) {
                case 'prevYear':
                    this.viewDate = this.fixTime(
                        dayjs(this.viewDate).add(-1, 'years')
                    );
                    break;
                case 'prevMonth':
                    this.viewDate = this.fixTime(
                        dayjs(this.viewDate).add(-1, 'months')
                    );
                    break;
                case 'nextYear':
                    this.viewDate = this.fixTime(
                        dayjs(this.viewDate).add(1, 'years')
                    );
                    break;
                case 'nextMonth':
                    this.viewDate = this.fixTime(
                        dayjs(this.viewDate).add(1, 'months')
                    );
                    break;
            }
            this.$emit('input', this.viewDate);
        }
    },
    mounted() {
        this.viewDate = this.value;
    }
};
</script>
