LocalGovIMS_localgov-ims/src/Admin/Views/EReturn/List.cshtml
@using BusinessLogic.Extensions
@using BusinessLogic.Enums
@using PagedList.Mvc
@model Admin.Models.EReturn.ListViewModel
@{
    ViewBag.Title = "eReturns";
}

@section Messages {
    @if (Model.Message != null) { Html.RenderPartial("_Message", Model.Message); }
}

@section Header { eReturns }

@section Subheader { Cash, cheque and PDQ returns }

@section Actions {

    @if (User.IsInRole(Role.EReturnAuthoriser))
    {
        <button id="approve" type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#approveModal">
            Approve <span class="approve-button-value"></span>
        </button>
    }
    @if (User.IsInRole(Role.EReturns))
    {
        @Html.ActionLink("Create eReturn", "Create", new { }, new { @class = "btn btn-primary", @id = "CreateButton" })

    }
}

<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.EReturns != null && Model.EReturns.Any())
{
    <div class="table-responsive">
        <table class="table table-striped table-bordered table-hover mt-2" id="ResultTable">
            <thead>
                <tr>
                    <th scope="col" style="width: 5%"><span class="visually-hidden">Select</span></th>
                    <th scope="col" style="width: 15%">Date and time</th>
                    <th scope="col" style="width: 10%">Status</th>
                    <th scope="col" style="width: 15%">eReturn number</th>
                    <th scope="col" style="width: 10%">Type</th>
                    <th scope="col" style="width: 15%">Template</th>
                    <th scope="col" style="width: 10%">Submitted by</th>
                    <th scope="col" style="width: 10%">Approved by</th>
                    <th scope="col" style="width: 10%">Amount (£)</th>
                    <th scope="col" style="width: 10%"><span class="visually-hidden">Actions</span></th>
                </tr>
            </thead>
            <tbody>
                @for (int i = 0; i < Model.EReturns.Count; i++)
                {
                    <tr>

                        <td>
                            @if (User.IsInRole(Role.EReturnAuthoriser) && Model.EReturns[i].EReturn.EReturnStatus.Id == 3)
                            {
                                <div class="text-center">
                                    <div class="form-check form-switch">
                                        @Html.CheckBox("EReturn_" + i, new { @class = "form-check-input", role = "switch", data_id = Model.EReturns[i].EReturn.Id, data_value = Model.EReturns[i].Amount, @data_type = Model.EReturns[i].EReturn.EReturnType.DisplayName, @data_num = Model.EReturns[i].EReturn.EReturnNo, @data_template = Model.EReturns[i].EReturn.Template.Name, @data_submittedBy = Model.EReturns[i].EReturn.SubmittedByUser.DisplayName })
                                        @Html.Label("Select", new { @for = "EReturn_" + i, @class = "form-check-label visually-hidden" })
                                    </div>
                                </div>
                            }
                        </td>

                        <td>@Model.EReturns[i].EReturn.CreatedAt</td>
                        <td class="center">
                            <span class="px-2 py-1 rounded-1 d-inline-flex label--ereturn-@Model.EReturns[i].EReturn.EReturnStatus.Id label">@Model.EReturns[i].EReturn.EReturnStatus.DisplayName</span>
                        </td>
                        <td>
                            @Model.EReturns[i].EReturn.EReturnNo
                        </td>
                        <td>@Model.EReturns[i].EReturn.EReturnType.DisplayName</td>
                        <td>@Model.EReturns[i].EReturn.Template.Name</td>

                        @if (Model.EReturns[i].EReturn.SubmittedByUser != null && Model.EReturns[i].EReturn.SubmittedByUser.DisplayName != null)
                        {
                            <td>@Model.EReturns[i].EReturn.SubmittedByUser.DisplayName</td>
                        }
                        else
                        {
                            <td></td>
                        }

                        @if (Model.EReturns[i].EReturn.ApprovedByUser != null && Model.EReturns[i].EReturn.ApprovedByUser.DisplayName != null)
                        {
                            <td>@Model.EReturns[i].EReturn.ApprovedByUser.DisplayName</td>
                        }
                        else
                        {
                            <td></td>
                        }

                        <td class="text-end">
                            @Html.ToCurrency(Model.EReturns[i].Amount)
                        </td>

                        <td class="text-end" style="min-width:200px;">
                            @if (Model.EReturns[i].EReturn.EReturnStatus.Id < 3)
                            {
                                @Html.ActionLink("Edit", "Edit", new { id = Model.EReturns[i].EReturn.Id }, new { @class = "btn btn-secondary" })
                            }
                            else if ((EReturnStatus)Model.EReturns[i].EReturn.EReturnStatus.Id == EReturnStatus.Submitted
                              && User.IsInRole(Role.EReturnAuthoriser))
                            {
                                @Html.ActionLink("Edit", "Approve", new { id = Model.EReturns[i].EReturn.Id }, new { @class = "btn btn-secondary" })
                            }
                            else
                            {
                                @Html.ActionLink("View", "Details", new { id = Model.EReturns[i].EReturn.Id }, new { @class = "btn btn-secondary" })
                            }
                            @Html.ActionLink("View notes", "List", "EReturnNote", new { id = Model.EReturns[i].EReturn.Id }, new { @class = "btn btn-secondary" })
                        </td>
                    </tr>
                }
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="10">
                        @if (Model.Pages > 1)
                        {
                            @Html.PagedListPager(Model.EReturns, page => Url.Action("Search", new
                            {
                                page = page,
                                amount = Model.SearchCriteria.Amount,
                                templateId = Model.SearchCriteria.TemplateId,
                                reference = Model.SearchCriteria.Reference,
                                startDate = Model.SearchCriteria.StartDate,
                                endDate = Model.SearchCriteria.EndDate,
                                statusId = Model.SearchCriteria.StatusId,
                                EReturnType = string.IsNullOrEmpty(Model.SearchCriteria.EReturnType.ToString()) ? Model.SearchCriteria.EReturnType.ToString() : ((int)Model.SearchCriteria.EReturnType).ToString()
                            }))
                        }
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
}
else
{
    Html.RenderPartial("_NoResultsMessage", "eReturns");
}

<div id="approval-details">
    @Html.Partial("_Approve")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/app/ereturn")

    <script type="text/javascript">

        $(document).ready(function () {

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

            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')
            });

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

        });

    </script>
}
