/*手机端通用获取订货商品列表*/
var querydata = new Object;
$(document).ready(function(){
    if(is_list == 1){
        get_cart_list();
    }
    $('.goods_list_down dd').removeClass("on");
    var catid=$(".child_on").data('id');
    if(!catid){
        var catid=$(".on_on").data('id');
    }
    querydata.catid=catid;
    querydata.brand_order = brand_order;
    querydata.screen_order = screen_order;
    $('.m360_leftNav_list').addClass("hide");
    if(catid){
        var catpid=$(".on_on").data('id');
        $('#child_cat'+catpid).removeClass('hide');
    }
    if(querydata.catid != 0){
        querydata.p=1;
    }else{
        if(keyword){
            querydata.keyword = keyword;
            querydata.p=1;
            querydata.is_search = 1;
        }
    }
    get_list_data();
    $(".wm-shop-left li").click(function(){
        querydata.p=1;
        var t=$(this).index();
        var catid=$(this).data('id');
        querydata.catid=catid;
        $('.m360_leftNav_list').addClass("hide");
        if(catid){
            $('#child_cat'+catid).removeClass('hide');
            $('#child_cat'+catid+' dl span').removeClass('on');
            $('#child_cat'+catid+' dl dd a').removeClass('on');
        }
        $('#child_catid'+catid+'_0').addClass('on');
        $('#child_catidp'+catid+'_0').addClass('on');
        l=$(".wm-shop-item");
        $(this).addClass("on").siblings().removeClass("on");
        l.eq(t).show().siblings().hide();
        if(querydata.catid != 0){
            querydata.keyword = '';
            querydata.is_search = 0;
            querydata.childcatid = 0;
            get_list_data();
        }else{
            if(keyword){
                querydata.keyword = keyword;
                querydata.is_search = 1;
                get_list_data();
            }else{
                querydata.keyword = '';
                querydata.is_search = 0;
                querydata.childcatid = 0;
                get_list_data();
            }
        }
        $(".fieldfen").addClass("hide");
        $(".popmask").hide();
    });
    $(".type a").click(function(){
        querydata.p=1;
        var catid=$(this).data('id');
        var childid=$(this).data('childid');
        if(childid != 0){return false;}
        querydata.catid=catid;
        $('#child_catid'+catid+'_0').addClass('on');
        $('#child_catidp'+catid+'_0').addClass('on');
        $(this).parent().addClass("on").siblings().removeClass("on");
        if(querydata.catid != 0){
            querydata.keyword = '';
            querydata.is_search = 0;
            querydata.childcatid = 0;
            get_list_data();
        }else{
            if(keyword){
                querydata.keyword = keyword;
                querydata.is_search = 1;
                get_list_data();
            }
        }
    });

    //列表页排序
    $('.order-select li').click(function(){
        var $t=$(this);
        if($t.hasClass('up')){
            $(".order-select li").removeClass('up');
            $('.downlist').hide();
            $('.popmask').hide();
            $('.wm-shop-main .wm-shop-right').css("overflow","");
        }else{
            $(".order-select li").removeClass('up');
            $('.downlist').hide();
            $t.addClass('up');
            $('.downlist').eq($(".order-select li").index($(this)[0])).show();
            $('.popmask').show();
            $('.wm-shop-main .wm-shop-right').css("overflow","hidden");
        }
    });
    $('.downlist ul li').click(function(){
        $(this).siblings().removeClass("on");
    });
    $('.popmask').click(function(){
        $('.slide').hide();
        $('.order-select li').removeClass('up');
        $(this).hide();
        $('.wm-shop-main .wm-shop-right').css("overflow","");
    });
});
function get_list_data_qk(){
    $('.wm-shop-left li').first().remove();
    $('.wm-shop-left li').first().addClass("on");
    querydata.catid=$(".on").data('id');
    querydata.keyword = '';
    querydata.p=1;
    querydata.is_search = 0;
    get_list_data();
}
function child_catid(pid,catid){
    $('.wm-shop-left li').removeClass("on");
    $('#catid'+pid).addClass("on");
    $('.child').removeClass("on");
    $('.childp').removeClass("on");
    $('#child_catid'+pid+'_'+catid).addClass('on');
    $('#child_catidp'+pid+'_'+catid).addClass('on');
    window.location.hash = '#child_catidp'+pid+'_'+catid;
    querydata.childcatid=catid;
    querydata.catid=pid;
    querydata.p=1;
    querydata.keyword = '';
    querydata.is_search = 0;
    // 导航居中
    var thisCtril=$('.table_menu #child_catidp'+pid+'_'+catid);
    var moveX = thisCtril.position().left+thisCtril.parent().scrollLeft();
    var pageX = $('.table_box').width();
    var blockWidth = thisCtril.width();
    var left = moveX-(pageX/2)+(blockWidth/2);
    $(".table_box").scrollLeft(left);

    get_list_data();
}
function get_cart_list(){
    var query = new Object();
    $.ajax({
        url: APP+"Mobile/Takeout/get_cart_list",
        type: "post",
        data: query,
        dataType: "json",
        success: function (ret) {
            if (ret.status == 1) {
                $('#footer').html(ret.data);
            }
        }
    });
}
//分类筛选
function cate_click(cid,catid){
    $('.type li').removeClass("on");
    $('#catid'+cid).addClass("on");
    $('#child_cat'+cid+' dd').removeClass("on");
    $('.goods_list_down dd').removeClass("on");
    if(catid){
        querydata.childcatid=catid;
        querydata.catid=cid;
        $('#child_catid'+cid+'_'+catid).addClass('on');
    }else{
        $('#child_catid'+cid+'_0').addClass('on');
        $('#catid'+cid).parent().addClass("on");
        querydata.catid = cid;
        querydata.childcatid = 0;
    }
    sort_click();
}
//列表页排序
function sort_click(id){
    if(id == 1){
        querydata.p = 1;
        querydata.order='sell_count';
    }else if(id == 2){
        querydata.p = 1;
        querydata.order='money_desc';
    }else if(id == 3){
        querydata.p = 1;
        querydata.order='money_asc';
    }else{
        querydata.p = 1;
        querydata.order='sort asc';
    }
    get_list_data();
    $('.downlist').fadeOut();
    $('.popmask').fadeOut();
    $(".order-select li").removeClass('up');
    $('.wm-shop-main .wm-shop-right').css("overflow","");
}
//列表页筛选
function screen_click(){
    var obj=document.getElementsByName('screen');
    var screen_order='';
    for(var i=0; i<obj.length; i++){
        if(obj[i].checked) screen_order+=obj[i].value+','; //如果选中，将value添加到变量s中
    }
    var obj1=document.getElementsByName('brand');
    var brand_order='';
    for(var i=0; i<obj1.length; i++){
        if(obj1[i].checked) brand_order+=obj1[i].value+','; //如果选中，将value添加到变量s中
    }
    querydata.screen_order = screen_order;
    querydata.brand_order = brand_order;
    get_list_data();
    $('.downlist').fadeOut();
    $('.popmask').fadeOut();
    $(".order-select li").removeClass('up');
    $('.wm-shop-main .wm-shop-right').css("overflow","");
}
function cancel_click(){
    $('.downlist').fadeOut();
    $('.popmask').fadeOut();
    $(".order-select li").removeClass('up');
    $('.wm-shop-main .wm-shop-right').css("overflow","");
}
function load_mores(){
    var page = $('#page').val();
    if(page){
        page++;
        querydata.p=page;
        get_list_data();
    }
}
function get_list_data(){
    $('.zhezhao').show();
    $('#shclProgress').show();
    var queryurl = APP+"Mobile/Takeout/get_list_data.html";
    $.ajax({
        url: queryurl,
        type: "post",
        data: querydata,
        dataType: "json",
        success: function (ret) {
            $('.zhezhao').hide();
            $('#shclProgress').hide();
            // $("#list_html").html(ret.data);
            $("#type").html(ret.cate_name);
            if (ret.status) {
                if(ret.page==1){
                    $('#list_html').html(ret.data);
                }else{
                    $('.load_mores').remove();
                    $('#list_html').append(ret.data);
                }
            }else{
                $('#list_html').html(ret.data);
            }
        }
    });
}













