@using MrCMS.Helpers
@using MrCMS.Web.Apps.Ecommerce.Entities.Discounts
@model Discount
@{
    ViewBag.Title = "Edit Discount";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
    Html.IncludeScript("~/Apps/Ecommerce/Areas/Admin/Content/Scripts/custom/discount.js");
    var discountUsages = ViewData["usages"] as IList<DiscountUsage> ?? new List<DiscountUsage>();
    var requestUrl = HttpContext.Current.Request.Url;
    var url = string.Format("{0}{1}{2}", requestUrl.Scheme, Uri.SchemeDelimiter, requestUrl.Authority);
}
@using (Html.BeginForm("Edit", "Discount", FormMethod.Post))
{
    <fieldset>
        <legend>
            Edit Discount
            <span class="btn-group  display-inline  pull-right">
                <a class="btn-default btn" href="@Url.Action("Index", "Discount")">Back</a>
                <input type="submit" class="btn-primary btn" value="Save" />
            </span>
        </legend>

        @Html.HiddenFor(x => x.Id)

        <ul class="nav nav-tabs" data-stickytabs="discount-@Model.Id">
            <li class="active">
                <a href="#details" data-toggle="tab">Details</a>
            </li>
            <li>
                <a href="#limitations" data-toggle="tab">Limitations</a>
            </li>
            <li>
                <a href="#application" data-toggle="tab">Application</a>
            </li>
            <li>
                <a href="#usages" data-toggle="tab">Usages (@discountUsages.Count)</a>
            </li>
        </ul>
        <div class="tab-content">
            <div class="tab-pane active" id="details">
                <div class="form-group">
                    @Html.LabelFor(x => x.Name)
                    @Html.TextBoxFor(x => x.Name, new { @class = "form-control" })
                    @Html.ValidationMessageFor(x => x.Name)
                </div>
                <div class="form-group">
                    @Html.InlineCheckboxFor(discount => discount.RequiresCode)
                </div>
                <div class="form-group" data-code-field>
                    @Html.LabelFor(x => x.Code)
                    @Html.TextBoxFor(x => x.Code, new { @class = "form-control" })
                    @Html.ValidationMessageFor(x => x.Code)
                </div>

                <div class="form-group">
                    @Html.LabelFor(x => x.ValidFrom)
                    @Html.TextBoxFor(x => x.ValidFrom, new { @class = "form-control date-time-picker" })
                    @Html.ValidationMessageFor(x => x.ValidFrom)
                </div>
                <div class="form-group">
                    @Html.LabelFor(x => x.ValidUntil)
                    @Html.TextBoxFor(x => x.ValidUntil, new { @class = "form-control date-time-picker" })
                    @Html.ValidationMessageFor(x => x.ValidUntil)
                </div>
                <div class="form-group">
                    @Html.InlineCheckboxFor(discount => discount.CanBeAppliedFromUrl, checkboxAttributes: new { @checked = "checked" })
                </div>
                <div class="form-group from-url-group"  data-applied-by-url>
                    <label>@string.Format("This discount can be applied by navigating to {0}/Discount/{1}", url, Model.Code)</label>
                </div>
                <div class="form-group from-url-group" data-applied-by-url>
                    @Html.LabelFor(x => x.SuccessMessage)
                    @Html.TextBoxFor(x => x.SuccessMessage, new {@class = "form-control"})
                    @Html.ValidationMessageFor(x => x.SuccessMessage)
                </div>
                <div class="form-group from-url-group" data-applied-by-url>
                    @Html.LabelFor(x => x.AppliedNotYetValidMessage)
                    @Html.TextBoxFor(x => x.AppliedNotYetValidMessage, new { @class = "form-control" })
                    @Html.ValidationMessageFor(x => x.AppliedNotYetValidMessage)
                </div>
                <div class="form-group from-url-group" data-applied-by-url>
                    @Html.LabelFor(x => x.RedirectUrl)
                    @Html.TextBoxFor(x => x.RedirectUrl, new { @class = "form-control" })
                    @Html.ValidationMessageFor(x => x.RedirectUrl)
                </div>
                @Html.InfoBlock("Heads up! ", "Leave Valid From and Until blank to make discount valid forever")
            </div>
            <div class="tab-pane" id="limitations">
                <fieldset>
                    <legend>
                        Limitations
                        <span class="pull-right display-inline btn-group">
                            @Html.ActionLink("Add", "Add", "DiscountLimitation", new { id = Model.Id }, new { data_toggle = "fb-modal", @class = "btn btn-primary" })
                        </span>
                    </legend>
                    @{Html.RenderAction("List", "DiscountLimitation", new { id = Model.Id });}
                </fieldset>
            </div>
            <div class="tab-pane" id="application">
                <fieldset>
                    <legend>
                        Applications
                        <span class="pull-right display-inline btn-group">
                            @Html.ActionLink("Add", "Add", "DiscountApplication", new { id = Model.Id }, new { data_toggle = "fb-modal", @class = "btn btn-primary" })
                        </span>
                    </legend>
                    @{Html.RenderAction("List", "DiscountApplication", new { id = Model.Id });}
                </fieldset>
            </div>
            <div class="tab-pane" id="usages">
                <fieldset>
                    <legend>
                        Usages
                    </legend>
                    @if (discountUsages.Any())
                    {
                        <table class="table table-striped table-condensed">
                            <thead>
                                <tr>
                                    <th>
                                        Order #
                                    </th>
                                    <th>
                                        Order Email
                                    </th>
                                    <th>
                                        Created On
                                    </th>
                                    <th>
                                        Actions
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var usage in discountUsages)
                                {
                                    <tr>
                                        <td>
                                            @usage.Id
                                        </td>
                                        <td>
                                            @usage.Order.OrderEmail
                                        </td>
                                        <td>
                                            @usage.CreatedOn
                                        </td>
                                        <td>
                                            @Html.ActionLink("View", "Edit", "Order", new { id = usage.Order.Id }, new { @class = "btn btn-default btn-xs" })
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    }
                </fieldset>
            </div>
        </div>
    </fieldset>
}