@model InvoicesModel
@{
    ViewData["Title"] = "Invoices";
}

@section HeadScripts {
    <script src="~/modal/btcpay.js"></script>
}

<section>
    <div class="container">

        <div class="row">
            <div class="col-lg-12 text-center">
                <partial name="_StatusMessage" for="StatusMessage" />
            </div>
        </div>

        <div class="row">
            <div class="col-lg-12 text-center">
                <h2 class="section-heading">@ViewData["Title"]</h2>
                <hr class="primary">
                <p>Create, search or pay an invoice. (<a href="#help" data-toggle="collapse">Help</a>)</p>
                <div id="help" class="collapse text-left">
                    <p>
                        You can search for invoice Id, deposit address, price, order id, store id, any buyer information and any product information.<br />
                        You can also apply filters to your search by searching for <code>filtername:value</code>, here is a list of supported filters
                    </p>
                    <ul>
                        <li><code>storeid:id</code> for filtering a specific store</li>
                        <li><code>orderid:id</code> for filtering a specific order</li>
                        <li><code>itemcode:code</code> for filtering a specific type of item purchased through the pos or crowdfund apps</li>
                        <li><code>status:(expired|invalid|complete|confirmed|paid|new)</code> for filtering a specific status</li>
                        <li><code>exceptionstatus:(paidover|paidlate|paidpartial)</code> for filtering a specific exception state</li>
                        <li><code>unusual:(true|false)</code> for filtering invoices which might requires merchant attention (those invalid or with an exceptionstatus)</li>
                    </ul>
                    <p>
                        If you want all confirmed and complete invoices, you can duplicate a filter <code>status:confirmed status:complete</code>.
                    </p>
                </div>
            </div>
        </div>

        <div class="row no-gutter" style="margin-bottom: 5px;">
            <div class="col-lg-6">
                <a asp-action="CreateInvoice" class="btn btn-primary" role="button"><span class="fa fa-plus"></span> Create a new invoice</a>

                <a class="btn btn-primary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Export
                </a>
                <a href="https://docs.btcpayserver.org/features/accounting" target="_blank"><span class="fa fa-question-circle-o" title="More information..."></span></a>
                <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                    <a asp-action="Export" asp-route-format="csv" asp-route-searchTerm="@Model.SearchTerm" class="dropdown-item" target="_blank">CSV</a>
                    <a asp-action="Export" asp-route-format="json" asp-route-searchTerm="@Model.SearchTerm" class="dropdown-item" target="_blank">JSON</a>
                </div>
            </div>

            <div class="col-lg-6">
                <div class="form-group">
                    <form asp-action="SearchInvoice" method="post" style="float:right;">
                        <div class="input-group">
                            <input asp-for="SearchTerm" class="form-control" style="width:300px;" />
                            <span class="input-group-btn">
                                <button type="submit" class="btn btn-primary" title="Search invoice">
                                    <span class="fa fa-search"></span> Search
                                </button>
                            </span>
                        </div>
                        <input type="hidden" asp-for="Count" />
                        <span asp-validation-for="SearchTerm" class="text-danger"></span>
                    </form>
                </div>
            </div>
        </div>

        <div class="row">
            <table class="table table-sm table-responsive-md">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>OrderId</th>
                        <th>InvoiceId</th>
                        <th>Status</th>
                        <th style="text-align:right">Amount</th>
                        <th style="text-align:right">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var invoice in Model.Invoices)
                    {
                        <tr>
                            <td>@invoice.Date.ToTimeAgo()</td>
                            <td>
                                @if (invoice.RedirectUrl != string.Empty)
                                {
                                    <a href="@invoice.RedirectUrl">@invoice.OrderId</a>
                                }
                                else
                                {
                                    <span>@invoice.OrderId</span>
                                }
                            </td>
                            <td>@invoice.InvoiceId</td>
                            <td>@invoice.Status</td>
                            <td style="text-align:right">@invoice.AmountCurrency</td>
                            <td style="text-align:right">
                                @if (invoice.ShowCheckout)
                                {
                                    <span>
                                        <a asp-action="Checkout" asp-route-invoiceId="@invoice.InvoiceId">Checkout</a>
                                        <a href="javascript:btcpay.showInvoice('@invoice.InvoiceId')">[^]</a>
                                        @if (!invoice.CanMarkStatus)
                                        {
                                            <span>-</span>
                                        }
                                    </span>
                                }
                                @if (invoice.CanMarkStatus)
                                {
                                    <a class="dropdown-toggle dropdown-toggle-split" href="#" style="cursor: pointer;" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                        Change status <span class="sr-only">Toggle Dropdown</span>
                                    </a>
                                    <div class="dropdown-menu pull-right">
                                        @if (invoice.CanMarkInvalid)
                                        {
                                            <form method="get" asp-action="ChangeInvoiceState" asp-route-invoiceId="@invoice.InvoiceId" asp-route-newState="invalid">
                                                <button class="dropdown-item small">Mark as invalid <span class="fa fa-times"></span></button>
                                            </form>
                                        }
                                        @if (invoice.CanMarkComplete)
                                        {
                                            <form method="get" asp-action="ChangeInvoiceState" asp-route-invoiceId="@invoice.InvoiceId" asp-route-newState="complete">
                                                <button class="dropdown-item small">Mark as complete <span class="fa fa-check-circle"></span></button>
                                            </form>
                                        }
                                    </div>
                                }
                                <a asp-action="Invoice" asp-route-invoiceId="@invoice.InvoiceId">Details</a>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>

            <nav aria-label="...">
                <ul class="pagination">
                    <li class="page-item  @(Model.Skip == 0 ? "disabled" : null)">
                        <a class="page-link" tabindex="-1" href="@Url.Action("ListInvoices", new
                        {
                            searchTerm = Model.SearchTerm,
                            skip = Math.Max(0, Model.Skip - Model.Count),
                            count = Model.Count,
                        })">Previous</a>
                    </li>
                    <li class="page-item disabled">
                        <span class="page-link">@(Model.Skip + 1) to @(Model.Skip + Model.Invoices.Count) of @Model.Total</span>
                    </li>
                    <li class="page-item @(Model.Total > (Model.Skip + Model.Invoices.Count) ? null : "disabled")">
                        <a class="page-link" href="@Url.Action("ListInvoices", new
                        {
                            searchTerm = Model.SearchTerm,
                            skip = Model.Skip + Model.Count,
                            count = Model.Count,
                        })">Next</a>
                    </li>
                </ul>
            </nav>

        </div>

    </div>
</section>