/*var page = 1;
 function get_goods_list(id,filter){
 querydata['page']=page;
 if(filter=='cat_id'&&id){querydata[filter]=id;}//分类
 if(filter=='brand_id'&&id){querydata[filter]=id;}//品牌
 if(filter=='attr'&&id){querydata[filter]=id;}//属性
 if(filter=='sell_count'&&id){querydata[filter]=id;}//属性
 if(filter=='label'&&id){querydata[filter]=id;}//标签
 if(filter=='order'&&id){querydata[filter]=id;}//排序
 if(filter=='keyword'&&id){querydata[filter]=id;}//关键字
 $(".area").hide();
 $(".popmask").hide();
 $.ajax({
 url: queryurl,
 type: "post",
 data: querydata,
 dataType: "json",
 success: function (ret) {
 if (ret.status) {
 if(page==1){
 $('#guessList').html(ret.data);
 }else{
 $('#guessList').append(ret.data);
 }
 }else{
 if(page==1){
 $('#no-content').html('暂无数据');
 }
 $.showSuccess('没有更多的数据了',1,'hc');
 }
 }
 });
 }*/

function body_css(){
    var body = $('body').css("position");
    if(body=='relative'){
        $('body').css("position","");
    }
}

var checkarrkey_str='';
var attr_price='';
var integra=0;
var combination = '';//属性组合方式
var combinationnum = '';//属性组合数量
var combinationprice = '';//拼接属性价格库存
var combinationpricenum = '';//拼接属性价格库存数量
// var price = '';//拼接属性价格库存数量
function init_choose(obj){
    var is_add = $('#is_add').val();
    if(is_add == 0){
        // $.showSuccess('库存不足！',1,'hc');
        // window.location.reload();
        showtoast('库存不足！');body_css();return false;
    }
    if($(obj).hasClass('no_num'))return true;
    $(obj).siblings().removeClass('on');//去除同级元素样式
    if($(obj).hasClass('on')){
        $(obj).removeClass('on');
    }else{
        $(obj).addClass('on');
    }
    var siblingskarr = new Array();
    $(obj).siblings().each(function(){
        siblingskarr.push($(this).attr('attr'));
    });
    $(".detailInfo_tags span").not(".on").each(function(){
        var checkarrfirst = new Array();
        $(".detailInfo_tags span.on").each(function(){
            checkarrfirst.push($(this).attr('attr'));
        });
        var checkarr = checkarrfirst;
        var siblingskthisarr = new Array();
        var ischeck = true;
        if($.inArray($(this).attr('attr'),siblingskarr)==-1){
            $(this).siblings().each(function(){
                siblingskthisarr.push($(this).attr('attr'));
            });
            for(var keys in siblingskthisarr){
                if($.inArray(siblingskthisarr[keys],checkarr)>=0){
                    checkarr.splice($.inArray(siblingskthisarr[keys],checkarr),1);
                }
            }
            if($.inArray($(this).attr('attr'),checkarr)==-1)checkarr.push($(this).attr('attr'));
            // alert(JSON.stringify(combination)+'=='+checkarr);
            for(var key in combination){
                if(isContained(combination[key],checkarr)){
                    ischeck = false;
                };
            }
            if($(this).hasClass('no_num'))$(this).removeClass('no_num');
            if(ischeck)$(this).addClass('no_num');
            checkarr.splice($.inArray($(this).attr('attr'),checkarr),1);
        }
    });
    //拼接属性价格库存
    checkarrkey = new Array();
    $(".detailInfo_tags span.on").each(function(){
        checkarrkey.push($(this).attr('attr'));
    });
    if(checkarrkey.length < parseInt(combinationpricenum)){
        $("#goods_price").html('￥'+foramtmoney($("#price").val(),2));
        return true;
    }
    checkarrkey = checkarrkey.sort();
    checkarrkey_str = checkarrkey.join("_");
    // alert(checkarrkey_str);
    // alert(JSON.stringify(combinationprice));
    // alert(JSON.stringify(combinationprice[checkarrkey_str]));
    if(combinationprice[checkarrkey_str]){
        //页面计算赋值
        var stock_cfg = combinationprice[checkarrkey_str]['stock_cfg'];//库存
        attr_price = combinationprice[checkarrkey_str]['attr_price'];//价格
        var sell_count = combinationprice[checkarrkey_str]['sell_count'];//已售数量
        // var goods_price = parseFloat($("#price").val())+parseFloat(attr_price);
        // $("#goods_price").html(foramtmoney(goods_price,2));
        var goods_price = parseFloat(attr_price);
        $("#format_market_price").html('￥'+foramtmoney(goods_price,2));
        $("#goods_price").html('￥'+foramtmoney(goods_price,2));
        price = goods_price;
        /*if(promotion>0){
         $("#goods_price").html(foramtmoney(promotion,2))
         price=foramtmoney(promotion,2);
         }else if(rank>0 ){
         $("#goods_price").html(foramtmoney(goods_price*rank,2));
         price=foramtmoney(goods_price*rank,2);
         }else{
         $("#goods_price").html(foramtmoney(goods_price,2));
         }*/
        /*$("#stock").html(stock_cfg);
         $("#sell_count").html(sell_count);*/
        //alert(stock_cfg);
    }
    $("#stock").html(stock_cfg);
    $("#sell_count").html(sell_count);
};
//是否被包含,是返回true,不是返回false
function isContained(a, b){
    if(!(a instanceof Array) || !(b instanceof Array)) return false;
    if(a.length < b.length) return false;
    var aStr = a.toString();
    //console.info(aStr);
    for(var i = 0, len = b.length; i < len; i++){
        //console.info(aStr.indexOf(b[i]));
        if(aStr.indexOf(b[i]) == -1) return false;
    }
    return true;
}

