@using Nop.Services.Stores
@model ProductReviewSearchModel

@{
    //page title
    ViewBag.PageTitle = T("Admin.Catalog.ProductReviews").Text;
    //active menu item (system name)
    NopHtml.SetActiveMenuItemSystemName("Product reviews");
}

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

<div class="content-header clearfix">
    <h1 class="float-left">
        @T("Admin.Catalog.ProductReviews")
    </h1>
    <div class="float-right">
        @if (!Model.IsLoggedInAsVendor)
        {
            //these buttons are not visible to vendors
            <button type="button" id="approve-selected" class="btn btn-success">
                <i class="fas fa-check-square"></i>
                @T("Admin.Catalog.ProductReviews.ApproveSelected")
            </button>
            <button type="button" id="disapprove-selected" class="btn btn-secondary">
                <i class="fas fa-minus-square"></i>
                @T("Admin.Catalog.ProductReviews.DisapproveSelected")
            </button>
            <button type="button" id="delete-selected" class="btn btn-danger">
                <i class="far fa-trash-alt"></i>
                @T("Admin.Catalog.ProductReviews.DeleteSelected")
            </button>
            <nop-action-confirmation asp-button-id="delete-selected" />
        }
        @await Component.InvokeAsync("AdminWidget", new { widgetZone = AdminWidgetZones.ProductReviewListButtons, additionalData = Model })
    </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-search" 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-5">
                                <div class="form-group row">
                                    <div class="col-md-4">
                                        <nop-label asp-for="CreatedOnFrom" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="CreatedOnFrom" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-md-4">
                                        <nop-label asp-for="CreatedOnTo" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="CreatedOnTo" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchText" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="SearchText" />
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-7">
                                <div class="form-group row">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchApprovedId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchApprovedId" asp-items="Model.AvailableApprovedOptions" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchProductId" />
                                    </div>
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" id="search-product-name" autocomplete="off" />
                                        <script>
                                        $(document).ready(function() {
                                            $('#search-product-name').autocomplete({
                                                delay: 500,
                                                minLength: 3,
                                                source: '@Url.Action("SearchAutoComplete", "SearchComplete")',
                                                select: function(event, ui) {
                                                    $('#@Html.IdFor(model => model.SearchProductId)')
                                                        .val(ui.item.productid);
                                                    $('#search-product-friendly-name').text(ui.item.label);
                                                    $('#search-product-clear').show();
                                                    return false;
                                                }
                                            });
                                        });
                                        </script>

                                        <span id="search-product-friendly-name"></span>
                                        <button type="button" id="search-product-clear" class="btn bg-gray" style="display: none; margin-top: 5px;">@T("Admin.Common.Clear")</button>
                                        <input asp-for="SearchProductId" autocomplete="off" style="display: none;" />

                                        <script>
                                        //remove button
                                        $('#search-product-clear').click(function() {
                                            $('#@Html.IdFor(model => model.SearchProductId)').val('0');
                                            $('#search-product-friendly-name').text('');
                                            $('#search-product-clear').hide();
                                            return false;
                                        });
                                        </script>
                                    </div>
                                </div>
                                <div class="form-group row" @(Model.HideStoresList ? Html.Raw("style=\"display:none\"") : null)>
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchStoreId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchStoreId" asp-items="Model.AvailableStores" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="text-center col-12">
                                <button type="button" id="search-productreviews" class="btn btn-primary btn-search">
                                    <i class="fas fa-search"></i>
                                    @T("Admin.Common.Search")
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="card card-default">
                <div class="card-body">
                    <nop-doc-reference asp-string-resource="@T("Admin.Documentation.Reference.ProductReviews", Docs.ProductReviews + Utm.OnAdmin)" />

                    @await Html.PartialAsync("Table", new DataTablesModel
                    {
                        Name = "productreviews-grid",
                        UrlRead = new DataUrl("List", "ProductReview", null),
                        SearchButtonId = "search-productreviews",
                        Length = Model.PageSize,
                        LengthMenu = Model.AvailablePageSizes,
                        Filters = new List<FilterParameter>
                        {
                            new FilterParameter(nameof(Model.CreatedOnFrom)),
                            new FilterParameter(nameof(Model.CreatedOnTo)),
                            new FilterParameter(nameof(Model.SearchText)),
                            new FilterParameter(nameof(Model.SearchStoreId)),
                            new FilterParameter(nameof(Model.SearchProductId)),
                            new FilterParameter(nameof(Model.SearchApprovedId))
                        },
                        ColumnCollection = new List<ColumnProperty>
                        {
                            new ColumnProperty(nameof(ProductReviewModel.Id))
                            {
                                IsMasterCheckBox = true,
                                Render = new RenderCheckBox("checkbox_product_reviews"),
                                Visible = !Model.IsLoggedInAsVendor,
                                ClassName =  NopColumnClassDefaults.CenterAll,
                                Width = "50"
                            },
                            new ColumnProperty(nameof(ProductReviewModel.StoreName))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.Store").Text,
                                Visible = (await storeService.GetAllStoresAsync()).Count > 1 ? true : false,
                                Width = "150"
                            },
                            new ColumnProperty(nameof(ProductReviewModel.ProductName))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.Product").Text,
                                Width = "200",
                                Render = new RenderLink(new DataUrl("~/Admin/Product/Edit", nameof(ProductReviewModel.ProductId)))
                            },
                            new ColumnProperty(nameof(ProductReviewModel.CustomerInfo))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.Customer").Text,
                                Width = "200",
                                Render = new RenderLink(new DataUrl("~/Admin/Customer/Edit", nameof(ProductReviewModel.CustomerId)))
                            },
                            new ColumnProperty(nameof(ProductReviewModel.Title))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.Title").Text,
                                Width = "200"
                            },
                            new ColumnProperty(nameof(ProductReviewModel.ReviewText))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.ReviewText").Text,
                                Encode = false
                            },
                            new ColumnProperty(nameof(ProductReviewModel.ReplyText))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.ReplyText").Text,
                                Encode = false
                            },
                            new ColumnProperty(nameof(ProductReviewModel.Rating))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.Rating").Text,
                                Width = "50",
                                ClassName =  NopColumnClassDefaults.CenterAll
                            },
                            new ColumnProperty(nameof(ProductReviewModel.IsApproved))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.IsApproved").Text,
                                Width = "100",
                                ClassName =  NopColumnClassDefaults.CenterAll,
                                Render = new RenderBoolean()
                            },
                            new ColumnProperty(nameof(ProductReviewModel.CreatedOn))
                            {
                                Title = T("Admin.Catalog.ProductReviews.Fields.CreatedOn").Text,
                                Width = "150",
                                Render = new RenderDate()
                            },
                            new ColumnProperty(nameof(ProductReviewModel.Id))
                            {
                                Title = T("Admin.Common.Edit").Text,
                                Width = "100",
                                ClassName =  NopColumnClassDefaults.Button,
                                Render = new RenderButtonEdit(new DataUrl("Edit"))
                            }
                        }
                    })

                    <script>
                        $(document).ready(function() {

                            //"approve selected" button
                            $('#approve-selected').click(function(e) {
                                e.preventDefault();

                                var postData = {
                                    selectedIds: selectedIds
                                };
                                addAntiForgeryToken(postData);

                                $.ajax({
                                    cache: false,
                                    type: "POST",
                                    url: "@(Url.Action("ApproveSelected", "ProductReview"))",
                                    data: postData,
                                    traditional: true,
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        showAlert('approveSelectedFailed', errorThrown);
                                    },
                                    complete: function (jqXHR, textStatus) {
                                        if (jqXHR.status === 204)
                                        {
                                            showAlert('nothingSelectedAlert', '@T("Admin.Common.Alert.NothingSelected")');
                                            return;
                                        }
                                        updateTable('#productreviews-grid');
                                    }
                                });
                                return false;
                            });

                            //"disapprove selected" button
                            $('#disapprove-selected').click(function(e) {
                                e.preventDefault();

                                var postData = {
                                    selectedIds: selectedIds
                                };
                                addAntiForgeryToken(postData);

                                $.ajax({
                                    cache: false,
                                    type: "POST",
                                    url: "@(Url.Action("DisapproveSelected", "ProductReview"))",
                                    data: postData,
                                    traditional: true,
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        showAlert('disapproveSelectedFailed', errorThrown);
                                    },
                                    complete: function (jqXHR, textStatus) {
                                        if (jqXHR.status === 204)
                                        {
                                            showAlert('nothingSelectedAlert', '@T("Admin.Common.Alert.NothingSelected")');
                                            return;
                                        }
                                        updateTable('#productreviews-grid');
                                    }
                                });
                                return false;
                            });

                            //"delete selected" button
                            $("#delete-selected-action-confirmation-submit-button").bind("click", function () {
                                var postData = {
                                    selectedIds: selectedIds
                                };
                                addAntiForgeryToken(postData);
                                $.ajax({
                                    cache: false,
                                    type: "POST",
                                    url: "@(Url.Action("DeleteSelected", "ProductReview"))",
                                    data: postData,
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        showAlert('deleteSelectedFailed', errorThrown);
                                    },
                                    complete: function (jqXHR, textStatus) {
                                        if (jqXHR.status === 204)
                                        {
                                            showAlert('nothingSelectedAlert', '@T("Admin.Common.Alert.NothingSelected")');
                                            return;
                                        }
                                        updateTable('#productreviews-grid');
                                    }
                                });
                                $('#delete-selected-action-confirmation').modal('toggle');
                                return false;
                            });
                        });
                    </script>
                </div>
            </div>
        </div>
    </div>
    </div>
</section>
<nop-alert asp-alert-id="approveSelectedFailed" />
<nop-alert asp-alert-id="disapproveSelectedFailed" />
<nop-alert asp-alert-id="deleteSelectedFailed" />
<nop-alert asp-alert-id="nothingSelectedAlert" />
