var isSubmit = true;

$(document).ready(function(){
	
	if ($('#userForm').length>0){
		$('#userForm').validate({
			
			rules : {
				"name" : {
					required : true				
				},
				"phone" : {
					required : true
				}
				
			},
			messages:{
				"name" : {
					required : "不能为空"					
				},
				"phone" : {
					required : "不能为空"
				}

			},

	        
	        
	        submitHandler: function(form){  
	        	user.save();
	        }
	        
	        
	        
		});
		
		
		laydate.render({
			  elem: '#birthday',
			  format: 'yyyy/MM/dd'
			});
	}
})


var user = {
	children : function(){
		var url = webPath + "/rainbow/user/list?type=0";
		user.search(url);
	},
	caringPeople : function() {
		var url = webPath + "/rainbow/user/list?type=1";
		user.search(url);
	},
	search : function(url){
		var searchStr = $("#searchStr").val();
		
		
		if (searchStr != ""){
			var param = "&searchStr=" + searchStr;		
			url = url + param;
		}
			
		window.location.href = url;
	},
	openlayer: function(id,name) {
		user.userId = id;
		layer.open({
			  type: 1,
			  title:name,
			  skin: 'layui-layer-rim', //加上边框
			  area: ['300px', '200px'], //宽高
			  content: activityHTML,
			  success: function(layero, index){
				  	
			  }
			});

	},
	userId:"",
	validateWish:function(){
		var activityId = $("#activityId").val()
		if (activityId == ""){
			$("#message").html("请选择活动！");
			return;
		}
		$.ajax({
			  type: 'POST',
			  url: '/rainbow/wish/validate',
			  dataType:'json',
			  data: {
				  activityId: $("#activityId").val(),
				  userId:user.userId
				  
			  },
			  success: function(data){
				  if (data.message=="success"){
					  $(".layui-layer-close").click();
					  window.location.href=webPath + "/rainbow/wish/view?userId="+user.userId+"&activityId="+activityId;
				  } else {
					  $("#message").html("已有愿望！")
				  }
				 
			  }
		  })
		
	},
	save : function(form){
		if (isSubmit){
			isSubmit = false;
			var formData = new FormData(); 
			formData.append('file', $('#input_file')[0].files[0]);
			formData.append('id', $('#id').val());
			formData.append('phone', $('#phone').val());
			formData.append('name', $('#name').val());
			formData.append('nickName', $('#nickName').val());
			formData.append('gender', $('input:radio[name="gender"]:checked').val());
			formData.append('birthday', $('#birthday').val());
			formData.append('bedNum', $('#bedNum').val());
			formData.append('disease', $('#disease').val());
			formData.append('receiveName', $('#receiveName').val());
			formData.append('connectTel', $('#connectTel').val());
			formData.append('receiveAddress', $('#receiveAddress').val());
			formData.append('status', $('#status').val());
			
			
			
			$.ajax({
		        url: webPath + "/rainbow/user/save",
		        type: 'POST',
		        cache: false,
		        data: formData,
		        processData: false,
		        contentType: false,
		        dataType:"json",
		        beforeSend: function(){
		            
		        },
		        success : function(data) {
		        	isSubmit = true;
		        	var status = data.status;
		        	var message = data.message;
		        	if (message == "nopic"){
		        		bootbox.dialog({
							message:"请上传图片",
								buttons:{
									"success":{
										"label":"确定",
										"className":"btn-sm btn-primary",
										"callback": function () {
											
				                        }
									}
						
								}
							});
		        	}
		        	
		        	if (status == 1){
		        		bootbox.dialog({
							message:"保存成功",
								buttons:{
									"success":{
										"label":"确定",
										"className":"btn-sm btn-primary",
										"callback": function () {
											history.go(-1);
				                        }
									}
						
								}
							});
		        	}
		        	
		        	if (status == 2){
		        		bootbox.dialog({
							message:"修改成功",
								buttons:{
									"success":{
										"label":"确定",
										"className":"btn-sm btn-primary",
										"callback": function () {
											window.location.reload();
				                        }
									}
						
								}
							});
		        	}
		        	
		        	
		        	
		        }
			})
		}
	}
	
	
}