var isSubmit = true;
$(document).ready(function(){
	$("#activity").select2({
		placeholder : "请选择所属活动",
		allowClear : true
	});
	
	$('.slim-scroll').each(function () {
		var $this = $(this);
		$this.slimScroll({
			height: $this.data('height') || 65,
			railVisible:true
		});
	});
	
	
	/*if ($('#wish_form').length>0){
		$('#wish_form').validate({
			
			rules : {
				"title" : {
					required : true					
				},
				"content" : {
					required : true
				}
				
			},
			messages:{
				"title" : {
					required : "不能为空"					
				},
				"content" : {
					required : "不能为空"
				}

			},

	        
	        
	        submitHandler: function(form){  
	        	wish.save();
	        }
	        
	        
	        
		});
	}*/
		
})


var wish = {
	
	exportExcel : function(){
		var activityId = $("#activity").val();
		var searchStr = $("#searchStr").val();
		var status = $("#status").val();
		var type = $("#type").val();
		var url = webPath + "/rainbow/wish/export";
		
		if (searchStr != ""){
			if (type == ""){
				bootbox.dialog({
					message:"请选择要查询的用户类型！",
					buttons:{
						"success":{
							"label":"OK",
							"className":"btn-sm btn-primary"
							}
						}
					});
				
				return;
			}
			
		}
		
		if(activityId != "" || searchStr != "" || status != ""){
			var param = "?"
			if (activityId != ""){
				param = param + "activityId=" + activityId ;
			}
			
			if (status != ""){
				if (param == "?"){
					param = param + "status=" + status;
				} else {
					param = param + "&status=" + status;
				}
			}
			
			if (type != ""){
				if (param == "?"){
					param = param + "type=" + type;
				} else {
					param = param + "&type=" + type;
				}
			}
			
			if (searchStr != ""){
				if (param == "?"){
					param = param + "searchStr=" + searchStr;
				} else {
					param = param + "&searchStr=" + searchStr;
				}
				
			}
			url = url + param;
		}
		window.open(url);
	},
	sort : function(order){
		wish.orderValue = order;
		var codeAsc = $("#codeAsc");
		var codeDesc = $("#codeDesc");
		if (codeAsc.css("display") != "none" && codeDesc.css("display") != "none"){
			codeAsc.css("display","") ;	
			wish.sortValue = "asc" ;
			wish.search();
			return;
		}else if (codeAsc.css("display") != "none" && codeDesc.css("display") == "none"){
			codeDesc.css("display","") ;
			codeAsc.css("display","none") ;	
			wish.sortValue = "asc" ;
			wish.search();
			return;
		}else if (codeAsc.css("display") == "none" && codeDesc.css("display") != "none"){
			codeAsc.css("display","") ;	
			codeDesc.css("display","none") ;
			wish.sortValue = "desc" ;
			wish.search();
			return;
		}
		
	},
	sortValue : "",
	orderValue : "",
	isSearch:false,
	search : function(){
		
		if (wish.isSearch){
			return;
		}
		wish.isSearch = true;
		var activityId = $("#activity").val();
		var searchStr = $("#searchStr").val();
		var status = $("#status").val();
		var type = $("#type").val();
		var url = webPath + "/rainbow/wish/list";
		
		if (searchStr != ""){
			if (type == ""){
				bootbox.dialog({
					message:"请选择要查询的用户类型！",
					buttons:{
						"success":{
							"label":"OK",
							"className":"btn-sm btn-primary",
							"callback": function () {
								wish.isSearch = false;
	                        	}
							}
						}
					});
				
				return;
			}
			
		}

		if(activityId != "" || searchStr != "" || status != "" || wish.sortValue != "" || wish.orderValue != ""){
			var param = "?"
			if (activityId != ""){
				param = param + "activityId=" + activityId ;
			}
			
			if (status != ""){
				if (param == "?"){
					param = param + "status=" + status;
				} else {
					param = param + "&status=" + status;
				}
			}
			
			if (type != ""){
				if (param == "?"){
					param = param + "type=" + type;
				} else {
					param = param + "&type=" + type;
				}
			}
			
			if (wish.sortValue != ""){
				if (param == "?"){
					param = param + "sort=" + wish.sortValue;
				} else {
					param = param + "&sort=" + wish.sortValue;
				}
			}
			
			if (wish.orderValue != ""){
				if (param == "?"){
					param = param + "order=" + wish.orderValue;
				} else {
					param = param + "&order=" + wish.orderValue;
				}
			}
			
			if (searchStr != ""){
				if (param == "?"){
					param = param + "searchStr=" + searchStr;
				} else {
					param = param + "&searchStr=" + searchStr;
				}
				
			}
			url = url + param;
		}
		window.location.href = url;
	},
	batchUpdateStatus : function(){
		var ids = []
		$("#table-data input[name='id']:checked").each(function(index){
			ids.push($(this).val())
		})
		
		if (ids.length==0){
			bootbox.dialog({
				message:"请选择愿望！",
				buttons:{
					"success":{
						"label":"OK",
						"className":"btn-sm btn-primary"
						}
					}
				});
		} else {
			wish.openlayer();
		}
		
		
		//页面层
		
		
//		$.ajax({
//			  type: 'POST',
//			  url: '/rainbow/wish/status/update',
//			  dataType:'json',
//			  data: {
//				  status:
//				  
//			  }
//		  })
		
	},
	openlayer: function() {
		var html = '<div style="display: block;margin-left:auto;margin-right:auto;margin-top:20px;text-align: center;">'
				  +'<select class="" id="changeStatus" >'
				  +'<option value="">请选择</option>'
				  +'<option value="0">发布</option>'
				  +'<option value="1">领取</option>'
				  +'<option value="2">寄送中</option>'
				  +'<option value="3">红会代收</option>'
				  +'<option value="4">完成</option>'
				  +'</select>'
				  +'<button class="btn-xs" onclick="wish.updateStatus()">确定</button>'
				  +'</div>'

	
		layer.open({
			  type: 1,
			  skin: 'layui-layer-rim', //加上边框
			  area: ['300px', '200px'], //宽高
			  content: html
			});

	},
	updateStatus: function() {
		var ids=[];
		$("#table-data input[name='id']:checked").each(function(index){
			ids.push($(this).val())
		})
		$.ajax({
			  type: 'POST',
			  url: '/rainbow/wish/status/update',
			  dataType:'json',
			  data: {
				  status: $("#changeStatus").val(),
				  id:ids
				  
			  },
			  success: function(data){
				  $(".layui-layer-close").click();
				  bootbox.dialog({
						message:"修改状态成功",
							buttons:{
								
								"success":{
									"label":"确定",
									"className":"btn-sm btn-primary",
									"callback": function () {
										window.location.reload();
			                        }
								}
					
							}
						});
			  }
		  })
	},
	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('code', $('#code').val());
			formData.append('activityId', $('#activityId').val());
			formData.append('rainbowPlanUserId', $('#rainbowPlanUserId').val());
			formData.append('content', $('#content').val());
			formData.append('status', $('#status').val());
			
			//console.info($('#reset'));
			//alert(typeof($('#reset').val()) != "undefined");
			if (typeof($('#reset').val()) != "undefined" ){
				formData.append('reset', $('#reset').val());
			}
		
			
			$.ajax({
		        url: webPath + "/rainbow/wish/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();
				                        }
									}
						
								}
							});
		        	}
		        	
		        	
		        	
		        }
			})
		}
	}
	
	
	
}