{echo:JS::import("form")} {echo:JS::import('dialog?skin=tinysimple');} {echo:JS::import('dialogtools');}
<link type="text/css" rel="stylesheet" href="{url:#css/simple.css}" /> {set:$items=array("确认订单信息","选择支付","订购完成");$allVirtual=true;} {widget:name=sub_navs action=crumbs items=$items step=3 current=1}
<div class="container">
    <div class="order-info">
        {if:isset($msg)}
        <div class="alert alert-{$msg[0]}">{$msg[1]}</div>
        {/if}
        <form action="{url:/simple/order_act}" class="tiny-form" method="post">
            <input type="hidden" name="type" value="{$order_type}">
            <input type="hidden" name="id" value="{$id}">
            <div class="clearfix mt15 address address-box">
                <h2>选择收货地址：<a class="fr btn btn-main" href="{url:/ucenter/address}" target="_blank">管理地址</a></h2>
                <ul class="address-list clearfix">
                    {set:$address_default=0;} {list:items=$address}
                    <li>

                        <a href="javascript:;" data-value="{$item['id']}" class="modify "> 修改地址 </a>
                         <div class="address-info " >
                            <input type="radio" name="address_id" city="{$item['city']}" value="{$item['id']}" {if:$item['is_default']==1}{set:$address_default=$item['id']}checked="checked"{/if}><label>{$parse_area[$item['province']]} <strong>{$parse_area[$item['city']]}</strong>（{$item['accept_name']} 收）</label>
                            <p>
                                {$parse_area[$item['county']]} {$item['addr']} {$item['mobile']}
                            </p>
                        </div>
                        <i class="icon-selected-32"></i>
                    </li>
                    {/list}
                </ul>
                <div><a id="address_other" class="btn btn-main" href="javascript:;">使用新地址</a></div>
            </div>
            <h2 class="f14">支付方式：</h2>
            <div class="clearfix">
                <ul class="payment-list">
                    {set:$payment_default = 1;} {query:name=payment as pa fields=pa.*,pp.logo,pp.class_name join=left join pay_plugin as pp on pa.plugin_id eq pp.id where=pa.status eq 0 and (pa.client_type eq 0 or pa.client_type eq 2) order=pa.sort desc}
                    <li>
                        <input type="radio" name="payment_id" {if:$key==0} {set:$payment_default=$item['id'];} checked="checked" {/if} value="{$item['id']}" payment_name="{$item['class_name']}">
                        <label><b>{$item['pay_name']}</b> {$item['pay_desc']}</label>
                        <div><img src="{url:@protected/classes/$item[logo]}"></div>
                    </li>
                    {if:$item['class_name']=="received" && $item['config']!=''}
                    {!$received_areas = unserialize($item['config'])}
                    {!$received_areas = $received_areas['areas']}
                    {/if}
                    {/query}
                </ul>
            </div>
            <h2 class="f14">商品清单：</h2>
            <div class="blank">
                {if:$order_type!='bundbuy'}
                <table width="100%" class="table table-line">
                    <tr>
                        <th style="width:60px;">商品</th>
                        <th>名称</th>
                        <th style="width:200px;">规格</th>
                        <th style="width:100px;">活动单价</th>
                        <th style="width:120px;">数量</th>
                        <th style="width:100px;">小计</th>
                    </tr>
                    {set:$total=0.00;$weight=0;$point=0;} {list:items=$product} {set:$total+=$item['amount'];$weight += ($item['weight']*$item['num']);$point += ($item['point']*$item['num']);}
                    {if:$item['goods_type']==0}{!$allVirtual=false}{/if}
                    <tr id="{$item['id']}">
                        <td>
                            <a href="{url:/index/product/id/$item[goods_id]}" target="_blank"><img src="{echo:Common::thumb($item['img'],100,100)}" width="50" height="50"></a>
                        </td>
                        <td><a href="{url:/index/product/id/$item[goods_id]}" target="_blank">{$item['name']} <input name="product_id[]" type="hidden" value="{$item['id']}"></a></td>
                        <td>{list:items=$item['spec'] item=$spec}
                            <p title="{$spec['name']}:{$spec['value'][2]}">{$spec['name']}：{$spec['value'][2]}</p>
                            {/list}</td>
                        <td class="tr">{$item['price']}</td>
                        <td class="tc">
                            <div class="buy-num-bar buy-num clearfix"><a class="btn-dec" href="javascript:;"><i class="icon-minus-16"></i></a>
                                <input name="buy_num[]" value="{$item['num']}" maxlength=5><a class="btn-add" href="javascript:;"><i class="icon-plus-16"></i></a></div>
                        </td>
                        <td class="amount red tr">{$item['amount']}</td>
                    </tr>
                    {/list}
                </table>
                {else:}
                <table width="100%" class="table table-line">
                    <tr>
                        <th style="width:60px;">商品</th>
                        <th>名称</th>
                        <th style="width:200px;">规格</th>
                        <th style="width:100px;">单价</th>
                    </tr>
                    {set:$total=0.00;$weight=0;$point=0;} {list:items=$product} {set:$total+=$item['amount'];$weight += ($item['weight']*$item['num']);$point += ($item['point']*$item['num']);}
                    {if:$item['goods_type']==0}{!$allVirtual=false}{/if}
                    <tr id="{$item['id']}">
                        <td>
                            <a href="{url:/index/product/id/$item[goods_id]}" target="_blank"><img src="{url:@$item[img]}" width="50" height="50"></a>
                        </td>
                        <td><a href="{url:/index/product/id/$item[goods_id]}" target="_blank">{$item['name']}</a>
                            <input name="product_id[]" type="hidden" value="{$item['id']}">
                        </td>
                        <td>{list:items=$item['spec'] item=$spec}
                            <p title="{$spec['name']}:{$spec['value'][2]}">{$spec['name']}：{$spec['value'][2]}</p>
                            {/list}</td>
                        <td class="tr">{$item['price']}</td>
                    </tr>
                    {/list}
                    <tr id="{$pid}">
                        <td></td>
                        <td><a href="{url:/index/bundbuy/id/$bund[id]}" target="_blank">套餐：{$bund['title']}</a></td>
                        <td class="tl">
                            <div class="buy-num-bar buy-num clearfix"><a class="btn-dec" href="javascript:;"><i class="icon-minus-16"></i></a>
                                <input name="buy_num[]" value="{$item['num']}" maxlength=5><a class="btn-add" href="javascript:;"><i class="icon-plus-16"></i></a><span class="p10"></span></div>
                        </td>
                        <td class="amount red tr">{$bund['price']}</td>
                    </tr>
                </table>
                {set:$total=$bund['price']} {/if} {set:$prom = new Prom($total);$proms = $prom->meetProms();} {set:$fare = new Fare($weight);}
                <table class="table table-list">
                    <tr>
                        <td>
                            <p>订单备注信息：
                                <input type="text" name="user_remark" style="width:346px;">
                            </p>
                        </td>
                        <td width="260" class="tr">购物车商品合计：</td>
                        <td width="140">
                            <div class="mb10 mt10" style=" background: #f0f0f0;"><span class="fr"><span style=""><span class="currency-symbol f18">{$currency_symbol}</span><b class="cart-total red f18" id="total-amount" total="{$total}">{echo:sprintf("%01.2f",$total)} </b></span>
                                </span>
                            </div>
                        </td>
                    </tr>
                    {if:$open_invoice}
                    <tr>
                        <td>
                            <p style="height:32px;line-height:32px;">索要发票({$tax}%)：
                                <input type="checkbox" class="magic-checkbox" name="is_invoice" id="is_invoice" value="1" data-value="{$tax}"><label for="is_invoice"></label>&nbsp;&nbsp;<span id="invoice" {if:isset($is_invoice) && $is_invoice==1} {else:}style="display:none;" {/if}>发票抬头：<select name="invoice_type"><option value="0">个人</option><option value="1">单位</option></select>&nbsp;&nbsp;<input type="text" name="invoice_title" ></span></p>
                        </td>
                        <td class="tr">税：</td>
                        <td>
                            <p class="fr">+ <b id="taxes" data-value="{$tax}">0</b></p>
                        </td>
                    </tr>
                    {/if}
                    <tr>
                        <td>
                        </td>
                        <td class="tr">运费：</td>
                        <td>
                            <p class="fr">+ <b id="fare" data-weight="{$weight}">{echo:$fare->calculate(16);}</b></p>
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td class="tr orange">送积分：</td>
                        <td>
                            <p class="fr orange"><b id="point" data-point="{$point}">{$point}</b></p>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="checkbox" class="magic-checkbox" id="voucher-select" name=""><label for="voucher-select"></label><a href="javascript:;" id="voucher-btn" style="line-height: 25px;height:25px;">使用代金券抵消部分总额：</a>
                        </td>
                        <td class="tr">代金券：</td>
                        <td class="tr">- <b id="voucher">0.00</b></td>
                    </tr>
                </table>
            </div>
            <div class="box" id="voucher-n" style="display: none">
                <p class="clearfix">提示：一个订单最多能使用一张代金券（<b class="red">注：代金券仅能抵扣商品金额,多出商品的部分忽略不计</b>）。<a id="voucher-cancel" class="fr btn btn-mini ">取消优惠券</a></p>
                <table class="voucher-list table table-line">
                    <tr style="background: #fff5cc;color: #000;height:20px;">
                        <td>名称</td>
                        <td>编号</td>
                        <td>面值</td>
                        <td>需满足金额</td>
                        <td>有效期</td>
                    </tr>
                    <tbody class="page-content">
                        <tr>
                            <td>
                                <input name="voucher" type="radio" value="{id}" data-value="{value}">
                                <label>{name}</label>
                            </td>
                            <td>{account}</td>
                            <td>{value}</td>
                            <td>{money}</td>
                            <td>{end_time}</td>
                        </tr>
                    </tbody>
                </table>
                <div class="page-nav">ww</div>
            </div>
            <div class=" clearfix" style="padding:10px; background: #f0f0f0;">
                <span class="fr f14">应付总额：<span style="font-size: 24px;font-family: tahoma"><span class="currency-symbol">{$currency_symbol}</span><b class="cart-total red" id="real-total">{echo:sprintf("%01.2f",$total)}</b></span>
                </span>
            </div>
            <div class="blank clearfix">
                <p class="fr">
                    <input type="submit" class="btn btn-main" value="提交订单">
                </p>
            </div>
            {token:key=order}
        </form>
    </div>
