@model PantheonRA.BackOffice.Models.PayOrdersGridFilterModel
@using PantheonRA.BackOffice.Models
@using PantheonRA.BackOffice.Enums
@using PantheonRA.WebApi.Contracts
@using PantheonRA.BackOffice.Helpers

@{
    ViewBag.Title = "Index";
}


@Html.Hidden("ChangeBank", Url.Action("ChangeBank", "PayOrders"))
@Html.Hidden("GetBankAccountsAsync", Url.Action("GetBankAccountsAsync", "PayOrders"))
@Html.Hidden("ChangeStatus", Url.Action("ChangeStatus", "PayOrders"))
@Html.Hidden("SubjectInfo", Url.Action("SubjectInfo", "PayOrders"))
@Html.Hidden("GetTotals", Url.Action("GetPayOrderTotals", "PayOrders"))
@Html.Hidden("PayOrderCreateUrl", Url.Action("Create", "PayOrders"))
@Html.Hidden("PayOrderEditUrl", Url.Action("Edit", "PayOrders"))
@Html.Hidden("TxtConfirm", Loc.Confirm)
@Html.Hidden("TxtInfo", Loc.Info)
@Html.Hidden("TxtPayOrderStatusChangedInfo", Loc.PayOrderStatusChangedInfo)
@Html.Hidden("PacOrdersIncomplete", Loc.PacOrdersIncomplete)
@Html.Hidden("OpenAcKeyPanel", Model.AcKey)
@Html.HiddenFor(model => model.DefaultDecimalPlacesPrice)
@Html.HiddenFor(model => model.DefaultDecimalPlacesQuantity)
@Html.HiddenFor(model => model.DefaultDecimalPlacesValue)


