<div class="header_menu">
    <div class="container">
        <div class="header-nav clearfix">
            <?php echo $this->partial('shop/layout/headerMenu'); ?>
        </div>
    </div>
</div>
<?php
$form = $this->form;
$form->prepare();

?>
<div class="container">
    <div class="xm-plain-box">
        <div class="box-hd">
            <h3 class="title"><?php echo $this->translate('结账'); ?></h3>
        </div>
    </div>
    <div class="box-hd" id="cart_step" style="margin-top: 20px;">
        <?php echo $this->form()->openTag($form); ?>
        <?php echo $this->partial('shop/message'); ?>

        <?php echo $this->formElement($form->get('shopCsrf')); ?>
        <?php echo $this->formElementErrors($form->get('shopCsrf'), ['class'=>'error-message']); ?>
        <div>
            <h3><?php echo $this->translate('配送方式'); ?></h3>
        </div>
        <div>
            <?php echo $this->formElement($form->get('deliveryType')->setValue($this->allGoodsVirtual ? 'autoDelivery' : 'delivery')); ?>
            <?php echo $this->formElementErrors($form->get('deliveryType'), ['class'=>'error-message']); ?>
            <?php if(!$this->allGoodsVirtual) { ?>
                <?php if($this->deliveryState == 1 && ($this->deliveryExist || !$this->userAddress)) { ?>
                    <a class="btn btn-primary" href="<?php echo $this->url('shop-checkout', ['action' => $this->buyNowStatus ? 'buyNow' : 'index']); ?>"><?php echo $this->translate('商家配送'); ?></a>
                <?php } ?>
                <?php if($this->selfFetchList && !$this->haveGoodsVirtual) { ?>
                    <a class="btn btn-default" href="<?php echo $this->url('shop-checkout', ['action' => $this->buyNowStatus ? 'buyNow' : 'index'], ['query' => ['deliveryType' => 'self-fetch']]); ?>"><?php echo $this->translate('到店自提'); ?></a>
                <?php } ?>
            <?php } else { ?>
                <a class="btn btn-primary" href="javascript:;"><?php echo $this->translate('自动发货'); ?></a>
            <?php } ?>
        </div>
        <?php if(!$this->allGoodsVirtual) { ?>
            <hr style="margin-bottom: 10px;margin-top: 20px;" />
            <div>
                <div class="span20" style="margin-bottom: 6px; font-size: 16px;">
                    <div class="span10" style="text-align: left; font-weight: bold;"><?php echo $this->translate('收货地址'); ?></div>
                    <div class="span10" style="text-align: right;"><a href="javascript:;" onclick="userAddressModal(0);" class="btn btn-default"><?php echo $this->translate('添加新地址'); ?></a></div></h5>
                </div>
                <?php if($this->userAddress) { ?>
                    <div id="shipping-existing">
                        <ul class="shipping_address_id">
                            <?php foreach ($this->userAddress as $key => $addressValue) { ?>
                                <li style="margin-bottom: 12px;" class="radio span20 <?php if ($this->addressInfo->getAddressId() == $addressValue->getAddressId()) echo 'selected'; ?>" data-address-id="<?php echo $addressValue->getAddressId(); ?>">
                                    <div class="items span1">
                                        <input type="radio" name="addressId" <?php if ($this->addressInfo->getAddressId() == $addressValue->getAddressId()) echo 'checked'; ?> value="<?php echo $addressValue->getAddressId(); ?>" onclick="changeAddressId(<?php echo $addressValue->getAddressId(); ?>);">
                                    </div>
                                    <div class="items span18">
                                        <div class="name"><?php echo $this->escapeHtml($addressValue->getTrueName());?> (<?php echo $this->escapeHtml($addressValue->getPhone());?>)</div>
                                        <div><?php echo $this->escapeHtml($addressValue->getRegionValue() . $addressValue->getAddressInfo()); ?></div>
                                    </div>
                                    <div class="address-actions span1">
                                        <a href="javascript:;" onclick="userAddressModal(<?php echo $addressValue->getAddressId(); ?>);">
                                            <span><i class="fa fa-pencil-square-o text" aria-hidden="true"></i></span>
                                        </a>
                                    </div>
                                </li>
                            <?php } ?>
                        </ul>
                    </div>
                <?php } else { ?>
                    <div class="span20" style="margin-bottom: 10px;"><a href="javascript:;" onclick="userAddressModal(0);" class="btn btn-primary d"><?php echo $this->translate('您还没有收货地址，请 点击 添加您的第一个收货地址'); ?></a></div>
                <?php } ?>
                <?php echo $this->formElementErrors($form->get('addressId'), ['class'=>'error-message']); ?>
            </div>
        <?php } ?>
        <hr style="margin-top: 20px;" />
        <div>
            <h3><?php echo $this->translate('支付方式'); ?></h3>
            <div>
                <?php echo $this->formElement($form->get('paymentType')->setValue('OnlinePay')); ?>
                <?php echo $this->formElementErrors($form->get('paymentType'), ['class'=>'error-message']); ?>
                <label>
                    <input type="radio" name="paymentMethod" value="OnlinePay" checked="checked" onclick="changePayment('OnlinePay');">
                    <?php echo $this->translate('在线支付'); ?>
                </label>
                <?php if(!$this->allGoodsVirtual && !$this->haveGoodsVirtual && $this->cashOnDelivery['paymentState']['checked'] == 1) { ?>
                    <label>
                        <input type="radio" name="paymentMethod" value="CashOnDelivery" onclick="changePayment('CashOnDelivery');">
                        <?php echo $this->translate('货到付款'); ?>
                    </label>
                <?php } ?>
            </div>
        </div>
        <?php if(!$this->allGoodsVirtual && $this->shippingDate && !empty($this->shippingDate)) { ?>
        <hr style="margin-top: 20px;" />
        <div>
            <h3><?php echo $this->translate('送货时间'); ?></h3>
        </div>
        <div id="shiptime-method-section">
            <?php echo $this->formElementErrors($form->get('deliveryTimeInfo'), ['class'=>'error-message']); ?>
            <?php foreach ($this->shippingDate as $dataKey => $dateValue) { ?>
                <label class="radio" data-shiptime-id="<?php echo $dataKey; ?>">
                    <input type="radio" name="deliveryTimeInfo" onclick="clickShipTime(<?php echo $dataKey; ?>);" value="<?php echo $this->escapeHtml($dateValue); ?>"><?php echo $this->escapeHtml($dateValue); ?>
                </label>
            <?php } ?>
        </div>
        <?php } ?>
        <hr style="margin-top: 20px;" />
        <?php if (!$this->buyNowStatus) { ?>
            <div class="span20">
                <h3 class="span10"><?php echo $this->translate('购物车'); ?></h3>
                <h3 class="span10" style="text-align: right;"><a href="<?php echo $this->url('shop-cart'); ?>" style="color: #ed6343;"><?php echo $this->translate('《返回修改购物车'); ?></a>
            </div>
        <?php } ?>
        <div class="span20 shop-cart-goods">
            <table>
                <thead>
                <tr>
                    <th width="36%"><?php echo $this->translate('产品'); ?></th>
                    <th width="10%"><?php echo $this->translate('货号'); ?></th>
                    <th width="10%"><?php echo $this->translate('规格'); ?></th>
                    <th width="10%"><?php echo $this->translate('数量'); ?></th>
                    <th width="10%"><?php echo $this->translate('价格'); ?></th>
                    <th width="10%"><?php echo $this->translate('总价'); ?></th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($this->cartGoodsList as $goodsValue) { ?>
                <tr>
                    <td>
                        <a href="<?php echo $this->url('shop-goods', ['id' => $goodsValue[0]->getGoodsId()]); ?>">
                            <img src="" data-original="<?php echo $this->shopHelper()->baseGoodsImagePath($goodsValue['goodsThumbnailImage']); ?>" class="lazy" style="height:65px;width:65px;border:1px solid #D7D7D7">
                        </a>
                        <a href="<?php echo $this->url('shop-goods', ['id' => $goodsValue[0]->getGoodsId()]); ?>">
                            <?php echo $this->escapeHtml($goodsValue['goodsName']); ?>
                        </a>
                    </td>
                    <td><?php echo $this->escapeHtml($goodsValue['goodsItem']); ?></td>
                    <td>
                        <?php if(isset($goodsValue['specGoodsTagInfo'])) { ?>
                            <?php foreach ($goodsValue['specGoodsTagInfo'] as $specInfo) { ?>
                                <small><?php echo $this->escapeHtml($specInfo['tagGroupName']); ?>：<?php echo $this->escapeHtml($specInfo[0]->getTagName()); ?></small>
                            <?php } ?>
                        <?php } ?>
                    </td>
                    <td><?php echo $goodsValue[0]->getBuyNum(); ?></td>
                    <td><?php echo $this->frontCurrency($goodsValue['goodsPrice']); ?></td>
                    <td><?php echo $this->frontCurrency($goodsValue['goodsAmount']); ?></td>
                </tr>
                <?php } ?>
                <tr>
                    <td colspan="6">
                        <p style="text-align: right;">
                            <?php if ($this->totalWeight > 0) { ?>
                                <strong><?php echo $this->translate('商品总重:').$this->totalWeight/1000; ?></strong><?php echo $this->translate('千克'); ?>&nbsp;&nbsp;
                            <?php } ?>
                            <span style="margin-right:40px;"><strong><?php echo $this->translate('应付金额'); ?>：<?php echo $this->frontCurrency($this->cartAmount); ?></strong></span>
                        </p>
                    </td>
                </tr>
                <tr>
                    <td colspan="6">
                        <p style="text-align: right;"><span style="padding-right:40px;">
                    <strong>
                    <?php echo $this->translate('应付金额'); ?>：<font color="#ED145B"><?php echo $this->frontCurrency($this->cartAmount); ?></font>
                    <?php if (!$this->allGoodsVirtual) { ?>
                        + <?php echo $this->translate('配送费用'); ?>：<font color="#ED145B"><?php echo $this->frontCurrency($this->deliveryFee); ?></font>
                    <?php } ?>
                    <?php if ($this->discountCost['discountCost'] > 0) { ?>
                        - <?php echo $this->translate('优惠立减'); ?>：<font color="#ED145B"><?php echo $this->frontCurrency($this->discountCost['discountCost']); ?></font>
                    <?php } ?>
                    <?php if (isset($this->useCouponList) && !empty($this->useCouponList)) { ?>
                        - <?php echo $this->translate('优惠券优惠'); ?>：<font color="#ED145B"><span id="show-couponPrice"><?php echo $this->frontCurrency(0); ?></span></font>
                    <?php } ?>
                    <?php if ($this->goodsIntegralBuy > 0) { ?>
                        - <?php echo $this->translate('积分购买'); ?>：<font color="#ED145B"><span id="show-integralPrice"><?php echo $this->frontCurrency(0); ?></span></font>
                    <?php } ?>
                        = <font color="#ED145B"><span id="show-orderAmount"><?php echo $this->frontCurrency($this->orderAmount); ?></span></font>
                    </strong>
                        </p>
                    </td>
                </tr>
                <?php if($this->integral['integralType1Num'] > 0 || $this->integral['integralType2Num'] > 0) { ?>
                <tr>
                    <td colspan="6">
                        <p style="text-align: right;padding-right:40px;">
                            <?php echo $this->translate('可获得'); ?>：
                            <?php if($this->integral['integralType1Num'] > 0) echo $this->integral['integralType1Num'].'&nbsp;'.$this->translate('消费积分').'&nbsp;'; ?>
                            <?php if($this->integral['integralType2Num'] > 0) echo $this->integral['integralType2Num'].'&nbsp;'.$this->translate('等级积分').'&nbsp;'; ?>
                        </p>
                    </td>
                </tr>
                <?php } ?>
                </tbody>
            </table>
        </div>
        <?php if (isset($this->formExtend)) { ?>
            <div class="span20">
                <hr style="margin-top: 20px;" />
                <?php foreach ($this->formExtend as $formInputValue) { ?>
                    <label>
                        <strong><?php echo $formInputValue['inputTitle']; ?></strong>
                        <?php echo $this->formElement($form->get($formInputValue['inputName'])->setAttributes(['class' => 'span'. $formInputValue['classSpanNum']])); ?>
                        <?php echo $this->formElementErrors($form->get($formInputValue['inputName']), ['class'=>'error-message']); ?>
                    </label>
                <?php } ?>
            </div>
        <?php } ?>

        <?php if (isset($this->useCouponList) && !empty($this->useCouponList)) { ?>
            <div class="span20">
                <hr style="margin-top: 20px;" />
                <label>
                    <strong><?php echo $this->translate('有可用优惠券:'); ?></strong>
                    <select name="couponId" id="couponId" onchange="ajaxDiscount();" class="span5">
                        <option value="0"><?php echo $this->translate('选择优惠券'); ?></option>
                        <?php foreach ($this->useCouponList as $couponValue) { ?>
                            <option value="<?php echo $couponValue[0]->getCouponId(); ?>"><?php echo $this->escapeHtml($couponValue['couponTitle']); ?></option>
                        <?php } ?>
                    </select>
                </label>
            </div>
        <?php } ?>

        <?php if ($this->userInfo->getIntegralType1Num() > 0 && $this->goodsIntegralBuy > 0) { ?>
        <div class="">
            <hr style="margin-top: 20px;" />
            <label>
                <strong><?php echo $this->translate('积分购买:'); ?></strong>
                <input type="number" min="1" value="" name="useIntegralNum" id="useIntegralNum" <?php echo 'max="' . ($this->userInfo->getIntegralType1Num() >= $this->goodsIntegralBuy ? $this->goodsIntegralBuy : $this->userInfo->getIntegralType1Num()) . '"'; ?> class="span4">
                <input type="button" class="btn btn-primary" onclick="ajaxDiscount();" value="<?php echo $this->translate('点击使用积分'); ?>">
                <b><?php echo $this->translate('本次可使用').'<span>'; ?><?php echo $this->userInfo->getIntegralType1Num() >= $this->goodsIntegralBuy ? $this->goodsIntegralBuy : $this->userInfo->getIntegralType1Num(); ?><?php echo '</span>'.$this->translate('消费积分'); ?></b>
                [<?php echo $this->translate('您拥有').' '.$this->userInfo->getIntegralType1Num().' '.$this->translate('消费积分'); ?>]
            </label>
        </div>
        <?php } ?>
        <hr style="margin-top: 20px;" />
        <div>
            <h3><?php echo $this->translate('订单留言'); ?></h3>
        </div>
        <div>
            <?php echo $this->formElement($form->get('orderMessage')->setAttributes(['class' => 'form-control span9', 'rows' => 3])); ?>
            <?php echo $this->formElementErrors($form->get('orderMessage'), ['class'=>'error-message']); ?>
        </div>

        <hr style="margin-top: 20px;" />
        <div>
            <div style="text-align: center;">
                <input type="submit" class="btn btn-large btn-primary" value="<?php echo $this->translate('确认订单'); ?>">
            </div>
        </div>

        <?php echo $this->form()->closeTag(); ?>
    </div>
