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

			},

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

	
})

var gift = {
	
	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 + '/gift/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('file', $('#input_file')[0].files[0]);
			formData.append('id', $('#id').val());
			formData.append('name', $('#name').val());
			formData.append('content', $('#content').val());
			formData.append('point', $('#point').val());
			formData.append('total', $('#total').val());
			formData.append('stock', $('#stock').val());

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

		
	}
	
}


















