@using PantheonRA.BackOffice.Enums

<div class="modal inmodal fade" id="invoiceSubjectRegNoMissingModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <div class="iris-title m-b-sm">
                    <div class="row">
                        <div class="col-xs-11 padding-right-none">
                            <span><strong>@Loc.SubjectPIBError</strong></span>
                        </div>
                        <div class="col-xs-1 pull-right">
                            <i class="fa fa-times clickable-icon m-l-xs" data-dismiss="modal"></i>
                        </div>
                    </div>
                </div>
                <span>@Loc.CodelistSubjectBuyerPIBError</span>
                <div class="well well-sm well-bg clickable-icon m-t-sm m-b-sm" data-dismiss="modal" onclick="openSubjectToEdit()">
                    <div class="row">
                        <div class="col-xs-2">
                            <div class="icon-arrow-right"></div>
                        </div>
                        <div class="col-xs-10">
                            <strong>@Loc.CodelistSubjectBuyer</strong>
                            <p class="no-margins">@Loc.CodelistSubjectBuyerPIB</p>
                        </div>
                    </div>
                </div>
                <div class="well well-sm well-bg clickable-icon m-b-sm" onclick="openInvoiceSubjectPIBHelp()">
                    <div class="row">
                        <div class="col-xs-2">
                            <div class="icon-help icon-full-size"></div>
                        </div>
                        <div class="col-xs-10 well-text-margin">
                            <strong>@Loc.ViewInstructions</strong>
                            <p class="no-margins">@Loc.ViewInstructionsDescription</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    var subjectId;

    function openInvoiceSubjectPIBHelp() {
        window.open('@Loc.HelpUrl@HelpIds.SubjectPIB', '_blank');
    }

    function openSubjectToEdit() {
        window.open('@Url.Action("Edit", "Subjects")' + "?id=" + subjectId, '_blank');
    }

    async function checkSubjectPIB($subject) {
        subjectId = $subject.val();

        return new Promise((resolve, reject) => {
            $.get("@Url.Action("GetSubjectById", "Subjects")" + "?subjectId=" + encodeURIComponent(subjectId), function (data) {
                if (!data || !data.RegNo || (data.RegNo.length !== 8 && data.RegNo.length !== 13)) {
                    $("#invoiceSubjectRegNoMissingModal").modal("show");
                    resolve(false);
                }
                resolve(true);
            }).fail(function () {
                $().customModal({
                    destroyOnClose: true,
                    showCloseButton: true,
                    type: "alert",
                    text: "@Loc.EnteredSubjectDoesNotExist",
                    buttons: [
                        {
                            icon: 'fa fa-check',
                            text: $("#ConfirmLoc").val(),
                            closeOnAction: true,
                            type: 'primary'
                        }
                    ]
                });

                resolve(false);
            });
        });
    }
</script>