//商品列表页获取属性
function attr_shop(g_id,s_id){
    $('.detailInfo_tags').html('');
    var query = new Object;
    query.goods_id = g_id;
    // query.shop_id = s_id;
    $.ajax({
        url: APP+"Mobile/Takeout/attr",
        type: "post",
        data: query,
        dataType: "json",
        success: function (data) {
            if (data.status) {
                $('.detailInfo_tags').html(data.html);
                $('#format_market_price').html(data.format_market_price);
                $('#gid').val(data.goods_id);
                combination = data.combination;//属性组合方式
                combinationnum = data.combinationnum;//属性组合数量
                combinationprice = data.combinationprice;//拼接属性价格库存
                combinationpricenum = data.combinationpricenum;//拼接属性价格库存数量
                $('.specifications_shops').fadeIn();
                $('.reveal-modal-bg').fadeIn();
                $('#is_add').val(data.is_add);
            }else{
                // if(page==1){
                // $('#no-content').html('暂无数据');
                // }
                // $.showSuccess(data.msg,'','hc');
                showtoast(data.msg);body_css();return false;
            }
        }
    });

}

$(function(){

    $('.close-buy-popup').click(function(){
        $('.specifications_shops').hide();
        $('.reveal-modal-bg').hide();
    })

    $('.reveal-modal-bg').click(function(){
        $('.specifications_shops').hide();
        $('.reveal-modal-bg').hide();
    })

})