@model PantheonRA.BackOffice.Models.PrintDropdownViewModel

<input type="hidden" id="PrintSettings" value="@(Model.PrintSettings != null ? string.Join(",", Model.PrintSettings.ToArray()) : null)" />
<div class="pull-right">
    <div class="btn-group dropdown">
        <button type="button"
                id="printButton"
                class="btn btn-primary printDropdown"
                onClick="@(Model.PrintButtonGroup.Buttons.Count() > 0 ? Model.PrintButtonGroup.Buttons.First().OnClick : string.Empty)"
                title="@Loc.Print">
            <i id="printButton-icon" class="far fa-print print-loader-icon printButton-icon"></i><span class="visible-lg-inline visible-md-inline m-l-xs">@Loc.Print</span>
        </button>
        <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu">
            @foreach (var button in Model.PrintButtonGroup.Buttons)
            {
                if (button.HideForLoc == false)
                {
                    <li class="print-option" data-print="@button.CustomAttr">
                        <a onclick="@button.OnClick" class="@button.Class">
                            <i class="@button.Icon"></i><span class="m-l-xs">@button.Text</span>
                        </a>
                    </li>
                }
            }

        </ul>
    </div>
</div>

<script>
    var printSettings = undefined;

    $(document).ready(function () {
        printSettings = $("#PrintSettings").val().length > 0 ? $("#PrintSettings").val().split(",") : null;
        updatePrintDisplay();
    });

    function updatePrintDisplay() {
        if (printSetting && @Model.PrintButtonGroup.Buttons.Count() > 1) {
            $(".printDropdown").closest(".dropdown").addClass("btn-group");
            $(".printDropdown + .dropdown-toggle").show();

            var displayedButtons = 0;
            $.each($(".print-option"), function (key, item) {
                if (printSettings.indexOf($(item).attr("data-print")) > -1) {
                    if (displayedButtons == 0) {
                        printButton.setAttribute("onClick", item.firstElementChild.getAttribute("onClick"));
                    }

                    $(item).show();
                    displayedButtons++;
                } else
                    $(item).hide();
            });
        }
        else {
            $(".printDropdown").closest(".dropdown").removeClass("btn-group");
            $(".printDropdown + .dropdown-toggle").hide();
        }
    }

    function refreshPrintDisplay(docType) {
        $.get('@Url.Action("GetDocTypePrintSettings", "DocumentType")?docType=' + docType, function (response) {
            if (response) {
                printSettings = response;
            }
            else
                printSettings = null;

            updatePrintDisplay();
        }, "json");
    }
</script>