@using MrCMS.Helpers
@using MrCMS.Web.Apps.Ecommerce.Entities.Orders
@using MrCMS.Web.Apps.Ecommerce.Helpers
@using MrCMS.Web.Apps.Ecommerce.Pages
@model UserOrder
@{
    var order = ViewData["Order"] as Order;

    var orderShipping = order.ShippingTotal.GetValueOrDefault() - order.ShippingTax.GetValueOrDefault();

}
<div class="container">
    <div class="padding-top-bottom-20">
        <div class="row">
            <div class="col-sm-12">
                <div class="row">
                    <div class="col-sm-6">
                        <h1>View Order @order.Id</h1>
                    </div>
                    <div class="col-sm-6">
                        <div class="pull-right btn-group">
                            <a href="@(UniquePageHelper.GetUrl<UserAccountOrders>())" class="btn btn-default">Return to account</a>
                            @if (order.OrderStatus.Equals(OrderStatus.Paid))
                            {
                                <a href="/Apps/Ecommerce/OrderPdf/ExportOrderToPdf/@order.Id" class="btn btn-primary">@Resource("Download Order Invoice")</a>
                            }
                        </div>
                    </div>
                </div>
                <br class="clearfix" />
                <table class="table table-condensed table-striped">
                    <tr>
                        <td>Order GUID</td>
                        <td>@order.Guid</td>
                    </tr>
                    <tr>
                        <td>Order Subtotal (ex TAX)</td>
                        <td>@order.Subtotal.ToCurrencyFormat()</td>
                    </tr>
                    <tr>
                        <td>Order Shipping (ex TAX)</td>
                        <td>@orderShipping.ToCurrencyFormat()</td>
                    </tr>
                    <tr>
                        <td>Order Tax</td>
                        <td>@order.Tax.ToCurrencyFormat()</td>
                    </tr>
                    @if (order.DiscountAmount > decimal.Zero)
                    {
                        <tr>
                            <td>@Resource("User Order - Discount", "Discount")</td>
                            <td>@((-order.DiscountAmount).ToCurrencyFormat())</td>
                        </tr>
                    }
                    @if (order.ShippingDiscountAmount > decimal.Zero)
                    {
                        <tr>
                            <td>@Resource("User Order - Shipping Discount", "Shipping Discount")</td>
                            <td>@((-order.ShippingDiscountAmount).ToCurrencyFormat())</td>
                        </tr>
                    }
                    <tr>
                        <td>Order Total</td>
                        <td>@order.Total.ToCurrencyFormat()</td>
                    </tr>
                </table>
            </div>
        </div>
    </div>



    @{
        if (order.OrderRefunds.Count > 0)
        {
            <div class="row">
                <div class="col-sm-12">
                    @if (order.OrderRefunds.Count > 0)
                    {
                        <p>Refunds:</p>
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>
                                        Amount
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in order.OrderRefunds)
                                {
                                    <tr>
                                        <td>
                                            @item.Amount.ToCurrencyFormat()
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    }
                </div>
            </div>
        }
    }

    <hr class="small-margin" />
    <div class="row">
        <div class="col-sm-6">
            <h4>Payment</h4>

            <table class="table table-condensed">
                <tr>
                    <td class="bold-font">Method</td>
                    <td>@order.PaymentMethod</td>
                </tr>
                <tr>
                    <td class="bold-font">Status</td>
                    <td>@order.PaymentStatus</td>
                </tr>
            </table>
        </div>
        <div class="col-sm-6">
            <h4>Shipping</h4>

            <table class="table table-condensed">
                <tr>
                    <td class="bold-font">Method</td>
                    <td>@order.ShippingMethodName</td>
                </tr>
                <tr>
                    <td class="bold-font">Status</td>
                    <td>@order.ShippingStatus</td>
                </tr>
                @{
                    if (!string.IsNullOrEmpty(order.TrackingNumber))
                    {
                        <tr>
                            <td class="bold-font">Tracking Number</td>
                            <td>@order.TrackingNumber</td>
                        </tr>
                    }
                }
            </table>
        </div>
    </div>
    <hr />
    <div class="row">
        <div class="col-sm-6">
            <h4>Shipping Address</h4>
            <div class="pull-left">
                @if (order.ShippingAddress != null)
                {
                    @Html.Raw(order.ShippingAddress.GetDescriptionHtml())
                }
            </div>
        </div>
        <div class="col-sm-6">
            <h4>Billing Address</h4>
            <div class="pull-left">
                @if (order.BillingAddress != null)
                {
                    @Html.Raw(order.BillingAddress.GetDescriptionHtml())
                }
            </div>
        </div>
    </div>


    <hr />
    <h4>Order Items</h4>
    <div class="row">
        <div class="col-sm-12">
            @if (order.OrderLines.Count == 0)
            {
                @:No lines.
            }
            else
            {
                <div class="table-responsive">
                    <table class="table table-striped table-condensed">
                        <thead>
                            <tr>
                                <th>
                                    Product
                                </th>
                                <th>
                                    SKU
                                </th>
                                <th>
                                    Quantity
                                </th>
                                <th>
                                    Unit Price
                                </th>
                                <th>
                                    Subtotal
                                </th>
                                <th>
                                    Tax
                                </th>
                                <th>
                                    Tax Rate
                                </th>
                                <th>
                                    Discount
                                </th>
                                <th>
                                    Weight
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in order.OrderLines)
                            {
                                <tr>
                                    <td>
                                        @(String.IsNullOrWhiteSpace(item.ProductVariant.Name) ? item.ProductVariant.Product.Name : item.ProductVariant.Name)
                                    </td>
                                    <td>
                                        @item.ProductVariant.SKU
                                    </td>
                                    <td>
                                        @item.Quantity
                                    </td>
                                    <td>
                                        @item.UnitPrice.ToCurrencyFormat()
                                    </td>
                                    <td>
                                        @item.Subtotal.ToCurrencyFormat()
                                    </td>
                                    <td>
                                        @item.Tax.ToCurrencyFormat()
                                    </td>
                                    <td>
                                        @item.TaxRate.ToString("n2")
                                    </td>
                                    <td>
                                        @item.Discount.ToCurrencyFormat()
                                    </td>
                                    <td>
                                        @item.Weight.ToString("n2")
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            }
        </div>
    </div>
</div>