<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-teachJob-add">
			<input id="classId" name="classId" type="hidden">
			<input name="accessId"  type="hidden" id="accessId" th:value="${quanxian}"/>

			<div class="form-group">	
				<label class="col-sm-3 control-label">班级编号：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text"  onclick="selectList()"  readonly="true" id="className"   >
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">作业时间：</label>
				<div class="col-sm-8">
					<input id="datetime" name="datetime" class="form-control" type="text" readonly="true">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">阶段章节：</label>
				<div class="col-sm-8">
					<input  id="jobStage" name="jobStage" class="form-control" type="text" placeholder="填写格式如：u1-day01">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">作业内容：</label>
				<div class="col-sm-8">
					<textarea  id="jobContent" name="jobContent" class="form-control" type="text"></textarea>
				</div>
			</div>
			<div id="BadstudenUser">
				<h4 style="display: inline-block">非在读学员：</h4>
			</div>
			<hr/>
			<h4 style="padding: 0px;margin: 0px">在读学员作业</h4>
			<hr style="margin: 0px"/>
			<div id="studenUser"></div>
		</form>
	</div>
    <div th:include="include::footer"></div>
    <script type="text/javascript">
		var prefix = ctx + "teach/teachJob"
		$("#form-teachJob-add").validate({
			rules:{
                className:{
					required:true,
				},
                datetime:{
                    required:true,
                },
                jobStage:{
                    required:true,
				}
			}
		});
        if($("#accessId").val()==0){
            //时间选择器
            $(function(){
                laydate.render({
                    elem: '#datetime',
                    trigger: 'click',
                    format: 'yyyy-MM-dd',
                    done: function(value, date, endDate){

                        var myDate = new Date()
                        var Y=myDate.getFullYear();    //获取完整的年份(4位,1970-????)
                        var M=myDate.getMonth()+1;       //获取当前月份(0-11,0代表1月)
                        var D=myDate.getDate();        //获取当前日(1-31)
                        myDate.setTime(myDate.getTime()-24*60*60*1000);
                        if(M<10){
                            M="0"+M
                        }
                        if(D<10){
                            D="0"+D
						}
                        var curDay =Y+"-"+M+"-"+D;
                        //当前时间
                        var Ncurdy = curDay.split("-");
                        //昨天时间
                        var Ndatatime = value.split("-");
                        var PresentTimes =Ncurdy[0]+Ncurdy[1]+Ncurdy[2];
                        var UserTime =Ndatatime[0]+Ndatatime[1]+Ndatatime[2];
                        if(parseInt(PresentTimes)>=parseInt(UserTime)){

                        }else{
                            layer.alert("选择时间不可以大于当前时间")
                            $("#datetime").val("")
                        }
                    }
                });
            });
        }else{
            //时间选择器
            $(function(){
                laydate.render({
                    elem: '#datetime',
                    trigger: 'click',
                    done: function(value, date, endDate){

                        var myDate = new Date()
                        var Y=myDate.getFullYear();    //获取完整的年份(4位,1970-????)
                        var M=myDate.getMonth()+1;       //获取当前月份(0-11,0代表1月)
                        var D=myDate.getDate();        //获取当前日(1-31)
                        myDate.setTime(myDate.getTime()-24*60*60*1000);
                        if(M<10){
                            M="0"+M
                        }
                        if(D<10){
                            D="0"+D
                        }
                        var s1 = myDate.getFullYear()+"-" + M+ "-" + myDate.getDate();
                        var curDay =Y+"-"+M+"-"+D;
                        //当前时间
                        var Ncurdy = curDay.split("-");
                        //昨天时间
                        var yesterday = s1.split("-");
                        var Ndatatime = value.split("-");
                        var PresentTimes =Ncurdy[0]+Ncurdy[1]+Ncurdy[2];
                        var UserTime =Ndatatime[0]+Ndatatime[1]+Ndatatime[2];
                        var yesterday1=yesterday[0]+yesterday[1]+yesterday[2];
                        if(parseInt(PresentTimes)>=parseInt(UserTime)){
                            if(parseInt(UserTime)<parseInt(yesterday1)){
                                layer.alert("作业时间只能是昨天或者今天")
                                $("#datetime").val("")
                            }
                        }else{
                            layer.alert("选择时间不可以大于当前时间")
                            $("#datetime").val("")
                        }
                    }

                });
            });
        }

        /*学生管理-新增-选择班级*/
        function selectList() {
            var classId = $.common.isEmpty($("#classId").val()) ? 1 : $("#classId").val();
            var url = ctx + "teach/teachClasses/selectList";
            var options = {
                title: '选择班级',
                width: "800",
                url: url,
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }
        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            var data = body.find('.selected')[0];
            var className = body.find('.selected')[0].children[2].innerHTML;
            var classId = body.find('.selected')[0].children[1].innerHTML;
            if(data != "undefined" && data != "" && data != null){
                $("#className").val(className);
                $("#classId").val(classId);
                stuMsg(classId)
                stuOtherMsg(classId)
                //判断是否为空
                if (classId != undefined && classId != null) {
                    checkInput();
                }
            }else{
                layer.msg("请选择一个班级");
            }
            layer.close(index);
        }

        //当班级发生改变的时候  清空下拉框  重新赋值
        function checkInput(){
            var classId = $("#classId").val();
            var codeurl = ctx + "teach/teachSatisfaction/classId";
            //清空之后再赋值
            $("#teacherId").children().remove();
            //判断是否为空
            if (classId != undefined && classId != null) {
                $.post(codeurl, {classId: classId}, function (data) {
                    $.each(data.rows, function (i, d) {
                        $("select[name='teacherId']").append("<option value='" + d.teacher_id + "'>" + d.teachName + "</option>");
                    });
                }, 'json');
            }
        }
        function stuMsg(classId){

            $.ajax({
                cache : true,
                type : "POST",
                url : ctx + "teach/student/classStudents",
                data : {"classNo" : classId},
                error : function(request) {
                    $.modal.alertError("系统错误");
                },
                success : function(data) {
                    $("#studenUser").children().remove();
                    var dataStu =data.rows;
                    for(var i=0;i<dataStu.length;i++){
                    var stu="<div class=\"col-sm-3 text-center\" style=u\"margin-top: 20px\">\n" +
                        "\t\t\t\t\t<input class=\"stuChildren\" type=\"hidden\"value='"+dataStu[i].studentId+"'>\n" +
                        "\t\t\t\t\t<span style='width: 55%;display: inline-block'>"+dataStu[i].studentName+"</span>&nbsp;&nbsp;&nbsp;\n" +
                        "\t\t\t\t\t<select class='selectId' style=\"display: inline-block\">\n" +
                        "\t\t\t\t\t\t<option value='2'>作业良好</option>\n" +
						"\t\t\t\t\t\t<option value='1'>作业优</option>\n" +
                        "\t\t\t\t\t\t<option value='3'>作业差</option>\n" +
                        "\t\t\t\t\t\t<option value='4'>未交</option>\n" +
                        "\t\t\t\t\t</select>\n" +
                        "\t\t\t\t</div>";
                        $("#studenUser").append(stu);
                    }
                }
            })
        }
		
		function submitHandler() {
	        if ($.validate.form()) {
	            //$.operate.save(prefix + "/add", $('#form-teachJob-add').serialize());
                add();
	        }
	    }

        function add() {
            var classId = $("input[name='classId']").val();
            var datetime = $("#datetime").val();
            var jobContent = $("#jobContent").val();
            var jobStage = $("#jobStage").val();
            var children =[];
            var stuNunber = document.getElementsByClassName("stuChildren");
            var stuSelectId = document.getElementsByClassName("selectId");
            for(var i=0;i<stuNunber.length;i++){
                var NameId = stuNunber[i].value;
                var index=stuSelectId[i].selectedIndex ;
                var SelectId= stuSelectId[i].options[index].value;
                children.push({studentId:NameId,stuSelectId:SelectId});
            }
            var aaaa = JSON.stringify(children)
            var data = {
                classId: classId,
                datetime: datetime,
                jobContent: jobContent,
                jobStage:jobStage,
                aaaa: aaaa
            }
            $.ajax({
                cache : true,
                type : "POST",
                url : prefix + "/add",
                data : data,
                error : function(request) {
                    $.modal.alertError("系统错误");
                },
                success : function(data) {
                    $.operate.successCallback(data);
                }
            })
		}

        function stuOtherMsg(classId){
            $.ajax({
                cache: true,
                type: "POST",
                url: ctx + "teach/student/classOtherStudents",
                data: {"classNo": classId},
                error: function (request) {
                    $.modal.alertError("系统错误");
                },
                success: function (data) {
                    var dataRow = data.rows;
                    $("#BadstudenUser>span").remove();
                    for(var i=0;i<dataRow.length;i++){
                        var datas="<span style='width: 80px;display: inline-block;text-align: center;height:30px;line-height:30px;margin-left: 15px;border-radius: 20px;font-size: 14px;color: #ffffff;background-color: #0d8ddb'>"+dataRow[i].studentName+"</span>";
                        $("#BadstudenUser").append(datas)
                    }
                    if(dataRow.length==0){
                        $("#BadstudenUser").append("<span>无</span>");
                    }
                }
            })
        }
	</script>
</body>
</html>
