<template>
    <van-picker
        :columns="columns"
        show-toolbar
        @cancel="$listeners.close()"
        @confirm="onTeachingConfirm"
    />
</template>

<script>
let minutes = []; // 分钟数
for (let i = 0; i < 60; i++) {
    let mi = i < 10 ? "0" + i : i;
    minutes.push(mi + "分");
}
export default {
    props: ['scheduleList', 'singleClassMinutes'],
    data() {
        return {
            // 排课弹窗
            columns: [{
                // 课程选项
                values: ["线上", "线下"],
                className: "type",
            }, {
                values: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"],
                className: "week",
            }, {
                values: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
                className: "hours",
                defaultIndex: 7,
            }, {
                values: minutes,
                className: "minutes",
            }],
        }
    },
    watch: {
        'singleClassMinutes'() {
            this.scheduleList = []
        }
    },
    methods: {
        onTeachingConfirm(value) {
            // 添加课程
            let scheduleList = this.scheduleList;
            let startTime =
                (value[2] >= 10 ? value[2] : "0" + value[2]) +
                ":" +
                value[3].split("分")[0];
            let endTime = this.MinutesTest(
                value[2],
                value[3],
                this.singleClassMinutes
            );

            let isAdd = true;
            scheduleList.forEach((item) => {
                let isStartTime = this.timeIsRange(startTime, endTime, item.startTime);
                let isEndTime = this.timeIsRange(startTime, endTime, item.endTime);
                if (isAdd) {
                    if (value[1] == item.weekStr) {
                        if (isStartTime || isEndTime) {
                            isAdd = false;
                        } else {
                            isAdd = true;
                        }
                    } else if (value[1] != item.weekStr) {
                        isAdd = true;
                    }
                }
            });

            // 判断结束时间
            const endTime1 = endTime ? Number(endTime.split(":")[0]) : 0
            const endTime2 = endTime ? Number(endTime.split(":")[1]) : 0
            if(endTime1 >= 21 && endTime2 > 0) {
                this.$toast("课程安排结束时间超过21:00");
                return;
            }

            if (isAdd) {
                // 判断时间范围是否有重复
                scheduleList.push({
                    type: value[0], // 线上还是线下
                    weekStr: value[1],
                    weekIndex: this.getWeek(value[1]),
                    startTime: startTime,
                    endTime: endTime,
                    id: Date.now(),
                });

                // this.courseForm.teachingStatus = false;
                this.$listeners.close()

                // this.setTimeTable();
            } else {
                this.$toast("该时间段已排课请重选时间");
                return;
            }
        },
        // 分钟小时相加减
        MinutesTest(hour, mins, interval) {
            let min = mins.split("分")[0];
            let sDate1 = new Date(1900, 1, 1, hour, min);
            sDate1.setMinutes(sDate1.getMinutes() + parseInt(interval));
            let H = sDate1.getHours();
            let M = sDate1.getMinutes();
            if (H < 10) H = "0" + H;
            if (M < 10) M = "0" + M;
            return H + ":" + M;
        },
        // 判断时间是否在时间段内
        timeIsRange(beginTime, endTime, nowTime) {
            var strB = beginTime.split(":");
            if (strB.length != 2) {
                return false;
            }
            var strE = endTime.split(":");
            if (strE.length != 2) {
                return false;
            }
            var strN = nowTime.split(":");
            if (strE.length != 2) {
                return false;
            }
            var b = new Date();
            var e = new Date();
            var n = new Date();
            b.setHours(strB[0]);
            b.setMinutes(strB[1]);
            e.setHours(strE[0]);
            e.setMinutes(strE[1]);
            n.setHours(strN[0]);
            n.setMinutes(strN[1]);

            if (n.getTime() - b.getTime() >= 0 && n.getTime() - e.getTime() <= 0) {
                // 在时间范围内
                return true;
            } else {
                // 不在时间范围内
                return false;
            }
        },
        getWeek(str) {
            // 获取周几索引值
            let template = {
                '周一': 1,
                '周二': 2,
                '周三': 3,
                '周四': 4,
                '周五': 5,
                '周六': 6,
                '周日': 0,
            };
            return template[str];
        },
    }
}
</script>