@model MrCMS.Web.Apps.Ecommerce.Pages.OrderPlaced
@using MrCMS.Helpers
@using MrCMS.Models.Auth
@using MrCMS.Web.Apps.Ecommerce.Entities.Orders
@using MrCMS.Web.Apps.Ecommerce.Entities.RewardPoints
@using MrCMS.Web.Apps.Ecommerce.Helpers
@using MrCMS.Web.Apps.Ecommerce.Models
@using MrCMS.Web.Apps.Ecommerce.Settings
@{
    var order = ViewData["order"] as Order;
    var emailRegistrationStatus = ViewData["user-can-register"] as EmailRegistrationStatus? ?? EmailRegistrationStatus.Available;
    
    Html.RenderPartial("GoogleAnalyticsOrderPartial", order);
}

<div class="padding-top-bottom-20">
    <div class="row">
        <div class="col-sm-8">
            <div class="border-all margin-bottom-20">
                <div class="sub-header grey-background">
                    <h1>@Resource("Order Placed - Title", "Thank you your order has been received")</h1>
                </div>
                <div class="padding-20">
                    <p>Order ID: @order.Id.ToString()</p>
                    <p>
                        @(string.Format(Resource("Order Placed - Details sent to", "Your order details have been sent to {0}"), order.OrderEmail))
                    </p>
                    @Editable(Model, placed => placed.BodyContent)

                    <h3 class="margin-0">@Resource("Your order is complete") <img src="~/Apps/Ecommerce/Content/Images/tick.png" alt="tick" /> </h3>
                </div>
            </div>


            @switch (emailRegistrationStatus)
            {
                case EmailRegistrationStatus.EmailInUse:
                    Html.RenderPartial("LoginAndAssociateOrder", new LoginModel { Email = order.OrderEmail });
                    break;
                case EmailRegistrationStatus.Available:
                    Html.RenderPartial("RegisterAndAssociateOrder", new RegisterModel { Email = order.OrderEmail, FirstName = order.BillingAddress.FirstName, LastName = order.BillingAddress.LastName });
                    break;
            }

            <div class="border-all margin-bottom-20">
                <div class="padding-20">
                    <div class="row">
                        <div class="col-xs-6 col-sm-4">
                            <a href="@(UniquePageHelper.GetUrl<MrCMS.Web.Apps.Ecommerce.Pages.ProductSearch>())" class="btn btn-default btn-block">
                                @Resource("Return to shop")
                            </a>
                        </div>
                        <div class="col-xs-6 col-sm-4">
                            <a href="/delivery-and-returns-info" class="btn btn-default btn-block">
                                @Resource("Delivery and returns info")
                            </a>
                        </div>
                    </div>
                </div>
            </div>

        </div>
        <div class="col-sm-4">
            @if (order.OrderLines.Any())
            {
                <div class="sub-header border-top">
                    @Resource("Order Summary")
                </div>
                <div class="grey-background padding-20 margin-bottom-20">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>@Resource("Description")</th>
                                <th>@Resource("Amount")</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (OrderLine item in order.OrderLines)
                            {
                                <tr>
                                    <td>
                                        <ul class="media-list">
                                            <li>@item.Name</li>
                                            <li>@Resource("Item Price"): @item.UnitPrice.ToCurrencyFormat()</li>
                                            <li>@Resource("Quantity"): @item.Quantity</li>
                                        </ul>
                                    </td>
                                    <td>@item.Price.ToCurrencyFormat()</td>
                                </tr>
                            }
                        </tbody>
                        <tfoot>
                            <tr>
                                <th>@Resource("Sub Total"):</th>
                                <td>@order.Subtotal.ToCurrencyFormat()</td>
                            </tr>
                            @if (order.ShippingTotal > decimal.Zero)
                            {
                                <tr>
                                    <th>@Resource("Shipping"):</th>
                                    <td>@order.ShippingTotal.ToCurrencyFormat()</td>
                                </tr>
                            }
                            @if (order.ShippingDiscountAmount > decimal.Zero)
                            {
                                <tr>
                                    <th>@Resource("Shipping Discount"):</th>
                                    <td>@((-order.ShippingDiscountAmount).ToCurrencyFormat())</td>
                                </tr>
                            }
                            @if (order.DiscountAmount > 0)
                            {
                                <tr>
                                    <th>@Resource("Discount"): </th>
                                    <td>@((-order.DiscountAmount).ToCurrencyFormat())</td>
                                </tr>
                            }
                            @if (SiteSettings<TaxSettings>().TaxesEnabled)
                            {
                                <tr>
                                    <th>@Resource("Tax"): </th>
                                    <td>@order.Tax.ToCurrencyFormat()</td>
                                </tr>
                            }
                            <tr>
                                <th>@Resource("Total"):</th>
                                <td><strong>@order.Total.ToCurrencyFormat()</strong></td>
                            </tr>
                            @if (order.GiftCardUsages.Any() || order.OrderRewardPointsHistories.OfType<PointsSpent>().Any())
                            {
                                foreach (var card in order.GiftCardUsages)
                                {
                                    <tr>
                                        <td>
                                            @Resource("Gift Card") @card.GiftCard.Code
                                        </td>
                                        <td>
                                            @card.Amount.ToCurrencyFormat()
                                        </td>
                                    </tr>
                                }
                                if (order.OrderRewardPointsHistories.OfType<PointsSpent>().Any())
                                {
                                    var pointsSpent = order.OrderRewardPointsHistories.OfType<PointsSpent>().First();
                                    <tr>
                                        <td>
                                            Reward points applied (@pointsSpent.Points)
                                        </td>
                                        <td>
                                            @((-order.RewardPointsAppliedAmount).ToCurrencyFormat())
                                        </td>
                                    </tr>
                                }
                                <tr>
                                    <th>@Resource("Total paid"):</th>
                                    <td><strong>@order.TotalPaid.ToCurrencyFormat()</strong></td>
                                </tr>
                            }
                        </tfoot>
                    </table>
                </div>


            }

            <div class="grey-background padding-20 margin-bottom-20">
                <p>
                    <strong>@Resource("Your Email")</strong>
                    <span class="pull-right"><img src="~/Apps/Ecommerce/Content/Images/tick-sm.png" alt="tick" /></span>
                </p>
                <p>@order.OrderEmail</p>
            </div>

            @if (order.ShippingAddress != null)
            {
                <div class="grey-background padding-20 margin-bottom-20">
                    <p>
                        <strong>@Resource("Delivery Details")</strong>
                        <span class="pull-right"><img src="~/Apps/Ecommerce/Content/Images/tick-sm.png" alt="tick" /></span>
                    </p>
                    <ul class="media-list">
                        <li>@(order.ShippingAddress.Name ?? String.Empty)</li>
                        <li>@(order.ShippingAddress.PhoneNumber ?? String.Empty)</li>
                        <li>@(order.ShippingAddress.Company ?? String.Empty)</li>
                        <li>@(order.ShippingAddress.Address1 ?? String.Empty)</li>
                        <li>@(order.ShippingAddress.Address2 ?? String.Empty)</li>
                        <li>@(order.ShippingAddress.City ?? String.Empty)</li>
                        <li>@(order.ShippingAddress.PostalCode ?? String.Empty)</li>
                        <li>@(order.ShippingAddress.GetCountryName())</li>
                    </ul>
                </div>
            }

            <div class="grey-background padding-20 margin-bottom-20">
                <p>
                    <strong>@Resource("Billing Details")</strong>
                    <span class="pull-right"><img src="~/Apps/Ecommerce/Content/Images/tick-sm.png" alt="tick" /></span>
                </p>
                <ul class="media-list">
                    <li>@(order.BillingAddress.Name ?? String.Empty)</li>
                    <li>@(order.BillingAddress.PhoneNumber ?? String.Empty)</li>
                    <li>@(order.BillingAddress.Company ?? String.Empty)</li>
                    <li>@(order.BillingAddress.Address1 ?? String.Empty)</li>
                    <li>@(order.BillingAddress.Address2 ?? String.Empty)</li>
                    <li>@(order.BillingAddress.City ?? String.Empty)</li>
                    <li>@(order.BillingAddress.PostalCode ?? String.Empty)</li>
                    <li>@(order.BillingAddress.GetCountryName())</li>
                </ul>
            </div>

        </div>
    </div>
</div>
