{echo:JS::import('dialog?skin=brief');}
{echo:JS::import('dialogtools');}
{echo:JS::import('form');}
<!--S 产品展示-->
<link type="text/css" rel="stylesheet" href="{url:#css/product.css}" />
<div class="bread-crumb">
    <ol class="container">
        <li><span>促销活动</span></li>
        <li>套餐规格选择</li>
    </ol>
</div>
<div class="container">
    <div class="clearfix mt20" style="position: relative;">
    <div class="content " style="overflow:visible">
            <ul class=" clearfix " style="position: relative;">
                {set:$sets_enable=true;}
                {list:items=$goods}
                <li class="clearfix" style="position: relative;background:#fff;border-bottom:#eee 1px solid;margin-top:20px;margin-left:10px;">
                    <div class="fl" style="height:100px;">
                        <a href="{url:/index/product/id/$item[id]}" target="_blank"><img class="big-pic" src="{echo:Common::thumb($item['img'],100,100)}" width=100></a>
                    </div>
                    <div class="product-info" style="margin-left:120px;" gid="{$item['id']}">
                        <ul>
                            <li ><span class="f18">{$item['name']}</span> <span class="product-price"><label>销售价：</label><span id="sell_price" class="price">{$currency_symbol}{$item['sell_price']}</span></span>
                            {if:$item['store_nums']<=0}{set:$sets_enable=false;}{/if}
                            <span class="product-price"><label>库存：</label>(<span>{$item['store_nums']}</span>)</span></li>
                            
                        </ul>
                        {set:$specs = unserialize($item['specs']);}
                        {if:!empty($specs)}

                        <div class=" mt10 p10 spec-info" >
                            {list:items=$specs item=$spec}
                            <dl class="spec-item ">
                                <dt>{$spec['name']}：</dt>
                                <dd>
                                    <ul class="spec-values" spec_id="{$spec['id']}" goods_id="{$item['id']}">
                                        {list:items=$spec['value'] item=$value}
                                        <li data-value="{$spec['id']}:{$value['id']}">{if:$value['img']==''}<span>{$value['name']}</span>{else:}<img  src="{echo:Common::thumb($value['img'],36,36)}" width="36" height="36"><label>{$value['name']}</label>{/if}<i></i></li>
                                        {/list}
                                    </ul>
                                </dd>
                            </dl>
                            {/list}
                            <dl class="spec-msg" class="spec-item " style="display: none;">
                                <dt></dt>
                                <dd ><p class="msg alert alert-fail"><i class="icon-alert-16"></i><span >请选择您要购买的商品规格</span></p>
                                </dd>
                            </dl>
                        </div>
                        {/if}
                    </div>
                </li>
                {/list}
            </ul>

        </div>
    </div>
    <table class="table table-list blank" >
        <tr>
            <td>原价：<del>{$currency_symbol}{$goods_price}</del></div></td><td>套餐价：<span class="price"><b>{$currency_symbol}{$bund['price']}</b></span></td>
        </tr>
        <tr>
            <td colspan="2">{if:$sets_enable}<a href="javascript:;" id="buy-now" class="btn btn-main fr">立即购买</a>
                {else:}
                <a href="javascript:;"  class="btn btn-disable fr">库存不足</a>
                {/if}</td>
        </tr>
    </table>
</div>

<script type="text/javascript">

    var skuMap = {echo:JSON::encode($skumap)};

    $(".product-info").each(function(){
        var spec = $(this);
        $(".spec-values li",spec).each(function(){
            $(this).on("click",function(){
                var disabled = $(this).hasClass('disabled');
                if(disabled) return;
                var flage = $(this).hasClass('selected');

                $(this).parent().find("li").removeClass("selected");
                if(!flage){
                    $(this).addClass("selected");
                }
                changeStatus(spec);
                if($(".spec-values",spec).length == $(".spec-values .selected",spec).length){
                    var sku = new Array();
                    $(".spec-values .selected",spec).each(function(i){
                        sku[i]= $(this).attr("data-value");
                    })
                    var sku_key = ";"+sku.join(";")+";";
                    if(skuMap[sku_key]!=undefined){
                        var sku = skuMap[sku_key];
                    }
                    $(".spec-msg",spec).css("display","none");
                }
            })
        })
    });
function changeStatus(spec){
    var specs_array = new Array();
    $(".spec-values",spec).each(function(i){
        var selected = $(this).find(".selected");
        if(selected.length>0)specs_array[i]=selected.attr("data-value");
        else specs_array[i] = "\\\d+:\\\d+";
    });
    $(".spec-values",spec).each(function(i){
        var selected = $(this).find(".selected");
        $(this).find("li").removeClass("disabled");
        var k = i;
        $(this).find("li").each(function(){

            var temp = specs_array.slice();
            temp[k] = $(this).attr('data-value');
            var flage = false;
            for(sku in skuMap){
                var reg = new RegExp(';'+temp.join(";")+';');
                if(reg.test(sku) && skuMap[sku]['store_nums']>0) flage = true;
            }
            if(!flage)$(this).addClass("disabled");
        })

    });
}

    //立即团购
    $("#buy-now").on("click",function(){
        var product = currentProduct();
        var error_flag = false;
        var ids = new Array();

        for(i in product){

            if(product[i]){
                ids[i] = product[i]['id'];
            }
            else{
                error_flag = true;
                $(".spec-msg:eq("+i+")").css("display","");
                showMsgBar('alert',"请选择您要购买的商品规格！");
            }
        }
        if(!error_flag){
            ids = ids.join('-');
            var url = "{url:/simple/order_info/type/bundbuy/id/$id/pid/}"+ids;
            window.location.href = url;
        }
        return false;
    });

    //取得当前商品
    function currentProduct(){
        var result = new Array();
        $(".product-info").each(function(i){
            var spec = $(this);
            if($(".spec-values",spec).length==0){
                result[i] = skuMap[spec.attr("gid")];
            }else if($(".spec-values",spec).length == $(".spec-values .selected",spec).length){
                var sku = new Array();
                $(".spec-values .selected",spec).each(function(i){
                    sku[i]= $(this).attr("data-value");
                })
                var sku_key = ";"+sku.join(";")+";"+spec.attr("gid");
                if(skuMap[sku_key]!=undefined){
                    result[i] = skuMap[sku_key];
                }else result[i] = null;
            }
            else result[i] = null;
        });
        return result;
    }
    //展示信息
    function showMsgBar(type,text){
        $(".msg").find("span").text(text);
        $(".msg").find("i").attr("class","icon icon-"+type+"-16");
    }
</script>
<!--E 产品展示-->
