ildar-shaymukhametov_grandnode-refactorings/Grand.Web/Areas/Admin/Views/Measure/Dimensions.cshtml
@{
    //page title
    ViewBag.Title = T("Admin.Configuration.Measures.Dimensions").Text;
}

<div class="row">
    <div class="col-md-12">
        <div class="x_panel light form-fit">
            <div class="x_title">
                <div class="caption level-caption">
                    <i class="fa fa-wrench"></i>
                    @T("Admin.Configuration.Measures.Dimensions")
                </div>
            </div>
            <div class="x_content form">
                <div class="note note-info">
                    @T("Admin.Configuration.Measures.Dimensions.Description")
                </div>
                <div class="form-horizontal">
                    <div class="form-body">
                        <div class="x_content">
                            <div id="measuredimension-grid"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function() {
        $("#measuredimension-grid").kendoGrid({
            dataSource: {
                transport: {
                    read: {
                        url: "@Html.Raw(Url.Action("Dimensions", "Measure"))",
                        type: "POST",
                        dataType: "json",
                        data: addAntiForgeryToken
                    },
                    create: {
                        url: "@Html.Raw(Url.Action("DimensionAdd", "Measure"))",
                        type: "POST",
                        dataType: "json",
                        data: addAntiForgeryToken
                    },
                    update: {
                        url: "@Html.Raw(Url.Action("DimensionUpdate", "Measure"))",
                        type: "POST",
                        dataType: "json",
                        data: addAntiForgeryToken
                    },
                    destroy: {
                        url: "@Html.Raw(Url.Action("DimensionDelete", "Measure"))",
                        type: "POST",
                        dataType: "json",
                        data: addAntiForgeryToken
                    },
                    parameterMap: function (data, operation) {
                        if (operation != "read") {
                            data.Ratio = kendo.toString(data.Ratio, "n8");
                            return data;
                        } else {
                            return data;
                        }
                    }
                },
                schema: {
                    data: "Data",
                    total: "Total",
                    errors: "Errors",
                    model: {
                        id: "Id",
                        fields: {
                            Name: { editable: true, type: "string" },
                            SystemKeyword: { editable: true, type: "string" },
                            Ratio: { editable: true, type: "number" },
                            DisplayOrder: { editable: true, type: "number" },
                            IsPrimaryDimension: { editable: false, type: "boolean" },
                            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")" }],
            editable: {
                confirmation: false,
                mode: "inline"
            },
            scrollable: false,
            columns: [
                {
                    field: "Name",
                    title: "@T("Admin.Configuration.Measures.Dimensions.Fields.Name")",
                    width: 300
                }, {
                    field: "SystemKeyword",
                    title: "@T("Admin.Configuration.Measures.Dimensions.Fields.SystemKeyword")",
                    width: 200
                }, {
                    field: "Ratio",
                    title: "@T("Admin.Configuration.Measures.Dimensions.Fields.Ratio")",
                    width: 200,
                    editor: function(container, options) {
                        $('<input name="' + options.field + '"/>')
                            .appendTo(container)
                            .kendoNumericTextBox({
                                format: "{0:n8}",
                                decimals: 8
                            });
                    }
                }, {
                    field: "DisplayOrder",
                    title: "@T("Admin.Configuration.Measures.Dimensions.Fields.DisplayOrder")",
                    //integer format
                    format: "{0:0}",
                    width: 100
                }, {
                    field: "IsPrimaryDimension",
                    title: "@T("Admin.Configuration.Measures.Dimensions.Fields.IsPrimaryDimension")",
                    width: 100,
                    headerAttributes: { style: "text-align:center" },
                    attributes: { style: "text-align:center" },
                    template: '# if(IsPrimaryDimension) {# <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> #} #'
                }, {
                    field: "Id",
                    title: "@T("Admin.Configuration.Measures.Dimensions.Fields.MarkAsPrimaryDimension")",
                    width: 150,
                    template: '<a onclick="markAsPrimaryDimension(\'#=Id#\')" class="k-button">@T("Admin.Configuration.Measures.Dimensions.Fields.MarkAsPrimaryDimension")</a>'
                }, {
                    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
                }
            ]
        });
    });
</script>
<script>
    function markAsPrimaryDimension(id) {
        if (confirm('@T("Admin.Common.AreYouSure")')) {
            var postData = {
                id: id
            };
            addAntiForgeryToken(postData);
            $.ajax({
                cache: false,
                type: "POST",
                url: "@(Url.Action("MarkAsPrimaryDimension", "Measure"))",
                data: postData,
                success: function(data) {
                    var grid = $("#measuredimension-grid").data('kendoGrid');
                    grid.dataSource.read();
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert('Failed to update dimension');
                }
            });
        }
    };
</script>
