<template>
    <!-- 首页mini课表，暂时不用 -->
    <div class="home-cus-plan dark-iview-table">
        <Table :columns="columns" :data="cusData" border>
            <!--星期一-->
            <template slot-scope="{ row, index }" slot="MON">
                <div v-if="row.MON == 1" class="active-cell" @click="toDetail">
                    <!-- <span class="course-dot"></span> -->
                    <Icon type="md-checkmark" class="has-cus-icon"/>
                </div>
            </template>
            <!--星期二-->
            <template slot-scope="{ row, index }" slot="TUE">
                <div v-if="row.TUE == 1" class="active-cell" @click="toDetail">
                    <!-- <span class="course-dot"></span> -->
                    <Icon type="md-checkmark" class="has-cus-icon"/>
                </div>
            </template>
            <!--星期三-->
            <template slot-scope="{ row, index }" slot="WED">
                <div v-if="row.WED == 1" class="active-cell" @click="toDetail">
                    <!-- <span class="course-dot"></span> -->
                    <Icon type="md-checkmark" class="has-cus-icon"/>
                </div>
            </template>
            <!--星期四-->
            <template slot-scope="{ row, index }" slot="THU">
                <div v-if="row.THU == 1" class="active-cell" @click="toDetail">
                    <!-- <span class="course-dot"></span> -->
                    <Icon type="md-checkmark" class="has-cus-icon"/>
                </div>
            </template>
            <!--星期五-->
            <template slot-scope="{ row, index }" slot="FRI">
                <div v-if="row.FRI == 1" class="active-cell" @click="toDetail">
                    <!-- <span class="course-dot"></span> -->
                    <Icon type="md-checkmark" class="has-cus-icon"/>
                </div>
            </template>
            <!--星期六-->
            <template slot-scope="{ row, index }" slot="SAT">
                <div v-if="row.SAT == 1" class="active-cell" @click="toDetail">
                    <!-- <span class="course-dot"></span> -->
                    <Icon type="md-checkmark" class="has-cus-icon"/>
                </div>
            </template>
            <!--星期日-->
            <template slot-scope="{ row, index }" slot="SUN">
                <div v-if="row.SUN == 1" class="active-cell" @click="toDetail">
                    <!-- <span class="course-dot"></span> -->
                    <Icon type="md-checkmark" class="has-cus-icon"/>
                </div>
            </template>
        </Table>
    </div>
