@model PantheonRA.BackOffice.Models.TravelOrdersGridFilterModel
@using PantheonRA.BackOffice.Models
@using PAW.Contracts.Enums

@{
    ViewBag.Title = Loc.TravelOrders;
    var currentLoc = Loc.CurrentLocalization;
    var previewParams = string.Empty;
    var previewLocals = string.Empty;
}

@Html.Hidden("Print", Url.Action("PrintMultiplePDF", "TravelOrders"))
@Html.Hidden("Copy", Url.Action("Copy", "TravelOrders"))
@Html.Hidden("Delete", Url.Action("Delete", "TravelOrders"))
@Html.Hidden("DeleteStatus", Url.Action("GetDeleteStatuses", "TravelOrders"))
@Html.Hidden("DeleteTravelOrder", Loc.DeleteTravelOrder)
@Html.Hidden("DeleteTravelOrders", Loc.DeleteTravelOrders)
@Html.Hidden("Confirm", Loc.Confirm)
@Html.Hidden("Cancel", Loc.Cancel)
@Html.Hidden("All", Loc.All)
@Html.HiddenFor(model => model.DefaultDecimalPlacesPrice)
@Html.HiddenFor(model => model.DefaultDecimalPlacesQuantity)
@Html.HiddenFor(model => model.DefaultDecimalPlacesValue)

@if (TempData["NotDeletedTravelOrder"] != null)
{
    <div class="alert alert-danger no-border">
        <button type="button" class="close" data-dismiss="alert"><span>×</span><span class="sr-only">@Loc.Close</span></button>
        <span class="text-semibold">@Loc.NotDeleted @TempData["NotDeletedTravelOrder"]</span>
    </div>
}

