@using PantheonRA.BackOffice.Models
@model PantheonRA.BackOffice.Models.Reports.JournalPrintData
@{
    ViewBag.Title = Loc.CashRegisterJournals;
}

<div class="wrapper wrapper-content">
    <div class="tabs-container">

        @Html.Partial("_JournalTabNavigation")

        <div class="row buttons-tabbed">
            <button type="button" class="btn btn-sm btn-primary dropdown-toggle m-l-xs" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="far fa-print print-loader-icon" title="@Loc.Print"></i> <span class="visible-lg-inline visible-md-inline">&nbsp;&nbsp;@Loc.Print</span> <span class="caret"></span>
            </button>
            <ul class="dropdown-menu">
                @if (Model.CashReg.Date == null)
                {
                    <li><a href="#" onclick="print(0, true)"><i class="fa fa-files-o"></i>&nbsp;&nbsp;<span class="bold">@Loc.CloseCashRegister</span></a></li>
                }
                @{ var anyPrinterActive = Model.PrintSettings != null && Model.PrintSettings.Any(); }
                @if (!anyPrinterActive || Model.PrintSettings.Any(x => x == "printer_A4"))
                {
                    <li><a href="#" id="printButton" onclick="print(1)"><i class="far fa-print fa-lg"></i>&nbsp;&nbsp; @Loc.Print A4</a></li>
                }
                @if (Model.PrintSettings != null && Model.PrintSettings.Any(x => x == "printer_80"))
                {
                    <li><a href="#" id="printButton" onclick="print(2)"><i class="far fa-print fa-lg"></i>&nbsp;&nbsp; @Loc.Print 80mm</a></li>
                }
                <li><a href="#" id="printButton" onclick="print(0)"><i class="fa fa-file-pdf-o fa-lg"></i>&nbsp;&nbsp; @Loc.SaveAsPDF</a></li>
            </ul>
            @Html.Partial("_FormButtons", new FormButtonsViewModel
            {
                ShowCloseBtn = true,
                BackUrl = ViewBag.ReturnUrl == null || string.IsNullOrEmpty(ViewBag.ReturnUrl) ? Url.Action("ReceivedCashDocuments", "Reports"): ViewBag.ReturnUrl
            })
        </div>

        <div class="tab-content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="divided-content clearfix">
                        <div class="divided-content-full">
                            <div class="ibox">
                                <div class="ibox-content">
                                    <div class="wrapper wrapper-content">
                                        <div class="row m-b-sm">
                                            @Html.HiddenFor(x => x.CashReg.Ackey)
                                            <div class="col-sm-9 col-xs-12">
                                                <div class="m-r-lg m-b-xs" style="display:inline-block; min-width: 120px">
                                                    @Html.CheckBox("PaymentMethod", true, new { @data = "PaymentMethodTable", @id = "PaymentMethodValue", @class = "display-checkbox form-control js-switch-small js-switch" })
                                                    @Loc.PaymentMethod
                                                </div>
                                                <div class="m-r-lg m-b-xs" style="display:inline-block; min-width: 120px">
                                                    @Html.CheckBox("Operator", true, new { @data = "OperatorTable", @id = "OperatorValue", @class = "display-checkbox form-control js-switch-small js-switch" })
                                                    @Loc.Operator
                                                </div>
                                                <div class="m-r-lg m-b-xs" style="display:inline-block; min-width: 120px">
                                                    @Html.CheckBox("Product", true, new { @data = "ProductTable", @id = "ProductValue", @class = "display-checkbox form-control js-switch-small js-switch" })
                                                    @Loc.Product
                                                </div>
                                                <div class="m-r-lg m-b-xs" style="display:inline-block; min-width: 120px">
                                                    @Html.CheckBox("Tax", true, new { @data = "TaxTable", @id = "TaxValue", @class = "display-checkbox form-control js-switch-small js-switch" })
                                                    @Loc.Tax
                                                </div>
                                                <div class="m-r-lg m-b-xs" style="display:inline-block; min-width: 120px">
                                                    @Html.CheckBox("Invoice", true, new { @data = "InvoiceTable", @id = "InvoiceValue", @class = "display-checkbox form-control js-switch-small js-switch" })
                                                    @Loc.Invoice
                                                </div>
                                            </div>
                                        </div>

                                        @Html.Partial("PrintJournalPartial")
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@section pageScripts{
    @Scripts.Render("~/bundles/moment")
    <script type="text/javascript">
        $(document).ready(function () {
            var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch-small'));
            elems.forEach(function (html) {
                var switchery = new Switchery(html, { size: 'small' });
            });

            $(".display-checkbox").on('change', function () {
                $("#" + $(this).attr("data")).css("display", $(this).is(':checked') ? "block" : "none");
            });
        });

        function print(printType, close) {
            var url = "";
            var urlData = "&PaymentMethod=" + $("#PaymentMethodValue").is(':checked');
            urlData += "&Operator=" + $("#OperatorValue").is(':checked');
            urlData += "&Partner=" + $("#PartnerValue").is(':checked');
            urlData += "&Product=" + $("#ProductValue").is(':checked');
            urlData += "&Tax=" + $("#TaxValue").is(':checked');
            urlData += "&Invoice=" + $("#InvoiceValue").is(':checked');

            if (printType === 1 || printType === 0) {
                url = "@Html.Raw(Url.Action("PrintPdf", "Journal", new { id = Model.CashReg.Ackey }))" + urlData;
            }
            else if (printType === 2) {
                url = "@Html.Raw(Url.Action("PrintSmallPdf", "Journal", new { id = Model.CashReg.Ackey }))" + urlData;
            }

            if (close) {
                $.get("@Html.Raw(Url.Action("PrintPreview", "Journal", new { id = Model.CashReg.Ackey, closeJournal = true }))", function (data) {
                    printPrompt(url);
                });
            }
            else {
                executePrint(url, printType !== 0);
            }
        }

        function executePrint(url, openPrintDialog) {
            var myWindow = window.open(url, '_blank');
            myWindow.document.close();
            $(myWindow.window).on("load", function () {
                myWindow.focus();
                if (openPrintDialog) {
                    myWindow.print();
                }
                //myWindow.close();
            });
        }

        function printPrompt(url) {
            swal({
                title: '@Loc.PrintJournal',
                text: '@Loc.JournalClosedContinuePrinting',
                type: "warning",
                showCancelButton: false,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: '@Loc.Print',
                closeOnConfirm: true
            }, function () {
                executePrint(url, false);
                window.location.href = '@Html.Raw(Url.Action("PrintPreview", "Journal", new { id = Model.CashReg.Ackey }))';
            });
        }

        function onClickBack(returnUrl) {
            if (!returnUrl)
                window.location.href = '@Url.Action("ReceivedCashDocuments", "Reports")';
            else
                window.location.href = returnUrl;
        }
    </script>
}