</template>
<script>
export default {
    data() {
        return {
            classList: [],
            schoolBase: {
                period: []
            },
            courseList: [],
            courseListShow: [],
            fullCus: [],
            columns: [
                {
                    title: ' ',
                    type: 'index',
                    align: 'center'
                },
                {
                    title: '一',
                    slot: 'MON',
                    align: 'center'
                },
                {
                    title: '二',
                    slot: 'TUE',
                    align: 'center'
                },
                {
                    title: '三',
                    slot: 'WED',
                    align: 'center'
                },
                {
                    title: '四',
                    slot: 'THU',
                    align: 'center'
                },
                {
                    title: '五',
                    slot: 'FRI',
                    align: 'center'
                },
                {
                    title: '六',
                    slot: 'SAT',
                    align: 'center'
                },
                {
                    title: '日',
                    slot: 'SUN',
                    align: 'center'
                }
            ],
            cusData: [],
            tmwCus:[]
        }
    },
    methods: {
        toDetail(){
            this.$router.push({
                name:'myCourse'
            })
        },
        //获取个人和校本课程列表，API调整后没有进行分开处理
        getCourseList() {
            this.listLoading = true
            let requestData = {
                'code': this.$store.state.userInfo.TEAMModelId,
                'schoolId': this.$store.state.userInfo.schoolCode,
                'scope': 'private'
            }
            this.$api.courseMgmt.findCourse(requestData).then(
                (res) => {
                    if (!res.error) {
                        this.courseList = res.courses
                        //过滤校本课程 
                        this.courseListShow = this.courseList.filter(item => {
                            return item.scope == 'school'
                        })
                        let promises = []
                        this.courseListShow.forEach(item => {
                            let requestData = {
                                'code': this.$store.state.userInfo.schoolCode,
                                'scope': 'school',
                                'id': item.id
                            }
                            promises.push(this.$api.courseMgmt.findCusInfo(requestData))
                        })
                        Promise.all(promises).then(
                            async resAll => {
                                for (const res of resAll) {
                                    if (res.courses && res.courses.length > 0) {
                                        res.courses[0].schedule = res.courses[0].schedule ? res.courses[0].schedule : []
                                        //过滤当前教师的schedule
                                        res.courses[0].schedule = res.courses[0].schedule.filter(item => {
                                            return item.teacherId == this.$store.state.userInfo.TEAMModelId
                                        })
                                        // 获取自定义名单信息
                                        let ids = res.courses[0].schedule.map(item => {
                                            return item.stulist
                                        })
                                        for (let i = 0; i < ids.length; i++) {
                                            if (!ids[i]) {
                                                ids.splice(i, 1)
                                                i--
                                            }
                                        }
                                        let stuList = []
                                        if (ids.length) {
                                            try {
                                                let listRes = await this.getListInfo([...ids])
                                                if (listRes) stuList.push(...listRes.stuList)
                                            } catch (e) {
                                                this.$Message.error('API ERROR!')
                                            }
                                        }

                                        res.courses[0].schedule.forEach(item => {
                                            //补充教室信息
                                            if (item.classId) {
                                                let classInfo = this.classList.find(classItem => {
                                                    return classItem.id == item.classId
                                                })
                                                item.classInfo = {
                                                    id: item.classId,
                                                    name: classInfo ? classInfo.name : '--'
                                                }
                                            }
                                            //补充名单信息
                                            if (ids.length && item.stulist) {
                                                let listInfo = stuList.find(listItem => {
                                                    return listItem.id == item.stulist
                                                })
                                                item.listName = listInfo ? listInfo.name : '--'
                                                item.students = listInfo ? listInfo.students : []
                                                item.fullStu = false
                                            }
                                        })
                                    }
                                    this.fullCus.push(res.courses[0])
                                }
                                this.dataToTable()
                            },
                            err => {
                                this.$Message.error('API ERROR')
                            }
                        ).finally(() => {
                            this.listLoading = false
                        })

                    } else {
                        this.$Message.error('API ERROR!')
                    }
                },
                (err) => {
                    this.$Message.error('API ERROR!')
                }
            ).finally(() => {
                this.listLoading = false
            })
        },
        //根据id获取stulist信息
        getListInfo(ids) {
            let requestData = {
                'code': this.$store.state.userInfo.schoolCode,
                'scope': 'school',
                'ids': ids
            }
            return this.$api.courseMgmt.findListSummary(requestData)
        },
        //根据schedule处理数据进行渲染
        dataToTable() {
            //目前默认显示第一个学段的课程
            let period = ''
            if (this.schoolBase.period && this.schoolBase.period.length) {
                period = this.schoolBase.period[0].id
            }
            let pdData = this.schoolBase.period.find(item => {
                return item.id == period
            })
            if (pdData) {
                let times = pdData.timetable
                if (times && times.length) {
                    this.cusData = Array.apply(null, { length: times.length }).map(() => ({
                        MON: '',
                        TUE: '',
                        WED: '',
                        THU: '',
                        FRI: '',
                        SAT: '',
                        SUN: ''
                    }))
                    let tmw = this.getNextDay()
                    // let tmw = 'WED'
                    this.fullCus.forEach((cusItem) => {
                        cusItem.schedule.forEach(schedItem => {
                            schedItem.time.forEach(timeItem => {
                                //处理mini课表数据
                                let index = times.findIndex(i => {
                                    return i.id == timeItem.id
                                })
                                if (index > -1) {
                                    this.cusData[index][timeItem.week] = '1'
                                }

                                // 获取明日课程
                                if(timeItem.week == tmw){
                                    let t = times.find(i=>{
                                        return i.id == timeItem.id
                                    })
                                    if(t){
                                        this.tmwCus.push(
                                            {
                                                time: t.time,
                                                timeLabel: t.label,
                                                course: cusItem.name,
                                                class: schedItem.classId ? schedItem.classInfo.name : schedItem.listName
                                            }
                                        )
                                    }
                                }
                            })
                        })
                    })
                    this.$emit('tmwCus',this.tmwCus)
                }
            }
        },
        getNextDay() {
            let today = new Date().getDay()
            let tomorrow
            if (today < 7) {
                tomorrow = today++
            } else {
                tomorrow = 1
            }
            let name
            switch (tomorrow) {
                case 1:
                    name = 'MON'
                    break
                case 2:
                    name = 'TUE'
                    break
                case 3:
                    name = 'WED'
                    break
                case 4:
                    name = 'THU'
                    break
                case 5:
                    name = 'FRI'
                    break
                case 6:
                    name = 'SAT'
                    break
                case 7:
                    name = 'SUN'
                    break
                default:
                    break
            }
            return name
        }
    },
    created() {
        //直接读取登录成功拿到得学校基础信息
        this.$store.dispatch('user/getSchoolProfile').then(res => {
            this.classList = res.school_classes
            this.schoolBase = res.school_base
            this.getCourseList()
        })
    }
}
</script>
<style scoped lang="less">
.course-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    background: #1cc0f3;
    border-radius: 4px;
}
.active-cell {
    width: 100%;
    height: 30px;
    line-height: 30px;
    // background: #383838;
    background-color: #1cc0f3;
    cursor: pointer;
}
.has-cus-icon{
    color: white;
    font-size: 16px;
}
</style>
<style>
.home-cus-plan .ivu-table-cell {
    padding: 0px;
}
.home-cus-plan .ivu-table-row-hover {
    background: none;
}
.home-cus-plan .ivu-table td,
.ivu-table th {
    height: 30px;
}
.home-cus-plan .ivu-table td, .home-cus-plan .ivu-table-header thead tr th, .home-cus-plan .ivu-table-wrapper{
    border-color: #383838;
}
.home-cus-plan .ivu-table::after{
    background-color: #383838;
}
</style>