LocalGovIMS_localgov-ims/src/Admin/Views/Import/List.cshtml
@using PagedList.Mvc

@model Admin.Models.Import.ListViewModel
@{
    ViewBag.Title = "Imports";
}

@section Header { Imports }

@section Subheader { Imports }

<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">
                        Type
                    </th>
                    <th scope="col">
                        Date
                    </th>
                    <th scope="col">
                        Number of rows
                    </th>
                    <th scope="col">
                        Status
                    </th>
                    <th scope="col">
                        Other
                    </th>
                    <th scope="col">
                        <span class="visually-hidden">Actions</span>
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.Items)
                {
                    <tr>
                        <td>
                            @item.ImportType.Name
                        </td>
                        <td>
                            @item.CreatedDate
                        </td>
                        <td>
                            @item.NumberOfRows
                        </td>
                        <td>
                            @item.CurrentStatus().GetDisplayName()
                            @if (item.HasErrors())
                            {
                                <span> (@item.Errors().Count()  @(item.Errors().Count() == 1 ? "error" : "errors"))</span>
                            }
                        </td>
                        <td>
                            @foreach(var metadataItem in item.Metadata.OrderBy(x => x.MetadataKey.Name))
                            {
                                <span class="fw-bold">@metadataItem.MetadataKey.Description</span>
                                <span> : @metadataItem.Value</span>
                                <br />
                            }
                        </td>
                        <td class="text-end">
                            @Html.ActionLink("View", "Details", new { id = item.Id }, new { @class = "btn btn-secondary" })
                        </td>
                    </tr>
                }
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="5">
                        @if (Model.Pages > 1)
                        {
                            @Html.PagedListPager(Model.Items, page => Url.Action("Search", new
                            {
                                page = page,
                                importTypeId = Model.SearchCriteria.ImportTypeId,
                                statusId = Model.SearchCriteria.StatusId,
                                startDate = Model.SearchCriteria.StartDate,
                                endDate = Model.SearchCriteria.EndDate,
                            }))
                        }
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
}
else
{
    Html.RenderPartial("_NoResultsMessage", "imports");
}

@section Scripts {
    <script type="text/javascript">
        $(document).ready(function () {

            accessibleAutocomplete.enhanceSelectElement({
                displayMenu: 'overlay',
                autoSelect: false,
                confirmOnBlur: false,
                showAllValues: true,
                defaultValue: '',
                preserveNullOptions: true,
                placeholder: 'Select data type',
                dropdownArrow: () => paymentsAdmin.controls.autocomplete.downarrow,
                selectElement: document.querySelector('#DataType')
            });

            accessibleAutocomplete.enhanceSelectElement({
                displayMenu: 'overlay',
                autoSelect: false,
                confirmOnBlur: false,
                showAllValues: true,
                defaultValue: '',
                preserveNullOptions: true,
                placeholder: 'Select import type',
                dropdownArrow: () => paymentsAdmin.controls.autocomplete.downarrow,
                selectElement: document.querySelector('#ImportTypeId')
            });

            accessibleAutocomplete.enhanceSelectElement({
                displayMenu: 'overlay',
                autoSelect: false,
                confirmOnBlur: false,
                showAllValues: true,
                defaultValue: '',
                preserveNullOptions: true,
                placeholder: 'Select status',
                dropdownArrow: () => paymentsAdmin.controls.autocomplete.downarrow,
                selectElement: document.querySelector('#StatusId')
            });

        });
    </script>
}
