GokhanSevim_Nop_Plugin_DiscountRules_HasCategory/Views/Configure.cshtml
@model Nop.Plugin.DiscountRules.HasCategory.Models.RequirementModel
@{
    Layout = "";
}

<script type="text/javascript">
    $(document).ready(function () {
        $('#saveHasCategoryrequirement@(Model.RequirementId)').click(function () {
            var categoryIds = $("#@Html.IdFor(model => model.CategoryIds)").val();
            var discountId = @Model.DiscountId;
            var requirementId = @Model.RequirementId;

            var postData = {
                DiscountId: discountId,
                RequirementId: requirementId,
                CategoryIds: categoryIds
            };
            addAntiForgeryToken(postData);

            $.ajax({
                cache:false,
                type: "POST",
                url: "@(Url.Action("Configure", "DiscountRulesHasCategory"))",
                data: postData,
                success: function (data, textStatus, jqXHR) {
                    $('#pnl-save-requirement-result@(Model.RequirementId)').fadeIn("slow").delay(1000).fadeOut("slow");
                    //notify parent if it's a new requirement
                    @if (Model.RequirementId == 0)
                    {
                        <text>$("#discountRequirementContainer").trigger('nopnewdiscountruleadded', [data.NewRequirementId]);</text>
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var $alertInfoEl = $("#saveHasCategoryrequirementAlert-info");
                    let response = jqXHR.responseJSON;
                    if (response instanceof Object && response.hasOwnProperty('Errors')) {
                        var errorMessages = '';
                        $.each(response.Errors, function (i, error) {
                            errorMessages += error;
                            if (response.Errors.length - 1 != i)
                                errorMessages += '</br>';
                        });
                        $alertInfoEl.html(errorMessages);
                    } else {
                        // display default error
                        $alertInfoEl.html('@T("Admin.Promotions.Discounts.Requirements.FailedToSave")');
                    }

                    $("#saveHasCategoryrequirementAlert").click();
                }
            });
        });
    });
</script>

<div class="form-group row">
    <div class="col-md-3 requirement-label-col">
        <nop-label asp-for="CategoryIds" />
    </div>
    <div class="col-md-9 requirement-data-col">
        <div class="requirement-data-input">
            <div class="input-group">
                <nop-editor asp-for="CategoryIds" />
                <div class="input-group-btn">
                    <input type="submit"
                           id="btnAddNewDiscountRequirementCategory@(Model.RequirementId)"
                           value="@T("Plugins.DiscountRules.HasCategory.Fields.Categories.AddNew")"
                           onclick="javascript:OpenWindow('@Url.Action("CategoryAddPopup", "DiscountRulesHasCategory", new {btnId = $"btnRefreshDiscountRequirementCategories{Model.RequirementId}", categoryIdsInput = Html.IdFor(model => model.CategoryIds)})', 800, 850, true); return false;"
                           class="btn btn-info" />
                    <input type="submit" id="btnRefreshDiscountRequirementCategories@(Model.RequirementId)" style="display: none" />
                </div>
            </div>
        </div>
        <div class="requirement-data-buttons">
            <input type="button" id="saveHasCategoryrequirement@(Model.RequirementId)" class="btn btn-primary" value="@T("Admin.Common.Save")" />
        </div>
    </div>
</div>
<div class="form-group row">
    <div class="col-md-offset-3 col-md-9 requirement-messages-col">
        <div class="requirement-category-names text-blue" id="discountrequirement-category-names@(Model.RequirementId)"></div>
        <div id="pnl-save-requirement-result@(Model.RequirementId)" style="display: none;" class="text-green margin-t-5">
            @T("Admin.Promotions.Discounts.Requirements.Saved")
        </div>
        <div id="discountrequirement-categories-check-progress@(Model.RequirementId)" class="please-wait pull-right" style="display: none;">
            @T("Common.Wait...")
        </div>
    </div>
</div>
<nop-alert asp-alert-id="saveHasCategoryrequirementAlert" />

<script type="text/javascript">
    $(document).ready(function() {
        $('#btnRefreshDiscountRequirementCategories@(Model.RequirementId)').click(function() {
            //refresh category list
            loadDiscountRequirementCategoryFriendlyNames@(Model.RequirementId)();

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

    $(document).ready(function() {
        loadDiscountRequirementCategoryFriendlyNames@(Model.RequirementId)();
    });
    $(document).ready(function() {
        $('#@Html.IdFor(model => model.CategoryIds)')
            .data('timeout', null)
            .keyup(function() {
                clearTimeout($(this).data('timeout'));
                //one second timeout
                $(this).data('timeout', setTimeout(loadDiscountRequirementCategoryFriendlyNames@(Model.RequirementId), 1000));
            });
    });

    function loadDiscountRequirementCategoryFriendlyNames@(Model.RequirementId)() {
        var inputValue = $('#@Html.IdFor(model => model.CategoryIds)').val();
        if (inputValue) {
            //load friendly names
            $('#discountrequirement-categories-check-progress@(Model.RequirementId)').show();

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

            $.ajax({
                cache: false,
                type: 'POST',
                url: '@Url.Action("LoadCategoryFriendlyNames", "DiscountRulesHasCategory")',
                data: postData,
                dataType: 'json',
                success: function (data, textStatus, jqXHR) {
                    $('#discountrequirement-categories-check-progress@(Model.RequirementId)').hide();
                    $('#discountrequirement-category-names@(Model.RequirementId)').text(data.Text);
                    $('#discountrequirement-category-names@(Model.RequirementId)').addClass('filled');
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $('#discountrequirement-categories-check-progress@(Model.RequirementId)').hide();
                    $('#discountrequirement-category-names@(Model.RequirementId)').text('');
                    $('#discountrequirement-category-names@(Model.RequirementId)').removeClass('filled');
                }
            });
        } else {
            $('#discountrequirement-category-names@(Model.RequirementId)').text('');
            $('#discountrequirement-category-names@(Model.RequirementId)').removeClass('filled');
        }
    }
</script>