<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/jsp/common/taglibs.jspf"%>
<div class="modal-dialog" style="width: 860px;"><!-- BEGIN modal-dialog -->
     <div class="modal-content"><!-- BEGIN modal-content -->
         <div class="modal-header">
             <button type="button" class="close" data-dismiss="modal">&times;</button>
             <h4 class="modal-title">新增用户</h4>
         </div>
         <form action="system/user/addUser.do" id="form_user_tables" method="post"><!-- BEGIN FORM -->
         	<!-- <input name="userId" id="userId" type="hidden" value=""> -->
         	<div class="modal-body">
            	<div class="row form-group">
                	<div class="col-sm-2">
						<label class="span">用户名称</label>
					</div>
					<div class="col-sm-4">
                    	<input name="userName" id="userName" type="text" class="form-control" placeholder="请输入用户名称">
                     </div>
                     <div class="col-sm-2">
						<label class="span">密码</label>
					</div>
					<div class="col-sm-4">
                    	<input name="loginPassword" id="loginPassword" type="password" class="form-control" placeholder="请输入密码">
                    </div>
                 	</div>
            		<div class="row form-group">
                    	<div class="col-sm-2">
							<label class="span">登录名称</label>
						</div>
							<div class="col-sm-4">
                       		<input name="loginName" id="loginNameAdd" type="text" class="form-control" placeholder="请输入登录名称">
                     	</div>
                     	<div class="col-sm-2">
							<label class="span">确认密码</label>
						</div>
						<div class="col-sm-4">
                       		<input name="password2" id="password2" type="password" class="form-control" placeholder="请输入确认密码">
                     	</div>
            		</div>
            		<div class="row form-group">
            		    <div class="col-sm-2">
							<label class="span">用户角色</label>
						</div>
						<div class="col-sm-4">
                        	<select id="roleId"  name="roleId" class="form-control">
                        		<option value="" selected="selected">请选择</option>
								<c:forEach items="${roleList}" var="roleList">
							    	<option value="${roleList.roleId}">
							    		${roleList.roleName}
							    	</option>
								</c:forEach>
                          	</select>
                     	</div>
            		</div>
             </div>
             <div class="modal-footer clearfix">
                 <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-times"></i>取消</button>
                 <button type="submit" class="btn btn-primary pull-left"><i class="fa fa-envelope"></i>确定</button>
             </div>
         </form><!-- END FORM -->
     </div><!-- END modal-content -->
</div><!-- END modal-dialog -->
<script type="text/javascript">
$(function() {
	// 验证表单
	var form = $('#form_user_tables');
	
	//检查登录名是否符合规范
  	var customError = '';
	jQuery.validator.addMethod("checkLoginName", function(value, element) {
		var returnVal = true;
		if(this.optional(element) || /^[0-9]+$/.test(value)){
			return true;
		}
		if(this.optional(element) || !/^[a-zA-Z].*$/.test(value)){
			customError='账号只能以字母开头';
			returnVal = false;
		}else if(this.optional(element) || !/^.*[a-zA-Z0-9]$/.test(value)){
			customError='账号只能以字母或数字结尾';
			returnVal = false;
		}else if(this.optional(element) || !/^[\w\.\-@]+$/.test(value)){
			customError='账号应为字母、数字、点、@、减号或下划线';
			returnVal = false;
		}
		jQuery.validator.messages.checkLoginName = customError;
		return returnVal;
	 }, customError);
	
	// 异步检测登录名是否可注册
	jQuery.validator.addMethod("doCheckLoginName", function(value, element) {
		var account = $("#loginNameAdd").val();
		var t = new Date();
		var result = "false";
		$.ajax({
		   type: "POST",
		   async: false,
		   url: "system/user/checkLoginName.jhtml",
		   data: {loginName:account, t:t},
		   dataType: "text",
		   success: function(res){
			   if(res == "true") result = "true";
		   }
		});
	 	return this.optional(element) || (result == "true");
	 }, "账号已经存在!");
	//检测登录信息是否正确
	function checkLoginInfo(){
		return form.element($("#loginNameAdd"));
	}
    form.validate({
		errorElement: 'span',errorClass: 'help-inline',focusInvalid: false,ignore: "",
      	rules: {userName:{required: true,maxlength:25},//用户名称
    			loginName: {required: true,minlength:4,maxlength:30,checkLoginName:true,doCheckLoginName:true},// 用户登录名称
    			loginPassword:{required: true,minlength: 6,maxlength:16},
    			password2:{required: true,minlength: 6,equalTo: "#loginPassword"},
				email:{required:true,email: true},//自带邮箱验证
    	  		userType:{required:true},
    	  		companyName:{required:true},
    	  		//职位
    	  		userPosition:{required:true},
    	  		businessLicense:{required:true,filetype: ["jpg", "png"]},
    	  		orgCode: {required: true,filetype: ["jpg", "png"]}
      },
      messages:{userName:{required: "请输入用户名称!", maxlength:"用户名称最大长度为25"},
	    	  loginName:{required:"请输入登录名称!", minlength:"登录名称最小长度为4", maxlength:"登录名称最大长度为30", remote:"账号已存在"},
	    	  loginPassword: {required: "请输入密码", minlength: "密码不能小于{0}个字符", maxlength: "密码不能大于{0}个字符"},
	    	  password2: {required: "请输入确认密码", minlength: "确认密码不能小于6个字符",equalTo: "两次输入密码不一致不一致"},
	          email: {required: "请输入Email地址", email: "请输入正确的email地址"},
	          userType: {required: "请选择用户类型!"},
	          companyName: {required: "请输入公司名称!"},
	          userPosition: {required: "请输入职位!"},
	    	  businessLicense:{required:"请选择图片文件!",filetype: "只能上传 jpg, png 格式文件"},
	    	  orgCode:{required:"请选择图片文件!", filetype: "只能上传 jpg, png 格式文件"}
	  },
	  highlight: function (element) {
          $(element).closest('.help-inline').removeClass('ok');
          $(element).closest('.control-group').removeClass('success').addClass('error');
      },
      unhighlight: function (element) {
          $(element).closest('.control-group').removeClass('error');
      },
	  submitHandler: function (form) {
			var loginName = $('#loginNameAdd').val();
      		$.ajax({
				async: false,
				url : 'system/user/validationLongName.do?loginName='+loginName,
				type : 'post',
				contentType: 'application/json;utf-8',
				dataType : 'json',
				success : function(data) {
					if(data.result){
						platformDanger("登录名称被占用，请重新输入！");
						$('#loginNameAdd').val('');
						$('#loginNameAdd').focus();
					}else{
						var parentId = $("#parentId").val();
						//如果存在主账号则验证此主账号是否还存在主账号如果还存在主账号则不允许添加
						if (parentId) {
							checkUserIsChildAccount(form ,'system/user/checkUserIsChildAccount.do', parentId);
						} else {
							form.submit();
						}
						
					}
				}
  			});
     	}
  	});
	//时间插件加载
	$("#birthday").datetimepicker({
		language : 'zh-CN',
		format : 'yyyy-mm-dd',
		todayBtn : true,
		autoclose : true,
		todayHighlight : true,
		startView : 2,
		minView : 2,
		maxView : 4,
		endDate : new Date()
	});
	//主账号的搜索功能
	getUserListByLoginName('#parentAccountNameId', '#parentId' ,'system/user/getUserByUserLoginName.do');
	$("#userType").change(function() {
		if ($(this).val() != '20') {
			$("#mainAccountId").hide();
		} else {
			$("#mainAccountId").show();
		}
	});
});
</script>