LocalGovIMS_localgov-ims/src/Admin/Views/Payment/Index.cshtml
@model Admin.Models.Payment.IndexViewModel
@{
    ViewBag.Title = "Payment";
}

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

@section Header { Payment }

@section Subheader { Create a payment }

@section Actions {

    @if (Model.Basket.Count > 0)
    {
        if (User.IsInRole(Role.SystemAdmin) || User.IsInRole(Role.Payments))
        {
            @Html.ActionLink(string.Format("Pay {0}", Model.Basket.TotalAmount.ToString("c")), "Pay", null, new { @class = "btn btn-primary mb-1" })
        }
    }
    @if (Model.Basket.Count > 0)
    {
        if (User.IsInRole(Role.SystemAdmin) || User.IsInRole(Role.ChequeProcess))
        {
            @Html.ActionLink(string.Format("Cheque {0}", Model.Basket.TotalAmount.ToString("c")), "Cheque", null, new { @class = "btn btn-primary mb-1" })
        }
    }
    @if (Model.Basket.Count > 0)
    {
        if (User.IsInRole(Role.SystemAdmin) || User.IsInRole(Role.PostPayment))
        {
            @Html.ActionLink(string.Format("Post {0}", Model.Basket.TotalAmount.ToString("c")), "PostPayment", null, new { @class = "btn btn-primary mb-1 post-payment" })
        }
    }
    @if (Model.Basket.Count > 0)
    {
        @Html.ActionLink("Empty basket", "EmptyBasket", null, new { @class = "btn btn-danger mb-1" })
    }
}

