@model PantheonRA.BackOffice.Models.FormButtonsViewModel
@using PantheonRA.BackOffice.Models

@if (Model.ShowCloseBtn)
{
    <a href="@Model.BackUrl" class="btn btn-transparent pull-right close-btn"><i class="fal fa-times"></i></a>
}

@if (Model.PrintButtonGroup != null && Model.PrintButtonGroup.Buttons != null)
{
    if (Model.PrintSettings != null)
    {
        Html.RenderPartial("~/Views/Shared/Components/_PrintButton.cshtml", new PrintDropdownViewModel() { PrintButtonGroup = Model.PrintButtonGroup, PrintSettings = Model.PrintSettings });
    }
    else
    {
        @ButtonGroup(Model.PrintButtonGroup.Buttons,
        Loc.Print,
        "printButton",
        "far fa-print",
        Model.PrintButtonGroup.IsActive ? "btn btn-success" : "btn btn-success disabled")
    }
}

@if (Model.PrintMenuButtonGroup != null && Model.PrintMenuButtonGroup.Categories != null)
{
    Html.RenderPartial("~/Views/Shared/Components/_PrintMenuButton.cshtml", new PrintMenuDropdownViewModel()
    {
        PrintButtonGroup = Model.PrintMenuButtonGroup,
        PrintSettings = Model.PrintSettings,
        PrintType = Model.PrintType
    });
}

@if (Model.SaveButtonGroup != null && Model.SaveButtonGroup.Buttons != null)
{
    @ButtonGroup(Model.SaveButtonGroup.Buttons,
    Loc.Save,
    "saveButton",
    "fa fa-save",
    Model.SaveButtonGroup.IsActive ? "btn btn-success" : "btn btn-default btn-outline")
}

@if (Model.ConfirmButtonGroup != null && Model.ConfirmButtonGroup.Buttons != null)
{
    @ButtonGroup(Model.ConfirmButtonGroup.Buttons,
    Loc.Confirm,
    "confirmButton",
    "fa fa-check",
    Model.ConfirmButtonGroup.IsActive ? "btn btn-success" : "btn btn-default btn-outline")
}

@if (Model.IsWizardVisible && Model.WizardButtonGroup != null && Model.WizardButtonGroup.Buttons != null)
{
    @ButtonGroup(Model.WizardButtonGroup.Buttons,
    (!string.IsNullOrEmpty(Model.WizardButtonGroup.ButtonName) ? Model.WizardButtonGroup.ButtonName : Loc.Wizards),
    "wizardButton",
    (!string.IsNullOrEmpty(Model.WizardButtonGroup.ButtonIcon) ? Model.WizardButtonGroup.ButtonIcon : "fas fa-wand-magic"),
    (!string.IsNullOrEmpty(Model.WizardButtonGroup.ButtonClass) ? Model.WizardButtonGroup.ButtonClass : "btn btn-default btn-outline"),
        true, true, Model.IsWizardDisabled, "collapsable-form-button" + (Model.IsWizardHidden ? " hidden" : ""), Model.WizardButtonGroup.ButtonTitle)
}

@if (Model.IsFiscalPrinterWizardVisible)
{
    var localApiUrl = System.Configuration.ConfigurationManager.AppSettings.Get("FiscalizationLocalApiUrl");
    var centralApiUrl = Url.Action("Index", "CashRegister") == "/" ? "/CashRegister" : Url.Action("Index", "CashRegister");
    @ButtonGroup(
    new List<FormButton> {
        new FormButton {
            Text = Loc.OpenDrawer,
            OnClick = "fiscalPrinterOpenDrawer('" + centralApiUrl + "', '" + localApiUrl +"', " + "'" + Loc.DrawerOpening + "', '" + Loc.DrawerOpeningSuccessful + "', this)",
            Class = "fiscalPrinterDrawer"
        },
        new FormButton {
            Text = Loc.DepositInOut,
            OnClick = "fiscalPrinterDeposit('" + centralApiUrl + "', '" + localApiUrl +"', " + "'" + Loc.DepositInOut + "'" + ", '" + Loc.DepositSuccessful + "')",
            Class = "fiscalPrinterCashOut fiscalPrinterCashIn"
        },
        new FormButton {
            Text = Loc.XReport,
            OnClick = "fiscalPrinterXReport('" + centralApiUrl + "', '" + localApiUrl +"', " + "'" + Loc.XReport + "'" + ", '" + Loc.XReportSuccessful + "', this)",
            Class = "fiscalPrinterXReport"
        },
        new FormButton {
            Text = Loc.PReport,
            OnClick = "fiscalPrinterPReport('" + centralApiUrl + "', '" + localApiUrl +"', " + "'" + Loc.PReport + "'" + ", '" + Loc.PReportSuccessful + "')",
            Class = "fiscalPrinterPReport"
        },
    },
    Loc.Wizards,
    "fiscalPrinterWizardButton",
    "fas fa-magic",
    "btn btn-default btn-outline",
    false)
}

