<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../../common/init.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel='stylesheet' href='${ctx}/resources/css/easyui.css' type='text/css' media='screen' />
<link rel='stylesheet' href='${ctx}/resources/css/user-center-style.css' type='text/css' media='screen' />

<link rel="stylesheet" href="${ctx}/resources/js/multiselect/jquery.multiselect.css" type="text/css" />
<%@ include file="../../common/top.jsp"%>
<script type="text/javascript" src="${ctx}/resources/js/multiselect/jquery-ui.min.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/multiselect/jquery.multiselect.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/jquery.checktable.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/jquery.easyui.min.js"></script>
<%@ include file="../../common/validation.jsp"%>
<script type="text/javascript">
	var flag;
	function checkLoginName(){
		var name=$("#loginName").val();
		var obj={
			"loginName" :name	
		};
		
		var User = JSON.stringify(obj);
		if(name!=""){
			$.ajax({
				type : "POST",
				url : "${ctx}/user/checkName",
				data : User,
				contentType : "application/json; charset=utf-8",
				dataType : "json",
				success : function(callback) {
					callback = eval(callback);
					if(callback.type=="fail"){
						flag="true";
						$("#loginNameLab").text("*该登陆名已存在");
						$("#loginNameLab").show();
					}else{
						flag="false";
						$("#loginNameLab").hide();
					}
				}
			});
		}
	}
	function add(){
		var f=true;
		var loginName=$("#loginName").val();
		if(loginName==""){
			f=false;
			$("#loginNameLab").text("*请输入登录名");
			$("#loginNameLab").show();
		}
		
		var name=$("#name").val();
		if(name==""){
			f=false;
			$("#nameLab").show();
		}
		var roleId=$("input[name='role']").val();
		if(roleId==""){
			f=false;
			$("#roleLab").show();
		}
		
		var course=$('#course').combotree('getText');
		
		if(course==""){
			f=false;
			$("#courseLab").show();
		}
		if(f&&flag=="false"){
			var obj={
				"isEnabled":1,
				"name" : name,
				"password" :123456,
				"loginName" : loginName,
				"userType" : 1
			};
			var User = JSON.stringify(obj);
			var arrCourse = new Array();
			var arrRole = new Array();
			$('*[name="course"]').each(function(){
				arrCourse.push($(this).val());
			});
			
			$('*[name="role"]').each(function(){
				arrRole.push($(this).val());
			});
			
			$.ajax({
				type : "POST",
				url : "${ctx}/user/addUser",
				data : "{User:" + User + ",arrCourse:" + "[" + arrCourse + "]" + ",arrRole:" + "[" + arrRole + "]"+"}",
				contentType : "application/json; charset=utf-8",
				dataType : "json",
				success : function(callback) {
					callback = eval(callback);
					if(callback.type=="success"){
						$.messager.alert('提示','新增用户成功!');
						location.href="${ctx}/user/list";
					}else{
						$.messager.alert('提示','新增用户失败!');
					}
				}
			});
			
		}
	}
	function check(){
		var name=$("#name").val();
		if(name!=""){
			$("#nameLab").hide();
		}
		if(flag=="false"){
			$("#loginNameLab").hide();
		}
		var roleId=$('.combo-value').val();
		if(roleId!=""){
			$("#roleLab").hide();
		}
		var course=$('#course').combotree('getText');
		
		if(course!=""){
			$("#courseLab").hide();
		}
	}
	
</script>
<title>新增用户</title>
</head>
<body >
    <%@ include file="../../common/header.jsp"%>
    <div id="content" class="content">
        <%@ include file="../../common/sub_menu.jsp"%>
        <div class="page" id="page" >
        <div class="page-header" >新增用户</div>
        <div class="page-body" align="center" onmouseover="check()">
            <div>
                <form id="search-form" method="post" >
                    <table class="label-table" border="0">
                        <tr>
                            <td class="label-table-label"><label>登录名：</label></td>
                            <td class="qBox label-table-content" colspan="2" style="width: 250px;"><input id="loginName" onBlur="checkLoginName()"/><label style="display: none;color: red;" id="loginNameLab" >*该登陆名已存在</label></td>
                        </tr>
                        <tr>
                            <td class="label-table-label"><label>用户姓名：</label></td>
                            <td class="qBox label-table-content" colspan="2" style="width: 250px;"><input name="name" id="name"/><label style="display: none;color: red;" id="nameLab" >*请输入用户姓名</label></td>
                        </tr>
                        <tr>
                            <td class="label-table-label"><label>角色：</label></td>
                            <td class="qBox label-table-content" colspan="2">
                            	<input id="role" name="role" class="easyui-combobox" style="width:450px;" data-options="url:'${ctx}/user/getAllRole',
                            																							method:'get',
                            																							valueField:'id',
																														textField:'text',
																														multiple:true,
																							   							panelHeight:'auto',
																														editable:false,
																														required:true "/>
                            	<label style="display: none;color: red;" id="roleLab" >*请选择角色</label>
                            </td>
                        </tr>
                        <tr>
                            <td class="label-table-label"><label>关联课程：</label></td>
                            <td class="qBox label-table-content" colspan="2">
                            	<select id="course" name="course" class="easyui-combogrid"   style="width:450px" data-options="
                            																   panelWidth: 400,
																							   multiple: true,
																							   idField: 'id',
																							   textField: 'text',
																							   url: '${ctx}/user/getAllSubject',
																							   editable:false,
																							   method: 'get',
																							   columns: [[
																									{field:'ck',checkbox:true},
																									{field:'id',title:'id',width:40},
																									{field:'code',title:'code',width:40},
																									{field:'text',title:'课程名称',width:160}
																								]],
																								fitColumns: true"></select>
								<label style="display: none;color: red;" id="courseLab" >*请至少选择一个关联课程</label>
                            </td>
                        </tr>
                        <tr>
                        	<td colspan="3" align="center"><input type="button" value="添加" class="btn" onclick="add()"/>    		
                        	</td>
                        </tr>
                    </table>                 
                </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>