</div>
<script type="text/javascript">
    function changeAddressId(addressId) {
        layer.load(2, {shade: [0.05, '#000'] });
        window.location.href = "<?php echo $this->url('shop-checkout', ['action' => $this->buyNowStatus ? 'buyNow' : 'index']); ?>?addressId="+addressId;
    }
    function userAddressModal(addressId) {
        layer.open({
            type: 2,
            title: addressId > 0 ? '<?php echo $this->translate('修改地址'); ?>' : '<?php echo $this->translate('添加新地址'); ?>',
            skin: 'address-form-modal',
            scrollbar: false,
            area: ['60%', '80%'],
            content:'<?php echo $this->url('home-user-addr', ['action' => 'userAddress']); ?>?addressId='+addressId,
            end: function(index) {
                layer.load(2, {shade: [0.05, '#000'] });
                window.location.reload();
            }
        });
    }

    function changePayment(paymentValue) {
        $('#paymentType').val(paymentValue);
    }

    function clickShipTime(inputId) {
        $('#shiptime-method-section label').each(function(){
            if($(this).attr('data-shiptime-id')==inputId) {
                $(this).attr('class', 'radio selected');
            } else {
                $(this).attr('class', 'radio');
            }
        });
    }

    function ajaxDiscount() {
        $.ajax({
            url: '<?php echo $this->url('shop-checkout', ['action'=>'checkBuyGoodsUse']); ?>?addressId=<?php echo $this->addressInfo ? $this->addressInfo->getAddressId() : '0'; ?>&buyNowStatus=<?php echo $this->buyNowStatus; ?>',
            type: 'post',
            dataType: 'json',
            data: $('#<?php echo $form->getName(); ?>').formSerialize(),
            beforeSend: function() {
                $('#button-integral').button('loading');
            },
            complete: function() {
                $('#button-integral').button('reset');
            },
            success: function(data) {
                showMessage(data.message);
                if(data.status == 'true') {
                    $('#show-integralPrice').html(data.integralPrice);
                    $('#show-couponPrice').html(data.couponPrice);
                    $('#show-orderAmount').html(data.orderAmount);
                }
            }
        });
    }
</script>