ildar-shaymukhametov_grandnode-refactorings/Grand.Web/Areas/Admin/Views/Product/_CreateOrUpdate.Categories.cshtml
@model ProductModel


@if (!String.IsNullOrEmpty(Model.Id))
{
    if (Model.AvailableCategories.Count > 0)
    { 
        <div class="panel panel-default">
            <vc:admin-widget widget-zone="product_details_categories_top" additional-data="Model" />
            <div class="panel-heading">
                @T("Admin.Catalog.Products.Categories.Fields.Category")
            </div>
            <div class="panel-body">
                <div id="productcategories-grid"></div>
            </div>
            <vc:admin-widget widget-zone="product_details_categories_bottom" additional-data="Model" />
        </div>
        <script>
            $(document).ready(function () {
                $("#productcategories-grid").kendoGrid({
                    dataSource: {
                        transport: {
                            read: {
                                url: "@Html.Raw(Url.Action("ProductCategoryList", "Product", new { productId = Model.Id }))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            },
                            create: {
                                url: "@Html.Raw(Url.Action("ProductCategoryInsert", "Product", new { productId = Model.Id }))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            },
                            update: {
                                url:"@Html.Raw(Url.Action("ProductCategoryUpdate", "Product"))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            },
                            destroy: {
                                url: "@Html.Raw(Url.Action("ProductCategoryDelete", "Product"))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            }
                        },
                        schema: {
                            data: "Data",
                            total: "Total",
                            errors: "Errors",
                            model: {
                                id: "Id",
                                fields: {
                                    Category: { editable: true, type: "string" },
                                    CategoryId: { editable: true, type: "string" },
                                    IsFeaturedProduct: { editable: true, type: "boolean" },
                                    DisplayOrder: { editable: true, type: "number" },
                                    Id: { editable: false, type: "string" }
                                }
                            }
                        },
                        requestEnd: function (e) {
                            if (e.type == "create" || e.type == "update") {
                                this.read();
                            }
                        },
                        error: function (e) {
                            display_kendoui_grid_error(e);
                            // Cancel the changes
                            this.cancelChanges();
                        },
                        serverPaging: true,
                        serverFiltering: true,
                        serverSorting: true
                    },
                    pageable: {
                        refresh: true,
                        numeric: false,
                        previousNext: false,
                        info: false
                    },
                    toolbar: [{ name: "create", text: "@T("Admin.Common.AddNewRecord")" }],
                    edit: function(e) {
                        if (e.model.isNew()) {
                            //little hack here
                            //pre-select the first value of kendoui dropdownlist datasource
                            //for some reasons (maybe, bug) it always sends 0
                            //if no value has been selected (changed) in the dropdownlist
                            if (allCategories.length > 0) {
                                e.model.CategoryId = allCategories[0].Id;
                            }
                        }
                    },
                    editable: {
                        confirmation: true,
                        mode: "inline"
                    },
                    scrollable: false,
                    columns: [{
                            field: "CategoryId",
                            title: "@T("Admin.Catalog.Products.Categories.Fields.Category")",
                            width: 200,
                            editor: categoryDropDownEditor,
                            template: '<a class="k-link" href="@Url.Action("Edit", "Category")/#=CategoryId#">#:Category#</a>'
                        },
                        @if (!Model.IsLoggedInAsVendor)
                        {
                            //vendor cannot edit this field
                            <text>{
                            field: "IsFeaturedProduct",
                            title: "@T("Admin.Catalog.Products.Categories.Fields.IsFeaturedProduct")",
                            width: 100,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '# if(IsFeaturedProduct) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
                    },</text>
                        }

                    {
                        field: "DisplayOrder",
                        title: "@T("Admin.Catalog.Products.Categories.Fields.DisplayOrder")",
                        width: 100,
                        minScreenWidth: 500,
                        headerAttributes: { style: "text-align:center" },
                        attributes: { style: "text-align:center" },
                        //integer format
                        format: "{0:0}"
                    }, {
                        command: [{
                            name: "edit",
                            text: {
                                edit: "@T("Admin.Common.Edit")",
                                update: "@T("Admin.Common.Update")",
                                cancel: "@T("Admin.Common.Cancel")"
                            }
                        }, {
                            name: "destroy",
                            text: "@T("Admin.Common.Delete")"
                        }],
                        width: 200
                    }]
                });
            });

            //local datasource
            var allCategories = [
                @for (int i = 0; i < Model.AvailableCategories.Count; i++)
                {
                    var categoryItem = Model.AvailableCategories[i];
                    <text>
                        {
                            Id: '@(categoryItem.Value)',
                            Name: "@(Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode(string.IsNullOrEmpty(categoryItem.Text) ? "": categoryItem.Text)))"
                        }
                        </text>
                    if (i != Model.AvailableCategories.Count - 1)
                    {
                        <text>,</text>
                    }
                }
            ];

            function categoryDropDownEditor(container, options) {
                $('<input required data-text-field="Name" data-value-field="Id" data-bind="value:CategoryId"/>')
                    .appendTo(container)
                    .kendoDropDownList({
                        autoBind: true,
                        dataSource: allCategories
                    });
            }
        </script>
    }
    else
    {
        <div class="note note-info">
            @T("Admin.Catalog.Products.Categories.NoCategoriesAvailable")
        </div>
    }
}
else
{
    <div class="note note-info">
        @T("Admin.Catalog.Products.Categories.SaveBeforeEdit")
    </div>
}

