@using BTCPayServer.Plugins.TicketTailor
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using BTCPayServer.Abstractions.Extensions
@using BTCPayServer.Views.Apps
@using Microsoft.AspNetCore.Routing
@using BTCPayServer
@using BTCPayServer.Abstractions.Models
@using BTCPayServer.Services.Apps
@model BTCPayServer.Plugins.TicketTailor.UpdateTicketTailorSettingsViewModel
@{
    var appId = Context.GetRouteValue("appId").ToString();
    var storeId = Context.GetCurrentStoreId();
    ViewData.SetActivePage(AppsNavPages.Update.ToString(), typeof(AppsNavPages).ToString(), "Update Ticket Tailor app", appId);
    Model.SpecificTickets ??= new List<SpecificTicket>();
}


<form method="post">

    <div class="sticky-header-setup"></div>
    <div class="sticky-header d-sm-flex align-items-center justify-content-between">
        <h2 class="mb-0">@ViewData["Title"]</h2>
        <div class="d-flex gap-3 mt-3 mt-sm-0">
            <input type="submit" value="Save" name="command" class="btn btn-primary"/>
            @if (Model.Archived)
            {
                <button type="button" class="btn btn-outline-secondary" onclick="document.getElementById('btn-archive-toggle').click()">Unarchive</button>
            }
            else if (this.ViewContext.ModelState.IsValid && Model.EventId is not null)
            {
                <a class="btn btn-secondary" target="_blank" href=" @Url.Action("View", "TicketTailor", new {appId})">
                    Ticket purchase page
                </a>
            }
        </div>
    </div>


    <partial name="_StatusMessage"/>

    @if (ViewContext.ModelState.IsValid && Model.EventId is not null)
    {
        <div class="alert alert-warning">
            Please ensure that emails in your store are configured if you wish to send the tickets via email to customers as TicketTailor does not handle it for tickets issued via its API. <a asp-action="StoreEmails" asp-controller="UIStores" asp-route-storeId="@storeId" class="alert-link">Configure here.</a>
        </div>
    }
    <div class="row">
        <div class="col-xl-8 col-xxl-constrain">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>

            <div class="form-group">
                <label asp-for="AppName" class="form-label" data-required>App name</label>
                <input asp-for="AppName" class="form-control" required/>
                <span asp-validation-for="AppName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ApiKey" class="form-label" data-required>TicketTailor API Key (<a href="https://get.tickettailor.com/8z1ee1o4xrne" target="_blank">get it here</a>)</label>
                <input asp-for="ApiKey" class="form-control" required/>
                <span asp-validation-for="ApiKey" class="text-danger"></span>
            </div>

            @if (Model.Events is not null)
            {
                <div class="form-group">
                    <label asp-for="EventId" class="form-label" data-required>Event</label>
                    <select asp-for="EventId" asp-items="Model.Events" class="form-select"></select>
                </div>
            }

            @if (Model.EventId is not null)
            {
                <div class="form-group form-check">
                    <input asp-for="ShowDescription" type="checkbox" class="form-check-input"/>
                    <label asp-for="ShowDescription" class="form-check-label">Show TicketTailor event description</label>
                    <span asp-validation-for="ShowDescription" class="text-danger"></span>
                </div>
                <div class="form-group form-check">
                    <input asp-for="BypassAvailabilityCheck" type="checkbox" class="form-check-input"/>
                    <label asp-for="BypassAvailabilityCheck" class="form-check-label">Bypass availability checks of TicketTailor (such as when in draft mode)</label>
                    <span asp-validation-for="BypassAvailabilityCheck" class="text-danger"></span>
                </div>
                <div class="form-group form-check">
                    <input asp-for="RequireFullName" type="checkbox" class="form-check-input"/>
                    <label asp-for="RequireFullName" class="form-check-label">Require full name to be provided (if unchecked and not provided, the gap will be filled in with a placeholder)</label>
                    <span asp-validation-for="RequireFullName" class="text-danger"></span>
                </div>
                <div class="form-group form-check">
                    <input asp-for="AllowDiscountCodes" type="checkbox" class="form-check-input"/>
                    <label asp-for="AllowDiscountCodes" class="form-check-label">Allow discount codes (due to the nature of Ticket Tailor's API, redemption limit is not applied)</label>
                    <span asp-validation-for="AllowDiscountCodes" class="text-danger"></span>
                </div>
                <div class="form-group form-check">
                    <input asp-for="SendEmail" type="checkbox" class="form-check-input"/>
                    <label asp-for="SendEmail" class="form-check-label">Send an email when an invoice settles and the ticket is issued. </label>
                    <span asp-validation-for="SendEmail" class="text-danger"></span>
                </div>

                <div class="form-group">
                    <label asp-for="CustomCSS" class="form-label">Additional Custom CSS</label>
                    <textarea asp-for="CustomCSS" rows="10" cols="40" class="form-control"></textarea>
                    <span asp-validation-for="CustomCSS" class="text-danger"></span>
                </div>

                @if (Model.TicketTypes?.Any() is true)
                {
                    var uniqueRemaining = Model.TicketTypes.Where(type => Model.SpecificTickets?.Any(ticket => ticket.TicketTypeId == type.Id) is false);
                    <div class="row mt-4">
                        <div class="h4 w-100">Specific tickets</div>
                        <p>Specific tickets allow you to override explicitly which tickets you wish to allow to sell, and also override the price, name, and description of these tickets.</p>
                        @if (uniqueRemaining.Any())
                        {
                            SelectList sl = new SelectList(uniqueRemaining, nameof(TicketTailorClient.TicketType.Id), nameof(TicketTailorClient.TicketType.Name));
                            <div class="form-group">

                                <div class="input-group">
                                    <select asp-for="NewSpecificTicket" asp-items="@sl" class="form-select"></select>

                                    <button type="submit" value="add-specific-ticket" name="command" class="btn btn-outline-primary input-group-btn">Add</button>
                                </div>
                            </div>
                        }
                        @for (var index = 0; index < Model.SpecificTickets.Count; index++)
                        {
                            var specific = Model.SpecificTickets[index];
                            var ticketType = Model.TicketTypes.SingleOrDefault(type => type.Id == specific.TicketTypeId);
                            if (ticketType is null)
                            {
                                continue;
                            }

                            <div class="card">
                                <div class="card-header h5">
                                    @ticketType.Name
                                </div>
                                <div class="card-body">
                                    <div class="form-group pt-2">
                                        <div class="input-group">
                                            <input asp-for="SpecificTickets[index].TicketTypeId" type="hidden"/>
                                            <input class="form-control" asp-for="SpecificTickets[index].Price" type="number" step="any" min="0" placeholder="Custom price"/>
                                            <input class="form-control" asp-for="SpecificTickets[index].Name" type="text" placeholder="Custom name"/>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <textarea class="form-control richtext" asp-for="SpecificTickets[index].Description" type="text"  placeholder="Custom description"></textarea>
                                    </div>
                                    <div class="form-group form-check">
                                        <input asp-for="SpecificTickets[index].Hidden" type="checkbox" class="form-check-input"/>
                                        <label asp-for="SpecificTickets[index].Hidden" class="form-check-label">Hidden</label>
                                        <span asp-validation-for="SpecificTickets[index].Hidden" class="text-danger"></span>
                                    </div>
                                </div>
                                <div class="card-footer">
                                    <button type="submit" value="remove-specific-ticket:@index" name="command" class="btn btn-outline-danger">Remove</button>
                                </div>
                            </div>
                        }
                    </div>
                }
            }
        </div>
    </div>

</form>


<div class="d-grid d-sm-flex flex-wrap gap-3 mt-3">
    <a class="btn btn-secondary" asp-action="ListInvoices" asp-controller="UIInvoice" asp-route-storeId="@storeId" asp-route-searchterm="@AppService.GetAppSearchTerm(TicketTailorApp.AppType, appId)">Invoices</a>
    <form method="post" asp-controller="UIApps" asp-action="ToggleArchive" asp-route-appId="@appId">
        <button type="submit" class="w-100 btn btn-outline-secondary" id="btn-archive-toggle">
            @if (Model.Archived)
            {
                <span class="text-nowrap">Unarchive this app</span>
            }
            else
            {
                <span class="text-nowrap" data-bs-toggle="tooltip" title="Archive this app so that it does not appear in the apps list by default">Archive this app</span>
            }
        </button>
    </form>
    <a id="DeleteApp" class="btn btn-outline-danger" asp-controller="UIApps" asp-action="DeleteApp" asp-route-appId="@appId" data-bs-toggle="modal" data-bs-target="#ConfirmModal" data-description="The app and its settings will be permanently deleted." data-confirm-input="DELETE">Delete this app</a>
</div>

<partial name="_Confirm" model="@(new ConfirmModel("Delete app", "This app will be removed from this store.", "Delete"))"/>

<partial name="_ValidationScriptsPartial"/>