
var PageSize = 20;//每页计数
var CurrentPage = 1; //当前页
//搜索条件
var searchUrl = '';
var data_adminMemberID = '';
var data_orderNo = '';
var data_name = '';
var data_IsConfirmed = '';
var data_IsPayed = '';
var data_IsShipped = '';
var data_IsFinished = '';
var data_IsCancelled = '';
var data_DateFrom = '';
var data_DateTo = '';

$(document).ready(function(){
    //loading
    $('#loadingContainer').ajaxStart(function(){
	    $(this).show();
	    $('#loadingbackground').show().css({"opacity": "0.5"});
	}).ajaxStop(function(){
	    $(this).hide();
	});
	
	setInterval(function(){
        $('div.sub_container_page').css('top',$(window.parent.frames.right).height()-$('div.sub_container_page').height()); //设置底部分页区域的位置
        $('div.sub_container_content').css('height',$(window.parent.frames.right).height()-$('div.sub_container_title').height()-$('div.sub_container_operation').height()-$('div.sub_container_listhead').height()-$('div.sub_container_page').height()-10); //设置内容区域的高度
        if($.browser.mozilla){setList();}
        $('#frmHeight').val($(window.parent.frames.right).height());//将窗口高度用hidden记录,在iframe中取值
    },100);
	
	data_adminMemberID = (getQueryStringRegExp('AdminMemberID') != '' ? '&AdminMemberID=' + getQueryStringRegExp('AdminMemberID') : '');
	data_IsConfirmed = (getQueryStringRegExp('AdminMemberID') != '' ? '&AdminMemberID=' + getQueryStringRegExp('AdminMemberID') : '');
	
	if(data_adminMemberID != ''){searchUrl = data_adminMemberID}else{searchUrl = '&IsConfirmed=0'};
	if(getQueryStringRegExp('test') == ''){
	    BindList('type=ajax&Action=BindOrderList&PageSize=' + PageSize + '&Page=' + CurrentPage + searchUrl);
	}
	
	//搜索
	$('#btnSearch').click(function(){
	    data_orderNo = $('#ddlKeyType').val() == 'OrderNo' ? '&OrderNo=' + escape($('#txtKeyword').val().Trim()) : '';
	    data_name = $('#ddlKeyType').val() == 'Name' ? '&Name=' + escape($('#txtKeyword').val().Trim()) : '';
	    data_IsConfirmed = $('#ddlConfirmed').val() > -1 ? '&IsConfirmed=' + $('#ddlConfirmed').val() : '';
	    data_IsPayed = $('#ddlPayed').val() > -1 ? '&IsPayed=' + $('#ddlPayed').val() : '';
	    data_IsShipped = $('#ddlShipped').val() > -1 ? '&IsShipped=' + $('#ddlShipped').val() : '';
	    data_IsFinished = $('#ddlFinished').val() > -1 ? '&IsFinished=' + $('#ddlFinished').val() : '';
	    data_IsCancelled = $('#ddlCancelled').val() > -1 ? '&IsCancelled=' + $('#ddlCancelled').val() : '';
	    
	    var datefrom = $('#txtDateFrom').val().Trim();
	    var dateto = $('#txtDateTo').val().Trim();
	    if((datefrom + dateto) != ''){
	        if(datefrom == ''){alert('请选择开始时间');return false;}
	        if(dateto == ''){alert('请选择结束时间');return false;}
	        
	        data_DateFrom = '&DateFrom=' + datefrom;
	        data_DateTo = '&DateTo=' + dateto;
	    }else{
	        data_DateFrom = data_DateTo = '';
	    }
	    
	    searchUrl = data_orderNo + data_name + data_IsConfirmed + data_IsPayed + data_IsShipped + data_IsFinished + data_IsCancelled + data_DateFrom + data_DateTo + data_adminMemberID;
	    CurrentPage = 1;
	    BindList('type=ajax&Action=BindOrderList&PageSize=' + PageSize + '&Page=' + CurrentPage + searchUrl);
        return false;
    });
    
    $('#txtDateFrom').click(function(){show_calendar('Form1.txtDateFrom');});
    $('#txtDateTo').click(function(){show_calendar('Form1.txtDateTo')});
	
	//全选
    $('input[@name=chkAll]').click(function(){
        $('input[@name=chkItem]').attr('checked',$(this).attr('checked'));
    });
    
    //批量删除
    $('#btnDelete').click(function(){
        var ids = '';
        $('input[@name=chkItem][@checked]').each(function(){
            ids += $(this).val() + ',';
        });
        if(ids != ''){
            ids = ids.substring(0,ids.length-1)
        }else{
            alert('请选择要删除的订单');
            return false;
        };
        if(confirm('确定要删除选中的订单吗?')){DeleteOrder(ids);}
        return false;
    });
    
//    $('#bt_print').show().click(function(){
//        window.open('OrderListPrint.aspx','print','top=0,left=0, toolbar=no, menubar=no,resizable=yes,location=no,status=no');
//        return false;
//    });
    
    
})//end ready
    
