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

$(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()); //设置底部分页区域的位置
        $('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); //设置内容区域的高度
    },1000);
    
    $('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();
        }
    });
    
    //删除会员
    $('#btnDelete').click(function(){
        var ids = GetSelectedIDs('chkItem');
        if(ids == ''){
            alert('请选择要删除的会员');
        }else if(confirm('确定要删除选中的会员吗?')){
            Delete(ids);
        };
    });
    //清空选中会员的积分
    $('#btnClearScore').click(function(){
        var ids = GetSelectedIDs('chkItem');
        if(ids == ''){
            alert('请选择要清除积分的会员');
        }else if(confirm('确定要清除选中的会员的积分吗?')){
            ClearScore(ids);
        };
    });
    
    //清空所有会员积分
    $('#btnClearAllScore').click(function(){
        if(confirm('确定要清除所有会员的积分吗?')){
            $.ajax({
                url:'Member.aspx?NoCopyRight=1&type=ajax&Action=ClearAllScore',
                cache:false,
                success:function(data){
                    if(!checkLoginStatus(data)){
                        return false;
                    }else if(data > 0){
                        BindList();
                        alert('操作成功');
                    }
                },error:function(){alert('error');}
            });
        };
    });
	
	$('#bt_ExportPart').click(function(){
		var memberIDs = GetSelectedIDs('chkItem');
        if(memberIDs == ''){
            alert('请选择要导出的会员');
			return false;
        };
	});
	
	//高级搜索
    $('#btnSearchAdv').toggle(function(){
        $('.searchDetails').show();
        $(this).val('关闭');
    },function(){
        $('.searchDetails').hide();
        $(this).val('高级搜索');
    });
    
    $('#selGroup option').eq(0).attr('selected','selected');
    
    $('#ddlTopAreas').change(function(){
        $('#ddlSubAreas').remove();$('#ddlThirdAreas').remove();
        if($(this).val() == '0'){return false;};
        $.ajax({
            url:'Member.aspx?Nocopyright=1&type=ajax&Action=GetSubArea&ParentID=' + $('#ddlTopAreas').val(),
            cache:false,
            success:function(data){
                if(!checkLoginStatus(data)){return false;}//是否要重新登陆
                var objSubArea = $('<select id="ddlSubAreas" name="ddlSubAreas" class="searchvalue"></select>');
                objSubArea.html(data);
                objSubArea.insertAfter($('#ddlTopAreas'));
                objSubArea.change(function(){
                    $('#ddlThirdAreas').remove();
                    if($(this).val() == '0'){return false;};
                    $.ajax({
                        url:'Member.aspx?Nocopyright=1&type=ajax&Action=GetSubArea&ParentID=' + $('#ddlSubAreas').val(),
                        cache:false,
                        success:function(data){
                            if(!checkLoginStatus(data)){return false;}//是否要重新登陆
                            var objthirdarea = $('<select id="ddlThirdAreas" name="ddlThirdAreas" class="searchvalue"></select>');
                            objthirdarea.html(data).insertAfter($('#ddlSubAreas'))
                        },error:function(){}
                    });
                });
            },error:function(){alert('get address error');}
        });
    });//end change
    
    $("#txtStartTime").datepicker().click(function(){$(this).blur()});
    $("#txtEndTime").datepicker().click(function(){$(this).blur()});
	
});//end ready


//绑定数据
function BindList(){
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"post",
        dataType:"json",
        url:'Member.aspx?NoCopyRight=1&type=ajax&Action=GetList' + '&Page=' + CurrentPage + '&PageSize=' + PageSize,
        data:$('.searchvalue').serialize(),
        cache:false,
        success:function(msg){
            $('input[@name=chkAll]').attr('checked','');
            $('tr:gt(0)','#datas').remove();
            var data = msg.Member;
            $.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.MemberID + '" />');//checkbox
                row.find('#ID').html(n.MemberID);
                row.find('#Name').html(n.Name);
                row.find('#Realname').html(n.Realname);
                row.find('#Email').html(n.Email);
                row.find('#Phone').html('<div class="ccenter">'+n.Phone+'</div><div class="ccenter">' + n.Mobile + '</div>');
                row.find('#Score').html(n.Score);
                row.find('#DeleteScore')
                    .html('<span class="hand">积分归零</span>')
                    .find('span')
                    .click(function(){
                        if(confirm('确定要将此用户的积分清除吗?')){
                            ClearScore(n.MemberID);
                        };
                    });
                row.find('#AddDate').html(n.AddDate);
                row.find('#Status')
                    .html('<img src="images/publish_' + n.Status + '.png" alt="更改状态" class="hand" />')
                    .find('img')
                    .click(function(){
                        var objImg = $(this);
                        $.ajax({
                            url:'Member.aspx?NoCopyRight=1&type=ajax&Action=ChangeStatus&MemberID='+n.MemberID+'&Status='+(n.Status==1 ? 0 : 1),
                            cache:false,
                            success:function(data){
                                if(!checkLoginStatus(data)){
                                    return false;
                                }else if(data > 0){
                                    objImg.attr('src','images/publish_'+(objImg.attr('src').indexOf('images/publish_1.png') > 0 ? '0' : '1')+'.png')
                                };
                            },
                            error:function(){alert('error');}
                        });
                    });
                row.find('#Edit').html('<span class="hand">修改</span>').click(function(){ShowModify('MemberAdd.aspx?from=list&MemberID='+n.MemberID);});
                row.find('#EditPwd').html('<span class="hand">密码重设</span>').click(function(){ShowModify('MemberPassModify.aspx?from=list&MemberName='+n.Name);});
                row.find('#Delete')
                    .html('<span class="hand">删除</span>')
                    .find('span')
                    .click(function(){
                        if(confirm('确定要删除此会员吗?')){
                            Delete(n.MemberID);
                        };
                    });
                
				row.attr("id","ready_" + n.ProductID);//改变绑定好数据的行的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

function Delete(ids){
    $.ajax({
        url:'Member.aspx?NoCopyRight=1&type=ajax&Action=Delete&IDs=' + ids,
        cache:false,
        success:function(data){
            if(!checkLoginStatus(data)){return false;}
            BindList();
        },
        error:function(){alert('error');}
    });//end ajax
};

function ClearScore(memberIDs){
    $.ajax({
        url:'Member.aspx?NoCopyRight=1&type=ajax&Action=ClearMemberScore&MemberIDs='+memberIDs,
        cache:false,
        success:function(data){
            if(!checkLoginStatus(data)){
                return false;
            }else if(data > 0){
                BindList();
                alert('操作成功');
            }
        }
    });
};

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

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

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


