ildar-shaymukhametov_grandnode-refactorings/Plugins/Grand.Plugin.DiscountRequirements.Standard/Views/HasAllProducts/Configure.cshtml
@{
    Layout = "";
}
@model Grand.Plugin.DiscountRequirements.HasAllProducts.Models.RequirementModel
@using Grand.Framework;

<script>
    $(document).ready(function () {
        $('#saveHasAllProductsrequirement@(Model.RequirementId)').click(function () {
            var productIds = $("#@Html.FieldIdFor(model => model.Products)").val();
            var discountId = '@Model.DiscountId';
            var requirementId = '@Model.RequirementId';

            var postData = {
                discountId: discountId,
                discountRequirementId: requirementId,
                productIds: productIds
            };
            addAntiForgeryToken(postData);

            $.ajax({
                cache:false,
                type: "POST",
                url: "@(Url.Action("Configure", "HasAllProducts"))",
                data: postData,
                success: function (data) {
                    $('#pnl-save-requirement-result@(Model.RequirementId)').fadeIn("slow").delay(1000).fadeOut("slow");
                    //notify parent if it's a new requirement
                    @if (String.IsNullOrEmpty(Model.RequirementId))
                    {
                        <text>$("#discountRequirementContainer").trigger('grandnewdiscountruleadded', [data.NewRequirementId]);</text>
                    }
                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert('@Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode(T("Admin.Promotions.Discounts.Requirements.FailedToSave").Text))');
                }
            });
        });
    });
</script>

<div class="form-body">
    <admin-label asp-for="Products" />
    <div class="col-md-8 col-sm-8">
        <admin-input asp-for="Products" />
        <span class="control-label" style="text-align:left" id="discountrequirement-product-names@(Model.RequirementId)"></span>
        <div class="group-btn btn-group-devided margin-top-10">
            <input type="button" id="saveHasAllProductsrequirement@(Model.RequirementId)" class="btn default" value="@T("Admin.Common.Save")" />
            <input type="submit" id="btnAddNewDiscountRequirementProduct@(Model.RequirementId)" value="@T("Plugins.DiscountRequirements.HasAllProducts.Fields.Products.AddNew")" onclick="javascript:OpenWindow('@(Url.RouteUrl("Plugin.DiscountRequirements.HasAllProducts.ProductAddPopup", new { btnId = "btnRefreshDiscountRequirementProducts" + Model.RequirementId, productIdsInput = Html.FieldIdFor(model => model.Products) }))', 800, 800, true); return false;" class="btn default" />
        </div>
        <div id="pnl-save-requirement-result@(Model.RequirementId)" style="display:none;">@T("Admin.Promotions.Discounts.Requirements.Saved")</div>

        <span id="discountrequirement-products-check-progress@(Model.RequirementId)" style="display: none;" class="please-wait">@T("Common.Wait...")</span>

        <input type="submit" id="btnRefreshDiscountRequirementProducts@(Model.RequirementId)" style="display: none" />
        <script>
                $(document).ready(function () {
                    $('#btnRefreshDiscountRequirementProducts@(Model.RequirementId)').click(function () {
                        //refresh product list
                        loadDiscountRequirementProductFriendlyNames@(Model.RequirementId)();

                        //return false to don't reload a page
                        return false;
                    });
                });

                $(document).ready(function () {
                    loadDiscountRequirementProductFriendlyNames@(Model.RequirementId)();
                });
                $(document).ready(function () {
                    $('#@Html.FieldIdFor(model => model.Products)')
                        .data('timeout', null)
                        .keyup(function() {
                            clearTimeout($(this).data('timeout'));
                            //one second timeout
                            $(this).data('timeout', setTimeout(loadDiscountRequirementProductFriendlyNames@(Model.RequirementId), 1000));
                    });
                });
                function loadDiscountRequirementProductFriendlyNames@(Model.RequirementId)() {
                    var inputValue = $('#@Html.FieldIdFor(model => model.Products)').val();
                    if (inputValue) {
                        //load friendly names
                        $('#discountrequirement-products-check-progress@(Model.RequirementId)').show();

                        var postData = {
                            productIds: inputValue
                        };
                        addAntiForgeryToken(postData);

                        $.ajax({
                            cache: false,
                            type: 'POST',
                            url: '@Url.RouteUrl("Plugin.DiscountRequirements.HasAllProducts.LoadProductFriendlyNames")',
                            data: postData,
                            dataType: 'json',
                            success: function(data) {
                                $('#discountrequirement-products-check-progress@(Model.RequirementId)').hide();
                                $('#discountrequirement-product-names@(Model.RequirementId)').text(data.Text);
                            },
                            failure: function () {
                                $('#discountrequirement-products-check-progress@(Model.RequirementId)').hide();
                                $('#discountrequirement-product-names@(Model.RequirementId)').text('');
                            }
                        });
                    } else {
                        $('#discountrequirement-product-names@(Model.RequirementId)').text('');
                    }
                }
        </script>
    </div>
</div>