@if (Model.DuplicateButtonGroup != null && Model.DuplicateButtonGroup.Buttons != null)
{
    @ButtonGroup(Model.DuplicateButtonGroup.Buttons,
    Loc.Duplicate,
    "duplicateButton",
    "fa fa-files-o",
    Model.DuplicateButtonGroup.IsActive ? "btn btn-success" : "btn btn-default btn-outline disabled")
}

@if (Model.ForwardButtonGroup != null && Model.ForwardButtonGroup.Buttons != null)
{
    @ButtonGroup(Model.ForwardButtonGroup.Buttons,
    Model.ForwardButtonGroup.IsButtonName ? Model.ForwardButtonGroup.ButtonName : Loc.Confirm,
    "forwardButton",
    "fa fa-mail-forward",
    Model.ForwardButtonGroup.IsActive ? "btn btn-success" : "btn btn-default btn-outline disabled")
}

@if (Model.ExportXmlButtonGroup != null && Model.ExportXmlButtonGroup.Buttons != null)
{
    @ButtonGroup(Model.ExportXmlButtonGroup.Buttons,
    Loc.Export,
    "exportXmlButton",
    "fa fa-file-text",
    Model.ExportXmlButtonGroup.IsActive ? "btn btn-success" : "btn btn-default btn-outline")
}

@if (Model.BackButtonGroup != null && Model.BackButtonGroup.Buttons != null)
{
    @ButtonGroup(Model.BackButtonGroup.Buttons,
    Model.BackButtonGroup.IsButtonName ? Model.BackButtonGroup.ButtonName : Loc.Back,
    "backButton",
    "fa fa-reply",
    Model.BackButtonGroup.IsActive ? "btn btn-warning" : "btn btn-default btn-outline disabled")
}

@if (Model.DeleteButtonGroup != null && Model.DeleteButtonGroup.Buttons != null)
{
    @ButtonGroup(Model.DeleteButtonGroup.Buttons,
    Loc.Delete,
    "deleteButton",
    "far fa-trash-alt",
    Model.DeleteButtonGroup.IsActive ? "btn btn-danger btn-outline" : "btn btn-default btn-outline disabled")
}

@if (Model.CreateButtonGroup != null && Model.CreateButtonGroup.Buttons != null)
{
    @ButtonGroup(Model.CreateButtonGroup.Buttons,
    Model.CreateButtonGroup.IsButtonName ? Model.CreateButtonGroup.ButtonName : Loc.New,
    "createButton",
    "fa fa-plus",
    Model.CreateButtonGroup.IsActive ? "btn btn-success btn-outline" : "btn btn-default btn-outline")
}

@if (Model.AdditionalFunctionalitiesGroup != null && Model.AdditionalFunctionalitiesGroup.Buttons != null)
{
    @ButtonGroup(Model.AdditionalFunctionalitiesGroup.Buttons,
    Loc.AdditionalFunctionalities,
    "additionalFuncButton",
    "fas fa-ellipsis-h",
    Model.AdditionalFunctionalitiesGroup.IsActive ? "btn btn-primary btn-outline" : "btn btn-primary btn-outline hidden",
    true,
    false)
}


