var PageSize = 50;//每页计数
var CurrentPage = 1; //当前页
var keyword = '';
var isConfirm = 0;

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.05+"px";
	var c2_width = pageWidth*0.05+"px";
	var c3_width = pageWidth*0.08+"px";
	var c4_width = pageWidth*0.08+"px";
	var c5_width = pageWidth*0.08+"px";
	var c6_width = pageWidth*0.1+"px";
//	var c7_width = pageWidth*0.15+"px";
//	var c8_width = pageWidth*0.08+"px";
//	var c9_width = pageWidth*0.08+"px";
//	var c10_width = pageWidth*0.08+"px";
	var c11_width = pageWidth*0.05+"px";
	var c12_width = pageWidth*0.05+"px";
	var c13_width = pageWidth*0.05+"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);
}


$(document).ready(function(){
    //loading
    $('#loadingContainer').ajaxStart(function(){$(this).show();}).ajaxStop(function(){$(this).hide();});	
	setInterval(function(){
        $('div.sub_container_page').css('top',$(window.parent.frames.right).height()-$('div.sub_container_page').height()-2); //设置底部分页区域的位置
        $('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中取值
    },1000);

    isConfirm=getQueryStringRegExp('IsConfirm');    
//    $('input[@name=chkAll]').click(function(){$('input[@name=chkItem]').attr('checked',$(this).attr('checked'));});//全选
    BindList();
    $('#btnSearch').click(function(){
        CurrentPage = 1;
        BindList();
        if($('#searchDetails').is(':visible')){
            $('#btnSearchAdv').click();
        }
    });
    
	//高级搜索
    $('#btnSearchAdv').toggle(function(){
        $('#searchDetails').show();
        $('#frmAdvSearch').addClass('frmAdvSearch');
        $(this).val('关闭');
    },function(){
        $('#searchDetails').hide();
        $('#frmAdvSearch').removeClass('frmAdvSearch');
        $(this).val('高级搜索');
    });
    
    $('#selGroup option').eq(0).attr('selected','selected');
     
    $("#txtStartTime").datepicker().click(function(){$(this).blur()});
    $("#txtEndTime").datepicker().click(function(){$(this).blur()});
	$('input[@name=chkAll]').click(function(){$('input[@name=chkItem]').attr('checked',$(this).attr('checked'));});//全选

});//end ready

//绑定数据
function BindList(){
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"post",
        dataType:"json",
        url:'AppForm.aspx?NoCopyRight=1&type=ajax&Action=GetList' + '&Page=' + CurrentPage + '&PageSize=' + PageSize+'&IsConfirm='+isConfirm,
        data:$('.searchvalue').serialize(),
        cache:false,
        success:function(msg){
            $('input[@name=chkAll]').attr('checked','');
            $('tr:gt(0)','#datas').remove();
            var data = msg.AppForm;
            $.each(data,function(i,n){
                var row = $('#template').clone();
                row.show().mouseover(function(){$(this).addClass('on')}).mouseout(function(){$(this).removeClass('on')}); //将复制的行显示出来并添加样式
                row.find('#checkbox').html('<input type="checkbox" name="chkItem" value="' + n.Ano + '" />');
                row.find('#ANo').html(n.ANo);
                row.find('#OrderNo').html(n.OrderNo);
				row.find('#Applicant').html(n.Applicant);
				row.find('#Mobile').html(n.Mobile);
                row.find('#Type').html(n.Type==1?"退货":n.Type==2?"换货":n.Type==3?"返修":"退款");
                row.find('#IsCompleted').html(GetStatus(n.Type,n.CanRefund,n.IsConfirm,n.IsReturn,n.ReturnConfirm,n.CheckConfirm,n.IsSend,n.SendConfirm,n.IsCompleted));
                row.find('#Edit').html('<a href="AppFormModify.aspx?from=list&ano='+n.ANo+'">修改</a>').
                 find('a').click(function(){ShowModify($(this).attr('href'));return false});
                row.find('#Delete')
                    .html('<span class="hand">删除</span>')
                    .find('span')
                    .click(function(){
                        if(confirm('确定要删除此记录吗?')){
                            Delete(n.Ano);
                        };
                    });                    
			    row.attr("id","ready_" + n.Ano);//改变绑定好数据的行的id
				row.appendTo("#datas");//添加到模板的容器中
            });
           
            //分页
            $('.sub_container_page').show();
            var pagerStyle = "flickr";//jPagerBar样式
            ShowPageBar("pageDataList1_Pager",//[containerId]
               "MsgList.aspx",//[url]
               {style:pagerStyle,mark:"pageDataList1Mark",
               totalCount:msg.totalCount,showPageNumber:3,pageCount:PageSize,currentPageIndex:CurrentPage,noRecordTip:" 没有记录",preWord:"上一页",nextWord:"下一页",
               onclick:"CurrentPage={pageindex};BindList();return false;"}//[attr]
            );
        },//end success
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!');}else{alert(textStatus)};}
    });//end ajax
};//end funciton

