@model PantheonRA.BackOffice.Models.PrintMenuDropdownViewModel
<div class="pull-right btn-group dropdown custom-dropdown">
    @Html.HiddenFor(model => model.PrintType)
    @Html.Hidden("PrintButtonGroupCategoriesExist", Model.PrintButtonGroup.Categories != null && Model.PrintButtonGroup.Categories.Count() > 0)
    @if (Model.PrintButtonGroup.Categories != null)
    {
        <button type="button"
                id="@(string.IsNullOrEmpty(Model.PrintButtonId) ? "printButton" : Model.PrintButtonId)"
                class="btn btn-primary printDropdown"
                title="@Loc.Print"
                onclick="printLastReport()"
                disabled>
            <i id="printButton-icon" class="far fa-print printButton-icon print-loader-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" disabled>
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu printMenuDropdown">
            @foreach (var category in Model.PrintButtonGroup.Categories)
            {
                <li class="dropdown-submenu print-menu-dropdown-submenu @(category.Buttons.Any(b => b.IsInternal == true) || category.SubCategory.Any(sub => sub.Buttons.Any(b => b.IsInternal == true)) ? " submenu-internal" : "")">
                    <a href="#" class="dropdown-menu-icon" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                        <span class="nav-label">
                            @category.CategoryText
                        </span>
                        <span class="caret caret-right"></span>
                    </a>
                    <ul class="dropdown-menu printMenuDropdown printMD">
                        @if (category.SubCategory.Count > 0)
                        {
                            foreach (var button in category.Buttons)
                            {
                                if (button.HideForLoc == false)
                                {
                                    <li class="print-option" data-print="@button.CustomAttr">
                                        <a onclick="@button.OnClick" class="print-link @button.Class" title="@button.Text">
                                            <span class="nav-label">@button.Text</span>
                                        </a>
                                        @if (!string.IsNullOrEmpty(button.OnPreview))
                                        {
                                            <a onclick="@button.OnPreview" class="preview-icon pull-right" title="@Loc.Preview">
                                                <i class="fa fa-eye fa-lg"></i>
                                            </a>
                                        }
                                    </li>
                                }
                            }
                            foreach (var subCategory in category.SubCategory)
                            {
                                <li class="dropdown-submenu">
                                    <a href="#" class="dropdown-menu-icon" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                                        <span class="nav-label">
                                            @subCategory.SubCategoryText
                                        </span>
                                        <span class="caret caret-right"></span>
                                    </a>

                                    <ul class="dropdown-menu printMenuDropdown menu-sub-category">
                                        @foreach (var subButton in subCategory.Buttons)
                                        {
                                            if (subButton.HideForLoc == false)
                                            {
                                                <li class="print-option" data-print="@subButton.CustomAttr">
                                                    <a onclick="@subButton.OnClick" class="print-link @subButton.Class" title="@subButton.Text">
                                                        <span class="nav-label">@subButton.Text</span>
                                                    </a>
                                                    @if (!string.IsNullOrEmpty(subButton.OnPreview))
                                                    {
                                                        <a onclick="@subButton.OnPreview" class="preview-icon pull-right" title="@Loc.Preview">
                                                            <i class="fa fa-eye fa-lg"></i>
                                                        </a>
                                                    }
                                                </li>
                                            }
                                        }
                                    </ul>
                                </li>
                            }
                        }
                        else
                        {
                            foreach (var button in category.Buttons)
                            {
                                if (button.HideForLoc == false)
                                {
                                    <li class="print-option" data-print="@button.CustomAttr">
                                        <a onclick="@button.OnClick" class="print-link @button.Class" title="@button.Text">
                                            <span class="nav-label">@button.Text</span>
                                        </a>
                                        @if (!string.IsNullOrEmpty(button.OnPreview))
                                        {
                                            <a onclick="@button.OnPreview" class="preview-icon pull-right" title="@Loc.Preview">
                                                <i class="fa fa-eye fa-lg"></i>
                                            </a>
                                        }
                                    </li>
                                }
                            }
                        }
                    </ul>
                </li>
            }
        </ul>
    }
</div>

<script>
    $(document).ready(function () {
        updatePrintDisplay();

        $(".print-link, .preview-icon").on("click", function () {
            printLastReport(true);
        });
    });

    function updatePrintDisplay() {
        if ($("#PrintButtonGroupCategoriesExist").val() == "@true") {
            var reports = [];
            var doctype = $("#DocType").val();
            $.each($(".print-option"), function (key, item) {
                if (!reports.includes($(item).attr("data-print")))
                    reports.push($(item).attr("data-print"));
            });

            if (reports.length > 0) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Action("GetActiveReports", "Reports")",
                    data: JSON.stringify({ reports: reports, docType: doctype }),
                    contentType: "application/json",
                    success: function (response) {
                        $.each($(".print-option"), function (key, item) {
                            var exist = response.includes($(item).attr("data-print"));
                            if (!exist)
                                $(item).hide();
                        });

                        printLastReport(true);
                    },
                });
            }
        }
    }

    function updatePrintMenuDisplay(onlyInternal) {
        $.each($(".print-menu-dropdown-submenu"), function (key, item) {
            if (onlyInternal) {
                if (!$(item).hasClass("submenu-internal"))
                    $(item).hide();
            } else
                    $(item).show();
        });
    }

    function printLastReport(onlySetTitle) {
        if ($(".print-option").length === 0)
            return;

        var printType = stringFormat($("#PrintType").val(), $("#DocType").val());

        $.get('@Url.Action("GetLastReportPrintForUserAsync", "Reports")?printType=' + printType, function (response) {
            var lastPrint;
            var printParts = response.split("_");
            if (printParts.length > 1)
                lastPrint = printParts[1];
            else
                lastPrint = response;

            var printOption = $(".print-option[data-print='" + lastPrint + "']")
            if ($("#DefaultReport").val())
                lastPrint = $("#DefaultReport").val();
            else if (printOption.length === 0)
                lastPrint = $(".print-option").first().attr("data-print");

            $("#DefaultReport").val(lastPrint);
            $.each($(".print-option"), function (key, item) {
                if ($(item).attr("data-print") === lastPrint) {
                    if (onlySetTitle) {
                        $(".printDropdown").attr("title", $(item).children("a").attr("title"));
                        if ($(item).closest(".dropdown-menu").hasClass("menu-sub-category"))
                            $(item).closest('.dropdown-submenu').addClass("submenu-open").parents(".dropdown-submenu").addClass("submenu-open");
                        else
                            $(item).closest('.dropdown-submenu').addClass("submenu-open");
                    }
                    else
                        $(item).children("a")[0].click();

                    return false;
                }
            });
        }, "json");
    }
</script>