@using BTCPayServer.Client.Models
@using BTCPayServer.Plugins.TicketTailor
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using BTCPayServer.Abstractions.TagHelpers
@model BTCPayServer.Plugins.TicketTailor.TicketTailorController.TicketReceiptPage
@{
    Layout = "_LayoutSimple";
    var reloadPage = false;
}
<style>
    footer {
        display: none;
    }
     .page-break {
            page-break-after: always;
        }
</style>
<div class="container d-flex h-100">
    <div class="justify-content-center align-self-center text-center mx-auto px-2 w-100 m-auto">
        <partial name="_StatusMessage"/>
        <div class="mb-4 d-print-none d-flex justify-content-center">

            <h1 >Ticket Receipt</h1>
            <button class="btn btn-link d-print-none" id="btnPrint">Print</button>
        </div>
        @if (Model.Status == InvoiceStatus.Processing)
        {
            reloadPage = true;
            <div class="alert alert-info">
                The invoice has detected a payment but is still waiting to be settled. This page will refresh periodically until it is settled.

            </div>
        }
        else if (Model.Status != InvoiceStatus.Settled)
        {
            <div class="alert alert-danger">
                The invoice is not settled.
            </div>
        }
        else if (Model.Tickets?.Any() is not true)
        {
            
            reloadPage = true;
            <div class="alert alert-warning">
                The invoice is settled but the ticket has not been issued yet. This page will refresh periodically until it is issued.
            </div>
        }
        else
        {

            <div class="row mb-4">
                          
                <div class="col col-12 col-lg-6 mb-md-0 mb-sm-4 bg-tile">
                    @{
                        for (var index = 0; index < Model.Tickets.Length; index++)
                        {
                            var ticket = Model.Tickets[index];
                            var ticketType = Model.Event.TicketTypes.FirstOrDefault(type => type.Id == ticket.TicketTypeId) ?? new TicketTailorClient.TicketType();
                            var specificTicketType = Model.Settings?.SpecificTickets?
                                .FirstOrDefault(specificTicket => specificTicket.TicketTypeId == ticket.TicketTypeId);

                            ticketType.Name = specificTicketType?.Name??ticketType.Name?? ticket.TicketTypeId;
                            ticketType.Description = specificTicketType?.Description ?? ticketType.Description;

                            <div class="bg-tile  m-0 p-3 p-sm-5 rounded ">
                                <h2 class="h4 mb-3">Ticket Details</h2>
                                <div class="d-flex mb-4">
                                    <div class="col-6 px-1">
                                        <img src="@ticket.QrCodeUrl" alt="Please ensure you can see this QR barcode" class="w-100">
                                    </div>
                                    <div class="col-6 px-1 py-4">
                                        <img src="@ticket.BarcodeUrl" alt="Please ensure you can see this barcode" class="w-100">
                                    </div>
                                </div>
                                <div class="d-flex d-print-inline-block flex-column mb-4 ">
                                    <dt class="h3 fw-semibold text-print-default order-2 order-sm-1 ">@ticket.Barcode</dt> <dd class="order-1 order-sm-2 ">TICKET CODE</dd>
                                </div>
                                @if (!string.IsNullOrEmpty(ticket.Reference))
                                {
                                    <div class="d-flex d-print-inline-block flex-column mb-4 px-4 ">
                                        <dt class="h3 fw-semibold text-print-default order-2 order-sm-1 ">@ticket.Reference</dt> <dd class="order-1 order-sm-2 ">REFERENCE</dd>
                                    </div>
                                }

                                @if (!string.IsNullOrEmpty(ticket.FullName))
                                {
                                    <div class="d-flex d-print-inline-block flex-column ">
                                        <dt class="h3 fw-semibold text-print-default order-2 order-sm-1 ">@ticket.FullName</dt> <dd class="order-1 order-sm-2 ">ATTENDEE NAME</dd>
                                    </div>
                                }
                                <div class="d-flex d-print-inline-block flex-column ">
                                    <dt class="h3 fw-semibold text-print-default order-2 order-sm-1 ">@ticketType.Name</dt> <dd class="order-1 order-sm-2 ">TICKET TYPE</dd>
                                </div>

                            </div>
                            @if (Model.Tickets.Length > 1)
                            {
                                
                                <div class="m-0 p-3 p-sm-5 d-none d-print-block ">
                                    <div class="d-flex d-print-inline-block flex-column mb-4 ">
                                        <dt class="h3 fw-semibold text-nowrap text-print-default order-2 order-sm-1 ">@Model.Event.Title</dt> <dd class="order-1 order-sm-2 ">EVENT</dd>
                                    </div>
                                    <div class="d-flex d-print-inline-block flex-column mb-4 ">
                                        <dt class="h3 fw-semibold text-print-default order-2 order-sm-1 ">
                                            <a href="@Model.Event.Url" style="word-wrap: break-word;" target="_blank">@Model.Event.Url</a>
                                        </dt> <dd class="order-1 order-sm-2 ">EVENT URL</dd>
                                    </div>
                                    <div class="d-flex d-print-inline-block flex-column mb-4 ">
                                        <dt class="h3 fw-semibold  text-print-default order-2 order-sm-1 ">@Model.Event.Start.Formatted - @Model.Event.EventEnd.Formatted</dt>
                                        <dd class="order-1 order-sm-2 ">Date</dd>
                                    </div>
                                    @if (!string.IsNullOrEmpty(Model.Event.Venue.Name))
                                    {
                                        <div class="d-flex d-print-inline-block flex-column ">
                                            <dt class="h3 fw-semibold text-print-default order-2 order-sm-1 ">@Model.Event.Venue.Name</dt>
                                            <dd class="order-1 order-sm-2 ">Venue</dd>
                                        </div>}
                        
                                </div>
                                
                                <hr class="page-break"/>
                            }
                        }
                    }
                </div>
                <div class="col col-12 col-lg-6 mb-md-0 mb-sm-4 @(Model.Tickets.Length > 1? "d-print-none": "")">
                    <div class="bg-tile h-100 m-0 p-3 p-sm-5 rounded">
                        <h2 class="h4 mb-3 d-print-none">Event Details</h2>
                        <div class="d-flex d-print-inline-block flex-column mb-4 ">
                            <dt class="h3 fw-semibold text-nowrap text-print-default order-2 order-sm-1 ">@Model.Event.Title</dt> <dd class="order-1 order-sm-2 ">EVENT</dd>
                        </div>
                        <div class="d-flex d-print-inline-block flex-column mb-4 ">
                            <dt class="h3 fw-semibold text-print-default order-2 order-sm-1 ">
                                <a href="@Model.Event.Url" style="word-wrap: break-word;" target="_blank">@Model.Event.Url</a>
                            </dt> <dd class="order-1 order-sm-2 ">EVENT URL</dd>
                        </div>
                        <div class="d-flex d-print-inline-block flex-column mb-4 ">
                            <dt class="h3 fw-semibold  text-print-default order-2 order-sm-1 ">@Model.Event.Start.Formatted - @Model.Event.EventEnd.Formatted</dt>
                            <dd class="order-1 order-sm-2 ">Date</dd>
                        </div>
                        @if (!string.IsNullOrEmpty(Model.Event.Venue.Name))
                        {
                            <div class="d-flex d-print-inline-block flex-column ">
                                <dt class="h3 fw-semibold text-print-default order-2 order-sm-1 ">@Model.Event.Venue.Name</dt>
                                <dd class="order-1 order-sm-2 ">Venue</dd>
                            </div>}
                        
                    </div>
                </div>
            </div>
        }
        <div class="row">
            <div class="powered__by__btcpayserver col-12">
                Powered by <a target="_blank" href="https://github.com/btcpayserver/btcpayserver" rel="noreferrer noopener">BTCPay Server</a>
            </div>
        </div>
    </div>
</div>

@if (reloadPage)
{
    <script type="text/javascript" >       
        setTimeout(function(){
            
            window.location.reload();
        }, 3000); 
    </script>
}
<script type="text/javascript" >
    
    document.addEventListener("DOMContentLoaded", function () {
        document.getElementById("btnPrint").addEventListener("click", function (){
            window.print();
        });
    });
    
</script>
