(function () {
    //数据   status 1 正常  2 早退 3 旷课 4 病假 5 迟到 6 事假 
    var page = {
        isScroll: false,
        init: function ($page, param) {
            //console.log(param) 传过来的参数 在param上
            //初始化
            console.log(param);
            loadData(param);
            $('.tSec3Time').html('<span></span>' + param.LessonDate + ' 第' + param.LessonTime.substring(1) + '节<span></span>');
            $('.tSec4T').html('<div>' +
                param.LessonName +
                '</div>')

            //选择考勤状态
            $page.find('.timeTableList').on('tap',
                'li',
                function () {
                    var that = $(this);
                    var jsons = JSON.parse(that.attr('student'));
                    // console.log(jsons)
                    var type = that.attr('class');
                    type = type.length == 0 ? 'sStatue1' : type;
                    that.addClass('active');
                    $('.timeTableAlertZ').css('display', 'block');;
                    $('.timeTableAlert').find('.timeTableAlertT').html(jsons.StuName).end().find('.timeTableAlertTxt').html('学号: ' + jsons.StuNo).end().addClass('active').find('.' + type).addClass('active').siblings().removeClass('active');
                });
            $page.find('.timeTableAlert').on('tap',
                'li',
                function () {
                    var type = $(this).attr('type');
                    var ketype = $(this).attr('num');
                    $('.timeTableList li.active').attr('class', '').addClass(type).attr('kqtype', ketype);
                    timeTableAlertFn();
                });
            $page.find('.timeTableAlertZ').on('tap', function () {
                timeTableAlertFn()
            });
            function timeTableAlertFn() {
                $('.timeTableAlertZ').css('display', 'none');
                $('.timeTableList li.active').removeClass('active');
                $('.timeTableAlert').removeClass('active');
            }
            $page.find(".timeTableBtn").on('tap',
                function () {

                    var rowData = new Array();

                    $(".timeTableList li").each(function () {
                        var keType = $(this).attr("kqtype") ? $(this).attr("kqtype") : "0";
                        var object = JSON.parse($(this).attr("student"));
                        object.kqType = keType;

                        rowData.push(object);
                    })

                    var jsonObj = JSON.parse(JSON.stringify(rowData));
                    console.log(JSON.stringify(jsonObj));

                    learun.layer.confirm('是否确认提交考勤？',
                        function (num) {
                            learun.layer.loading(true, '正在提交');
                            learun.httppost(
                                config.webapi + '/Learun/EducationalAdministration/StuAttendanceLeave/apply',
                                {
                                    lessonTime: param.LessonTime,
                                    lessonDate: param.LessonDate,
                                    classRoomNo: param.ClassRoomNo,
                                    dataJson: JSON.stringify(rowData)
                                },
                                (data) => {
                                    learun.layer.loading(false);
                                    if (data) {  
                                        learun.layer.toast("提交成功");
                                        loadData(param);
                                    }
                                });

                        })

                });

        }
    };

    function loadData(param) {
        var _postParam = {
            pagination: {
                rows: "1000",
                page: "1",
                sidx: 'ClassNo',
                sord: 'DESC'
            },
            queryJson: JSON.stringify({
                year: param.AcademicYear,
                semester: param.Semester,
                empno: param.EmpNo,
                lessonNo: param.LessonNo,
                teachClassNo: param.TeachClassNo,
                lessonTime: param.LessonTime,
                LessonSortNo: param.LessonSortNo,
            }
            )
        };
        learun.httpget(config.webapi + "/teachattendance/students", _postParam, (data) => {
            console.log(data);
            studentCheckData(data, param)
        })
    }
    function studentCheckData(weekData2, param) {
        var studCheckList = $('.timeTableList');
        var html = '';
        if (weekData2.rows.length == 0) {
            html = '<li>暂时没有信息</li>'
        } else {
            $.each(weekData2.rows, function (j, k) {
                var statusName = !k.Sort ? 'sStatue1' : k.Sort == "准时" ? 'sStatue1' : k.Sort == "早退" ? 'sStatue2' : k.Sort == "旷课" ? 'sStatue3' : k.Sort == "病假" ? 'sStatue4' : k.Sort == "迟到" ? 'sStatue5' : 'sStatue6';
                html += '<li student=' + JSON.stringify(k) + ' class="' + statusName + '"><div>' +
                    k.StuName + '<span class="iconfont icon-check"></span>' +
                    '</div></li>';
            })
            var htmls = '<i class="iconfont icon-addressbook1"></i>' + param.EmpName + ' &nbsp;&nbsp; <i class="iconfont icon-locationfill"></i>' + param.ClassroomName + '<div><span>应到</span> ' + weekData2.records + '</div>';
            $('#shouldArrive').html(htmls);
        }

        studCheckList.html(html)
    }
    return page;
})();