function setList()
{
	var pageWidth=document.documentElement.clientWidth-9;
	$(".sub_container_content").css("width",pageWidth);
	$(".sub_container_listhead").css("width",pageWidth);
	$(".llist").css("width",pageWidth);
	var c1_width = pageWidth*0.02+"px";
	var c2_width = pageWidth*0.09+"px";
	var c3_width = pageWidth*0.11+"px";
	var c4_width = pageWidth*0.05+"px";
	var c5_width = pageWidth*0.04+"px";
	var c6_width = pageWidth*0.04+"px";
	var c7_width = pageWidth*0.04+"px";
	var c8_width = pageWidth*0.04+"px";
	var c9_width = pageWidth*0.05+"px";
	var c10_width = pageWidth*0.05+"px";
	var c11_width = pageWidth*0.05+"px";
	var c12_width = pageWidth*0.05+"px";
	var c13_width = pageWidth*0.05+"px";
	var c14_width = pageWidth*0.07+"px";
	var c15_width = pageWidth*0.04+"px";
	var c16_width = pageWidth*0.03+"px";
	var c17_width = pageWidth*0.03+"px";
	$('.c1').css("width",c1_width);
	$('.c2').css("width",c2_width);
	$('.c3').css("width",c3_width);
	$('.c4').css("width",c4_width);
	$('.c5').css("width",c5_width);
	$('.c6').css("width",c6_width);
	$('.c7').css("width",c7_width);
	$('.c8').css("width",c8_width);
	$('.c9').css("width",c9_width);
	$('.c10').css("width",c10_width);
	$('.c11').css("width",c11_width);
	$('.c12').css("width",c12_width);
	$('.c13').css("width",c13_width);
	$('.c14').css("width",c14_width);
	$('.c15').css("width",c15_width);
	$('.c16').css("width",c16_width);
	$('.c17').css("width",c17_width);
}

