<template>
    <view class="van-calendar__month" :style="computed.getMonthStyle(visible, date, rowHeight)">
        <view v-if="showMonthTitle" class="van-calendar__month-title">
            {{ computed.formatMonthTitle(date) }}
        </view>

        <view v-if="visible" class="van-calendar__days">
            <view v-if="showMark" class="van-calendar__month-mark">
                {{ computed.getMark(date) }}
            </view>

            <view
                :style="computed.getDayStyle(item.type, index, date, rowHeight, color)"
                :class="utils.bem('calendar__day', [item.type]) + ' ' + item.className"
                :data-index="index"
                @tap="onClick"
                v-for="(item, index) in days"
                :key="index"
            >
                <view v-if="item.type === 'selected'" class="van-calendar__selected-day" :style="'background: ' + color">
                    <view v-if="item.topInfo" class="van-calendar__top-info">{{ item.topInfo }}</view>
                    {{ item.text }}
                    <view v-if="item.bottomInfo" class="van-calendar__bottom-info">
                        {{ item.bottomInfo }}
                    </view>
                </view>

                <view v-else>
                    <view v-if="item.topInfo" class="van-calendar__top-info">{{ item.topInfo }}</view>
                    {{ item.text }}
                    <view v-if="item.bottomInfo" class="van-calendar__bottom-info">
                        {{ item.bottomInfo }}
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>
<script module="computed" lang="wxs" src="@/node_modules/@vant/weapp/dist/calendar/components/month/index.wxs"></script>
<script module="utils" lang="wxs" src="@/node_modules/@vant/weapp/dist/wxs/utils.wxs"></script>
<script>
import { VantComponent } from '../../../common/component';
import { getMonthEndDay, compareDay, getPrevDay, getNextDay } from '../../utils';
export default {
    data() {
        return {
            visible: true,
            days: []
        };
    },
    props: {
        date: {
            type: null
        },
        type: {
            type: String
        },
        color: String,
        minDate: {
            type: null
        },
        maxDate: {
            type: null
        },
        showMark: Boolean,
        rowHeight: [Number, String],
        formatter: {
            type: null
        },
        currentDate: {
            type: [null, Array]
        },
        allowSameDay: Boolean,
        showSubtitle: Boolean,
        showMonthTitle: Boolean
    },
    methods: {
        onClick(event) {
            const { index } = event.currentTarget.dataset;
            const item = this.days[index];
            if (item.type !== 'disabled') {
                this.$emit('click', item);
            }
        },
        setDays() {
            const days = [];
            const startDate = new Date(this.date);
            const year = startDate.getFullYear();
            const month = startDate.getMonth();
            const totalDay = getMonthEndDay(startDate.getFullYear(), startDate.getMonth() + 1);
            for (let day = 1; day <= totalDay; day++) {
                const date = new Date(year, month, day);
                const type = this.getDayType(date);
                let config = {
                    date,
                    type,
                    text: day,
                    bottomInfo: this.getBottomInfo(type)
                };
                if (this.formatter) {
                    config = this.formatter(config);
                }
                days.push(config);
            }
            this.setData({
                days
            });
        },
        getMultipleDayType(day) {
            const { currentDate } = this;
            if (!Array.isArray(currentDate)) {
                return '';
            }
            const isSelected = (date) => currentDate.some((item) => compareDay(item, date) === 0);
            if (isSelected(day)) {
                const prevDay = getPrevDay(day);
                const nextDay = getNextDay(day);
                const prevSelected = isSelected(prevDay);
                const nextSelected = isSelected(nextDay);
                if (prevSelected && nextSelected) {
                    return 'multiple-middle';
                }
                if (prevSelected) {
                    return 'end';
                }
                return nextSelected ? 'start' : 'multiple-selected';
            }
            return '';
        },
        getRangeDayType(day) {
            const { currentDate, allowSameDay } = this;
            if (!Array.isArray(currentDate)) {
                return;
            }
            const [startDay, endDay] = currentDate;
            if (!startDay) {
                return;
            }
            const compareToStart = compareDay(day, startDay);
            if (!endDay) {
                return compareToStart === 0 ? 'start' : '';
            }
            const compareToEnd = compareDay(day, endDay);
            if (compareToStart === 0 && compareToEnd === 0 && allowSameDay) {
                return 'start-end';
            }
            if (compareToStart === 0) {
                return 'start';
            }
            if (compareToEnd === 0) {
                return 'end';
            }
            if (compareToStart > 0 && compareToEnd < 0) {
                return 'middle';
            }
        },
        getDayType(day) {
            const { type, minDate, maxDate, currentDate } = this;
            if (compareDay(day, minDate) < 0 || compareDay(day, maxDate) > 0) {
                return 'disabled';
            }
            if (type === 'single') {
                return compareDay(day, currentDate) === 0 ? 'selected' : '';
            }
            if (type === 'multiple') {
                return this.getMultipleDayType(day);
            }
            /* istanbul ignore else */
            if (type === 'range') {
                return this.getRangeDayType(day);
            }
        },
        getBottomInfo(type) {
            if (this.type === 'range') {
                if (type === 'start') {
                    return '开始';
                }
                if (type === 'end') {
                    return '结束';
                }
                if (type === 'start-end') {
                    return '开始/结束';
                }
            }
        }
    },
    watch: {
        date: {
            handler: function () {
                const days = [];
                const startDate = new Date(this.date);
                const year = startDate.getFullYear();
                const month = startDate.getMonth();
                const totalDay = getMonthEndDay(startDate.getFullYear(), startDate.getMonth() + 1);
                for (let day = 1; day <= totalDay; day++) {
                    const date = new Date(year, month, day);
                    const type = this.getDayType(date);
                    let config = {
                        date,
                        type,
                        text: day,
                        bottomInfo: this.getBottomInfo(type)
                    };
                    if (this.formatter) {
                        config = this.formatter(config);
                    }
                    days.push(config);
                }
                this.setData({
                    days
                });
            },

            immediate: true
        },

        type: {
            handler: function () {
                const days = [];
                const startDate = new Date(this.date);
                const year = startDate.getFullYear();
                const month = startDate.getMonth();
                const totalDay = getMonthEndDay(startDate.getFullYear(), startDate.getMonth() + 1);
                for (let day = 1; day <= totalDay; day++) {
                    const date = new Date(year, month, day);
                    const type = this.getDayType(date);
                    let config = {
                        date,
                        type,
                        text: day,
                        bottomInfo: this.getBottomInfo(type)
                    };
                    if (this.formatter) {
                        config = this.formatter(config);
                    }
                    days.push(config);
                }
                this.setData({
                    days
                });
            },

            immediate: true
        },

        minDate: {
            handler: function () {
                const days = [];
                const startDate = new Date(this.date);
                const year = startDate.getFullYear();
                const month = startDate.getMonth();
                const totalDay = getMonthEndDay(startDate.getFullYear(), startDate.getMonth() + 1);
                for (let day = 1; day <= totalDay; day++) {
                    const date = new Date(year, month, day);
                    const type = this.getDayType(date);
                    let config = {
                        date,
                        type,
                        text: day,
                        bottomInfo: this.getBottomInfo(type)
                    };
                    if (this.formatter) {
                        config = this.formatter(config);
                    }
                    days.push(config);
                }
                this.setData({
                    days
                });
            },

            immediate: true
        },

        maxDate: {
            handler: function () {
                const days = [];
                const startDate = new Date(this.date);
                const year = startDate.getFullYear();
                const month = startDate.getMonth();
                const totalDay = getMonthEndDay(startDate.getFullYear(), startDate.getMonth() + 1);
                for (let day = 1; day <= totalDay; day++) {
                    const date = new Date(year, month, day);
                    const type = this.getDayType(date);
                    let config = {
                        date,
                        type,
                        text: day,
                        bottomInfo: this.getBottomInfo(type)
                    };
                    if (this.formatter) {
                        config = this.formatter(config);
                    }
                    days.push(config);
                }
                this.setData({
                    days
                });
            },

            immediate: true
        },

        formatter: {
            handler: function () {
                const days = [];
                const startDate = new Date(this.date);
                const year = startDate.getFullYear();
                const month = startDate.getMonth();
                const totalDay = getMonthEndDay(startDate.getFullYear(), startDate.getMonth() + 1);
                for (let day = 1; day <= totalDay; day++) {
                    const date = new Date(year, month, day);
                    const type = this.getDayType(date);
                    let config = {
                        date,
                        type,
                        text: day,
                        bottomInfo: this.getBottomInfo(type)
                    };
                    if (this.formatter) {
                        config = this.formatter(config);
                    }
                    days.push(config);
                }
                this.setData({
                    days
                });
            },

            immediate: true
        },

        currentDate: {
            handler: function () {
                const days = [];
                const startDate = new Date(this.date);
                const year = startDate.getFullYear();
                const month = startDate.getMonth();
                const totalDay = getMonthEndDay(startDate.getFullYear(), startDate.getMonth() + 1);
                for (let day = 1; day <= totalDay; day++) {
                    const date = new Date(year, month, day);
                    const type = this.getDayType(date);
                    let config = {
                        date,
                        type,
                        text: day,
                        bottomInfo: this.getBottomInfo(type)
                    };
                    if (this.formatter) {
                        config = this.formatter(config);
                    }
                    days.push(config);
                }
                this.setData({
                    days
                });
            },

            immediate: true
        }
    }
};
</script>
<style>
@import './index.css';
</style>
