(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 loadData(start, end) {
        judgeDate(todayWeek.SundayTime);
        var param = { StartDate: start, EndDate: end };
        learun.httpget(config.webapi + "learun/adms/timetable/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) {
                                var lessonInfo = JSON.stringify(k);
                                console.log(lessonInfo);
                                html += '<li lessonInfo=\'' + lessonInfo + '\' id="' + k.id + '">' +
                                    '<div class="tSec2ListL">第' +
                                    k.sectionTime +
                                    '节</div>' +
                                    '<div class="tSec2ListR">' +
                                    '<div class="tSec2ListBox">' +
                                    '<div class="tSec2ListT">' +
                                    k.LessonName +
                                    '</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');
            judgeDate();
            weekTimeSpan.eq(0).text(todayWeek.Monday).end().eq(1).text(todayWeek.Sunday);
            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);
                });
            $page.find('.tSec2List').on('tap', 'li', function () {
                var id = $(this).attr('id');
                var className = $(this).find('.tSec2ListT').text();
                var locaName = $(this).find('.tSec2Location').text();
                var times = $(this).find('.tSec2ListL').text();
                var lessonInfo = JSON.parse($(this).find(".tSec2ListL").parent().attr("lessonInfo"));
                console.log(lessonInfo);
                learun.nav.go({ path: 'myAttendance/studentAtten', title: '学生考勤', isBack: true, isHead: true, param: lessonInfo, type: 'right' });
            });
        }
    };
    return page;
})();