@using PantheonRA.BackOffice.Enums
@using PantheonRA.BackOffice.Helpers
@model PantheonRA.BackOffice.Models.BalanceViewModel
@{
    ViewBag.Title = Loc.Balances;
}

<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="printBalances()" 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="clearFilters()" 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">
                <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">
                                @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("BalanceType", Loc.BalanceType, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-9">
                                        @Html.DropDownList("BalanceType", new SelectList(ViewBag.BalanceTypes != null ? ViewBag.BalanceTypes : new List<string>(), "Id", "Display"), Loc.Select,
                                            HtmlAttributesHelper.CreateInputAttributes("form-control custom-select select-filter", "BalanceType", false, Loc.BalanceType, true))
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("DocType", Loc.DocumentTypes, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-9">
                                        @Html.DropDownList("DocType", new SelectList(ViewBag.DocTypes != null ? ViewBag.DocTypes : new List<string>(), "Id", "Name"), 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("Konto", Loc.Konto, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-9">
                                        @Html.DropDownList("Konto", new SelectList(ViewBag.Accts != null ? ViewBag.Accts : new List<string>(), "Id", "Display"), Loc.Select,
                                            HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker selectpicker-filter", "Konto", false, Loc.Konto, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.Select }))
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("Subject", Loc.Subject, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-9">
                                        <input type="hidden" id="partnerPlaceholder" title="@Loc.Partner" />
                                        <input type="text" class="typeahead_3 form-control" id="SubjectFilterSearch" dataUrl="@Url.Action("SearchSubjects", "Reports")" placeholder="@Loc.Partner" autocomplete="off" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("Department", Loc.Department, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-9">
                                        @Html.DropDownList("Department", new SelectList(ViewBag.Departments != null ? ViewBag.Departments : new List<string>(), "Id", "Name"), Loc.Select,
                                            HtmlAttributesHelper.CreateInputAttributes("form-control custom-select select-filter", "Department", false, Loc.Department, true))
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("CostCarrier", Loc.CostCarrierShort, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-9">
                                        @Html.DropDownList("CostCarrier", new SelectList(ViewBag.CostCarriers != null ? ViewBag.CostCarriers : new List<string>(), "Id", "Display"), Loc.Select,
                                            HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker selectpicker-filter", "CostCarrier", false, Loc.CostCarrier, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.Select }))
                                    </div>
                                </div>
                            </div>

                            <div class="col-lg-6">
                                <div class="form-group">
                                    @Html.Label("ForCurrency", Loc.ForCurrency, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-3">
                                        @Html.DropDownList("ForCurrency", new SelectList(ViewBag.Currencies != null ? ViewBag.Currencies : new List<string>(), "Id", "Name"), Loc.Select,
                                            HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker selectpicker-filter", "ForCurrency", false, Loc.DocumentTypes, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.All }))
                                    </div>
                                    @Html.Label("ToCurrency", Loc.ToCurrency, new { @class = "col-md-2 control-label" })
                                    <div class="col-md-3 padding-left-none">
                                        @Html.DropDownList("ToCurrency", new SelectList(ViewBag.Currencies != null ? ViewBag.Currencies : new List<string>(), "Id", "Name"), Loc.Select,
                                            HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker selectpicker-filter", "ToCurrency", false, Loc.DocumentTypes, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.All }))
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("PeriodFrom", Loc.PeriodFrom, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-3">
                                        <div class="input-group">
                                            <span class="input-group-addon">
                                                <i class="far fa-calendar alt"></i>
                                            </span>
                                            @Html.TextBox("PeriodFrom", null, HtmlAttributesHelper.CreateInputAttributes("form-control datepicker", "PeriodFrom", false, Loc.PeriodFrom))
                                        </div>
                                    </div>
                                    @Html.Label("PeriodTo", Loc.To, new { @class = "col-md-2 control-label" })
                                    <div class="col-md-3 padding-left-none">
                                        <div class="input-group">
                                            <span class="input-group-addon">
                                                <i class="far fa-calendar alt"></i>
                                            </span>
                                            @Html.TextBox("PeriodTo", null, HtmlAttributesHelper.CreateInputAttributes("form-control datepicker", "PeriodTo", false, Loc.To))
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    @Html.Label("PeriodDate2From", Loc.PeriodDate2From, new { @class = "col-md-3 control-label" })
                                    <div class="col-md-3">
                                        <div class="input-group">
                                            <span class="input-group-addon">
                                                <i class="far fa-calendar alt"></i>
                                            </span>
                                            @Html.TextBox("PeriodDate2From", null, HtmlAttributesHelper.CreateInputAttributes("form-control datepicker", "PeriodDate2From", false, Loc.PeriodDate2From))
                                        </div>
                                    </div>
                                    @Html.Label("PeriodDate2To", Loc.To, new { @class = "col-md-2 control-label" })
                                    <div class="col-md-3 padding-left-none">
                                        <div class="input-group">
                                            <span class="input-group-addon">
                                                <i class="far fa-calendar alt"></i>
                                            </span>
                                            @Html.TextBox("PeriodDate2To", null, HtmlAttributesHelper.CreateInputAttributes("form-control datepicker", "PeriodDate2To", false, Loc.To))
                                        </div>
                                    </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 () {
            $("#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');
                });

            initializeBasicPartnerTypeahead($('#SubjectFilterSearch').attr('dataUrl'), $('#SubjectFilterSearch'), $("#SubjectFilterSearch"), function (item) {
                $("#partnerPlaceholder").val(item.value);
                $("#SubjectFilterSearch").val(item.value);
                return { selected: true, value: item.value };
            });

            if ($("#DocType option:selected").attr("data-defaultReport"))
                setDefaultPrintType();
            else {
                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();
            }
        });

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

        var printWindow;
        function printBalances() {
            if (!$("#PeriodFrom").val() || !$("#PeriodTo").val()) {
                $().customModal({
                    destroyOnClose: true,
                    showCloseButton: true,
                    type: "alert",
                    text: "@Loc.PeriodIsRequired",
                    buttons: [
                        {
                            icon: 'fa fa-check',
                            text: "@Loc.Confirm",
                            closeOnAction: true,
                            type: 'primary'
                        }
                    ]
                });
                return;
            }

            swapLoadingIcon(true, "printButton", "far fa-print");

            var url = "@Url.Action("BalancesPrint", "Reports")"
                + "?PeriodFrom=" + reformatDate($("#PeriodFrom").val())
                + "&PeriodTo=" + reformatDate($("#PeriodTo").val())
                + "&PeriodDate2From=" + reformatDate($("#PeriodDate2From").val())
                + "&PeriodDate2To=" + reformatDate($("#PeriodDate2To").val())
                + "&Subject=" + $("#partnerPlaceholder").val()
                + "&BalanceType=" + $("#BalanceType").val()
                + "&ReportType=" + $("#ReportType").val();

            var docType = $("#DocType").val() ? $("#DocType").val().toString() : "";
            var department = $("#Department").val() ? $("#Department").val().toString() : "";
            var costCarrier = $("#CostCarrier").val() ? $("#CostCarrier").val().toString() : "";
            var konto = $("#Konto").val() ? $("#Konto").val().toString() : "";
            var forCurr = $("#ForCurrency").val() ? $("#ForCurrency").val().toString() : "";
            var toCurr = $("#ToCurrency").val() ? $("#ToCurrency").val().toString() : "";

            url = url + "&DocType=" + docType + "&Department=" + department + "&CostCarrier=" + costCarrier + "&Konto=" + konto + "&ForCurrency=" + forCurr + "&ToCurrency=" + toCurr;
            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 clearFilters() {
            $("#PeriodFrom").val("");
            $("#PeriodTo").val("");
            $("#PeriodDate2From").val("");
            $("#PeriodDate2To").val("");
            $("#printOptions div.custom-option-selected").removeClass("custom-option-selected");
            $("#ReportType").val("");
            $('.select-filter').prop('selectedIndex', 0);
            $(".selectpicker-filter").val('default').selectpicker("refresh");
            $("#SubjectFilterSearch").val("");
            $("#partnerPlaceholder").val("");
        }
    </script>
}