@*<button type="button" onclick="copy()" class="btn btn-default btn-outline pull-right" id="copyButton">
        <i class="fas fa-copy"></i> <span class="visible-lg-inline visible-md-inline m-l-xs">@Loc.MakeCopy</span>
    </button>*@

@helper ButtonGroup(List<PantheonRA.BackOffice.Models.FormButton>
            buttons, string text, string buttonId, string icon, string buttonClass, bool hasParentButtonAction = true, bool showText = true,
            bool isDisabled = false, string buttonGroupClass = "", string buttonTitle = "")
{
    var titleText = string.IsNullOrEmpty(buttonTitle) ? text : buttonTitle;
    if (buttons.Count > 1)
    {
        var iconId = buttonId + "-icon";
        <div id="formButtonsBtnGroupContainer" class="pull-right dropdown @buttonGroupClass @(hasParentButtonAction ? "btn-group" : string.Empty)">
            @if (hasParentButtonAction)
        {
            var firstOnClick = "";
            foreach (var btn in buttons)
            {
                if (btn.HideForLoc == false)
                {
                    firstOnClick = btn.OnClick;
                    break;
                }
            }

                <button type="@(buttons.First().IsSubmit ? " submit" : "button" )" onclick="@firstOnClick" class="@buttonClass" id="@buttonId" title="@titleText" @(isDisabled ? "disabled" : "")>
                    @if (showText == true)
        {
                        <i id="@iconId" class="@icon"></i><span class="visible-lg-inline visible-md-inline m-l-xs">@text</span>
    }
                    else
                    {
                        <i id="@iconId" class="@icon"></i>
                    }
                </button>
                <button id="buttonDropdown" type="button" class="@buttonClass dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" @(isDisabled ? "disabled" : "")>
                    <span class="caret"></span>
                </button>
            }
            else
            {
                <button type="button" class="@buttonClass" id="@buttonId" title="@titleText" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    @if (showText == true)
{
                        <i id="@iconId" class="@icon"></i><span class="visible-lg-inline visible-md-inline m-l-xs">@text</span>
                    }
                    else
                    {
                        <i id="@iconId" class="@icon"></i>
                    }
                    <span class="caret"></span>
                </button>
            }
            <ul class="dropdown-menu">
                @foreach (var button in buttons)
                {
                    if (button.HideForLoc == false)
                    {
                        <li class=@(button.IsHidden ? "hidden" : string.Empty)>
                            <a href="@(string.IsNullOrEmpty(button.Url) ? " #" : button.Url)" @(string.IsNullOrEmpty(button.Target) ? string.Empty : string.Format("target={0}", button.Target))
                               onclick="@button.OnClick" class="@button.Class">
                                <i class="@button.Icon"></i><span class="m-l-xs">@button.Text</span>
                            </a>
                        </li>
                    }
                }
            </ul>
        </div>
    }
    else
    {
        var iconId = buttonId + "-icon";
        if (buttons.First().IsSubmit)
        {
            <button type="submit" onclick="@buttons.First().OnClick" class="@buttonClass pull-right" id="@buttonId" title="@titleText">
                @if (showText == true)
{
                    <i id="@iconId" class="@icon"></i><span class="visible-lg-inline visible-md-inline m-l-xs">@text</span>
                }
                else
                {
                    <i id="@iconId" class="@icon"></i>
                }
            </button>
        }
        else
        {
            <a href="
               @(string.IsNullOrEmpty(buttons.First().Url) ? " #" : buttons.First().Url)"
               @(string.IsNullOrEmpty(buttons.First().Target) ? string.Empty : string.Format("target={0}", buttons.First().Target))
               onclick="@buttons.First().OnClick"
               class="@buttonClass @buttons.First().Class pull-right"
               id="@buttonId"
               title="@titleText" @buttons.First().CustomAttr>
                @if (showText == true)
{
                    <i id="@iconId" class="@icon"></i><span class="visible-lg-inline visible-md-inline m-l-xs">@text</span>
                }
                else
                {
                    <i id="@iconId" class="@icon"></i>
                }
            </a>
        }
    }
}
