(function () {
    var weekData = [];
    var weekTimeSpan;
    var weekTimeI;
    function compare(property) {
        return function (a, b) {
            var value1 = a[property];
            var value2 = b[property];
            return value1 - value2;
        };
    }
    //处理数据
    function ProcessingData(data) {
        var dataM = [{
            weekTime: 1,
            lessonData: []
        },
        {
            weekTime: 2,
            lessonData: []
        },
        {
            weekTime: 3,
            lessonData: []
        },
        {
            weekTime: 4,
            lessonData: []
        },
        {
            weekTime: 5,
            lessonData: []
        },
        {
            weekTime: 6,
            lessonData: []
        },
        {
            weekTime: 7,
            lessonData: []
        },
        ]
        $.each(data, function (i, n) {
            n.sectionTime = n.LessonTime.slice(1);
            $.each(dataM, function (j, k) {
                if (k.weekTime == n.LessonTime.slice(0, 1)) {
                    k.lessonData.push(n);
                }
            });
        });
        return dataM;
    }

    //判断学期
    function judgeDate(obj) {
        var date = new Date(obj);
        var y = 1900 + date.getYear();
        var m = date.getMonth() + 1;
        if (m >= 2) {
            if (m < 8) {
                $('#semester span').text((y - 1) + '-' + y + '学年第二学期');
            } else {
                $('#semester span').text(y + '-' + (y + 1) + '学年第一学期');
            }
        }
    }

    //根据今天得到周一与周日的时间
    var oneDayTime = 24 * 60 * 60 * 1000;
    function weekday() {
        var now = new Date();
        var nowTime = now.getTime();
        var day = now.getDay();

        //显示周一
        var MondayTime = nowTime - (day - 1) * oneDayTime ;
        //显示周日
        var SundayTime = nowTime + (7 - day) * oneDayTime ;
        return {
            MondayTime: MondayTime,
            Monday: fmtDate(MondayTime),
            SundayTime: SundayTime,
            Sunday: fmtDate(SundayTime)
        };
    }
    function fmtDate(obj) {
        var date = new Date(obj);
        var y = 1900 + date.getYear();
        var m = "0" + (date.getMonth() + 1);
        var d = "0" + date.getDate();
        return y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length);
    }

    function loadData(start, end) {
        judgeDate(todayWeek.SundayTime);
        var param = { startTime: start, endTime: end };
        learun.httpget(config.webapi + "arrangeExam/list", param, (data) => {
            weekData = data;
            var weekData2 = ProcessingData(weekData);
            var tSec2List = $('.tSec2List');
            weekTimeSpan.eq(0).text(todayWeek.Monday).end().eq(1).text(todayWeek.Sunday);
            $.each(weekData2,
                function (i, n) {
                    var html = '';
                    n.lessonData.sort(compare('sectionTime'));
                    if (n.lessonData.length == 0) {
                        html += '<li><div class="noHtml">该时间段没有考试</div></li>';
                    } else {
                        $.each(n.lessonData,
                            function (j, k) {
                                html += '<li>' +
                                    '<div class="tSec2ListL">第' +
                                    k.sectionTime +
                                    '节</div>' +
                                    '<div class="tSec2ListR">' +
                                    '<div class="tSec2ListBox">' +
                                    '<div class="tSec2ListT">' +
                                    k.LessonName +
                                    '</div>' +
                                    '<div class="tSec2ListT">' +
                                    k.ClassName +
                                    '</div>' +
                                    '<div class="tSec2ListTxt">' +
                                    '<i class="iconfont icon-addressbook1"></i>' +
                                    k.EmpName +
                                    '</div>' +
                                    '</div>' +
                                    '<div class="tSec2Location">' +
                                    '<i class="iconfont icon-locationfill"></i>' +
                                    k.ClassroomName +
                                    '</div>' +
                                    '</div>' +
                                    '</li>';
                            });
                    }
                    tSec2List.eq(n.weekTime - 1).html(html);
                });
        }
        );
    }

    var todayWeek = weekday();

    var page = {
        isScroll: false,
        init: function ($page) {
            weekTimeSpan = $page.find('#weekTime span');
            weekTimeI = $page.find('#weekTime i');
            loadData(todayWeek.Monday, todayWeek.Sunday);
            $page.find('.tSec2Top li').on('tap',
                function () {
                    $(this).addClass('active').siblings().removeClass('active');
                    var ind = $(this).index();
                    $('.tSec2Con').css('display', 'none').eq(ind).stop().fadeIn();
                });
            //上下周切换
            var weekDayTime = 7 * oneDayTime;
            weekTimeI.on('tap',
                function () {
                    var index = weekTimeI.index($(this));
                    if (index == 0) {
                        todayWeek.MondayTime = todayWeek.MondayTime - weekDayTime;
                        todayWeek.SundayTime = todayWeek.SundayTime - weekDayTime;
                    } else {
                        todayWeek.MondayTime = todayWeek.MondayTime + weekDayTime;
                        todayWeek.SundayTime = todayWeek.SundayTime + weekDayTime;
                    }
                    todayWeek.Monday = fmtDate(todayWeek.MondayTime);
                    todayWeek.Sunday = fmtDate(todayWeek.SundayTime);
                    weekTimeSpan.eq(0).text(todayWeek.Monday).end().eq(1).text(todayWeek.Sunday);
                    judgeDate(todayWeek.SundayTime);
                    loadData(todayWeek.Monday, todayWeek.Sunday);
                });
        }
    };
    return page;
})();