var isSubmit = true;
$(document).ready(function(){
	
	if ($('#typeForm').length>0){
		$('#typeForm').validate({
			
			rules : {
				"name" : {
					required : true					
				},
				"point" : {
					required : true
				}
				
				
				
			},
			messages:{
				"name" : {
					required : "不能为空"					
				},
				"point" : {
					required : "不能为空"
				}

			},

	        
	        
	        submitHandler: function(form){  
	        	points.save(form);
	        }
	        
	        
	        
		});
		
		
		
	}
	

	
})

var points = {
	
	remove : function(id){
		
		bootbox.confirm({
			buttons: {
				cancel: {
					label: '取消',
					className: 'btn-default'
				},
				confirm: {
					label: '确认',
					className: 'btn-myStyle'
				}
				
			},
			message: '请确认是否删除?',
			callback: function(result) {
				if(result) {
					$.ajax({
						  type: 'GET',
						  url: webPath + '/points/type/del?id='+id,
						  dataType:'json',
						  success: function(data){
							  window.location.reload();
						  }
					})
				} else {
					
				}
			},
			title: "删除"
		    });
		
	},
	save : function(form){
		if (isSubmit){
			isSubmit = false;
						
			var formData = new FormData(); 
			
			formData.append('id', $('#id').val());
			formData.append('name', $('#name').val());
			formData.append('point', $('#point').val());
			formData.append('available', $('#available').val());

			$.ajax({
			    url: webPath + '/points/type/save',
			    type: 'POST',
				cache: false,
				data: formData,
				processData: false,
				contentType: false,
				dataType:"json",
				beforeSend: function(){
				    
				},
				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 + '/points/type/list';
				                        }
									},
									"success":{
										"label":"继续添加",
										"className":"btn-sm btn-primary",
										"callback": function () {
											window.location.href = webPath + '/points/type/add';
				                        }
									}
						
								}
							});
						
					} else if (status == 2){
						isSubmit = true;
						bootbox.dialog({
							message:"修改成功",
								buttons:{
									"success":{
										"label":"确定",
										"className":"btn-sm btn-primary",
										"callback": function () {
											window.location.reload();
				                        }
									}
						
								}
							});
						
						
					}
				  
				  
				}
			});
		}
			

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


















