<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<style>
	.stageNew{
		width: 100px;
	}
	.stageStarttime{
		width: 40%;
		display: inline-block;
	}
	.stageEndtime{
		width: 40%;
		display: inline-block;
	}

</style>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-teachClasses-edit" th:object="${teachClasses}">
            <input id="classId" name="classId" th:field="*{classId}"  type="hidden">
			<input id="schoolId" name="schoolId" th:field="*{schoolId}" type="hidden">
			<input id="majorId" name="majorId" th:field="*{majorId}" type="hidden">
			<div class="form-group">	
				<label class="col-sm-3 control-label">班级名称：</label>
				<div class="col-sm-8">
					<input id="className" name="className" th:field="*{className}" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">所属校区：</label>
				<div class="col-sm-8">
					<input id="schoolName" name="schoolName" th:field="*{schoolName}" 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="majorName" name="majorName" th:field="*{majorName}" 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="createtime" name="createtime" th:field="*{createtime}" 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">
					<div class="onoffswitch">
						<input type="checkbox" th:checked="${teachClasses.linkStatus == 0 ? true : false}" class="onoffswitch-checkbox" id="linkStatus" name="linkStatus">
						<label class="onoffswitch-label" for="linkStatus">
							<span class="onoffswitch-inner"></span>
							<span class="onoffswitch-switch"></span>
						</label>
					</div>
				</div>
			</div>
			<button type="button" class="btn btn-primary" onclick="adds()">添加</button>
			<table class="table " id="Professional">
				<tr>
					<th>阶段</th>
					<th>阶段时间</th>
					<th>班主任</th>
					<th>教员</th>
					<th>助教</th>
					<th>操作</th>
				</tr>
			</table>
		</form>
    </div>
    <div th:include="include::footer"></div>
    <script type="text/javascript" >
		var prefix = ctx + "teach/teachClasses";
		$("#form-teachClasses-edit").validate({
			rules:{
                schoolName:{
                    required:true,
                },
                className:{
                    required:true,
                },
                majorId:{
                    required:true,
                }
			}
		});
        selectMajor();
        //根据选择的专业级联查询阶段存入当前阶段下拉框
        function selectMajor() {
            var majorId1 =$("#majorId").val();
            $.ajax({
                cache : true,
                type : "POST",
                url : ctx + "teach/teachMajorStage/list",
                data : {"majorId" : majorId1},
                error : function(request) {
                    $.modal.alertError("系统错误");
                },
                success : function(data) {
                    if (data && data.rows.length > 0) {
                        project = data.rows;
                        addOptions(project);
                    }
                }
            })
        }
        function addOptions(project) {
            var pro = $('select[name="stageNew"]');
            pro.children().remove();
            var options = "";
            $(project).each(function() {
                options += '<option value="' + this.id + '" >' + this.stageName + '</option>';
            });
            pro.append(options);
            var stageId = document.getElementsByClassName("stageId");
            for(var i=0;i<$(".stageNew").length;i++){
                for(var j=0;j<$(".stageNew").eq(i).children().length;j++){
                    if($(".stageNew").eq(i).children().eq(j).val()==stageId[i].value){
                        $(".stageNew").eq(i).children().eq(j).attr("selected",true);
                    }
                }
            }
        }
        var myajax = $.ajax({
            cache: true,
            type: "POST",
            url: ctx + "teach/teachClassesTeacher/list",
            data: {"classId": $("#classId").val()},
            success : function(data) {
                var dataRow = data.rows;
                for (var i = 0; i < dataRow.length; i++) {
                    if(dataRow[i].stageEndtime == null){
                        dataRow[i].stageEndtime = "";
                    }
                    if(dataRow[i].teacherName2 == null){
                        dataRow[i].teacherName2 = "";
                        dataRow[i].assistantId = "";
                    }
                    if(dataRow[i].teacherName3 == null){
                        dataRow[i].teacherName3 = "";
                        dataRow[i].instructorId = "";
                    }
                    var trs="\t\t\t\t<tr class='trs'>\n" +
                        "\t\t\t\t\t<td>\n" +
                        "\t\t\t\t\t\t<div class=\"form-group jieduan\" style=\"margin-bottom: 0px;\">\n" +
                        "\t\t\t\t\t\t\t<div class=\"col-sm-8\">\n" +
                        "<input type='hidden' class='stageId' value='"+dataRow[i].stageId+"'>" +
                        '<select name="stageNew"  class="form-control stageNew" type="text">' +
                        '</select>'+
                        "\t\t\t\t\t\t\t</div>\n" +
                        "\t\t\t\t\t\t</div>\n" +
                        "\t\t\t\t\t</td>\n" +
                        '<td><input id="stageStarttime'+timeNum+'"  name="stageStarttime " class="form-control stageStarttime" value="'+dataRow[i].stageStarttime+'"  type="text" readonly="true">-'+
                        '<input id="stageEndtime'+timeNum+'" name="stageEndtime" class="form-control stageEndtime" type="text" value="'+dataRow[i].stageEndtime+'" readonly="true"></td>'+
                        "\t\t\t\t\t</td>\n" +
                        "\t\t\t\t\t<td>\n" +
                        "<input type='hidden' class='teacherId' value='"+dataRow[i].teacherId+"'>" +
                        "<input class=\"nunbers\" type='hidden' value='"+(Num)+"'>" +
                        "\t\t\t\t\t\t<input class=\"form-control TeacherName\"  type=\"text\" name=\"classNo\" required=\"true\" placeholder=\"点击请选择班主任\" onclick=\"selectTeacherList(this)\" readonly=\"true\" value='"+dataRow[i].teacherName+"'>\n" +
                        "\t\t\t\t\t</td>\n" +
                        "\t\t\t\t\t<td>\n" +
                        "<input type='hidden' class='teacherId' value='"+dataRow[i].instructorId+"'>" +
                        "<input class=\"nunbers\" type='hidden' value='"+(Num)+"'>" +
                        "\t\t\t\t\t\t<input class=\"form-control TeacherName\"  type=\"text\" name=\"classNo\"  placeholder=\"点击请选择教员\" onclick=\"selectTeacherList1(this)\" readonly=\"true\" value='"+dataRow[i].teacherName3+"'>\n" +
                        "\t\t\t\t\t</td>\n" +
                        "\t\t\t\t\t<td>\n" +
                        "<input type='hidden' class='teacherId' value='"+dataRow[i].assistantId+"'>" +
                        "<input class=\"nunbers\" type='hidden' value='"+(Num++)+"'>" +
                        "\t\t\t\t\t\t<input class=\"form-control TeacherName\"  type=\"text\" name=\"classNo\" placeholder=\"点击请选择助教\" onclick=\"selectTeacherList2(this)\" readonly=\"true\" value='"+dataRow[i].teacherName2+"'>\n" +
                        "\t\t\t\t\t</td>\n" +
                        "\t\t\t\t\t<td>\n" +
                        "<input type='hidden' class='id' value='"+dataRow[i].id+"'>" +
                        "\t\t\t\t\t\t<button type=\"button\" class=\"btn btn-danger\" onclick=\"del(this)\">删除</button>\n" +
                        "\t\t\t\t\t</td>\n" +
                        "\t\t\t\t</tr>\n"
                    selectMajor();
                    $("#Professional").children().append(trs);
                    var xData;
                    var EedData;
                    var jieshu =document.getElementById('stageStarttime'+timeNum);
                    var kaishi = document.getElementById('stageEndtime'+timeNum);
                    laydate.render({
                        elem: '#stageStarttime'+timeNum,
                        trigger: 'click',
                        format: 'yyyy-MM-dd',
                        done: function(value, date, endDate) {
                            var Sdata = value.split("-");	//截取字符串
                            xData = Sdata[0] + Sdata[1] + Sdata[2];  	//从新组织新的字符串
                            if (xData > EedData) {
                                layer.alert("开始日期不能大于结束日期,请正确选择日期");
                                jieshu.value = kaishi.value
                            }
                        }
                    });
                    laydate.render({
                        elem: '#stageEndtime'+timeNum,
                        trigger: 'click',
                        format: 'yyyy-MM-dd',
                        done: function(value, date, endDate) {
                            var Sdata = value.split("-");	//截取字符串
                            EedData = Sdata[0] + Sdata[1] + Sdata[2];  	//从新组织新的字符串
                            if (EedData < xData) {
                                layer.alert("结束日期不能小于开始日期,请正确选择日期");
                                kaishi.value = jieshu.value
                            }
                        }
                    });
                    timeNum++;
                }
            }
        })



        //删除方法
        function del(data) {
			var id = $(data).prev().val();
            //清空子标签的值
            data.parentNode.parentNode.remove();
            //重新给隐藏域里面的值进行赋值
            for (var i=0;i<$(".trs").length;i++){
                $(".trs").eq(i).children().eq(2).children().eq(1).val(i);
                $(".trs").eq(i).children().eq(3).children().eq(1).val(i);
                $(".trs").eq(i).children().eq(4).children().eq(1).val(i);
            }
            //把最大值返回给Num变量
            Num=$(".trs").length;
            $.ajax({
                cache: true,
                type: "POST",
                url: ctx + "teach/teachClassesTeacher/delete",
                data: {"id": id},
                error : function(request) {
                    layer.msg("删除失败");
                },
                success : function(data) {
                    layer.msg("删除成功");
                }
            })
        }

        function submitHandler() {
	        if ($.validate.form()) {
	            //$.operate.save(prefix + "/edit", $('#form-teachClasses-edit').serialize());
                edit()
	        }
	    }

        function edit() {
            var children =[];
            var linkStatus = $("input[name='linkStatus']").is(':checked') == true ? 0 : 1;
            var no = document.getElementsByClassName("id");
            var jieduan = document.getElementsByClassName("stageNew");
            var starttime = document.getElementsByClassName("stageStarttime");
            var endtime = document.getElementsByClassName("stageEndtime");

            for(var i=0;i<starttime.length;i++){
                var stageId= jieduan[i].value;
                var id= no[i].value;
                var stageStarttime = starttime[i].value;
                var stageEndtime = endtime[i].value;
                var teacherId1 = $(".trs").eq(i).children().eq(2).children().eq(0).val();
                var teacherId2 = $(".trs").eq(i).children().eq(3).children().eq(0).val();
                var teacherId3 = $(".trs").eq(i).children().eq(4).children().eq(0).val();
                children.push({teacherId1:teacherId1,teacherId2:teacherId2,teacherId3:teacherId3,stageId:stageId,stageStarttime:stageStarttime,stageEndtime:stageEndtime,id:id});
            }
            var aaaa = JSON.stringify(children);
            $.ajax({
                cache : true,
                type : "POST",
                url : prefix + "/edit",
                data : {
                    "classId": $("#classId").val(),
                    "className": $("#className").val(),
                    "aaaa": aaaa,
                    "linkStatus":linkStatus
                },
                async : false,
                error : function(request) {
                    $.modal.alertError("系统错误");
                },
                success : function(data) {
                    $.operate.successCallback(data);
                }
            });

		}
        //定义隐藏域判定值
        var Num=1;
        var timeNum=0;
        //先判断下拉框是否存在，不存在把隐藏值更改为0
        if($("trs").length==0){
            Num=0;
        }
        function adds() {
            var trs="\t\t\t\t<tr class='trs'>\n" +
                "\t\t\t\t\t<td>\n" +
                "\t\t\t\t\t\t<div class=\"form-group jieduan\" style=\"margin-bottom: 0px;\">\n" +
                "\t\t\t\t\t\t\t<div class=\"col-sm-8\">\n" +
                "\t\t\t\t\t\t\t\t<select  name=\"stageNew\" class=\"form-control stageNew\" type=\"text\"></select>\n" +
                "\t\t\t\t\t\t\t</div>\n" +
                "\t\t\t\t\t\t</div>\n" +
                "\t\t\t\t\t</td>\n" +
                "\t\t\t\t\t<td>\n" +
                "\t\t\t\t\t\t<input id=\"stageStarttime"+timeNum+"\" name=\"stageStarttime\" class=\"form-control stageStarttime\" type=\"text\" required=\"required\" readonly=\"true\">-\n" +
                "\t\t\t\t\t\t<input id=\"stageEndtime"+timeNum+"\" name=\"stageEndtime\" class=\"form-control stageEndtime\" type=\"text\" readonly=\"true\">\n" +
                "\t\t\t\t\t</td>\n" +
                "\t\t\t\t\t<td>\n" +
                "\t\t\t\t\t\t<input type=\"hidden\" class=\"teacherId\">\n" +
                "\t\t\t\t\t\t<input class=\"nunbers\" type=\"hidden\" value="+(Num)+">\n" +
                "\t\t\t\t\t\t<input class=\"form-control TeacherName\"  type=\"text\" name=\"classNo\" required placeholder=\"点击请选择班主任\" onclick=\"selectTeacherList(this)\" readonly=\"true\">\n" +
                "\t\t\t\t\t</td>\n" +
                "\t\t\t\t\t<td>\n" +
                "\t\t\t\t\t\t<input type=\"hidden\" class=\"teacherId\">\n" +
                "\t\t\t\t\t\t<input class=\"nunbers\" type=\"hidden\" value="+(Num)+">\n" +
                "\t\t\t\t\t\t<input class=\"form-control TeacherName\"  type=\"text\" name=\"classNo\" placeholder=\"点击请选择教员\" onclick=\"selectTeacherList1(this)\" readonly=\"true\">\n" +
                "\t\t\t\t\t</td>\n" +
                "\t\t\t\t\t<td>\n" +
                "\t\t\t\t\t\t<input type=\"hidden\" class=\"teacherId\">\n" +
                "\t\t\t\t\t\t<input class=\"nunbers\" type=\"hidden\" value="+(Num++)+">\n" +
                "\t\t\t\t\t\t<input class=\"form-control TeacherName\"  type=\"text\" name=\"classNo\" placeholder=\"点击请选择助教\" onclick=\"selectTeacherList2(this)\" readonly=\"true\">\n" +
                "\t\t\t\t\t</td>\n" +
                "\t\t\t\t\t<td>\n" +
				"<input type='hidden' class='id' value=''>" +
                "\t\t\t\t\t\t<button type=\"button\" class=\"btn btn-danger\" onclick=\"del(this)\">删除</button>\n" +
                "\t\t\t\t\t</td>\n" +
                "\t\t\t\t</tr>\n"
                selectMajor();
                $("#Professional").children().append(trs);
				var xData;
				var EedData;
				var jieshu =document.getElementById('stageStarttime'+timeNum);
				var kaishi = document.getElementById('stageEndtime'+timeNum);
				laydate.render({
					elem: '#stageStarttime'+timeNum,trigger: 'click',
                    format: 'yyyy-MM-dd',
                    done: function(value, date, endDate) {
                        var Sdata = value.split("-");	//截取字符串
                        xData = Sdata[0] + Sdata[1] + Sdata[2];  	//从新组织新的字符串
                        if (xData > EedData) {
                            layer.alert("开始日期不能大于结束日期,请正确选择日期");
                            jieshu.value = kaishi.value


                        }
                    }
				});
				laydate.render({
					elem: '#stageEndtime'+timeNum,trigger: 'click',
                    format: 'yyyy-MM-dd',
                    done: function(value, date, endDate) {
                        var Sdata = value.split("-");	//截取字符串
                        EedData = Sdata[0] + Sdata[1] + Sdata[2];  	//从新组织新的字符串
                        if (EedData < xData) {
                            layer.alert("结束日期不能小于开始日期,请正确选择日期");
                            kaishi.value = jieshu.value
                        }
                    }
				});
            	timeNum++;
        }

        function selectTeacherList2(data) {
            var url = ctx + "system/user/selectList";
            var options = {
                title: '选择老师',
                width: "800",
                url: url,
                data:data,
                callBack: TeacherOpen2
            };
            $.modal.openOptions(options);
        }
        function selectTeacherList1(data) {
            var url = ctx + "system/user/selectList";
            var options = {
                title: '选择老师',
                width: "800",
                url: url,
                data:data,
                callBack: TeacherOpen1
            };
            $.modal.openOptions(options);
        }
        function selectTeacherList(data) {
            var url = ctx + "system/user/selectList";
            var options = {
                title: '选择老师',
                width: "800",
                url: url,
                data:data,
                callBack: TeacherOpen
            };
            $.modal.openOptions(options);
        }
        function TeacherOpen2(index, layero){
            //获取相对应的隐藏域的值
            var indexs =this.data.previousSibling.previousSibling.value;
            var body = layer.getChildFrame('body', index);
            var data = body.find('.selected')[0];
            if(data != "undefined" && data != "" && data != null){
                for(var i=0;i<$(".trs").length;i++){
                    //如果和找到的相等就添加
                    if(i==indexs){
                        $(".trs").eq(i).children().eq(4).children().eq(2).val(body.find('.selected')[0].children[2].innerHTML)
                        $(".trs").eq(i).children().eq(4).children().eq(0).val(body.find('.selected')[0].children[1].innerHTML)
                    }
                }
            }else{
                layer.msg("请选择一个老师");
            }
            layer.close(index);

        }
        function TeacherOpen1(index, layero){
            //获取相对应的隐藏域的值
            var indexs =this.data.previousSibling.previousSibling.value;
            var body = layer.getChildFrame('body', index);
            var data = body.find('.selected')[0];
            if(data != "undefined" && data != "" && data != null){
                for(var i=0;i<$(".trs").length;i++){
                    //如果和找到的相等就添加
                    if(i==indexs){
                        $(".trs").eq(i).children().eq(3).children().eq(2).val(body.find('.selected')[0].children[2].innerHTML)
                        $(".trs").eq(i).children().eq(3).children().eq(0).val(body.find('.selected')[0].children[1].innerHTML)
                    }
                }
            }else{
                layer.msg("请选择一个老师");
            }
            layer.close(index);

        }
        function TeacherOpen(index, layero){
            //获取相对应的隐藏域的值
            var indexs =this.data.previousSibling.previousSibling.value;
            var body = layer.getChildFrame('body', index);
            var data = body.find('.selected')[0];
            if(data != "undefined" && data != "" && data != null){
                for(var i=0;i<$(".trs").length;i++){
                    //如果和找到的相等就添加
                    if(i==indexs){
                        $(".trs").eq(i).children().eq(2).children().eq(2).val(body.find('.selected')[0].children[2].innerHTML)
                        $(".trs").eq(i).children().eq(2).children().eq(0).val(body.find('.selected')[0].children[1].innerHTML)
                    }
                }
            }else{
                layer.msg("请选择一个老师");
            }
            layer.close(index);
        }
	</script>
</body>
</html>
