保誠-保戶業務員媒合平台
wayne
2022-02-17 a3716f72066d25d745f4d5103ff23a553c3e102b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<template>
    <el-time-select
        class="pam-time cursor--pointer"
        popper-class="pam-time-popper"
        v-model="timeValue"
        :clearable="false"
        :editable="false"
        :picker-options="pickerOptions"
        placeholder="選擇時間"
        prefix-icon="icon-down down-icon"
        value-format="timestamp"
        @change="changeTime"
    >
    </el-time-select>
</template>
 
<script lang="ts">
import { Component, Emit, Prop, Vue, Watch } from "nuxt-property-decorator";
 
@Component
export default class UiTimePicker extends Vue {
    timeValue = '';
 
    @Prop()
    defaultValue!: string;
 
    @Prop({default: ''})
    changeDate!: Date | string;
 
    @Prop()
    isPastDateDisabled!: boolean;
 
    @Prop()
    isFutureDateDisabled!: boolean;
 
    ///////////////////////////////////////////////////////////////////////
 
    @Emit('changeTime')
    changeTime() {
        return this.timeValue;
    }
 
    @Watch('defaultValue', {immediate: true})
    updateDefault() {
        if (this.defaultValue) {
            const defaultDate = new Date(this.defaultValue);
            this.timeValue = this.formatTimeString(defaultDate);
            this.changeTime();
        }
    }
 
    ///////////////////////////////////////////////////////////////////////
 
    get pickerOptions() {
        let minTime = '';
        let maxTime = '';
        const currentDate = new Date();
 
        if (this.changeDate && this.isPickedToday(currentDate)) {
 
            if (this.isPastDateDisabled) {
                minTime = this.formatTimeString(currentDate);
                this.isPickedDisableTime(currentDate, minTime);
            }
 
            if (this.isFutureDateDisabled) {
                maxTime = this.formatTimeString(currentDate);
                this.isPickedDisableTime(currentDate, maxTime);
            }
 
        }
 
        return {
            start: '09:00',
            step: '00:15',
            end: '21:00',
            minTime: minTime,
            maxTime: maxTime
        }
    }
 
    private isPickedDisableTime(currentDate: Date, minMaxTime: string) {
        const currentTime = this.getTimeValue(currentDate, minMaxTime);
        const pickedTime = this.getTimeValue(currentDate, this.timeValue);
        if (this.isPastDateDisabled && pickedTime < currentTime) {
            this.timeValue = '';
            this.changeTime();
        }
        if (this.isFutureDateDisabled && currentTime < pickedTime) {
            this.timeValue = '';
            this.changeTime();
        }
    }
 
    private isPickedToday(currentDate: Date) {
        const pickedDate = new Date(this.changeDate);
        const today = this.formatDateString(currentDate);
        const picked = this.formatDateString(pickedDate);
        return today === picked;
    }
 
    private getTimeValue(date: Date, time: string) {
        const hour = time.split(':')[0];
        const minute = time.split(':')[1];
        return date.setHours(+hour, +minute);
    }
 
    private formatDateString(date: Date) {
        return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
    }
 
    private formatTimeString(date: Date) {
        const hours = date.getHours();
        const minutes = date.getMinutes();
        return `${hours < 10 ? '0' + hours : hours}:${minutes < 10 ? '0' + minutes : minutes}`;
    }
}
</script>