@using PantheonRA.BackOffice.Enums
@using PantheonRA.BackOffice.Helpers
@using PantheonRA.WebApi.Contracts.Enums
@using PAW.Contracts.Enums
@model PantheonRA.BackOffice.Models.IssuedInvoicesBillingKEPGridModel
@{
    ViewBag.Title = Loc.KEP;
}

<div class="wrapper wrapper-content">
    <div class="tabs-container">
        @Html.Partial("_ReportsTabNavigation")
        <div class="row btn-group pull-right buttons-tabbed">
            <button id="printButton" type="button" onclick="printKEP()" title="@Loc.Print"
                    class="btn btn-primary pull-right collapsable-form-button">
                <i class="far fa-print m-r-xs"></i>
                <span class="visible-lg-inline visible-md-inline">@Loc.Print</span>
            </button>

            <button id="clearButton" type="button" onclick="clearFiltersKEP()" title="@Loc.DeleteCriteria"
                    class="btn btn-primary btn-outline pull-right collapsable-form-button">
                <i class="far fa-times m-r-xs"></i>
                <span class="visible-lg-inline visible-md-inline">@Loc.DeleteCriteria</span>
            </button>
        </div>

        <div class="tab-content">
            <div class="row document-head">
                <div class="col-md-2 col-xs-6">
                    @Html.Partial("~/Views/Reports/_IssuedBillsBillingTabNavigation.cshtml", IssuedBillsType.IssuedInvoicesBillingKEP)
                </div>
            </div>

            <div class="row tab-content-full-page">
                <div class="col-lg-3 tabs-column-container full-height overflow-x-auto column-full-height-border-right">
                    <div class="tabs-container">
                        <ul class="nav nav-tabs horizontal-menu">
                            <li class="active divided-tab-1"><a data-toggle="tab" href="#printOptions" class="local-tab">@Loc.PrintoutType</a></li>
                        </ul>
                    </div>
                    <div class="tab-content">
                        <input id="ReportType" hidden />
                        <div id="printOptions" class="tab-pane fade in active m-l-n m-r-n">
                            <div class="print-options-container">
                                @Html.HiddenFor(x => x.LastPrint)
                                @if (Model.ReportTree != null)
                                {
                                    foreach (var report in Model.ReportTree)
                                    {
                                        <div class="print-option-group">
                                            <div class="print-option-label p-l-sm" onclick="togglePrintOptionGroup(this)">
                                                <div class="print-option-border"></div>
                                                <span class="caret caret-left"></span>
                                                <span>
                                                    @report.Category
                                                </span>
                                            </div>
                                            @foreach (var formReport in report.FormReports)
                                            {
                                                <div onclick="selectPrintType(this, '@formReport.Report')" class="preview-print-option cursor-pointer" data-print="@formReport.Report" data-fisc="@formReport.Fiscalize">
                                                    <div class="print-option-border"></div>
                                                    <div class="print-option-border-horizontal"></div>
                                                    <a title="@formReport.Report - @formReport.Name">
                                                        @formReport.Report - @formReport.Name
                                                    </a>
                                                </div>
                                            }
                                        </div>
                                    }
                                }
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-lg-9">
                    <h3 class="m-t-md">@Loc.Criteria</h3>
                    <div class="row">
                        <div class="form-horizontal form-md">
                            <div class="col-lg-6">
                                <div class="form-group">
                                    @Html.Label("DateFrom", Loc.BillFrom, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-4">
                                        <div class="input-group">
                                            <span class="input-group-addon">
                                                <i class="far fa-calendar alt"></i>
                                            </span>
                                            @Html.TextBox("DateFrom", null, HtmlAttributesHelper.CreateInputAttributes("form-control datepicker", "DateFrom", false, Loc.DateFrom))
                                        </div>
                                    </div>
                                    @Html.Label("DateTo", Loc.To, new { @class = "col-md-1 control-label padding-left-none" })
                                    <div class="col-md-4 padding-left-none">
                                        <div class="input-group">
                                            <span class="input-group-addon">
                                                <i class="far fa-calendar alt"></i>
                                            </span>
                                            @Html.TextBox("DateTo", null, HtmlAttributesHelper.CreateInputAttributes("form-control datepicker", "DateTo", false, Loc.To))
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("ObjectPointOfSale", Loc.ObjectPointOfSale, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-9">
                                        @Html.DropDownList("ObjectPointOfSale", new SelectList(ViewBag.Departments != null ? ViewBag.Departments : new List<string>(), "Id", "Name"), Loc.Select,
                                            HtmlAttributesHelper.CreateInputAttributes("form-control custom-select select-filter", "ObjectPointOfSale", false, Loc.ObjectPointOfSale, true))
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("Warehouse", Loc.WarehouseKEPU, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-9">
                                        @Html.DropDownList("Warehouse", new SelectList(ViewBag.Warehouses != null ? ViewBag.Warehouses : new List<string>(), "Id", "Name"), Loc.Select,
                                            HtmlAttributesHelper.CreateInputAttributes("form-control custom-select select-filter", "Warehouse", false, Loc.WarehouseKEPU, true))
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("Status", Loc.Status, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-9">
                                        @Html.DropDownList("Status", new List<SelectListItem>() {
                                            new SelectListItem{ Text = Loc.All, Value = "0" },
                                            new SelectListItem{ Text = Loc.ConfirmedStatus, Value = "1" },
                                            new SelectListItem{ Text = Loc.NotConfirmedStatus, Value = "2" },
                                        }, HtmlAttributesHelper.CreateInputAttributes("form-control custom-select select-filter", "Status", false, Loc.Status))
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("DateOption", Loc.DateToConsider, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-9">
                                        @Html.DropDownList("Status", new List<SelectListItem>() {
                                            new SelectListItem{ Text = Loc.DeliveryNotes, Value = "0" },
                                            new SelectListItem{ Text = Loc.IssuingOrReceiptOfInvoice, Value = "1" },
                                        }, HtmlAttributesHelper.CreateInputAttributes("form-control custom-select select-filter", "DateOption", false, Loc.DateToConsider))
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("PriceType", Loc.PriceType, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-9">
                                        @Html.DropDownList("PriceType", new List<SelectListItem>() {
                                            new SelectListItem{ Text = Loc.PurchasePrice, Value = "0" },
                                            new SelectListItem{ Text = Loc.SellingPriceWithTax, Value = "1" },
                                            new SelectListItem{ Text = Loc.SellingPriceWithoutTax, Value = "2" },
                                            new SelectListItem{ Text = Loc.WholesalePriceOne, Value = "3" },
                                            new SelectListItem{ Text = Loc.WholesalePriceTwo, Value = "4" },
                                            new SelectListItem{ Text = Loc.WholesalePriceOneWithTax, Value = "5" },
                                            new SelectListItem{ Text = Loc.WholesalePriceTwoWithTax, Value = "6" },
                                        }, HtmlAttributesHelper.CreateInputAttributes("form-control custom-select select-filter", "PriceType", false, Loc.PriceType))
                                    </div>
                                </div>
                                <div class="form-group" id="searchPriceTypeContainer" hidden>
                                    @Html.Label("SearchPriceType", Loc.HowToFindSalePrice, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-9">
                                        @Html.DropDownList("SearchPriceType", new List<SelectListItem>() {
                                            new SelectListItem{ Text = Loc.PriceFromPriceList, Value = "0" },
                                            new SelectListItem{ Text = Loc.PriceFromDocumentPosition, Value = "1" },
                                            new SelectListItem{ Text = Loc.ActualSellingPrice, Value = "2" }
                                        }, HtmlAttributesHelper.CreateInputAttributes("form-control custom-select select-filter", "SearchPriceType", false, Loc.HowToFindSalePrice))
                                    </div>
                                </div>
                            </div>

                            <div class="col-lg-6">
                                <div class="form-group">
                                    @Html.Label("DocType", Loc.DocumentTypes, new { @class = "col-md-4 control-label" })
                                    <div class="col-md-8">
                                        @Html.DropDownList("DocType", new SelectList(ViewBag.DocTypes != null ? ViewBag.DocTypes : new List<string>(), "Id", "Value"), Loc.Select,
                                            HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker selectpicker-filter", "DocType", false, Loc.DocumentTypes, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.All }))
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("SetOfItem", Loc.ItemTypes, new { @class = "col-md-4 control-label" })
                                    <div class="col-md-8">
                                        @Html.DropDownList("SetOfItem", new SelectList(ViewBag.SetItemTypes != null ? ViewBag.SetItemTypes : new List<string>(), "Id", "Display"), Loc.Select,
                                            HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker selectpicker-filter", "SetOfItem", false, Loc.ItemTypes, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.All }))
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("DocTypeNegProcurement", Loc.TypeOfDocSalesWhichIsShownAsNegProcurement, new { @class = "col-md-4 control-label" })
                                    <div class="col-md-8">
                                        @Html.DropDownList("DocTypeNegProcurement", new SelectList(ViewBag.DocTypesNegProcurement != null ? ViewBag.DocTypesNegProcurement : new List<string>(), "Value", "Value"), Loc.Select,
                                            HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker selectpicker-filter", "DocTypeNegProcurement", false, Loc.TypeOfDocSalesWhichIsShownAsNegProcurement,
                                            true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.All }))
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("MergeInvoices", Loc.MergeInvoicesInOneDay, new { @class = "col-md-4 control-label" })
                                    <div class="col-md-8 selectpicker-container m-t-xs">
                                        @Html.CheckBox("MergeInvoices", new { @id = "MergeInvoices", @class = "form-control m-t-xs js-switch kep-js-switch" })
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("CollectiveDocumentsOnly", Loc.CollectiveDocumentsOnly, new { @class = "col-md-4 control-label" })
                                    <div class="col-md-8 selectpicker-container m-t-xs">
                                        @Html.CheckBox("CollectiveDocumentsOnly", new { @id = "CollectiveDocumentsOnly", @class = "form-control m-t-xs js-switch kep-js-switch" })
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<input type="hidden" id="RepeatAnimation" value="@Url.Content("~/Content/Animations/DocumentLoading.gif")" />
<input type="hidden" id="LoadingDocumentLoc" value="@Loc.LoadingDocument" />
<input type="hidden" id="ConfirmLoc" value="@Loc.Confirm" />

@section pageScripts{
    <script>
        $(document).ready(function () {
            setDefaultPrintType();
            $("#printButton").prop("disabled", false);
            $("#printButton + button").prop("disabled", false);

            $('.datepicker').datepicker()
                .on('click', function (e) {
                    $(this).select();
                }).on('changeDate', function () {
                    $(this).datepicker('hide');
                }).on('change', function () {
                    $(this).datepicker('hide');
                });

            var elems = Array.prototype.slice.call(document.querySelectorAll('.kep-js-switch'));
            elems.forEach(function (html) {
                var switchery = new Switchery(html, { size: 'small' });
            });

            $("#PriceType").on('change', function () {
                $("#searchPriceTypeContainer").attr("hidden", $("#PriceType").val() < 1);
            });
        });

        function format(item) {
            return item.Text;
        }

        function setDefaultPrintType() {
            var printParts = $("#LastPrint").val() ? $("#LastPrint").val().split("_") : "";
            if (printParts && printParts.length > 1) {
                var lastPrint = printParts[1];
                selectPrintType($(".preview-print-option[data-print='" + lastPrint + "']"), lastPrint);
            } else
                selectPrintType();
        }

        var printWindow;
        function printKEP() {
            swapLoadingIcon(true, "printButton", "far fa-print");

            var url = "@Url.Action("IssuedInvoicesBillingKEPPrint", "Reports")"
                + "?DateFrom=" + reformatDate($("#DateFrom").val())
                + "&DateTo=" + reformatDate($("#DateTo").val())
                + "&Warehouse=" + $("#Warehouse option:selected").val()
                + "&ObjectPointOfSale=" + $("#ObjectPointOfSale option:selected").val()
                + "&Status=" + $("#Status option:selected").val()
                + "&DateOption=" + $("#DateOption option:selected").val()
                + "&PriceType=" + $("#PriceType option:selected").val()
                + "&MergeInvoices=" + $("#MergeInvoices").is(':checked')
                + "&CollectiveDocumentsOnly=" + $("#CollectiveDocumentsOnly").is(':checked')
                + "&PrintType=" + $("#ReportType").val();
                + "&SearchPriceType=" + $("#SearchPriceType").val();

            var docType = $("#DocType").val() ? $("#DocType").val().toString() : "";
            var setOfItem = $("#SetOfItem").val() ? $("#SetOfItem").val().toString() : "";
            var docTypeNegProcurement = $("#DocTypeNegProcurement").val() ? $("#DocTypeNegProcurement").val().toString() : "";
            url = url + "&DocType=" + docType + "&SetOfItem=" + setOfItem + "&DocTypeNegProcurement=" + docTypeNegProcurement;

            printWindow = createPrintWindow(printWindow);

            $.get(url,
                function (data) {
                    if (data) {
                        printPAAS(null, false, false, url, false);
                    }
                    else {
                        printWindow.close();
                        $("#errorModalMessage").text('@Loc.PrintingError');
                        $("#errorModal").modal('show');
                    }
                    swapLoadingIcon(false, "printButton", "far fa-print");
                }).fail(function () {
                    swapLoadingIcon(false, "printButton", "far fa-print");
                    printWindow.close();
                    $("#errorModalMessage").text('@Loc.GeneralError');
                    $("#errorModal").modal('show');
                });
        }

        function swapLoadingIcon(loading, buttonId, icon) {
            var $button = $("#" + buttonId);
            var $buttonIcon = $button.find("i");
            var $buttonGroupButtons = $button.parent().find("button");

            if (loading) {
                $buttonIcon.removeClass(icon).addClass("fa fa-spinner fa-spin fa-fw");
                $buttonGroupButtons.attr("disabled", true);
            }
            else {
                $buttonIcon.removeClass("fa fa-spinner fa-spin fa-fw").addClass(icon);
                $buttonGroupButtons.removeAttr("disabled");
            }
        }

        function selectPrintType($element, type) {
            if (!$element || $element.length <= 0) {
                $element = $(".preview-print-option").first();
                type = $element.attr("data-print");
            }

            $("#printOptions div.custom-option-selected").removeClass("custom-option-selected");
            $($element).addClass("custom-option-selected");
            $("#ReportType").val(type);
        }

        function clearFiltersKEP() {
            $("#DateFrom").val("");
            $("#DateTo").val("");
            $("#printOptions div.custom-option-selected").removeClass("custom-option-selected");
            $("#ReportType").val("");
            $('.select-filter').prop('selectedIndex', 0);
            $(".selectpicker-filter").val('default').selectpicker("refresh");
            if ($("#MergeInvoices").is(':checked')) $("#MergeInvoices").click();
            if ($("#CollectiveDocumentsOnly").is(':checked')) $("#CollectiveDocumentsOnly").click();
        }
    </script>
}