var PageSize = 15;
var CurrentPage = 1;
$(document).ready(function(){
    $('#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()-2); //设置底部分页区域的位置
    },100);

    var search_urldata = 'type=ajax&action=SearchMemberBookTG&PageSize='+PageSize+'&Page='+CurrentPage+'&gid='+gid;
    BindRules('MemberBookTG.aspx',search_urldata);//绑定数据        
    
    //--添加活动
    $('#btnAdd').click(function(){
        location.href="pmtrule_tg.aspx";
    });
    
    //全选
    $('input[@name=chkAll]').click(function(){
        $('input[@name=chkItem]').each(function(){
            if($(this).attr("disabled")==false)
            {
                $(this).attr("checked",$('input[@name=chkAll]').attr("checked"));
            }
        });
    });    
    
    //批量删除规则
    $('#btnDelete').click(function(){
        var uids = '';
        $('input[@name=chkItem][@checked]').each(function(){
            uids += $(this).val() + ',';
        });
        if(uids != ''){
            uids = uids.substring(0,uids.length-1);
            if(confirm('确定要删除选中的吗?删除将不可恢复'))
            {
                $.ajax({
				    url:'MemberBookTG.aspx?type=ajax&action=BatchDeleteMemberBookTG',
				    type:'post',
				    data:'uids=' + uids,
				    async:false,
				    success:function(msg){
					    if(msg == '1'){
						    alert('删除失败');
					    }else{
						    alert('删除成功');
					    }
                    location.href=window.location.href;
				    },error:function(){alert('error');}
			    }); 
            }
        }else{
            alert('请选择要删除');
        }
    });

    //批量生成订单
    $('#btnBatchCreate').click(function(){
        var uids = '';
        $('input[@name=chkItem][@checked]').each(function(){
            uids += $(this).val() + ',';
        });
        if(uids != ''){
            uids = uids.substring(0,uids.length-1);
            if(confirm('确定要批量生成订单吗?'))
            {
			    $.ajax({
				    url:'MemberBookTG.aspx?type=ajax&action=BatchCreateOrders',
				    type:'post',
				    data:'uids=' + uids,
				    async:false,
				    success:function(msg){
					    if(msg == '1'){
						    alert('生成失败');
					    }else{
						    alert('生成成功');
					    }
                    location.href=window.location.href;
				    },error:function(){alert('error');}
			    });            
            }
        }else{
            alert('请选择');
        }
    });
    
    
    $(".create_order").livequery('click',function(){
	    var uid =$(this).attr('value');
	    if(confirm('确定要生成团购订单吗?'))
        {
	        $.ajax({
		        url:'MemberBookTG.aspx?type=ajax&action=BatchCreateOrders',
		        type:'post',
		        data:'uids=' + uid,
		        async:false,
		        success:function(msg){
			        if(msg == '1'){
				        alert('生成失败');
			        }else{
				        alert('生成成功');
			        }
                location.href=window.location.href;
		        },error:function(){alert('error');}
	        });            
        }
    });
            
});//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.04+"px";
	var c2_width = pageWidth*0.18+"px";
	var c3_width = pageWidth*0.05+"px";
	var c4_width = pageWidth*0.10+"px";
	var c5_width = pageWidth*0.10+"px";
	var c6_width = pageWidth*0.30+"px";
	var c7_width = pageWidth*0.06+"px";
	var c8_width = pageWidth*0.06+"px";
	
	var cc1_width = pageWidth*0.05+"px";
	var cc2_width = pageWidth*0.10+"px";
	var cc3_width = pageWidth*0.10+"px";
	var cc4_width = pageWidth*0.30+"px";
	var cc5_width = pageWidth*0.45+"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);
	
}

//绑定数据
function BindRules(weburl,urldata){
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"get",
        dataType:"json",
        url:weburl+'?NoCopyRight=1',
        data:urldata,
        cache:false,
        success:function(msg)
        {
            $('input[@name=chkAll]').attr('checked','');
            $('tr:gt(0)','#datas').remove();
            var data = msg.MemberBookTG;
            $.each(data,function(i,n){
                var row = $('#template').clone();
                if(n.Status==1){
                var str='<input type="checkbox"  disabled  name="chkItem" value="' + n.UID + '" />'
                }
                else{
                var str='<input type="checkbox"  name="chkItem" value="' + n.UID + '" />'
                }
                row.find('#checkbox').html(str);//checkbox
                row.find('#PName').text(n.ProductName); //名称                
                row.find('#PQty').text(n.Quantity); 
                row.find('#Member').text(n.MemberID);
                row.find('#Address').text(n.ReceiverAddress);
                row.find('#Postalcode').text(n.ReceiverPostalCode);
                row.find('#Name').text(n.ReceiverName);
                row.find('#Phone').text(n.ReceiverPhone);
                row.find('#Mobile').text(n.ReceiverMobile);
                row.find('#Create').html(n.Status==1?"<span style='color:red'>已生成订单</span>":"<span class='create_order' value='"+n.UID+"' >生成订单</span>");
                                    
				row.attr("id","ready_" + n.UID);//改变绑定好数据的行的id
				row.show().mouseover(function(){$(this).css("cursor","pointer");}); //将复制的行显示出来并添加样式
				row.appendTo("#datas");//添加到模板的容器中
            });
            
            //分页
            $('.sub_container_page').show();
            var pagerStyle = "flickr";//jPagerBar样式
            ShowPageBar("pageDataList1_Pager",//[containerId]
               "Binding.aspx",//[url]
               {style:pagerStyle,mark:"pageDataList1Mark",
               totalCount:msg.totalCount,showPageNumber:3,pageCount:PageSize,currentPageIndex:CurrentPage,noRecordTip:" 没有记录",preWord:"上一页",nextWord:"下一页",
               onclick:"CurrentPage={pageindex};BindRules('MemberBookTG.aspx','type=ajax&action=SearchMemberBookTG&PageSize=" + PageSize + "&Page={pageindex}&gid="+gid+");return false;"}
            );
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });
}



