$(document).ready(function(){
	
	/** 使用div实现复选框的操作 **/
	$(".checkboxStyleTd").click(function(){
	    $(this).toggleClass("checkboxStyle_change");
	    $(this).parent().parent().toggleClass("trChange");
	    var checkboxs = $("table.tableFig tbody").find("td .checkboxStyleTd").length;
	    var checkbox_change = $("table.tableFig tbody").find("td .checkboxStyle_change").length;
	    if(checkbox_change == checkboxs){
	    	$(".checkboxStyleTh").addClass("checkboxStyle_change");
	    }else{
	    	$(".checkboxStyleTh").removeClass("checkboxStyle_change");
	    }
	});
	$(".checkboxStyleTh").click(function(){
	    if($(this).hasClass("checkboxStyle_change")){
	    	$(".checkboxStyleTh").removeClass("checkboxStyle_change");
		    $(".checkboxStyleTd").parent().parent().removeClass("trChange");
		    $(".checkboxStyleTd").removeClass("checkboxStyle_change");
	    }else{
	    	$(".checkboxStyleTh").addClass("checkboxStyle_change");
			$(".checkboxStyleTd").addClass("checkboxStyle_change");
			$(".checkboxStyleTd").parent().parent().addClass("trChange");
		}
	});
	
	//搜索框
	if($("#searchValue").val() == ""){
		$("#searchValue").val("请输入关键字");
	}
	$("#searchValue").focus(function(){
		$(this).val("");
	});
	$("#searchValue").blur(function(){
		if($(this).val()==""){
			$(this).val("请输入关键字");
		}
	});
	
	$("#serachData_img").click(function(){
		refreshData();
	});
	
	// 分页中超链接跳转
	$('#pagecontent .pagination ul li a[href]').click(function(){
		var _page = $(this).attr('href').substr(1);
		$('#myform').find('input:hidden[name="page"]').val(_page);
		refreshData();
	});
		
});

//开启，暂停，删除
function singleOper(obj,url){
	var ids = [];
	var id = parseInt(obj.attr("value"));
	ids.push(id);
	$.ajax({
		async: false, 
		traditional: true, 
		type: "POST",
		url: url,
		data:{"id":ids},
		success: function(data){
			refreshData();
		}
	});
}

//批量操作:批量开启,批量暂停,批量删除
function batchOpear(obj,url){
	var checkboxs = $("table tbody .checkboxStyle_change");
	var ids = [];
	if(checkboxs.length > 0){
		bootbox.confirm("确定" +obj.text() + "吗?", function(ensure) {					
			if (ensure) {	
				checkboxs.each(function(){
					var id = parseInt($(this).attr("id"));
					ids.push(id);
				});
				$.ajax({
					async: false, 
					traditional: true, 
		            type: "POST",
		             url: url,
		             data:{"id":ids},
		             success: function(data){
		            	 refreshData();
		             }
				});
			}
		});
	}else{
		bootbox.alert("请选择需要"+obj.text()+"的数据！");
	}
}

function refreshData(){
	if($("#searchValue").val() == "请输入关键字"){
		$("#searchValue").val("");
	}
	$("#myform").submit();
	return false;
}

function singlePassOrUnpassOper(obj,url,type){
	var ids = [];
	var id = parseInt(obj.attr("value"));
	ids.push(id);
	passOrUnPasshandler(obj,ids,url,type)
}

function batchPassOrUnpassOper(obj,url,type){
	var checkboxs = $("table tbody .checkboxStyle_change");
	var ids = [];
	if(checkboxs.length > 0){
		bootbox.confirm("确定" +obj.text() + "吗?", function(ensure) {					
			if (ensure) {	
				checkboxs.each(function(){
					var id = parseInt($(this).attr("id"));
					ids.push(id);
				});
				passOrUnPasshandler(obj,ids,url,type);
			}
		});
	}else{
		bootbox.alert("请选择需要"+obj.text()+"的数据！");
		return false;
	}
}

function passOrUnPasshandler(obj,ids,url,type){
	if(type == "pass"){
		$.ajax({
			async: false, 
			traditional: true, 
			type: "POST",
			url: url,
			data:{"id":ids},
			success: function(data){
				if(data == "ok"){
					obj.addClass("btn-auditChange");
					obj.removeAttr('disabled');
					refreshData();
				} else {
					obj.removeAttr('disabled');
					bootbox.alert("审核失败!");
				}
			}
		});
	}else if(type == "unpass"){
		 bootbox.prompt("请输入审核不通过的理由", function(result) {
			 if(result === null){
	            obj.removeAttr('disabled');
	         } else if (result == '') {
	        	 obj.removeAttr('disabled');
				 bootbox.alert("请输入审核不通过的理由");
	         } else {
	        	 $.ajax({
	     			async: false, 
	     			traditional: true, 
	     			type: "POST",
	     			url: url,
	     			data:{"id":ids},
	     			success: function(data){
	     				if(data == "ok"){
	            			obj.removeAttr('disabled');
	            			obj.addClass("btn-auditChange");
	            			refreshData();
						} else {
							obj.removeAttr('disabled');
							bootbox.alert("审核失败!");
						}
	     			}
	     		});
	         }
	     });
	}
	return false;
}