@using MrCMS.Helpers
@using MrCMS.Paging
@using MrCMS.Web.Apps.Ecommerce
@using MrCMS.Web.Apps.Ecommerce.Entities.Orders
@using MrCMS.Web.Apps.Ecommerce.Helpers
@using MrCMS.Web.Apps.Ecommerce.Models
@using MrCMS.Web.Apps.Ecommerce.Services.Cart
@model OrderSearchModel
@{
    ViewBag.Title = "Orders";
    var results = ViewData["results"] as IPagedList<Order> ?? PagedList<Order>.Empty;
    var paymentStatusOptions = ViewData["payment-status-options"] as List<SelectListItem> ?? new List<SelectListItem>();
    var shippingStatusOptions = ViewData["shipping-status-options"] as List<SelectListItem> ?? new List<SelectListItem>();
    var ordersShipped = TempData["orders-shipped"] is bool ? (bool)TempData["orders-shipped"] : false;
    Html.IncludeScript("~/Apps/Ecommerce/Areas/Admin/Content/Scripts/custom/FileSaver.min.js");
    Html.IncludeScript("~/Apps/Ecommerce/Areas/Admin/Content/Scripts/custom/bulk-orders.js");
    var exportStatus = TempData["export-status"] as string;
}
@using (Html.BeginForm("Index", "Order", FormMethod.Get))
{
    <fieldset>
        <legend>
            Orders
            <span class="btn-group  display-inline  pull-right">
                <button type="submit" class="btn btn-primary" name="Command" value="Search">Search</button>
            </span>
        </legend>
        @Html.HiddenFor(x => x.Page)
        <div class="row">
            <div class="col-lg-4">
                <div class="form-group">
                    @Html.LabelFor(x => x.SearchText, "Email or Lastname")
                    @Html.TextBoxFor(x => x.SearchText, new { @class = "form-control" })
                </div>
                <div class="form-group">
                    @Html.LabelFor(x => x.OrderId)
                    @Html.TextBoxFor(x => x.OrderId, new { @class = "form-control" })
                </div>
                <div class="form-group">
                    @Html.LabelFor(x => x.SalesChannel)
                    @Html.DropDownListFor(x => x.SalesChannel, Model.SalesChannelOptions, new { @class = "form-control" })
                </div>
            </div>
            <div class="col-lg-4">
                <div class="form-group">
                    @Html.LabelFor(x => x.DateFrom)
                    @Html.TextBoxFor(x => x.DateFrom, new { @class = "form-control date-time-picker" })
                </div>
                <div class="form-group">
                    @Html.LabelFor(x => x.PaymentStatus)
                    @Html.DropDownListFor(x => x.PaymentStatus, paymentStatusOptions, new { @class = "form-control" })
                </div>
                <div class="form-group">
                    @Html.LabelFor(x => x.OrderTotalFrom)
                    @Html.TextBoxFor(x => x.OrderTotalFrom, new { @class = "form-control" })
                </div>
            </div>
            <div class="col-lg-4">
                <div class="form-group">
                    @Html.LabelFor(x => x.DateTo)
                    @Html.TextBoxFor(x => x.DateTo, new { @class = "form-control date-time-picker" })
                </div>
                <div class="form-group">
                    @Html.LabelFor(x => x.ShippingStatus)
                    @Html.DropDownListFor(x => x.ShippingStatus, shippingStatusOptions, new { @class = "form-control" })
                </div>
                <div class="form-group">
                    @Html.LabelFor(x => x.OrderTotalTo)
                    @Html.TextBoxFor(x => x.OrderTotalTo, new { @class = "form-control" })
                </div>
            </div>
        </div>
        <hr />
        @if (!results.Any())
        {
            @:No orders.
        }
        else
        {
            if (ordersShipped)
            {
                <div class="alert alert-success">
                    @Resource("Selected order(s) successfully marked as shipped.")
                </div>
            }
            if (!string.IsNullOrWhiteSpace(exportStatus))
            {
                <div class="alert alert-danger">
                    @exportStatus
                </div>
            }
            <table class="table table-condensed table-striped">
                <thead>
                    <tr>
                        <th>
                            <input type="checkbox" id="selectAll" value="selectAll" />
                        </th>
                        <th colspan="9">
                            <input type="button" class="btn btn-primary bullk-action-button" value="@Resource("Download Invoice(s)")" data-download-invoices />
                            &nbsp;&nbsp;
                            <input type="button" class="btn btn-primary bullk-action-button" value="@Resource("Picking list")" data-picking-list />
                            &nbsp;&nbsp;
                            <input type="button" class="btn btn-primary bullk-action-button" value="@Resource("Mark as shipped")" data-mark-as-shipped />
                        </th>
                    </tr>
                    <tr>
                        <th>

                        </th>
                        <th>
                            Channel
                        </th>
                        <th>
                            Order Id
                        </th>
                        <th>
                            Email
                        </th>
                        <th>
                            Orders
                        </th>
                        <th>
                            Payment Info
                        </th>
                        <th>
                            Shipping Info
                        </th>
                        <th>
                            Amount
                        </th>
                        <th>
                            Order Date
                        </th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var order in results)
                    {
                        var status = "warning";
                        if (order.ShippingStatus.Equals(ShippingStatus.Shipped) && order.PaymentStatus.Equals(PaymentStatus.Paid))
                        {
                            status = "success";
                        }
                        else if (order.IsCancelled)
                        {
                            status = "danger";
                        }
                        <tr class="@status">
                            <td>
                                <input type="checkbox" value="@order.Id" />
                            </td>
                            <td>
                                @(order.SalesChannel ?? EcommerceApp.DefaultSalesChannel)
                            </td>
                            <td>
                                @order.Id
                            </td>
                            <td style="word-break: break-all">
                                @order.OrderEmail
                            </td>
                            <td>
                                @order.GetTotalOrders()
                            </td>
                            <td>
                                @if (!order.IsCancelled)
                                {
                                    @order.PaymentStatus

                                    if (order.PaymentStatus == PaymentStatus.Pending)
                                    {
                                        <span></span>
                                        @Html.ActionLink("Mark as Paid", "MarkAsPaid", "Order", new { id = order.Id, index = true }, new { @class = "btn btn-xs btn-default", data_toggle = "fb-modal" })
                                        <span></span>
                                        @Html.ActionLink("Void", "MarkAsVoided", "Order", new { id = order.Id, index = true }, new { @class = "btn btn-xs btn-default", data_toggle = "fb-modal" })
                                    }
                                    <br />
                                    @order.PaymentMethod
                                }
                                else
                                {
                                    @:Cancelled
                                }
                            </td>
                            <td>
                                @if (!order.IsCancelled)
                                {
                                    <span></span>
                                    if (order.ShippingStatus == ShippingStatus.ShippingNotRequired)
                                    {
                                        @order.ShippingMethodName
                                    }
                                    else
                                    {
                                        @order.ShippingStatus
                                        if (order.ShippingStatus != ShippingStatus.Shipped)
                                        {
                                            @Html.ActionLink("Mark as Shipped", "MarkAsShipped", "Order", new { id = order.Id, index = true }, new { @class = "btn btn-xs btn-default", data_toggle = "fb-modal" })
                                        }
                                        <br />
                                        if (!string.IsNullOrWhiteSpace(order.ShippingMethodName))
                                        {
                                            @order.ShippingMethodName
                                        }
                                    }
                                }
                                else
                                {
                                    @:Cancelled
                                }
                            </td>
                            <td>
                                @order.Total.ToCurrencyFormat()
                            </td>
                            <td>
                                @order.OrderDate.GetValueOrDefault(order.CreatedOn)
                            </td>
                            <td>
                                <div class="btn-group">
                                    @Html.ActionLink("Invoice (PDF)", "Create", "OrderInvoice", new { id = order.Id }, new { @class = "btn btn-xs btn-default" })
                                    @Html.ActionLink("Details", "Edit", "Order", new { id = order.Id }, new { @class = "btn btn-xs btn-primary" })
                                </div>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
            @Html.Pager(results, "Index", i => new { page = i })
        }

    </fieldset>
}