<div class="wrapper wrapper-content">
    <div class="tabs-container">
        @Html.Partial("~/Views/TravelOrders/_TravelOrdersTabNavigation.cshtml")

        <div class="tab-content">
            <div class="row buttons-tabbed">
                @if (currentLoc == Localization.Code.HR ||
                     currentLoc == Localization.Code.BA ||
                     currentLoc == Localization.Code.MK)
                {
                    previewParams = "loc=" + currentLoc;
                    previewLocals = string.Format("{0},{1}", Loc.PrintTravelOrder, currentLoc.GetTravelOrderReportName());

                    <div class="btn-group dropdown pull-right">
                        <button type="button" disabled class="btn btn-primary printDropdown" onClick="printTravelOrder()" title="@Loc.Print">
                            <i class="far fa-print print-loader-icon"></i><span class="visible-lg-inline visible-md-inline m-l-xs">@Loc.Print</span>
                        </button>
                        <button type="button" disabled class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu">
                            <li class="print-option"><a onClick="printTravelOrder()"><i class="far fa-print"></i>&nbsp;&nbsp; @Loc.PrintTravelOrder</a></li>
                            <li class="print-option"><a onClick="printTravelOrder('@currentLoc')"><i class="far fa-print"></i>&nbsp;&nbsp; @currentLoc.GetTravelOrderReportName()</a></li>
                        </ul>
                    </div>
                }
                else
                {
                    <button type="button" class="btn btn-primary pull-right printDropdown" onClick="printTravelOrder()" title="@Loc.Print" disabled>
                        <i class="far fa-print print-loader-icon"></i><span class="visible-lg-inline visible-md-inline m-l-xs">@Loc.Print</span>
                    </button>
                }
                @Html.Partial("_FormButtons", new FormButtonsViewModel
                {
                 ShowCloseBtn = false,
                 CreateButtonGroup = new FormButtonGroup
                 {
                     IsButtonName = true,
                     ButtonName = Loc.NewAccount,
                     Buttons = new List<FormButton>
                          {
                              new FormButton { Url = Url.Action("Create", "TravelOrderAllData") }
                          }
                 },

                 DeleteButtonGroup = new FormButtonGroup
                 {
                     IsActive = false,
                     Buttons = new List<FormButton>
                          {
                              new FormButton { OnClick = "deleteMultipleTravelOrders()", Class="disabled"  }
                          }
                 },

                 DuplicateButtonGroup = new FormButtonGroup
                 {
                     IsActive = false,
                     Buttons = new List<FormButton>
                          {
                              new FormButton { OnClick = "copyTravelOrder()", Class = "btn btn-default btn-outline disabled" }
                          }
                 }

             })
            </div>

            <div class="mobile-scroll-wrapper">
                <div class="row document-head">
                    <div class="col-md-1 col-sm-6 col-xs-6 @(PantheonRA.BackOffice.CurrentUser.HasGranule(PantheonRA.WebApi.Contracts.Enums.Granules.TravelOrderGranule) ? "hidden" : string.Empty)">
                        <div class="form-group selectpicker-dropdown">
                            @Html.LabelFor(model => model.TravelOrdersFilterData.DocTypes, Loc.DocumentType, new { @class = "control-label" })
                            @Html.DropDownListFor(model => model.TravelOrdersFilterData.DocTypes,
                                new SelectList(Model.TravelOrdersFilterData.DocTypes, "DocType", "DocName"), Loc.All,
                                HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker",
                                    "DocumentTypes", false, Loc.DocumentType, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.All }))
                        </div>
                    </div>
                    <div class="col-md-2 col-sm-6 col-xs-6 @(PantheonRA.BackOffice.CurrentUser.HasGranule(PantheonRA.WebApi.Contracts.Enums.Granules.TravelOrderGranule) ? "hidden" : string.Empty)">
                        <div class="form-group selectpicker-dropdown">
                            @Html.LabelFor(model => model.TravelOrdersFilterData.Employees, Loc.Worker, new { @class = "control-label" })

                            @Html.DropDownList("Workers",
                                new SelectList(Model.TravelOrdersFilterData.Employees, "WorkerCode", "WorkerName",
                                PantheonRA.BackOffice.CurrentUser.HasGranule(PantheonRA.WebApi.Contracts.Enums.Granules.TravelOrderGranule) ? PantheonRA.BackOffice.CurrentUser.EmployeeId : null), Loc.All,
                                HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker",
                                    "Workers", false, Loc.Worker, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.All }))
                        </div>
                    </div>
                    <div class="col-md-2 col-sm-6 col-xs-6">
                        <div class="form-group selectpicker-dropdown">
                            @Html.LabelFor(model => model.TravelOrdersFilterData.Departments, Loc.Department, new { @class = "control-label" })
                            @Html.DropDownListFor(model => model.TravelOrdersFilterData.Departments,
                                new SelectList(Model.TravelOrdersFilterData.Departments.Select(q => q.Name)), Loc.All,
                                HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker",
                                    "Departments", false, Loc.Department, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.All }))
                        </div>
                    </div>
                    <div class="col-md-2 col-sm-6 col-xs-6">
                        <div class="form-group selectpicker-dropdown">
                            @Html.LabelFor(model => model.TravelOrdersFilterData.Statuses, Loc.Status, new { @class = "control-label" })
                            @Html.DropDownListFor(model => model.TravelOrdersFilterData.Statuses,
                                new SelectList(Model.TravelOrdersFilterData.Statuses.Select(q => q.Name)), Loc.All,
                                HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker",
                                    "Statuses", false, Loc.Status, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.All }))
                        </div>
                    </div>
                    <div class="col-md-2 col-sm-6 col-xs-6">
                        <div class="form-group selectpicker-dropdown">
                            @Html.LabelFor(model => model.TravelOrdersFilterData.Vehicles, Loc.Vehicle, new { @class = "control-label" })
                            @Html.DropDownListFor(model => model.TravelOrdersFilterData.Vehicles,
                                new SelectList(Model.TravelOrdersFilterData.Vehicles, "CarRegNo", "Name"), Loc.All,
                                HtmlAttributesHelper.CreateInputAttributes("form-control selectpicker",
                                    "Vehicles", false, Loc.Vehicle, true, new { @multiple = "multiple", data_live_search = "true", @title = Loc.All }))
                        </div>
                    </div>
                    <div class="col-md-1 col-sm-6 col-xs-6">
                        <div class="form-group">
                            @Html.LabelFor(model => model.TravelDateFrom, Loc.DateFrom, new { @class = "control-label" })
                            <div class="input-group">
                                <span class="input-group-addon"><i class="far fa-calendar alt"></i></span>
                                @Html.TextBox("TravelDateFrom", DateTime.Now.Date.AddMonths(-1).ToString(Loc.DateFormat), new { @id = "TravelDateFrom", @class = "form-control filter-datepicker", @autocomplete = "off" })
                            </div>
                        </div>
                    </div>
                    <div class="col-md-1 col-sm-6 col-xs-6">
                        <div class="form-group">
                            @Html.LabelFor(model => model.TravelDateTo, Loc.DateTo, new { @class = "control-label" })
                            <div class="input-group">
                                <span class="input-group-addon"><i class="far fa-calendar alt"></i></span>
                                @Html.TextBox("TravelDateTo", DateTime.Now.Date.ToString(Loc.DateFormat), new { @id = "TravelDateTo", @class = "form-control filter-datepicker", @autocomplete = "off" })
                            </div>
                        </div>
                    </div>
                    <div class="@(PantheonRA.BackOffice.CurrentUser.HasGranule(PantheonRA.WebApi.Contracts.Enums.Granules.TravelOrderGranule) ? "col-md-4 col-sm-6 col-xs-6" : "col-md-1 col-sm-6 col-xs-6")">
                        <div class="form-group">
                            <label class="control-label">@Loc.SearchFilter</label>
                            @Html.TextBoxFor(model => model.Keywords, new { @id = "Keywords", @class = "form-control", @placeholder = Loc.Search, autocomplete = "off" })
                            <span class="filter-search-icon"><i class="fas fa-search"></i></span>
                        </div>
                    </div>
                </div>

                <div class="project-list mobile-content">
                    <table id="travelorders" data-grid-id="travelorders" data-url="@Url.Action("TravelOrdersList", "TravelOrders")" class="list-table">
                        <thead>
                            <tr>
                                <th width="2%" class="dt-body-center"></th>
                                <th width="2%" data-function="openPdfPreview('@Url.Action("PrintPDF", "TravelOrders")?acKey={0}&loc=@Loc.CurrentLocalization')" data-params="@previewParams" data-local="@previewLocals"></th>
                                <th width="14%" data-url="@Url.Action("Edit","TravelOrderAllData")?acKey=">@Loc.AcKey</th>
                                <th width="10%">@Loc.DateOfOrder</th>
                                <th width="10%">@Loc.Worker</th>
                                <th width="9%" data-default-hidden="true">@Loc.DateOfArrival</th>
                                <th width="9%" data-default-hidden="true">@Loc.DateOfDeparture</th>
                                <th width="9%">@Loc.Expenses</th>
                                <th width="9%">@Loc.Vehicle</th>
                                <th width="9%">@Loc.Department</th>
                                <th width="9%">@Loc.Payer</th>
                                <th width="9%">@Loc.Status</th>
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

@Html.Partial("~/Views/Shared/Components/_sidePanel.cshtml", new PantheonRA.BackOffice.Models.SidePanelViewModel { Id = "pdfPanel" })

@* TODO: remove after refactor *@
@section pageScripts{
    @Scripts.Render("~/bundles/validation")
    <script src="~/Scripts/global-script.js"></script>
    <script src="~/Scripts/custom/travelorders/travelorders.js"></script>

    <script>
        $(document).ready(function () {
            initializeTravelOrders();
        });

        function onFilterTravelOrder() {
            $.fn.dataTable.reload($dataTable);
        }

        function openPdfPreview(url) {
            loadPdfPanelContent($("#pdfPanel"), url, true);
        }
    </script>
}

<style>
    .select2-selection__rendered {
        border-bottom: unset !important;
    }

    .select2-search__field {
        padding: unset !important;
    }
</style>
