var isSubmit = true;
$(document).ready(function(){
	$("#searchBtn").bind("click",function(){
		user.search();
	});
	
	if ($('#userForm').length>0){
		$('#userForm').validate({
			
			rules : {
				"name" : {
					required : true					
				},
				"birthday" : {
					required : true
				},
				"paperId" : {
					required : true
				},
				"nativePlace" : {
					required : true
				},
				"addr" : {
					required : true
				},
				"mobile" : {
					required : true,
					isMobile: true,
	                maxlength:11,
	                minlength:11
				},
				"qq": {
					required : true
				},
				"contactsName1" : {
					required : true
				},
				"contactsMobile1" : {
					required : true,
					isMobile: true,
	                maxlength:11,
	                minlength:11
				},
				
				"contactsName2" : {
					required : true
				},
				"contactsMobile2" : {
					required : true,
					isMobile: true,
	                maxlength:11,
	                minlength:11
				}
				
				
			},
			messages:{
				
				"name" : {
					required : "不能为空"					
				},
				"birthday" : {
					required : "不能为空"		
				},
				"paperId" : {
					required : "不能为空"
				},
				"nativePlace" : {
					required : "不能为空"
				},
				"addr" : {
					required : "不能为空"
				},
				"mobile" : {
					required : "不能为空"
				},
				"qq": {
					required : "不能为空"
				},
				"contactsName1" : {
					required : "不能为空"
				},
				"contactsMobile1" : {
					required : "不能为空"
				},
				
				"contactsName2" : {
					required : "不能为空"
				},
				"contactsMobile2" : {
					required : "不能为空"
					
				}
				
				
			},

	        
	        
	        submitHandler: function(form){  
	        	user.save(form);
	        }
	        
	        
	        
		});
		
		laydate.render({
			  elem: '#birthday'
			});
		$('#birthday').datepicker()
		
		$("#nation").select2({
			placeholder : "请选择",
			allowClear : true
		});
		
		$("#nativePlace").select2({
			placeholder : "请选择",
			allowClear : true
		});

		
	}
	

	
	
/*	$("#domicilePlace").hsCheckData({
        isShowCheckBox: false, //默认为false
        data: cityData
    });*/
	
	
})

var user = {
	
	save : function(form){
		if (isSubmit){
			isSubmit = false;
			
			
			$.ajax({
				  type: 'POST',
				  url: '/api/user/save',
				  dataType:'json',
				  data: {
					  id:$('#id').val(),
					  role:$('#role').val(),
					  status:$('#status').val(),
					  userName:$('#mobile').val(),
					  name:$('#name').val(),
					  gender:$('input:radio[name="gender"]:checked').val(),
					  birthday:$('#birthday').val(),
					  paperType:$('#paperType').val(),
					  paperId:$('#paperId').val(),
					  nation:$('#nation').val(),
					  nationality:$('#nationality').val(),
					  nativePlace:$('#nativePlace').val(),
					  domicilePlace:$('#domicilePlace').val(),
					  addr:$('#addr').val(),
					  education:$('#education').val(),
					  unit:$('#unit').val(),
					  profession:$('#profession').val(),
					  unitAddr:$('#unitAddr').val(),
					  unitTel:$('#unitTel').val(),
					  mobile:$('#mobile').val(),
					  tel:$('#tel').val(),
					  email:$('#email').val(),
					  qq:$('#qq').val(),
					  weixin:$('#weixin').val(),
					  donateBloodCount:$('#donateBloodCount').val(),
					  bloodType:$('#bloodType').val(),
					  height:$('#height').val(),
					  weight:$('#weight').val(),
					  remark:$('#remark').val(),
					  contactsName1:$('#contactsName1').val(),
					  contactsQQ1:$('#contactsQQ1').val(),
					  contactsRelation1:$('#contactsRelation1').val(),
					  contactsWeixin1:$('#contactsWeixin1').val(),
					  contactsMobile1:$('#contactsMobile1').val(),
					  contactsAddr1:$('#contactsAddr1').val(),
					  contactsName2:$('#contactsName2').val(),
					  contactsQQ2:$('#contactsQQ2').val(),
					  contactsRelation2:$('#contactsRelation2').val(),
					  contactsWeixin2:$('#contactsWeixin2').val(),
					  contactsMobile2:$('#contactsMobile2').val(),
					  contactsAddr2:$('#contactsAddr2').val() 
				  },
				  success: function(data){
					  var status=data.status;
					  
					  if(status==1){//注册成功
							bootbox.dialog({
								message:"新增用户成功",
									buttons:{
										"cancel":{
											"label":"返回",
											"className":"btn-sm btn-primary",
											"callback": function () {
												window.location.href = webPath + '/user/list';
					                        }
										},
										"success":{
											"label":"继续添加",
											"className":"btn-sm btn-primary",
											"callback": function () {
												window.location.href = webPath + '/user/add';
					                        }
										}
							
									}
								});
							
						} else if (status == 0){
							isSubmit = true;
							var message=data.message;
							if(message=="userIsExist_userName"){
								bootbox.dialog({
									message:"手机号已存在，新增失败！",
									buttons:{
										"success":{
											"label":"OK",
											"className":"btn-sm btn-primary"

										}
									}
								});
							}else if(message=="userIsExist_paperId"){
								bootbox.dialog({
								message:"证件号已存在，添加失败！",
								buttons:{
									"success":{
										"label":"OK",
										"className":"btn-sm btn-primary"
										}
									}
								});
							}
						} else if (status == 2){
							isSubmit = true;
							bootbox.dialog({
								message:"修改用户成功",
									buttons:{
										"success":{
											"label":"确定",
											"className":"btn-sm btn-primary",
											"callback": function () {
					                        }
										}
							
									}
								});
							
							
						}
					  
					  
				  }
			});
			

			
			
		}
		
	},
	search : function(){
		var role = $("#role").val();
		var status = $("#status").val();
		var searchStr = $("#searchStr").val();
		var url = webPath + "/user/list";
		
		if(role!="" || status!="" || searchStr!=""){
			var param = "?"
			if (role != ""){
				param = param + "role=" + role ;
			}
			
			
			if (status != ""){
				if (param == "?"){
					param = param + "status=" + status;
				} else {
					param = param + "&status=" + status;
				}
				
			}
			if (searchStr != ""){
				if (param == "?"){
					param = param + "searchStr=" + searchStr;
				} else {
					param = param + "&searchStr=" + searchStr;
				}
				
			}
			url = url + param;
		}
		window.location.href = url;
	}
	
}


















