/// class PD.Manage.Modules.EntityManagementGrid
// using PD.Web.UI.Controls.Control

PD.Manage.Modules.EntityManagementGrid =
PD.Web.UI.Controls.Control.extend({

    //===================================
    // FIELDS
    //===================================

    _grid: null,

    //===================================
    // PROPERTIES
    //===================================

    get_grid: function () {
        return this._grid;
    },

    //===================================
    // EVENT HANDLERS
    //===================================

    onInit: function () {

        var that = this,
            changeFn = this.get_options()["change"];

        this._grid = new PD.Web.UI.Controls.ViewGrid({
            insertAt: "[data-id='grid']",
            selectionMode: "multiple",
            selectOnRightClick: true,
            change: function (e) {
                PD.context.get_default().get_commands().setState({
                    "pd/entity-management": {
                        entities: e.selections.toArray()
                    }
                });
            },
            dblclick: function () {
                PD.context.get_default().get_commands().execute("pd/entity-management:entity/properties-dialog");
            },
            contextMenu: {
                type: "PD.Manage.Modules.EntityManagementContextMenu",
                context: function () {
                    return {
                        grid: that._grid
                    };
                }
            }
        });

        this.get_children().add(this._grid);

    },

    onActivate: function (e) {
    
        var that = this;

        this.subscriptions.add("entity/create", function (e) {
            that._grid.refresh({
                selections: [e.id]
            });
        });

        this.subscriptions.add("entity/change-status", function (e) {
            that._grid.refresh();
        });

        this.subscriptions.add("entity/delete", function (e) {
            that._grid.refresh({
                clear: true
            });
        });

        this.subscriptions.add("entity/update", function (e) {
            that._grid.refresh();
        });

    },

    //===================================
    // PUBLIC METHODS
    //===================================

    refresh: function (e) {
        this.get_grid().refresh(e);
    },

    load: function (entityType) {
        this._grid.setViewState(entityType.viewStateId);
        this.get_node().find(".-pd-title-compact").text(entityType.name + " List");
    },

    //===================================
    // PROTECTED METHODS
    //===================================

    template: function () {
        return [
            "<div>",
                "<div style='position:absolute;top:0;left:0;right:0;height:30px;border-width:0 0 1px 0;' class='-pd-title-compact'></div>",
                "<div style='position:absolute;top:30px;left:0;right:0;bottom:0;' data-id='grid'></div>",
            "</div>"
        ];
    },


});