var CurrentPage = 1;
var PageSize = 20;

$(document).ready(function(){    
    setInterval(function(){
        SetPosition();
    },1000);
    
    $('#chk_search').click(function(){$('input[@name=chkItem_search]').attr('checked',$(this).attr('checked'));});//全选
    
    $('#txtKeyword').Enter('#btnSearch');
    $('#ddlCategory').Enter('#btnSearch');
    $("#txtLowDate").datepicker().click(function(){$(this).blur()});
    $("#txtHighDate").datepicker().click(function(){$(this).blur()});
      
    $('#btnSearchAdv').toggle(function(){
        $('#searchDetails').show();
        $(this).val('关闭');
    },function(){
        $('#searchDetails').hide();
        $(this).val('高级搜索');
    });
    $('#btnSearch').click(function(){
        if(isNaN($.trim($('#txtLowPrice').val()))){alert('价格填写有误');$(this).focus();return false;}
        if(isNaN($.trim($('#txtHighPrice').val()))){alert('价格填写有误');$(this).focus();return false;}
        if(isNaN($.trim($('#txtLowStorage').val()))){alert('库存填写有误');$(this).focus();return false;}
        if(isNaN($.trim($('#txtHighStorage').val()))){alert('库存填写有误');$(this).focus();return false;}
        CurrentPage = 1;
        SearchProductsAjax();
        if($("#searchDetails").is(":visible")){
            $('#btnSearchAdv').click();
        };
    });
});
//绑定数据
function SearchProductsAjax(options){
    var status = $('[@name=rdStatus][@checked]').val(); //搜索商品的审核状态,-1审核未通过，0未审核，1已审核，2不限制
    var isForSale=$('[@name=rdIsForSale][@checked]').val();//搜索商品商品的发布状态，0未发布，1已发布，2不限制
    var tag = $('#sel_tag').val(); tag = (tag != null ? tag : '');
    var lp = $.trim($('#txtLowPrice').val());
    var hp = $.trim($('#txtHighPrice').val());
    var ls = $.trim($('#txtLowStorage').val());
    var hs = $.trim($('#txtHighStorage').val());
    var st = $.trim($('#txtLowDate').val());
    var et = $.trim($('#txtHighDate').val());
    var searchType = $('#sel_searchType').val();
    var keyword = $('#txtKeyword').val().Trim();
    var catID = $('#sel_category').val(); catID = (catID == null ? '0' : catID);
    var brands = $('#sel_brand').val(); brands = (brands == null ? '0': brands);
    
    var paras = 'searchType=' + searchType + '&keyword=' + escape(keyword) + '&CatID=' + escape(catID) + '&BrandIDs=' + escape(brands) + '&PageSize=' + escape(PageSize) + '&Page=' + CurrentPage + '&Status=' + escape(status) + '&IsForSale=' + escape(isForSale) + '&Tag=' + escape(tag) + '&lp=' + escape(lp) + '&hp=' + escape(hp) + '&ls=' + escape(ls) + '&hs=' + escape(hs) + '&st=' + escape(st) + '&et=' + escape(et);
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"get",
        dataType:"json",
//        url:adminUrl + 'Ajax.ashx?NoCopyRight=1&Action=SearchProductsAjax',
        url:adminUrl + 'SearchProduct.aspx?NoCopyRight=1&type=ajax&Action=SearchGoodsAjax',
        data:paras,
        cache:false,
        success:function(msg){
            $('input[@name=chk_search]').attr('checked','');
            $('tr:gt(0)','#datas').remove();
            var data = msg.Goods;
            $.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_search" value="' + n.GoodsID + '" /><input type="hidden" id="h_spec_' + n.GoodsID + '" value="' + n.Spec + '" /><input type="hidden" id="h_propsdesc_' + n.GoodsID + '" value="' + n.PropsDesc + '" /><input type="hidden" id="h_gname_' + n.GoodsID + '" value="' + n.ProductName + '" /><input type="hidden" id="h_gno_' + n.GoodsID + '" value="' + n.GoodsNo + '" />');//checkbox
                row.find('.Name').html('<a href="/products/product-' + n.ProductID + '.html" target="_blank">'+n.ProductName+'</a>'); //商品名称
                row.find('.ProductNo').text(n.ProductNo);   //编码
                var brandname = (n.BrandID != 0 ? $('#sel_brand option[@value=' + n.BrandID + ']').text() : "");
                row.find('.Brand').text(brandname);//商品品牌
                row.find('.MemberPrice').text(Number(n.MemberPrice).toFixed(2));//两位小数
                row.find('.MarketPrice').text(Number(n.MarketPrice).toFixed(2));//两位小数
                row.find('.Storage').text(n.Storage);
                row.find('.CategoryID').text(n.CategoryID);
				row.attr("id","ready_" + n.ProductID);//改变绑定好数据的行的id
				row.appendTo("#datas");//添加到模板的容器中
            });
            if(data.length > 0){
                $('#btnSelPdts').show();
            }else{
                $('#btnSelPdts').hide();
            };
            
            //分页
            $('.sub_container_page').show();
            var pagerStyle = "flickr";//jPagerBar样式
            ShowPageBar("pageDataList1_Pager",//[containerId]
               "Products.aspx",//[url]
               {style:pagerStyle,mark:"pageDataList1Mark",
               totalCount:msg.totalCount,showPageNumber:3,pageCount:PageSize,currentPageIndex:CurrentPage,noRecordTip:" 没有记录",preWord:"上一页",nextWord:"下一页",
               onclick:"CurrentPage={pageindex};SearchProductsAjax();return false;"}//[attr]
            );
            
//            if(p.func){setTimeout(function(){p.func(data)},0)};
            
        },//end success
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error12')};}
    });//end ajax
};//end funciton

function SetPosition(){
    $('#popupContact').height($(window).height() * 0.8);
    $('#datacontainer').height($('#popupContact').height()-$('#popupHead').height()-$('#pageDataList1_Pager').height()-$('#sub_search_save').height()-80);
}