
var pageCount;//总页数
var currentPage;//当前页

function adminLoad()
{
	userLoginList(1);
}

//用户列表
function userLoginList(page)
{
	currentPage = page;
	$('#currentPage').text(currentPage);//显示当前页码
	var nameKey = $('#nameKey').val();
	var userType = $('#userType').val();
	var actId = $('#activityId').val();
	var startDate = $('#startDate').val();
	var endDate = $('#endDate').val();
	
	$.ajax
	({
		url:'././userManageAction.do?action=userLoginList&page='+page+'&nameKey='+encodeURIComponent(nameKey)+'&type='+userType+'&activityId='+actId+'&startDate='+startDate+'&endDate='+endDate,
		type:'GET',
		error:function(){alert('网络连接异常！！！')},
		success:function(data)
		{
			data = eval("("+data+")");
			var items = data.items;
			var count = data.count;
			pageCount = data.pageCount;
			$('#num_id').text(count);
			$('#pageNum').text("共"+pageCount+"页");
			$('.context').empty();
			for(var i=0; i<items.length; i++)
			{
				var selected_sx = "";
				var button = "";//按钮
				
				var checkbox = "-";//多选框
				if (items[i].onLineName=='-'){
					var activityIds = items[i].activityIds;
					var tmp = "";
					if (activityIds != "" && activityIds.indexOf('101') != -1)
					{
						tmp = "checked";
						button = button + "<input type='button' value='编辑返点' onclick='skipRate(this,0)'/>";	
					}
					checkbox = "<input type='checkbox' name='activity' "+tmp+" value='101'>返点</input>";	
				}
				
				if(items[i].type=='1'){
					selected_sx = " selected='selected'";
					button = button + "<input type='button' value='编辑代理配额' onclick='skipRate(this,2)'/>";
				}
				var trNode =
				"<tr>"+
					"<input type='hidden' value='"+items[i].loginId+"' id='loginId'/>"+
					"<input type='hidden' value='"+items[i].userId+"' id='userId'/>"+
					"<input type='hidden' value='"+items[i].loginName+"' id='loginName'/>"+
					"<input type='hidden' value='"+items[i].nickName+"' id='nickName'/>"+
					"<input type='hidden' value='"+items[i].balance+"' id='balance'/>"+
					"<input type='hidden' value='"+items[i].awardBets+"' id='awardBets'/>"+
					"<input type='hidden' value='"+items[i].freezedMoney+"' id='freezedMoney'/>"+
					"<input type='hidden' value='"+items[i].sourceId+"' id='sourceId'/>"+//给一个链接
					"<td align='center' class='tab_content'>"+items[i].loginName+"</td>"+
				/*	"<td align='center' class='tab_content'>"+
						"<select id='type'>"+
							"<option value='0'>普通</option>"+
							"<option value='1'"+selected_sx+">代理</option>"+
						"</select>"+
					"</td>"+*/
					"<td align='center' class='tab_content'>"+sorceType[items[i].sourceId]+"</td>"+
					"<td align='center' class='tab_content'>"+formatTime(items[i].registerTime)+"</td>"+
					"<td align='center' class='tab_content'>"+items[i].idCardNumber+"</td>"+
					"<td align='center' class='tab_content'>"+items[i].cardAccount+"</td>"+
					"<td align='center' class='tab_content'>"+items[i].bank+"</td>"+
					"<td align='center' class='tab_content'>"+items[i].userTel+"</td>"+
					
					"<td align='center' class='tab_content'>"+
						checkbox+
					"</td>"+
					"<td align='center' class='tab_content'>"+items[i].zhye+"</td>"+
					"<td align='center' class='tab_content'>"+items[i].balance+"</td>"+
					"<td align='center' class='tab_content'>"+items[i].awardBets+"</td>"+
					"<td align='center' class='tab_content'>"+items[i].bets+"</td>"+
					"<td align='center' class='tab_content'>"+items[i].freezedMoney+"</td>"+
					"<td align='center' class='tab_content'>"+
						//"<input type='button' value='修改用户' onclick='updateUserLogin(this)'/>"+
						"<input type='button' value='用户充值' onclick='getAdminUserInfo(this)'/>"+
						//"<input type='button' value='删除用户' onclick='deleteUserLogin(this)'/>"+
						button+
						"<input type='button' value='转账' onclick='turnMoneyForm(this)'/>"+
					"</td>"+
				"</tr>";
				$('.context').append(trNode);
			}
			$('#data tbody tr:even').css("backgroundColor","#CCCCCC");
		}
	});
}

//打开转账form
function turnMoneyForm(_this)
{
	var trNode = $(_this).parent().parent();
	var loginName = trNode.find("#loginName").val();
	var balance = trNode.find("#balance").val();
	var awardBets = trNode.find("#awardBets").val();
	var freezedMoney = trNode.find("#freezedMoney").val();
	var money = (balance*1+awardBets*1-freezedMoney*1).toFixed(2);
	$("#userName").text(loginName);
	$("#money").text(money);
	$('.userInfo_div_ZZ').show();
}

//取消按钮
function cannel_zz(){
	$('.userInfo_div_ZZ').hide();
	$("#turnMoney").val("");
	$("#incomeUserName").val("");
}