</div>

<script type="text/javascript">
    {if:$allVirtual}
        $('.address-box').hide();
    {/if}
    $("#address_other").on("click",function(){
        layer.open({
            type: 2,
            title: '添加收货地址',
            shadeClose: true,
            shade: 0.8,
            maxmin: true,
            area: ['960px', '480px'],
            content: '{url:/simple/address_other}'
        });
        return false;
    })
    $(".address-list .modify").each(function(){

        $(this).on("click",function(){
            var id = $(this).attr("data-value");
            layer.open({
                type: 2,
                title: '修改收货地址',
                shadeClose: true,
                shade: 0.8,
                maxmin: true,
                area: ['960px', '480px'],
                content: '{url:/simple/address_other/id/}'+id
            });
            return false;
        });
    });
    $("#voucher-n").Paging({
        url:'{url:/simple/get_voucher}',
        params:{amount:{$total}},
        callback:function(){

            calculate();
            $("#voucher-n input[name='voucher']").each(function(){
                $(this).on("click",function(){
                    calculate();
                });
            });
        }
    });
    $("#voucher-cancel").on("click",function(){
        if($("#voucher-n input[name='voucher']:checked").size()>0){
            $("#voucher-n input[name='voucher']:checked").prop("checked",false);
            calculate();
        }
    })
    $("#voucher-select").on("click",function(){
        $("#voucher-n").toggle();
    })

    $(".address-list li").each(function(){
        $(this).has("input[name='address_id']:checked").addClass("selected");
        $(this).on("click",function(){
            if(!check_received_areas($(this).find("input").attr("city"),"")){
                layer.alert('此地区不支持货到付款方式。');
                return;
            }
            $(".address-list li").removeClass("selected");
            $("input[name='address_id']").removeAttr("checked");
            $("input[name='address_id']",this).prop("checked","checked");
            $(this).addClass("selected");
            $("a.default").hide();
            $("a.default",this).show();
            var id = $("input[name='address_id']",this).val();
            var weight = $("#fare").attr("data-weight");
            var allvirtual = {!=$allVirtual?'true':'false'};
            $.post("{url:/ajax/calculate_fare}",{weight:weight,id:id,allvirtual:allvirtual},function(data){
                if(data['status']=='success'){
                    $("#fare").text(data['fee']);
                    calculate();
                }
            },'json');
        });
    });
    FireEvent($(".address-list  input[name='address_id']:checked").get(0),"click");

    $(".payment-list li").each(function(){
        $(this).has("input[name='payment_id']:checked").addClass("selected");
        $(this).on("click",function(){
            if(!check_received_areas("",$(this).find("input").attr("payment_name"))){
                layer.alert('此地区不支持货到付款方式。');
                return;
            }
            $(".payment-list li").removeClass("selected");
            $("input[name='payment_id']").removeAttr("checked");
            $("input[name='payment_id']",this).prop("checked",true);
            $(this).addClass("selected");
        });
    });

    $("#is_invoice").on("click",function(){
        if(!!$(this).prop("checked")){
            $("#invoice").show();
        }
        else $("#invoice").hide();
        calculate();
    })

    //计算实付金额
    function calculate(){
        var total = parseFloat($("#total-amount").attr("total"));
        var voucher = 0;
        var fare = parseFloat($("#fare").text());
        if($("#voucher-n input[name='voucher']:checked").size()>0){
            voucher = parseFloat($("#voucher-n input[name='voucher']:checked").attr('data-value'));
            if(voucher==undefined) voucher =0;
        }
        total -= voucher;
        $("#voucher").text(voucher.toFixed(2));
        if(total<=0) total = 0;

        if($("#is_invoice").size()>0){
            if(!!$("#is_invoice").prop("checked")){
                var tax_fee = ((total*{$tax})/100);
                total += tax_fee;
                $("#taxes").text(tax_fee.toFixed(2));
            }
            else{
                $("#taxes").text("0.00");
            }
        }

        total += fare;
        var data_point =parseInt($("#point").attr("data-point"));
        data_point = data_point * $("input[name='buy_num[]']").val();
        $("#point").text(data_point);

        $("#real-total").text(total.toFixed(2));
    }
    calculate();

    //数量调整
    $(".btn-dec").on("click",function(){
        var parent = $(this).parent().parent();
        var id = parent.parent().attr("id");
        var num = parent.find("input").val();
        if(num>1) num--;
        else num=1;
        if($("#"+id).find("input").val()!=num) changeInfo(id,num);
        parent.find("input").val(num);
    });
    $(".btn-add").on("click",function(){
        var parent = $(this).parent().parent();
        var id = parent.parent().attr("id");
        var num = parent.find("input").val();
        num++;
        if($("#"+id).find("input").val()!=num) changeInfo(id,num);
        parent.find("input").val(num);
    });

    $(".buy-num-bar input").on("change",function(){
        var num = parseInt($(this).val());
        var parent = $(this).parent().parent();
        var id = parent.parent().attr("id");
        changeInfo(id,num);
    })

    function changeInfo(id,num){
        var order_type = "{$order_type}_num";
        var address_id = $("input[name='address_id']:checked").val();
        if(num<=0)num = 1;
        $.post("{url:/simple/}"+order_type,{id:{$id},num:num,pid:id,address_id:address_id},function(data){
            var total = 0.00;
            for(var i in data) total += parseFloat(data[i]['amount']);
                var freight = data[i]['freight'];
            var totalWeight = data[i]['totalWeight'];
            $("#"+id).find(".amount").text(data[id]['amount']);
            $("#"+id).find(".prom").text(data[id]['prom']);
            if(parseInt($("#"+id+" .buy-num-bar").find("input").val())>data[id]['store_nums']){
                $("#"+id+" .buy-num-bar").find("input").val(data[id]['store_nums']);
                var parent = $("#"+id+" .buy-num-bar").find("input").parent().parent();
                if(parent.find(".alert-min").size()==0)parent.append("<div class='alert alert-fail alert-min'>限购数量："+data[id]['store_nums']+"</div>");
            }else{
                $("#"+id+" .buy-num-bar").find("input").val(data[id]['num']);
                $("#"+id+" .buy-num-bar").find("input").parent().parent().find(".alert-min").remove();
            }
            $("#fare").text(freight);
            $("#fare").attr('data-weight',totalWeight);
            $("#total-amount").text(total.toFixed(2));
            $("#total-amount").attr("total",total.toFixed(2));
            calculate();
        },"json");
}
var form = new Form();
form.setValue('address_id',"{$address_id|$address_default}");
form.setValue('payment_id',"{$payment_id|$payment_default}");
form.setValue('user_remark',"{$user_remark}");
form.setValue('prom_id',"{$prom_id}");
{if:isset($is_invoice) && $is_invoice == 1}
form.setValue('is_invoice',"{$is_invoice}");
form.setValue('invoice_type',"{$invoice_type}");
form.setValue('invoice_title',"{$invoice_title}");
{/if}
function check_received_areas(city,payment_name){
            if(city=="") city = $("input[name='address_id']:checked").attr("city");
            if(payment_name=="") payment_name = $("input[name='payment_id']:checked").attr("payment_name");
            var received_areas = "{$received_areas}";
            var areas = received_areas.split(",");
            var flag =  false;
            if(payment_name == "received"){
                for(i in areas){
                    if(areas[i]==city) flag = true;
                }
            }else{
                flag = true;
            }
            return flag;
        }
</script>