@using (Html.BeginForm("Create", "Payment", FormMethod.Post, new { id = "PaymentForm" }))
{
    <div>

        <div class="row">

            <div class="col-12 col-lg-5 mb-3 mb-lg-0">

                <h3>
                    Payment details
                </h3>

                <div class="row mb-3">
                    <div class="col">
                        @Html.LabelFor(m => m.FundCode, new { @class = "form-label" })
                        <select id="FundCode" name="FundCode">
                            <option value="">None</option>
                            @foreach (var item in Model.Funds.Items)
                            {
                                var dataAttributes = item.DataAttributes.Aggregate("", (current, attribute) => current + ("data-" + attribute.Key.ToLower() + "=" + attribute.Value + " "));

                                <option class="item" value="@item.Value" @(!Model.FundCode.IsNullOrEmpty() && Model.FundCode.Equals(item.Value) ? "selected" : String.Empty) @dataAttributes>@item.Text</option>
                            }
                        </select>
                        <div class="form__element--error">
                            @Html.ValidationMessageFor(m => m.FundCode)
                        </div>
                    </div>
                </div>

                <div class="row mb-3">
                    @if (User.IsInRole(Role.PostPayment))
                    {
                        if (Model.MopCodes.Items != null && Model.MopCodes.Items.Count > 1)
                        {
                            <div class="col">
                                @Html.LabelFor(m => m.MopCode, new { @class = "form-label" })
                                <select id="MopCode" name="MopCode">
                                    <option value="">None</option>
                                    @foreach (var item in Model.MopCodes.Items)
                                    {
                                        var dataAttributes = item.DataAttributes.Aggregate("", (current, attribute) => current + ("data-" + attribute.Key.ToLower() + "=" + attribute.Value + " "));

                                        <option class="item" value="@item.Value" @(!string.IsNullOrWhiteSpace(Model.MopCode) && Model.MopCode.Equals(item.Value) ? "selected" : String.Empty) @dataAttributes>@item.Text</option>
                                    }
                                </select>
                                <div class="form__element--error">
                                    @Html.ValidationMessageFor(m => m.MopCode)
                                </div>
                            </div>
                        }
                        else
                        {
                            <div class="col">
                                @Html.LabelFor(m => m.MopCode, new { @class = "form-label" })
                                @Html.Display(Model.MopCodes.Items.FirstOrDefault().Text, new { @class = "form-control" })
                                @{
                                    var dataAttributes = Model.MopCodes.Items.FirstOrDefault().DataAttributes.Aggregate("", (current, attribute) => current + ("data-" + attribute.Key.ToLower() + "=" + attribute.Value + " "));
                                }
                                <input type="hidden" id="MopCode" name="MopCode" value="@Model.MopCodes.Items.FirstOrDefault().Value" @dataAttributes />
                            </div>
                        }
                    }
                    else
                    {
                        <div class="col">
                            @Html.Hidden("MopCode", Model.MopCodes.Items.FirstOrDefault().Value)
                            <div class="form__element--error">
                                @Html.ValidationMessageFor(m => m.MopCode)
                            </div>
                        </div>
                    }
                </div>

                <div class="row mb-3">
                    <div class="col">
                        @Html.LabelFor(m => m.AccountReference, new { @class = "form-label" })
                        <div class="account-reference-wrapper">
                            @Html.TextBoxFor(m => m.AccountReference, new { @class = "form-control " })
                            @Html.ActionLink("Search", "AccountHolderSearch", "Payment", new { id = Model.FundCode }, new { @class = "btn btn-primary account-reference-search", @style = "display:none;" })
                        </div>
                        <div id="AccountNameWrapper"></div>
                        <div id="AccountBalanceWrapper"></div>
                        <div class="form__element--error">
                            <div class="account-reference-validator"></div>
                            @Html.ValidationMessageFor(m => m.AccountReference)
                        </div>
                    </div>
                </div>

                <div class="row mb-3 vat-option">
                    <div class="col">
                        @Html.LabelFor(m => m.VatCode, new { @class = "form-label" })
                        <select id="VatCode" name="VatCode">
                            <option value="">None</option>
                            @foreach (var item in Model.VatCodes.Items)
                            {
                                <option class="item" value="@item.Value" @(!Model.VatCode.IsNullOrEmpty() && Model.VatCode.Equals(item.Value) ? "selected" : String.Empty)>@item.Text</option>
                            }
                        </select>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col">
                        @Html.LabelFor(m => m.Narrative, new { @class = "form-label" })<small><span class="fw-lighter"> (optional)</span></small>
                        @Html.TextAreaFor(m => m.Narrative, new { @class = "form-control" })
                        <div class="form__element--error">
                            @Html.ValidationMessageFor(m => m.Narrative)
                        </div>
                    </div>
                </div>

                <div class="row mb-3 payment-reversal-warning" style="display:none;">
                    <div class="col">
                        <div class="alert alert-warning mb-0">
                            <div class="fw-bold pb-1">
                                Check amount
                            </div>
                            <p class="mb-1">A negative amount is expected for a write-back MOP.</p>
                            <p class="mb-0">Please check your input before continuing.</p>
                        </div>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col">
                        @Html.LabelFor(m => m.Amount, new { @class = "form-label" })
                        <div class="input-group w-50">
                            <span class="input-group-text">£</span>
                            @Html.TextBoxFor(m => m.Amount, new { @class = "form-control w-50" })
                        </div>
                        <div class="form__element--error">
                            <span class="field-validation-error amount-validator"></span>
                            @Html.ValidationMessageFor(m => m.Amount)
                        </div>
                    </div>
                </div>

                <div>
                    <input type="submit" value="Add to basket" class="btn btn-primary add-to-basket one-click-submit-button" />
                </div>

            </div>
            <div class="col-12 col-lg-6 offset-lg-1">
                @{ Html.RenderPartial("_Basket", Model.Basket); }
            </div>
        </div>
    </div>
}

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

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

            paymentsAdmin.pages.payment.index.searchAction = '@Url.Action("AccountHolderSearch")';
            paymentsAdmin.pages.payment.index.searchEnabledFundCodes = JSON.parse('@Html.Raw(Model.SearchEnabledFundCodes)');

            var selectedFundCode = getSelectedAccessibleAutocompleteOptionValue('#FundCode', $('#FundCode').val());
            showHideSearchButton(selectedFundCode);
            showHideVatOptions();
            loadAccountDetails();

            if ($(".basket__mop").first().data("mop")) {
                var option = getAccessibleAutocompleteOptionByValue('#MopCode', $(".basket__mop").first().data("mop"));
                handleMopCodeChange(option.text);
            }

        });
    </script>

}