LocalGovIMS_localgov-ims/src/Admin/Views/Import/_List.cshtml
@model Admin.Models.Import.ListViewModel

@if (Model.Items != null && Model.Items.Any())
{
    var metadata = Model.Items
        .SelectMany(x => x.Metadata);

    var metadataKeys = metadata
        .Select(x => x.MetadataKey)
        .Distinct()
        .OrderBy(x => x.Id);

    <div class="table-responsive">
        <table class="table table-striped table-bordered 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>

                    @foreach (var key in metadataKeys)
                    {
                        <th scope="col">@metadata.First(x => x.MetadataKeyId == key.Id).MetadataKey.Description</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())
                            {
                                <br />
                                <span>(@item.Errors().Count()  @(item.Errors().Count() == 1 ? "error" : "errors"))</span>
                            }
                        </td>

                        @foreach (var key in metadataKeys)
                        {
                            <td>
                                @(item.Metadata.FirstOrDefault(x => x.MetadataKeyId == key.Id) == null
                                    ? string.Empty
                                    : item.Metadata.FirstOrDefault(x => x.MetadataKeyId == key.Id).Value)
                            </td>
                        }
                    </tr>
                }
            </tbody>
        </table>
    </div>
}
else
{
    <div class="alert alert-info my-3">
        <p class="mb-0">
            There are no imports in the system
        </p>
    </div>
}