<div class="wrapper wrapper-content">
    <div class="tabs-container">
        @*@Html.Partial("_PayOrdersTabsNavigation")*@
        <ul class="nav nav-tabs horizontal-menu custom-scrollable-tabs">
            <li class="nav-item @(Model.Status == "Z" ? "active" : string.Empty)">
                <a data-toggle="tab" href="#other" class="local-tab"
                   onclick="setSelectedStatus('Z'); setGlobalHelpUrl('@SiteHelpAttributeHelper.GetHelpUrl(HelpIds.PaymentOrdersInPreparation)');
                   setGlobalExampleHelpUrl('@SiteHelpAttributeHelper.GetHelpUrl(HelpIds.ExamplePaymentOrdersInPreparation)');">
                    @Loc.InProgress <span id="in-progress-count" class="badge"></span>
                </a>
            </li>
            <li class="nav-item @(Model.Status == "P" ? "active" : string.Empty)">
                <a data-toggle="tab" href="#other" class="local-tab"
                   onclick="setSelectedStatus('P'); setGlobalHelpUrl('@SiteHelpAttributeHelper.GetHelpUrl(HelpIds.PaymentOrdersInPayment)');
                   setGlobalExampleHelpUrl('@SiteHelpAttributeHelper.GetHelpUrl(HelpIds.ExamplePaymentOrdersInPayment)');">
                    @Loc.OnPayment <span id="on-payment-count" class="badge"></span>
                </a>
            </li>
            <li class="nav-item @(Model.Status == "O" ? "active" : string.Empty)">
                <a data-toggle="tab" href="#other" class="local-tab"
                   onclick="setSelectedStatus('O'); setGlobalHelpUrl('@SiteHelpAttributeHelper.GetHelpUrl(HelpIds.SentPaymentOrders)');
                   setGlobalExampleHelpUrl('@SiteHelpAttributeHelper.GetHelpUrl(HelpIds.ExampleSentPaymentOrders)');">
                    @Loc.Sent <span id="sent-count" class="badge"></span>
                </a>
            </li>
            <li class="nav-item @(Model.Status == "R" ? "active" : string.Empty)">
                <a data-toggle="tab" href="#other" class="local-tab"
                   onclick="setSelectedStatus('R'); setGlobalHelpUrl('@SiteHelpAttributeHelper.GetHelpUrl(HelpIds.RealizedPaymentOrders)');
                   setGlobalExampleHelpUrl('@SiteHelpAttributeHelper.GetHelpUrl(HelpIds.ExampleRealizedPaymentOrders)');">
                    @Loc.Realized <span id="realized-count" class="badge"></span>
                </a>
            </li>
            @{
                var errors = TempData["DownloadXml"] as IEnumerable<PayOrderXmlError>;

                var errorCnt = errors != null ? errors.Count() : 0;
            }
            <li class="nav-item @(Model.Status == PayOrderStatusCode.Errors ? "active" : string.Empty)">
                <a data-toggle="tab" href="#other" class="local-tab"
                   onclick="setSelectedStatus('ERR'); setGlobalHelpUrl('@SiteHelpAttributeHelper.GetHelpUrl(HelpIds.Mistakes)');
                   setGlobalExampleHelpUrl('@SiteHelpAttributeHelper.GetHelpUrl(HelpIds.ExampleMistakes)');">
                    @Loc.ExportErrors <span class="badge">@errorCnt </span>
                </a>
            </li>
        </ul>

        <div class="row buttons-tabbed">
            @Html.Partial("_FormButtons", new FormButtonsViewModel
           {
               ShowCloseBtn = false,
               BackUrl = Url.Action("Index", "PayOrders"),
               CreateButtonGroup = new FormButtonGroup
               {
                   IsButtonName = true,
                   ButtonName = Loc.NewAccount,
                   Buttons = new List<FormButton>
                            {
                                new FormButton { OnClick = "openPayOrderPanel()" }
                            }
               },
               BackButtonGroup = new FormButtonGroup
               {
                   IsActive = false,
                   IsButtonName = true,
                   ButtonName = Loc.ReturnToPreparation,
                   Buttons = new List<FormButton>
                            {
                                new FormButton { Class = "btnBack changeStatus", CustomAttr = "data-back-status=true" }
                            }
               },
               ForwardButtonGroup = new FormButtonGroup
               {
                   IsActive = false,
                   IsButtonName = true,
                   ButtonName = Loc.ConfirmPayorder,
                   Buttons = new List<FormButton>
                            {
                                new FormButton { Class = "btnNext changeStatus", CustomAttr = "data-back-status=false" }
                            }
               },
               ExportXmlButtonGroup = new FormButtonGroup
               {
                   Buttons = new List<FormButton>
                            {
                                new FormButton { OnClick="exportXml()", Class = "disabled" }
                            }
               },
               DeleteButtonGroup = new FormButtonGroup
               {
                   IsActive = false,
                   Buttons = new List<FormButton>
                            {
                                new FormButton { OnClick = "deletePayOrder()", Class="disabled"  }
                            }
               }
           })
        </div>

        <div class="divided-content">

            <div class="divided-content-main">
                <div class="tab-content">
                    <div class="project-list pay-orders-list-content">
                        <!-- main -->
                        <div class="document-list" id="PayOrdersList">
                            @{Html.RenderAction("PayOrdersIndex", "PayOrders", new { acKey = Model.AcKey, status = Model.Status, docType = Model.DocumentType });}
                        </div>
                        <!--footer-->
                        <div id="payOrdersFooter" class="row document-footer document-footer-sm">
                            <div class="col-md-10 col-xs-6">
                                <div class="form-group">
                                    @if (Model.Status == "Z")
                                    {
                                        <span>@Loc.InProgress:</span>
                                    }
                                    @if (Model.Status == "P")
                                    {
                                        <span>@Loc.OnPayment:</span>
                                    }
                                    <span class="status-count font-bold"></span>
                                </div>
                            </div>
                            <div class="col-md-2 col-xs-6 document-head-highlight-input">
                                <div class="form-group text-center">
                                    <span>@Loc.Total: </span><span class="status-amount font-bold"></span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="hidden">
                        <label id="filterColumn"></label>
                        <label id="filterCriteria"></label>
                        <label id="filterValue"></label>
                        <label id="filterValue2"></label>
                    </div>
                </div>
            </div>

            <div id="PayOrdersBankAccountsList" class="divided-content-side">
                @{Html.RenderAction("BankAccountsIndex", "PayOrders");}
            </div>

        </div>
    </div>
</div>

@Html.Partial("_IRIS")

<div class="wrapper-content">
    @Html.Partial("~/Views/Shared/Components/_sidePanel.cshtml", new PantheonRA.BackOffice.Models.SidePanelViewModel { Id = "payOrderPanel", HandleText = Loc.PayOrderTitle })
</div>
@Scripts.Render("~/bundles/moment")

@* TODO: remove after refactor *@
@section pageScripts{
    @Scripts.Render("~/bundles/validation")
    <script src="~/Scripts/global-script.js"></script>
    <script>
        function openPayOrderPanel(acKey) {
            let url = acKey ? $("#PayOrderEditUrl").val() + "?acKey=" + acKey : $("#PayOrderCreateUrl").val();

            $('.bank-selected').removeClass('bank-selected');
            $("#Lnk" + acKey).parent().parent().addClass('bank-selected');

            loadSidePanelContent($("#payOrderPanel"), url, true);
        }

        $(document).ready(function () {
            let acKey = '@Model.AcKey';

            if (acKey) {
                $("#OpenAcKeyPanel").val(acKey);
                openPayOrderPanel(acKey);
            }
        });
    </script>
}