function BindList(urldata){
//    alert(urldata);
    $.ajax({
        url:'OrderList.aspx?NoCopyRight=1',
        data:urldata,
        dataType:'json',
        cache:false,
        success:function(msg){
            var data = msg.Orders;
            //搜索条件
            $('#keys').val(msg.keys);
            //权限
            var orderDelete = msg.power.OrderDelete == 'true' ? true : false;
            
            if(!orderDelete){
                $('#template').find('#Delete').hide();$('#btnDelete').hide();
                $('#listhead').find('th.delete').hide();
                $('.c16').css('text-align','left');
            }else{
                $('#template').find('#Delete').show();$('#btnDelete').show();
                $('#listhead').find('th.delete').show();
            };
            
            $('input[@name=chkAll]').attr('checked','');
            $('tr:gt(0)','#datas').remove();

            $.each(data,function(i,n){
                var row = $('#template').clone().show();
                row.mouseover(function(){$(this).addClass('on')}).mouseout(function(){$(this).removeClass('on')}); //将复制的行显示出来并添加样式
                row.find('#CheckBox').html('<input type="checkbox" name="chkItem" value="' + n.OrderID + '" />');//checkbox
                
                row.find('#OrderNo').html(n.OrderNo);//订单号
                row.find('#OrderTime').html(n.OrderTime);
                row.find('#ProductPrice').html(Number(n.ProductPrice).toFixed(2));
                row.find('#ShipPrice').html(Number(n.ShipPrice).toFixed(2));
                row.find('#TotalPrice').html(Number(n.TotalPrice).toFixed(2));
                row.find('#AdvancePayment').html(Number(n.AdvancePayment).toFixed(2));
                row.find('#NeedPay').html(Number(n.NeedPay).toFixed(2));
                row.find('#ShippingType').html(n.ShippingType);
                row.find('#PaymentType').html(n.PaymentType);
                row.find('#MemberName').html(n.MemberName);
                row.find('#ReceiverName').html(n.ReceiverName);
                 row.find('#ReceiverPhone').html((n.ReceiverPhone.split(',')[0] != undefined ? n.ReceiverPhone.split(',')[0] : '') +(n.ReceiverPhone.split(',')[1] != undefined ?  '<br />' + n.ReceiverPhone.split(',')[1] : ''));
                var isConfirmed = (n.IsConfirmed == '1' ? '<font color=blue>[已确认]</font>' : '<font color=red>[未确认]</font>') + '<br />';
                var payStatus = (n.PayStatus > 0 ? '<font color=blue>[已付款]</font>' : '<font color=red>[未付款]</font>') + '<br />';
                var shippingStatus = (n.ShippingStatus > 0 ? '<font color=blue>[已发货]</font>' : '<font color=red>[未发货]</font>') + '<br />';
                var isFinished = (n.IsFinished == '1' ? '<font color=blue>[已归档]</font>' : '<font color=red>[未归档]</font>');
                var cancelled = (n.IsCancelled == '1' ? '<font color=red>[已取消]</font>' : '');
                row.find('#Status').html(n.IsCancelled == '1' ? cancelled : (isConfirmed + payStatus + shippingStatus + isFinished));//状态
                row.find('#Modify').html('<a href="OrderModify.aspx?from=list&OrderID=' + n.OrderID + '">管理</a>').find('a').click(function(){ShowModify($(this).attr('href'));return false;});//修改
                if(orderDelete){
                    row.find('#Delete').html('<span style="cursor:pointer">删除</span>').find('span').click(function(){if(confirm('确定要删除吗?')){DeleteOrder(n.OrderID);};});//删除
                }

                row.attr('id','ready_' + n.OrderID);//id=ready_OrderID
                $('#datas').append(row);
                
            })//end each
            //如果是firefox,将过长的订单号换行
            if($.browser.mozilla){
                $('tr:gt(0)','#datas').each(function(){
                    var strOrderNo = $(this).find('#OrderNo').html();
                    $(this).find('#OrderNo').html(strOrderNo.BreakWord(15));
                })
            }
            
            //分页
            $('.sub_container_page').show();
            var pagerStyle = "flickr";//jPagerBar样式
            ShowPageBar("pageDataList1_Pager",//[containerId]
               "OrderList.aspx",//[url]
               {style:pagerStyle,mark:"pageDataList1Mark",
               totalCount:msg.totalCount,showPageNumber:3,pageCount:PageSize,currentPageIndex:CurrentPage,noRecordTip:" 没有记录",preWord:"上一页",nextWord:"下一页",
               onclick:"CurrentPage={pageindex};BindList('type=ajax&Action=BindOrderList&PageSize=" + PageSize + "&Page={pageindex}" + searchUrl + "');return false;"}//[attr]
            );//分页结束
            
        },//end success
        error:function(XMLHttpRequest,textStatus){
            if(textStatus=='parsererror'){
                alert('身份已过期,请重新登陆!')
            }else if(textStatus == 'error'){
                alert('查询条件有误');
            };
        }//end error
    });//end ajax
}

//点击修改,显示修改商品的页面
function ShowModify(href){
    $('#frmModify').attr('src',href+'&ran='+Math.random()).css('height',$(window.parent.frames.right).height()).show().siblings().hide();
}

//删除订单(一次可删除多个订单)
function DeleteOrder(orderID){
    $.ajax({
        url:'OrderList.aspx?type=ajax&Action=DeleteOrdersAjax&IDs='+orderID + '&NoCopyRight=1',
        cache:false,
        success:function(data){
            if(!checkLoginStatus(data)){return false;}//是否要重新登陆
            if(data>0){
                BindList('type=ajax&Action=BindOrderList&PageSize=' + PageSize + '&Page=' + CurrentPage + searchUrl);
            };
        },error:function(){alert('error');}
    });
};

function CheckOn(obj){
    obj.parent().siblings('td[@id=CheckBox]').find('input[@name=chkItem]').attr('checked','true');
}

//去掉字符串前后空格
String.prototype.Trim = function() { 
    return this.replace(/(^\s*)|(\s*$)/g, ""); 
}

//将过长的字符换行(firefox不能自动换行) 参数maxLength:每行显示的最大字符数量
String.prototype.BreakWord = function(intLen){
    var strContent=this; 
    var strTemp="";
    while(strContent.length>intLen){
    strTemp+=strContent.substr(0,intLen)+"<br />"; 
    strContent=strContent.substr(intLen,strContent.length); 
    }
    strTemp+=strContent;
    return strTemp;
}

//获取URL中参数
function getQueryStringRegExp(name){    
	var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
	if (reg.test(location.href))
		return unescape(RegExp.$2.replace(/\+/g, " "));
		return "";
}

function checkLoginStatus(data){
    var result = true;
    if(data.indexOf('<!DOCTYPE') != -1){
        result = false;
        alert('身份已过期,请重新登陆!');
    }
    return result;
}