$(function() {
	var O_Type = $("#O_Type");
	var mainlist = $("#mainlist");
	var O_SimpleSearchBtn = $("#O_SimpleSearchBtn");
	var O_Flush = $("#O_Flush");
	O_Flush.hide();
	mainlist.find(".page").click(function() {
		mainlist.find("#loading").show();
		pageClick(this);
	});
	//点击快捷搜索按钮
	O_SimpleSearchBtn.click(function() {
		var v_field = $("#simpleSearch").val();
		var O_Key = $("#simpleSearchKey");
		var v_key = O_Key.val().replace(/ /g, '').replace(/'/g, '');
		//设置列表类型
		if(v_key == ""){ O_Type.val("list"); }
		else { O_Type.val("simplesearch"); }
		O_Flush.show();
		$.post("?m=admin/appMoney.simpleSearch", { field:v_field, key:v_key }, function(data) {
			mainlist.html(data);
			mainlist.find(".page").click(function() {
				mainlist.find("#loading").show();
				pageClick(this);
			});
			O_Flush.hide();
		});
	});
	getCount();
});
//点击状态按钮
function statusClick(v_status) {
	var mainlist = $("#mainlist");
	var O_Flush = $("#O_Flush");
	//设置列表类型
	$("#O_Type").val("bystatus");
	$("#currentStatus").val(v_status);
	O_Flush.show();
	$.post("?m=admin/appMoney.getListByStatus", { status:v_status }, function(data) {
		mainlist.html(data);
		mainlist.find(".page").click(function() {
			mainlist.find("#loading").show();
			pageClick(this);
		});
		O_Flush.hide();
	});
}
//加载列表
function loadList(page, linehighId) {
	var mainlist = $("#mainlist");
	var v_type = $("#O_Type").val();
	var O_Flush = $("#O_Flush");
	O_Flush.show();
	if(v_type == "list"){
		$.post("?m=admin/appMoney.getList", { page:page }, function(data) {
			mainlist.html(data);
			mainlist.find(".page").click(function() {
				mainlist.find("#loading").show();
				pageClick(this);
			});
			if(linehighId) {
				mainlist.find("#O_Row_" + linehighId).addClass("on");//高亮行
			}
			O_Flush.hide();
		});
	} else if(v_type == "simplesearch") {
		var v_field = $("#simpleSearch").val();
		var O_Key = $("#simpleSearchKey");
		var v_key = O_Key.val().replace(/ /g, '').replace(/'/g, '');
		$.post("?m=admin/appMoney.simpleSearch", { field:v_field, key:v_key, page:page }, function(data) {
			mainlist.html(data);
			mainlist.find(".page").click(function() {
				mainlist.find("#loading").show();
				pageClick(this);
			});
			if(linehighId) {
				mainlist.find("#O_Row_" + linehighId).addClass("on");//高亮行
			}
			O_Flush.hide();
		});
	} else if(v_type == "bystatus") {
		var v_status = $("#currentStatus").val();
		$.post("?m=admin/appMoney.getListByStatus", { status:v_status, page:page }, function(data) {
			mainlist.html(data);
			mainlist.find(".page").click(function() {
				mainlist.find("#loading").show();
				pageClick(this);
			});
			if(linehighId) {
				mainlist.find("#O_Row_" + linehighId).addClass("on");//高亮行
			}
			O_Flush.hide();
		});
	}
}
//分页点击
function pageClick(t) {
	loadList(t.id);
}
//首页
function firstPage() {
	var O_FirstPage = document.createElement("span");
	O_FirstPage.id = "1";
	$("#loading").show();
	pageClick(O_FirstPage);
}
//上一页
function prevPage() {
	var prevPage = parseInt($("#O_CurrentPage").val()) - 1;
	var O_PrevPage = $("#" + prevPage.toString());
	O_PrevPage.triggerHandler("click");
}
//下一页
function nextPage() {
	var nextPage = parseInt($("#O_CurrentPage").val()) + 1;
	var O_NextPage = $("#" + nextPage.toString());
	O_NextPage.triggerHandler("click");
}
//尾页
function lastPage() {
	var pageCount = parseInt($("#O_PageCount").val());
	var O_LastPage = document.createElement("span");
	O_LastPage.id = pageCount.toString();
	$("#loading").show();
	pageClick(O_LastPage);
}
//跳转到下拉列
function goPage() {
	var O_Page = $("#O_Page");
	var O_Go = $("#O_Go");
	var O_GoPage  = document.createElement("span");
	O_GoPage.id = O_Page.val();
	$("#loading").show();
	pageClick(O_GoPage);
}
//浏览
function viewClick(t) {
	$("#divView h1 span").html("提现信息浏览");
	var O_Content = $("#contentView");
	O_Content.html('<div style="width:580px;height:200px;_height:300px;font-size:14px;text-align:center;"><span style="margin-top:120px;margin-bottom:15px;">正在加载...</span><img src="theme/admin/images/loading.gif"></div>');
	setView();
	var id = t.id.replace(/o_/g, '');
	highLightRow(id);//高亮行
	$.post("?m=admin/appMoney.info", { id:id }, function(data) {//alert(data);
		O_Content.html(data);
		setView();
	});
	//启动浏览模式
	openView();
}
//添加
function addClick() {
	$("#divUpdate h1 span").html("提现信息添加");
	var O_Content = $("#contentUpdate");
	O_Content.html('<div style="width:580px;height:200px;_height:300px;font-size:14px;text-align:center;"><div style="margin-top:120px;margin-bottom:15px;">正在加载...</div><img src="theme/admin/images/loading.gif"></div>');
	setUpdate();
	$.post("?m=admin/appMoney.add", {}, function(data) {//alert(data);
		O_Content.html(data);
		setUpdate();
		$("#O_Member").change(function() {
			$.post("?m=admin/appMoney.getBalance", { id:$(this).val() }, function(data) {//alert(data);
				$("#balance").html(data.balance);
			}, "json");
		});
		$("#O_SumbitUpdate").attr("disabled", false).unbind("click").click(function() { add(); });
	});
	//启动浏览模式
	openUpdate();
}
//状态更新
function updateStatusClick(t) {
	$("#divUpdate h1 span").html("提现状态更新");
	var O_Content = $("#contentUpdate");
	O_Content.html('<div style="width:580px;height:200px;_height:300px;font-size:14px;text-align:center;"><div style="margin-top:120px;margin-bottom:15px;">正在加载...</div><img src="theme/admin/images/loading.gif"></div>');
	setUpdate();
	var id = t.id.replace(/o_/g, '');
	highLightRow(id);//高亮行
	$.post("?m=admin/appMoney.updateStatus", { id:id }, function(data) {//alert(data);
		O_Content.html(data);
		setUpdate();
		$("#O_SumbitUpdate").attr("disabled", false).unbind("click").click(function() { updateStatus(); });
	});
	//启动浏览模式
	openUpdate();
}
//信息更新
function updateClick(t) {
	$("#divUpdate h1 span").html("提现信息更新");
	var O_Content = $("#contentUpdate");
	O_Content.html('<div style="width:580px;height:200px;_height:300px;font-size:14px;text-align:center;"><div style="margin-top:120px;margin-bottom:15px;">正在加载...</div><img src="theme/admin/images/loading.gif"></div>');
	setUpdate();
	var id = t.id.replace(/o_/g, '');
	highLightRow(id);//高亮行
	$.post("?m=admin/appMoney.update", { id:id }, function(data) {//alert(data);
		O_Content.html(data);
		setUpdate();
		$("#O_SumbitUpdate").attr("disabled", false).unbind("click").click(function() { update(); });
	});
	//启动浏览模式
	openUpdate();
}
//删除
function delClick(t) {
	if(confirm("删除所选提现，确认？")) { 
		var id = t.id.replace(/o_/g, '');
		$.post("?m=admin/appMoney.delHandle", { id:id }, function(data) {
			if(data[0] == "success") {
				//刷新列表
				var page = $("#O_CurrentPage").val();
				loadList(page);
				//刷新统计
				getCount();
			} else {
				alert(data[1]);
			}
		}, "json");
		return true;
	}
	return false;
}
//全选删除
function checkDelAll() {
	$(".delAll").attr("checked", $("#o_delall").attr("checked"));
}
//批量删除
function delSelectClick() {
	if(confirm("删除所选提现，确认？")) { 
		//遍历得到每个checkbox的value值
        var arrChk = $(":input[class='delAll']:checked");  
        var v_ids = "";  
        for (var i = 0; i < arrChk.length; i++){ 
            v_ids += arrChk[i].value;
            if(i < arrChk.length - 1) { v_ids += ","; }
        }
		$.post("?m=admin/appMoney.delSelectHandle", { ids:v_ids }, function(data) {
			//刷新列表
			var page = $("#O_CurrentPage").val();
			loadList(page);
			//刷新统计
			getCount();
		});
		return true;
	}
	return false;
}
//获取统计
function getCount() {
	$.post("?m=admin/appMoney.getCount", {}, function(data) {
		var str = "共 <span class=\"f_14b_f00\">" + data.all + "</span> 个, 已支付 <span class=\"f_14b_f00\">" + data.pay + "</span> 个, 待支付 <span class=\"f_14b_f00\">" + data.wait + "</span> 个, 审核中 <span class=\"f_14b_f00\">" + data.check + "</span> 个, 已取消 <span class=\"f_14b_f00\">" + data.cancel + "</span> 个";
		$("#O_Count").html(str);
		$("#O_Flush").hide();
	}, "json");
}
//高亮行
function highLightRow(id) {
	$(".row").removeClass("on");
	$("#O_Row_" + id).addClass("on");
}
//获取窗体区域
function getRange() {//得到屏幕的大小
    var top = document.body.scrollTop;
    var left = document.body.scrollLeft;
    var height = document.body.clientHeight;
    var width = document.body.clientWidth;
    if (top == 0 && left == 0 && height == 0 && width == 0) {
        top = document.documentElement.scrollTop;
        left = document.documentElement.scrollLeft;
        height = document.documentElement.clientHeight;
        width = document.documentElement.clientWidth;
    }
    return { top: top, left: left, height: height, width: width };
}
//处理
function add() {
	var page = $("#O_CurrentPage").val();
	var O_Member = $("#O_Member");
	var O_Money = $("#O_Money");
	var O_Bank = $("#O_Bank");
	var O_Name = $("#O_Name");
	var O_Account = $("#O_Account");
	var O_Contact = $("#O_Contact");
	var O_Phone = $("#O_Phone");
	var O_Remark = $("#O_Remark");
	
	var v_member = O_Member.val();
	var v_balance = $("#balance").html();
	var v_money = O_Money.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_bank = O_Bank.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_name = O_Name.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_account = O_Account.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_contact = O_Contact.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_phone = O_Phone.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_remark = O_Remark.val();
	var v_status = $("[name='O_Status']:checked").val();
	
    if(v_member == "") {
		alert("请选择会员！");
		O_Member.focus();
	} else if(isNaN(v_money)) {
		alert("提现金额格式错误！");
		O_Money.val("");
		O_Money.focus();
	} else if(v_money <= 0) {
		alert("提现金额必须大于0！");
		O_Money.val("");
		O_Money.focus();
	} else if(parseInt(v_balance) < parseInt(v_money)) {
		alert("账户余额不足！");
		O_Money.focus();
	} else if(v_bank == "") {
		alert("银行名称不能为空！");
		O_Bank.val("");
		O_Bank.focus();
	} else if(v_name == "") {
		alert("开户名不能为空！");
		O_Name.val("");
		O_Name.focus();
	} else if(v_account == "") {
		alert("账号不能为空！");
		O_Account.val("");
		O_Account.focus();
	} else if(v_contact == "") {
		alert("联系人不能为空！");
		O_Contact.val("");
		O_Contact.focus();
	} else if(v_phone == "") {
		alert("手机号不能为空！");
		O_Phone.val("");
		O_Phone.focus();
	} else {
		var O_Submit = $("#O_SumbitUpdate");
		O_Submit.attr("disabled", true);
		$.post("?m=admin/appMoney.addHandle", { memberId:v_member, value:v_money, bank:v_bank, name:v_name, account:v_account, contact:v_contact, phone:v_phone, remark:v_remark, status:v_status }, function(data) {//alert(data);
			if(data[0] == "success") {
				//刷新列表
				var page = $("#O_CurrentPage").val();
				loadList(page);
				//刷新统计
				getCount();
				//隐藏阴影
				closeUpdate();
			} else {
				alert(data[1]);
				if(data[2] == "memberId is null") {
					O_Member.focus();
				} else if(data[2] == "value is null") {
					O_Money.focus();
				} else if(data[2] == "bank is null") {
					O_Bank.focus();
				} else if(data[2] == "name is null") {
					O_Name.focus();
				} else if(data[2] == "account is null") {
					O_Account.focus();
				} else if(data[2] == "contact is null") {
					O_Contact.focus();
				} else if(data[2] == "phone is null") {
					O_Phone.focus();
				}
			}
			O_Submit.attr("disabled",false);
		}, "json");
	}
}
function update() {
	var page = $("#O_CurrentPage").val();
	var id = $("#O_ID").val();
	var O_Money = $("#O_Money");
	var O_Bank = $("#O_Bank");
	var O_Name = $("#O_Name");
	var O_Account = $("#O_Account");
	var O_Contact = $("#O_Contact");
	var O_Phone = $("#O_Phone");
	var O_Remark = $("#O_Remark");
	
	var v_balance = $("#O_Balance").val();
	var v_money = O_Money.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_bank = O_Bank.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_name = O_Name.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_account = O_Account.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_contact = O_Contact.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_phone = O_Phone.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_remark = O_Remark.val();
	
    if(isNaN(v_money)) {
		alert("提现金额格式错误！");
		O_Money.val("");
		O_Money.focus();
	} else if(v_money <= 0) {
		alert("提现金额必须大于0！");
		O_Money.val("");
		O_Money.focus();
	} else if(parseInt(v_balance) < parseInt(v_money)) {
		alert("账户余额不足！");
		O_Money.focus();
	} else if(v_bank == "") {
		alert("银行名称不能为空！");
		O_Bank.val("");
		O_Bank.focus();
	} else if(v_name == "") {
		alert("开户名不能为空！");
		O_Name.val("");
		O_Name.focus();
	} else if(v_account == "") {
		alert("账号不能为空！");
		O_Account.val("");
		O_Account.focus();
	} else if(v_contact == "") {
		alert("联系人不能为空！");
		O_Contact.val("");
		O_Contact.focus();
	} else if(v_phone == "") {
		alert("手机号不能为空！");
		O_Phone.val("");
		O_Phone.focus();
	} else {
		var O_Submit = $("#O_SumbitUpdate");
		O_Submit.attr("disabled", true);
		$.post("?m=admin/appMoney.updateHandle", { id:id, value:v_money, bank:v_bank, name:v_name, account:v_account, contact:v_contact, phone:v_phone, remark:v_remark }, function(data) {//alert(data);
			if(data[0] == "success") {
				//刷新列表
				var page = $("#O_CurrentPage").val();
				loadList(page, id);
				//刷新统计
				getCount();
				//隐藏阴影
				closeUpdate();
			} else {
				alert(data[1]);
				if(data[2] == "value is null") {
					O_Money.focus();
				} else if(data[2] == "bank is null") {
					O_Bank.focus();
				} else if(data[2] == "name is null") {
					O_Name.focus();
				} else if(data[2] == "account is null") {
					O_Account.focus();
				} else if(data[2] == "contact is null") {
					O_Contact.focus();
				} else if(data[2] == "phone is null") {
					O_Phone.focus();
				}
			}
			O_Submit.attr("disabled", false);
		}, "json");
	}
}
function updateStatus() {
	var page = $("#O_CurrentPage").val();
	var O_Submit = $("#O_SumbitUpdate");
	O_Submit.attr("disabled", true);
	var id = $("#O_ID").val();
	var status = $("[name='O_Status']:checked").val();
	$.post("?m=admin/appMoney.updateStatusHandle", { id:id, status:status }, function(data) {//alert(data);
		if(data[0] == "success") {
			//刷新列表
			var page = $("#O_CurrentPage").val();
			loadList(page, id);
			//刷新统计
			getCount();
			//隐藏阴影
			closeUpdate();
		} else {
			alert(data[1]);
		}
		O_Submit.attr("disabled", false);
	}, "json");
}
//点击预订类型
function changeType(obj) {
	changeTypeDo(obj.value);
}
//点击预订类型执行
function changeTypeDo(type) {
	var O_Member = $("#O_Member");
	if(type == "普通会员") {
		O_Member.get(0).options.length = 0;
		O_Member.append("<option value=\"\">==普通会员==</option>");
		$.post("?m=admin/member.getListByStatus", { pageSize:0, status:"已激活", json:true }, function(data) {//alert(data);
			$.each(data, function(id, item) {
				O_Member.append("<option value=\"" + item.id + "\">" + item.account + "</option>");
			});
		}, "json");
	} else if(type == "分销会员") {
		O_Member.get(0).options.length = 0;
		O_Member.append("<option value=\"\">==分销会员==</option>");
		$.post("?m=admin/distribution.getListByStatus", { pageSize:0, status:"已激活", json:true }, function(data) {//alert(data);
			$.each(data, function(id, item) {
				O_Member.append("<option value=\"" + item.id + "\">" + item.account + "</option>");
			});
		}, "json");
	}
}