//转账操作
function turnMoney()
{
	var userName = $("#userName").text();
	var money = $("#money").text();
	var turnMoney = $("#turnMoney").val();
	var incomeUserName = $("#incomeUserName").val();
	if (turnMoney== "" || isNaN(turnMoney) || turnMoney==0)
	{
		alert("转款金额输入不正确！！！");
		return;
	}
	if ((turnMoney*1)>(money*1))
	{
		alert("转款人没有足够的余额！！！");
		return;
	}
	if (incomeUserName=="")
	{
		alert("请输入收款用户名！！！");
		return;
	}
	if (userName==incomeUserName)
	{
		alert("不能为自己转账！！！");
		return;
	}
	$.ajax
	({
		url:'././userManageAction.do?action=userTurnMoney&userName='+encodeURIComponent(userName)+'&turnMoney='+turnMoney+'&incomeUserName='+encodeURIComponent(incomeUserName),
		type:'GET',
		error:function(){alert('转账失败！！！')},
		success:function(data)
		{
			if (data == '0000')
			{
				alert("转账成功！！！");
				cannel_zz();	
			}
			else if (data == '1111')
			{
				alert("转账失败！！！");
			}
			else if (data == '2222')
			{
				alert("用户不存在！！！");	
			}
			userLoginList(currentPage);
		}
	 });
}

//修改用户信息
function updateUserLogin(_this)
{
	if (confirm("确定修改？"))
	{
		var trNode = $(_this).parent().parent();
		var loginId = trNode.find("#loginId").val();
		var type = trNode.find("#type").val();
		var activityIds = "";
		var checkbox = trNode.find("input:checkbox");
		if (checkbox.length > 0)
		{
			for (var i=0; i<checkbox.length; i++)
			{
				if (checkbox.eq(i).attr("checked")==true)
				{
					activityIds = activityIds + checkbox.eq(i).val() + ",";	
				}
			}
		}
		//if (activityIds.endsWith(","))
		//{
			activityIds = activityIds.substring(0,activityIds.length-1);
		//}
		$.ajax
		({
			url:'././userManageAction.do?action=updateUserLogin&loginId='+loginId+'&type='+type+'&activityIds='+activityIds,
			type:'GET',
			error:function(){alert('修改失败！！！')},
			success:function(data)
			{
				if (data > 0)
				{
					alert('修改成功');
					userLoginList(currentPage);
				}
			}
		 });
	}
}

//删除用户
function deleteUserLogin(_this)
{
	if (confirm("确定删除？"))
	{
		var trNode = $(_this).parent().parent();
		var loginId = trNode.find("#loginId").val();
		var type = trNode.find("#type").val();
		var activityIds = "";
		var checkbox = trNode.find("input:checkbox");
		if (checkbox.length > 0)
		{
			for (var i=0; i<checkbox.length; i++)
			{
				if (checkbox.eq(i).attr("checked")==true)
				{
					activityIds = activityIds + checkbox.eq(i).val() + ",";	
				}
			}
		}
		//if (activityIds.endsWith(","))
		//{
			activityIds = activityIds.substring(0,activityIds.length-1);
		//}
		$.ajax
		({
			url:'././userManageAction.do?action=deleteUserLogin&loginId='+loginId+'&type='+type+'&activityIds='+activityIds,
			type:'GET',
			error:function(){alert('删除失败！！！')},
			success:function(data)
			{
				if (data > 0)
				{
					alert('删除成功');
					userLoginList(currentPage);
				}
			}
		 });
	}
}

//跳至编辑汇率页面
function skipRate(_this,rateType)
{
	var trNode = $(_this).parent().parent();
	var userId = trNode.find("#userId").val();
	var loginName = trNode.children("td").eq(0).text();
	var url = "";
	if (rateType == 1)
	{//加奖
		url='LetouBack/detailPage/editRateJiaJiang.jsp?userId='+userId+'&loginName='+loginName;	
	}
	else
	{
		url='LetouBack/detailPage/editRate.jsp?userId='+userId+'&type='+rateType+'&loginName='+loginName;
	}
	location.href=rootUrl+url;
}

//页数操作
function checkPage(_this)
{
	var tmpPage = 1;
	if($(_this).attr('id')=='aa'){
		tmpPage = pageCount;
	}else if($(_this).attr('id')=='back'){
		if (currentPage > 1){
			tmpPage = currentPage-1;
		}
	}else if($(_this).attr('id')=='next'){
		if (currentPage < pageCount){
			tmpPage = currentPage+1;	
		}
		if (currentPage >= pageCount){
			tmpPage = pageCount;	
		}
	}
	userLoginList(tmpPage);
}
//格式化时间
function formatTime(time){
	var dt = "";
	if (time != "" && time != 'null'){
		var year=time.substring(0,4);
		var month=time.substring(4,6);
		var day=time.substring(6,8);
		var hours=time.substring(8,10);
		var minute=time.substring(10,12);
		var seconds=time.substring(12,14);
		dt=year+"-"+month+"-"+day+" "+hours+":"+minute+":"+seconds;
	}
	return dt;
}
var sorceType={
		'1001':'四合网站',
		'1002':'四合短信',
		'1003':'手机网站',
		'2001':'WEB',
		'1009':'多麦'
	}