@model AddProductToOrderSearchModel

@{
    //page title
    ViewBag.PageTitle = string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderId);
    //active menu item (system name)
    NopHtml.SetActiveMenuItemSystemName("Orders");
}

@{
    const string hideSearchBlockAttributeName = "AddProductToOrderPage.HideSearchBlock";
    var hideSearchBlock = await genericAttributeService.GetAttributeAsync<bool>(await workContext.GetCurrentCustomerAsync(), hideSearchBlockAttributeName);
}

<div class="content-header clearfix">
    <h1 class="float-left">
        @string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderId)
        <small>
            <i class="fas fa-arrow-circle-left"></i>
            <a asp-action="Edit" asp-route-id="@Model.OrderId">@T("Admin.Orders.Products.AddNew.BackToOrder")</a>
        </small>
    </h1>
    <div class="float-right">
        &nbsp;
    </div>
</div>

<section class="content">
    <div class="container-fluid">
        <div class="form-horizontal">
            <div class="cards-group">
                <div class="card card-default card-search">
                    <div class="card-body">
                        <div class="row search-row @(!hideSearchBlock ? "opened" : "")"  data-hideAttribute="@hideSearchBlockAttributeName">
                            <div class="search-text">@T("Admin.Common.Search")</div>
                            <div class="icon-search"><i class="fas fa-magnifying-glass" aria-hidden="true"></i></div>
                            <div class="icon-collapse"><i class="far fa-angle-@(!hideSearchBlock ? "up" : "down")" aria-hidden="true"></i></div>
                        </div>

                        <div class="search-body @(hideSearchBlock ? "closed" : "")">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchProductName" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-editor asp-for="SearchProductName" />
                                        </div>
                                    </div>
                                    <div class="form-group row" @(Model.AvailableCategories.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchCategoryId" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-select asp-for="SearchCategoryId" asp-items="Model.AvailableCategories" />
                                        </div>
                                    </div>
                                    <div class="form-group row" @(Model.AvailableManufacturers.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchManufacturerId" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-select asp-for="SearchManufacturerId" asp-items="Model.AvailableManufacturers" />
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchProductTypeId" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-select asp-for="SearchProductTypeId" asp-items="Model.AvailableProductTypes" />
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <div class="col-md-8 offset-md-4">
                                            <button type="button" id="search-products" class="btn btn-primary btn-search">
                                                <i class="fas fa-magnifying-glass"></i>
                                                @T("Admin.Common.Search")
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card card-default">
                    <div class="card-body">
                        @await Html.PartialAsync("Table", new DataTablesModel
                        {
                            Name = "products-grid",
                            UrlRead = new DataUrl("AddProductToOrder", "Order", new RouteValueDictionary { [nameof(Model.OrderId)] = Model.OrderId }),
                            SearchButtonId = "search-products",
                            Length = Model.PageSize,
                            LengthMenu = Model.AvailablePageSizes,
                            Filters = new List<FilterParameter>
                            {
                                new FilterParameter(nameof(Model.SearchProductName)),
                                new FilterParameter(nameof(Model.SearchCategoryId)),
                                new FilterParameter(nameof(Model.SearchManufacturerId)),
                                new FilterParameter(nameof(Model.SearchProductTypeId))
                            },
                            ColumnCollection = new List<ColumnProperty>
                            {
                                new ColumnProperty(nameof(ProductModel.Id))
                                {
                                    Title = T("Admin.Common.Select").Text,
                                    Render = new RenderCustom("renderColumnSelectProduct"),
                                    ClassName =  NopColumnClassDefaults.Button,
                                    Width = "50",
                                },
                                new ColumnProperty(nameof(ProductModel.Name))
                                {
                                    Title = T("Admin.Orders.Products.AddNew.Name").Text,
                                    Width = "400"
                                },
                                new ColumnProperty(nameof(ProductModel.Sku))
                                {
                                    Title = T("Admin.Orders.Products.AddNew.SKU").Text,
                                    Width = "100"
                                }
                            }
                        })
                    
                        <script>
                            function renderColumnSelectProduct(data, type, row, meta) {
                                return '<button type="button" onclick="addProductToOrder(' + row.Id + ' )" class="btn btn-default">@T("Admin.Common.Select").Text</button>';
                            }
                            function addProductToOrder(productId) {
                                //load "product details page" block
                                var productDetailsActionUrl = '@Html.Raw(Url.Action("AddProductToOrderDetails", "Order", new {orderId = Model.OrderId, productId = "productidplaceholder"}))';
                                productDetailsActionUrl = productDetailsActionUrl.replace("productidplaceholder", productId);
                                setLocation(productDetailsActionUrl);
                            }
                        </script>
                    </div>
                </div>
                <div id="productdetails">
                </div>
                <input asp-for="OrderId" type="hidden" />
            </div>
        </div>
    </div>
</section>