$(document).ready(function(){
    //loading
    $('#loadingContainer').ajaxStart(function(){
	    $(this).show();
	    $('#loadingbackground').show().css({"opacity": "0.5"});
	}).ajaxStop(function(){
	    $(this).hide();
	});
	$('.datepicker').datepicker();
	Start();
	
	$('#ddlFrom').change(function(){
		Start();
	});
	
	$('#btnSearch').click(function(){
		Start();
	});
	
	$('th[@name^=sort]').click(function(){
		$('th[@name^=sort]').css('color','');
		$(this).css('color','blue');
		if($(this).attr('name') == "sort1")
		{
			$('#sort').val('Order By Quantity Desc');
		}
		else
		{
			$('#sort').val('Order By TotalPrice Desc');
		}
		Start();
	});
	
});

function Start()
{
	var memberTotal = SearchMemberCountAjax('MemberStatistical.aspx','type=ajax&Action=SearchMemberCountAjax&fid=' + $('#ddlFrom').val());
	$('#memberTotal').html(memberTotal);
	var memberOrderTotal = SearchMemberOrderAjax('MemberStatistical.aspx','type=ajax&Action=SearchMemberOrderAjax&from=' + $.trim($('#txtDateTimeFrom').val()) + '&to=' + $.trim($('#txtDateTimeTo').val()) + '&fid=' + $('#ddlFrom').val() + '&sort=' + $('#sort').val());
	
}

function SearchMemberCountAjax(weburl,urldata){
	var shtml = '';
	$.ajax({
        type:"get",
        url:weburl+'?NoCopyRight=1',
        data:urldata,
        cache:false,
		async:false,
        success:function(msg){
			var mmsg = msg.split(',');
			shtml += '<td>' + mmsg[0] + '</td>';
			shtml += '<td>' + mmsg[1] + '</td>';
			shtml += '<td>' + mmsg[2] + '%</td>';
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus == 'parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });
	return shtml;
}

function SearchMemberOrderAjax(weburl,urldata){
	$.ajax({
        type:"get",
		dataType:"json",
        url:weburl+'?NoCopyRight=1',
        data:urldata,
        cache:false,
		async:false,
        success:function(msg){
			var data = msg.MemberOrder;
			var row = '';
			$.each(data,function(i,n){
				row += '<tr><td align="center">' + (i + 1) + '</td>';
				row += '<td>' + n.MemberName + '</td>';
				row += '<td>' + n.Realname + '</td>';
				row += '<td align="center">' + n.Quantity + '</td>';
				row += '<td align="center">' + n.TotalPrice + '</td></tr>';
			});
			$('#memberOrderTotal').nextAll().remove();
			$('#memberOrderTotal').after(row);
			$('#memberOrderTotal').hide();
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });
}