//20110228 modify by liwenhai
function Delete(ids){
    $.ajax({
        url:'AppForm.aspx?NoCopyRight=1&type=ajax&Action=Delete&IDs=' + ids,
        cache:false,
        success:function(data){
            if(!checkLoginStatus(data)){return false;}
            if(data>0){
                alert("删除成功");
            }
            else{
                alert("删除失败");
            }
            BindList();
        },
        error:function(){alert('error');}
    });//end ajax
};


function ShowModify(href){
    $('#frmModify').attr('src',href+'&ran='+Math.random()).css('height',$(window).height()).show().siblings().hide();
}

//获取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;
};

function GetSelectedIDs(controlName){
    var ids = '';
    $("input[@name=chkItem]:checked").each(function(i,n){
        ids += $(this).val() + ',';
    });
    if(ids != ''){
        ids = ids.substring(0,ids.length-1)
    };
    return ids;
};


function GetStatus(type,canRefund,isConfirm,isReturn,returnConfirm,checkConfirm,isSend,sendConfirm,isCompleted){
    var status;
    switch (type){
    case "1":  //退货的状态
        if(isCompleted==2){
            status='已完成';
        }
        else if(isCompleted==1){
            status='已关闭';
        }
        else if(canRefund=='True'){
            status='等待退款';
        }
        else if(checkConfirm==1){
            status='等待退款';
        }
        else if(checkConfirm==2&&isSend=='True'&&sendConfirm=='False'){
            status='等待客户签收';
        }
        else if(checkConfirm==2&&isSend=='False' &&returnConfirm=='True'){
            status='等待商家发货';
        }
        else if(isReturn=='True'&&returnConfirm=='False'){
            status='等待商家签收';
        }
        else if(isConfirm==1&&isReturn=='False'){
            status='等待客户发货';
        }
        else if(isConfirm==2){
            status='申请不通过';
        }    
        else if(isConfirm==1){
            status='申请通过';
        }
        else if(isConfirm==0){
            status='申请中';
        }
      break
    case "2"://返修的的判态
        if(isCompleted==2){
            status='已完成';
        }
        else if(isCompleted==1){
            status='已关闭';
        }
        else if(isSend=='True'&&sendConfirm=='False'){
            status='等待客户签收';
        }
        else if(isSend=='False' &&returnConfirm=='True'){
            status='等待商家发货';
        }
        else if(isReturn=='True'&&returnConfirm=='False'){
            status='等待商家签收';
        }
        else if(isConfirm==1&&isReturn=='False'){
            status='等待客户发货';
        }
        else if(isConfirm==2){
            status='申请不通过';
        }    
        else if(isConfirm==1){
            status='申请通过';
        }
        else if(isConfirm==0){
            status='申请中';
        }
        break
    case "3"://返修的的判态
        if(isCompleted==2){
            status='已完成';
        }
        else if(isCompleted==1){
            status='已关闭';
        }
        else if(isSend=='True'&&sendConfirm=='False'){
            status='等待客户签收';
        }
        else if(isSend=='False' &&returnConfirm=='True'){
            status='等待商家发货';
        }
        else if(isReturn=='True'&&returnConfirm=='False'){
            status='等待商家签收';
        }
        else if(isConfirm==1&&isReturn=='False'){
            status='等待客户发货';
        }
        else if(isConfirm==2){
            status='申请不通过';
        }    
        else if(isConfirm==1){
            status='申请通过';
        }
        else if(isConfirm==0){
            status='申请中';
        }
        break        
    case "4"://直接退款的状态
        if(isCompleted==2){
            status='已完成';
        }
        else if(isCompleted==1){
            status='已关闭';
        }      
        else if(canRefund=='True'){
            status='等待退款';
        }
        else if(isConfirm==2){
            status='申请不通过';
        }    
        else if(isConfirm==1){
            status='申请通过';
        }
        else if(isConfirm==0){
            status='申请中';
        }
        break
    default:
        status='';
    }
    return status;
}