LocalGovIMS_localgov-ims/src/Admin/Views/Suspense/List.cshtml
@using PagedList.Mvc
@using System.Linq
@model Admin.Models.Suspense.ListViewModel
@{
    ViewBag.Title = "Suspense";
}

@section Header { Suspense }

@section Subheader { Suspense transactions }

@section Actions {
    @if (User.IsInRole(Role.Finance))
    {
        <a class="btn btn-success credit-notes" id="credit-notes" data-bs-toggle="modal" data-bs-target="#creditNoteModal">
            Add credit notes <span class="credit-button-value"></span>
        </a>
        <a class="btn btn-primary journal" id="journal" data-bs-toggle="modal" data-bs-target="#journalModal">
            Create journal <span class="journal-button-value"></span>
        </a>
    }
}

<h3 class="visually-hidden">Search criteria</h3>

<div class="border border-1 rounded p-2">
    @Html.Partial("_Search", Model.SearchCriteria)
</div>

<h3 class="visually-hidden">Search results</h3>

@if (Model.Items != null && Model.Items.Any())
{

    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover mt-2">
            <thead>
                <tr>
                    <th scope="col"><span class="visually-hidden">Select</span></th>
                    <th scope="col">Date and time</th>
                    <th scope="col">Narrative</th>
                    <th scope="col">Latest note</th>
                    <th scope="col">Account number</th>
                    <th scope="col">Amount (£)</th>
                    <th scope="col"><span class="visually-hidden">Actions</span></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.Items)
                {
                    <tr>
                        <td>
                            @if (User.IsInRole(Role.Finance) && item.AmountRemaining > 0)
                            {
                                <div class="text-center">
                                    <div class="form-check form-switch">
                                        @Html.CheckBox("Suspense_" + item.Item.Id, new { @class = "form-check-input", role = "switch", data_id = @item.Item.Id, data_value = @item.AmountRemaining })
                                        @Html.Label("Select", new { @for = "Suspense_" + item.Item.Id, @class = "form-check-label visually-hidden" })
                                    </div>
                                </div>
                            }
                        </td>
                        <td>@item.Item.CreatedAt</td>
                        <td>@item.Item.Narrative</td>
                        <td class="text-truncate" style="max-width:350px">
                            @if (item.LatestNote != null)
                            {
                                <div class="w-100 mb-2">
                                    <span class="me-2 fw-bold">@item.LatestNote.CreatedByUser.DisplayName</span>
                                    <small>@item.LatestNote.CreatedAt</small>
                                </div>
                                <p class="mb-1">@item.LatestNote.Note</p>
                            }
                        </td>
                        <td>@item.Item.AccountNumber</td>
                        <td class="text-end">@Html.ToCurrency(item.Item.Amount)</td>
                        <td class="text-end" style="min-width:243px;">
                            @Html.ActionLink("View details", "Details", "Suspense", new { id = item.Item.Id }, new { @class = "btn btn-secondary" })
                            @if (User.IsInRole(Role.Finance))
                            {
                                @Html.ActionLink("View notes", "List", "SuspenseNote", new { id = item.Item.Id }, new { @class = "btn btn-secondary" })
                            }
                        </td>
                    </tr>
                }

            </tbody>
            <tfoot>
                <tr>
                    <td colspan="7">
                        @if (Model.Pages > 1)
                        {
                            @Html.PagedListPager(Model.Items, page => Url.Action("Search", new
                            {
                                page = page,
                                amount = Model.SearchCriteria.Amount,
                                transactionDateFrom = Model.SearchCriteria.TransactionDateFrom,
                                transactionDateTo = Model.SearchCriteria.TransactionDateTo,
                                accountNumber = Model.SearchCriteria.AccountNumber,
                                narrative = Model.SearchCriteria.Narrative,
                                status = Model.SearchCriteria.Status
                            }))
                        }
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
}
else
{
    Html.RenderPartial("_NoResultsMessage", "suspense items");
}

@if (User.IsInRole(Role.Finance))
{
    <div id="transfer-details">
        @Html.Action("_Journal")
    </div>

    <div id="credit-details">
        @Html.Action("_CreditNote")
    </div>
}

@section Scripts {

    @Scripts.Render("~/bundles/app/suspense")

    <script type="text/javascript">
        $(document).ready(function () {
            accessibleAutocomplete.enhanceSelectElement({
                displayMenu: 'overlay',
                autoSelect: false,
                confirmOnBlur: false,
                showAllValues: true,
                defaultValue: '',
                preserveNullOptions: true,
                placeholder: 'Select status',
                dropdownArrow: () => paymentsAdmin.controls.autocomplete.downarrow,
                selectElement: document.querySelector('#Status')
            });

        });</script>
}
