@using System.Linq
@using MrCMS.Helpers
@using MrCMS.Web.Apps.Ecommerce.Controllers
@using MrCMS.Web.Apps.Ecommerce.Entities.Cart
@using MrCMS.Web.Apps.Ecommerce.Entities.GiftCards
@using MrCMS.Web.Apps.Ecommerce.Helpers
@using MrCMS.Web.Apps.Ecommerce.Helpers.Cart
@using MrCMS.Web.Apps.Ecommerce.Models
@using MrCMS.Web.Apps.Ecommerce.Pages
@using MrCMS.Web.Apps.Ecommerce.Settings
@model MrCMS.Web.Apps.Ecommerce.Models.CartModel
@{
    var ecommerceSettings = SiteSettings<EcommerceSettings>();
    var taxSettings = SiteSettings<TaxSettings>();
    var enterOrderEmailUrl = UniquePageHelper.GetUrl<EnterOrderEmail>();

    Html.IncludeScript("~/Apps/Ecommerce/Content/Scripts/custom/discount.js");
    Html.IncludeScript("~/Apps/Ecommerce/Content/Scripts/custom/cart.js");
    Html.IncludeScript("~/Apps/Ecommerce/Content/Scripts/custom/gift-cards.js");
    Html.IncludeScript("~/Apps/Ecommerce/Content/Scripts/custom/reward-points.js");
}
<div data-cart-info="@Url.Action("Details", "Cart")">
    <div class="container">
        <div class="row margin-bottom-10">
            <div class="col-sm-6">
                <h1>@Resource("Shopping Basket")</h1>
            </div>
            <div class="col-sm-6">
                <div class="row">
                    <div class="@(!Model.CanEnterPaymentFlow ? "col-sm-offset-6" : "") col-sm-6">
                        <a class="btn btn-default btn-block " href="@(UniquePageHelper.GetUrl<ProductSearch>())">@Resource("Continue Shopping")</a>
                    </div>
                    @if (Model.CanEnterPaymentFlow)
                    {
                        <div class="visible-xs" style="padding-top: 5px;"></div>
                        <div class="col-sm-6">
                            <a class="btn btn-primary btn-block" href="@enterOrderEmailUrl">@Resource("Pay Securely NOW!")</a>
                        </div>
                    }
                </div>
            </div>
        </div>

        @if (Model.CannotCheckoutReasons.Any())
        {
            <div class="alert alert-danger">
                <p>@Resource("Sorry, we cannot begin checkout because"):</p>
                <ul>
                    @foreach (var reason in Model.CannotCheckoutReasons)
                    {
                        <li>@reason</li>
                    }
                </ul>
            </div>
        }

        <div id="cart-items" class="margin-bottom-20">
            @if (Model.Items.Any())
            {
                <div class="row margin-bottom-10 padding-bottom-10 grey-border-bottom" id="header">
                    <div class="col-xs-6">
                        <strong>Item Description</strong>
                    </div>
                    <div class="col-xs-2 text-center">
                        <strong class="hidden-xs">Quantity</strong>
                        <strong class="visible-xs">Qty</strong>
                    </div>
                    <div class="col-xs-3 col-sm-2 text-right">
                        <strong>Price</strong>
                    </div>
                    <div class="col-xs-3 col-sm-2 text-right">
                        <strong>Total</strong>
                    </div>
                </div>
                foreach (var item in Model.Items)
                {
                    Html.RenderPartial("CartItem", item);
                }
            }
            else
            {
                <p class="red">No items in basket.</p>
            }
        </div>

        @if (Model.Items.Any())
        {
            <div class="row margin-bottom-20">
                <div class="col-sm-8 col-sm-offset-4 col-lg-6 col-lg-offset-6">
                    <div class="row">
                        <div class="col-sm-6">
                            <button type="button" class="btn btn-default btn-block" id="update-basket">Update Basket</button>
                        </div>
                        <div class="visible-xs" style="padding-top: 5px;"></div>
                        <div class="col-sm-6">
                            <button type="button" class="btn btn-danger btn-block" id="empty-basket">Empty Basket</button>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row margin-bottom-20" id="cart-discount">
                <div class="col-sm-8 col-sm-offset-4 col-lg-6 col-lg-offset-6">

                    @{ Html.RenderAction("Discounts", "Cart"); }

                    @if (ecommerceSettings.GiftCardsEnabled)
                    {
                        Html.RenderAction("GiftCards", "Cart");
                    }
                </div>
            </div>
        }

        <div class="row">
            <div class="col-sm-8 col-sm-offset-4 col-lg-6 col-lg-offset-6" id="cart-summary">
                @if (Model.Items.Any())
                {
                    <div class="row margin-bottom-10">
                        <div class="col-xs-8 col-sm-6">
                            Subtotal before delivery charges
                        </div>
                        <div class="col-xs-4 col-sm-6 text-right" id="cart-subtotal">
                            @Model.Subtotal.ToCurrencyFormat()
                        </div>
                    </div>
                    if (Model.HasOrderTotalDiscount())
                    {
                        <div class="row margin-bottom-10">
                            <div class="col-xs-8 col-sm-6">
                                Order Discount
                            </div>
                            <div class="col-xs-4 col-sm-6 text-right" id="cart-discount-total">
                                @((-Model.OrderTotalDiscount).ToCurrencyFormat())
                            </div>
                        </div>
                    }
                    if (taxSettings.TaxesEnabled)
                    {
                        <div class="row margin-bottom-10">
                            <div class="col-xs-8 col-sm-6">
                                VAT
                            </div>
                            <div class="col-xs-4 col-sm-6 text-right" id="cart-tax">
                                @Model.Tax.ToCurrencyFormat()
                            </div>
                        </div>
                    }
                    <div class="row margin-bottom-10">
                        <div class="col-xs-8 col-sm-6">
                            Shipping
                        </div>
                        <div class="col-xs-4 col-sm-6 text-right" id="cart-shipping">
                            @Model.CartPageShipping()
                        </div>
                    </div>
                    if (Model.HasShippingDiscount())
                    {
                        <div class="row margin-bottom-10">
                            <div class="col-xs-8 col-sm-6">
                                Shipping Discount
                            </div>
                            <div class="col-xs-4 col-sm-6 text-right" id="cart-discount-total">
                                @((-Model.ShippingDiscount).ToCurrencyFormat())
                            </div>
                        </div>
                    }


                    if (ecommerceSettings.RewardPointsEnabled && Model.AvailablePoints > 0)
                    {
                        <div class="row margin-bottom-10">
                            <div class="col-sm-12 line-height-30">
                                @Html.InlineCheckboxFor(x => x.UseRewardPoints, checkboxAttributes: new { @class = "checkbox", data_use_reward_points = true })
                                @if (!Model.UseRewardPoints)
                                {
                                    <span>
                                        @Resource("Available balance"): @Model.AvailablePoints (@Model.AvailablePointsValue.ToCurrencyFormat())
                                    </span>
                                }
                            </div>
                            <div class="col-sm-4 text-right">
                            </div>
                        </div>
                        if (Model.AppliedRewardPointsAmount > 0)
                        {
                            <div class="row margin-bottom-10">
                                <div class="col-sm-8 line-height-30">
                                    Applied reward points
                                </div>
                                <div class="col-sm-4 text-right">
                                    @((-Model.AppliedRewardPointsAmount).ToCurrencyFormat())
                                </div>
                            </div>
                        }
                    }

                    <hr />
                    <div class="row margin-bottom-10">
                        <div class="col-xs-8 col-sm-6">
                            <strong>@Resource("Total")</strong>
                        </div>
                        <div class="col-xs-4 col-sm-6 text-right" id="cart-total">
                            <strong>@Model.CartPageTotal()</strong>
                        </div>
                    </div>
                    if (Model.GiftCardAmount > 0)
                    {
                        <hr />
                        <div class="row margin-bottom-10">
                            <div class="col-xs-8 col-sm-6">
                                <strong>@Resource("Gift Card Total")</strong>
                            </div>
                            <div class="col-xs-4 col-sm-6 text-right">
                                <strong>-@Model.GiftCardAmount.ToCurrencyFormat()</strong>
                            </div>
                        </div>
                    }
                    if (Model.TotalToPay != Model.Total)
                    {
                        <hr />
                        <div class="row margin-bottom-10">
                            <div class="col-xs-8 col-sm-6">
                                <strong>Total to pay</strong>
                            </div>
                            <div class="col-xs-4 col-sm-6 text-right" id="cart-total">
                                <strong>@Model.CartPageTotalToPay()</strong>
                            </div>
                        </div>
                    }
                    <hr />
                }
            </div>
        </div>
        <div class="row margin-bottom-10">
            <div class="col-sm-6 col-sm-offset-6">
                @if (Model.CanEnterPaymentFlow)
                {
                    <a class="btn btn-primary btn-block btn-lg" href="@enterOrderEmailUrl">@Resource("Pay Securely NOW!")</a>
                }
                @if (Model.CanUsePayPalExpress)
                {
                    using (Html.BeginForm("SetExpressCheckout", "PayPalExpressCheckout", FormMethod.Post))
                    {
                        <div class="text-center">
                            <p>Or</p>
                            <input type="image" src="/Apps/Ecommerce/Content/Images/paypal-button.png" />
                        </div>
                    }
                }
            </div>
        </div>
    </div>
</div>
