<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta name="description" content="Xenon Boostrap Admin Panel" />
	<meta name="author" content="" />
	<title>系统用户管理</title>
</head>
<body class="page-body">
	<div class="page-container">
		<%@ include file="../../common/menu.jsp"%>
		<div class="main-content">
			<%@ include file="../../common/header.jsp"%>
			<div class="page-title">
				<div class="title-env">
					<h1 class="title">新增系统用户</h1>
				</div>
				<div class="breadcrumb-env">
					<ol class="breadcrumb bc-1">
						<li>
							<a href="${ROOT_PATH}/sysUser"><i class="fa-home"></i>系统管理</a>
						</li>
						
						<li class="active">
							<strong>新增系统用户</strong>
						</li>
					</ol>
				</div>
			</div>
			<!-- Removing search and results count filter -->
			<div class="row">
				<div class="col-sm-12">
					<div class="panel panel-default">
						<div class="panel-body">
							<form role="form" id="addUserForm" class="form-horizontal">
								<div class="form-group">
									<label class="col-sm-2 control-label" for="user_name"><font color="red">*</font>用户名</label>
									<div class="col-sm-4">
										<input type="text" class="form-control" id="user_name" name="user_name" value="">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label" for="password"><font color="red">*</font>密码</label>
									<div class="col-sm-4">
										<input type="password" class="form-control" id="password" name="password" value=""  />
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label"><font color="red">*</font>性别</label>
									<div class="col-sm-4">
											<label class="radio-inline">
												<input type="radio" value="1" checked="checked" id="sex" name="sex" style="margin-top: 2px;">
												男
											</label>
											<label class="radio-inline">
												<input type="radio" value="2" id="sex" name="sex" style="margin-top: 2px;">
												女
											</label>
									</div>
									<div class="col-sm-6"></div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label" for="real_name"><font color="red">*</font>真实姓名</label>
									<div class="col-sm-4">
										<input type="text" class="form-control" id="real_name" name="real_name" value="" />
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label" for="phone"><font color="red">*</font>手机号码</label>
									<div class="col-sm-4">
										<input type="text" class="form-control" name="phone" id="phone" value="">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label" for="email">邮箱</label>
									<div class="col-sm-4">
										<input type="text" class="form-control" name="email" id="email" value="">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label" ><font color="red">*</font>用户角色</label>
									<div class="col-sm-4">
										<select class="form-control" id="role" name="role">
											<option value=""  >请选择用户角色</option>
											<c:forEach var="r" items="${rList }" >
											    <option value="${r.id }">${r.name }</option>
											</c:forEach>
										</select>		
									</div>
								</div>
								<div class="form-group-separator"></div>
								<div class="form-group">
									<div class="col-sm-6" style="text-align: center;">
										<button type="button" id="btn_save" class="btn btn-info" >保存</button>
										<button type="button" id="btn_cancel" class="btn btn-white" >返回</button>
										<input id="token" type="hidden" name="token" value="${SESSION_TOKEN }"/>
									</div>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
			<%@ include file="../../common/footer.jsp"%>
		</div>
	</div>
	
	<script type="text/javascript">
		
		$(function() {
			
			$("#btn_save").click(function(){
				var user_name = $("#user_name").val();
				var password = $("#password").val();
				var real_name = $("#real_name").val();
				var phone = $("#phone").val();
				var role = $("#role").val();
				var email = $("#email").val();
				if(!checkStr(user_name)){
					toastr.error("用户名不能为空", null, opts);
					return false;
				}
				if(!checkStr(password)){
					toastr.error("密码不能为空", null, opts);
					return false;
				}
				if(!checkStr(real_name)){
					toastr.error("真实姓名不能为空", null, opts);
					return false;
				}
				if(!checkStr(user_name)){
					toastr.error("手机号码不能为空", null, opts);
					return false;
				}
				if(!Constants.phoneReg.test(phone)){    
		            toastr.error("请输入正确的手机号码", null, opts);
		            return false;
		        }
				if(!isNull(email) && !Constants.emailReg.test(email)){    
		            toastr.error("请输入正确的邮箱", null, opts);
		            return false;
		        }
				if(!checkStr(role)){
					toastr.error("用户角色不能为空", null, opts);
					return false;
				}
				$("#btn_save").attr("disabled","disabled");
				 $("#addUserForm").ajaxSubmit({  
		                type:'post',
		                dataType: "json",
		                url : "${ROOT_PATH}/sysUser/addUser.do",
		                success:function(data){  
		                	window.setTimeout("enableBtn('#btn_save');",Constants.reloadTime);
		                	if (data.result == '01') {
		                		toastr.success("新增成功", null, opts);
		                		window.setTimeout("reloadPage();",Constants.reloadTime);
		                	} else {
		                		toastr.error(data.msg, null, opts);
		                	}
		                },  
		                error:function(XmlHttpRequest,textStatus,errorThrown){
		                	window.setTimeout("enableBtn('#btn_save');",Constants.reloadTime);
		                	toastr.error(Constants.netError, null, opts);
		                }  
		            }); 
				
			});
			
			
			$("#btn_cancel").click(function(){
				history.back(-1);
			});
			
			//根据省份id获取城市列表
			$("#province_code").bind("change",function(){
				//清除city下拉框下的下拉项  
				$("#city_code option:not(:first)").remove();
				if ($(this).val() == '') {  
			        return false;  
			    }else{  
			        var province_id = $(this).val();  
			     	// 根据省查找城市
				    $.ajax({
		                url : "${ROOT_PATH}/pcd/cityListByProvince.do",
		                data : {"province_id":province_id},
		                type : 'post',
		                dataType : 'json',
		                async : false,
		                success : function(data) {
		                	 //得到回调数据  
		    		        if (data.citys.length != 0) {
		    		        	for (var i = 0; i < data.citys.length; i++) {
		    		                var id = data.citys[i].id;  
		    		                var cityName = data.citys[i].name;
		    		                //添加下拉项  
		    		                $("#city_code").append("<option value="+ id +">"+ cityName +"</option>"); 		    		                
		    		            }
		    		        }
		                },
		                error : function(msg) {
		                	
		                	
		                }
		            });
			    }  
			});
		});
		
		function reloadPage(){
			window.location.href = '${ROOT_PATH}/sysUser';
		}
	</script>
</body>
</html>