/// class PD.Manage.Modules.FormFieldFinderDialog
// using PD.Web.UI.Dialog

PD.Manage.Modules.FormFieldFinderDialog =
PD.Web.UI.Dialog.extend({

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

    _grid: null,

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

    get_okButton: function () {
        return this.get_node().find("button[data-id=ok]");
    },

    get_cancelButton: function () {
        return this.get_node().find("button[data-id=cancel]");
    },

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

    onInit: function () {

        var that = this;

        PD.ajax({
            url: PD.Path.resolve("~/Manage/FormFields"),
            success: function (e) {
                that.grid_init(e.viewStateId);
            }
        });

        this.grid_init();
    },

    onActivate: function () {
        this.get_okButton().on("click", _.bind(this.okButton_onClick, this));
        this.get_cancelButton().on("click", _.bind(this.cancelButton_onClick, this));
    },

    okButton_onClick: function (e) {
        this.close({
            command: "ok",
            data: this.getSelections()
        });
    },

    cancelButton_onClick: function (e) {
        this.close({
            command: "cancel"
        });
    },
    
    //===================================
    // PROTECTED METHODS
    //==================================

    getDefaultOptions: function () {
        return {
            extensionId: null,
            filters:[],
            config: {
                title: "Form Field Finder",
                height: 400,
                width: 700,
                resizable: true
            }
        }
    },

    template: function (data) {
        return [
            "<div class='-pd-form-field-finder-dialog'>",
                "<div data-role='grid'></div>",
                "<div data-role='button-deck'>",
                    "<button class='-pd-button' data-id='ok'>OK</button>",
                    "<button class='-pd-button' data-id='cancel'>Cancel</button>",
                "</div>",
            "</div>"
        ];
    },

    //===================================
    // PRIVATE METHODS
    //==================================
    
    grid_init: function (viewStateId) {

        var that = this;
        var isList = this.get_options().isList;

        this._grid = new PD.Web.UI.Controls.ViewGrid({
            insertAt: "[data-role=grid]",
            viewStateId: viewStateId,
            selectionMode: "multiple",
            needContext: function (e) {
                e.addFilter({ id: "ListData", params: { isList: isList ? 1 : 0} });
                if (that.get_options().isFile) {
                    e.addFilter({ id: "FileBased", params: { isFile: 1 }  });
                }
                if (that.get_options().extensionId != null) {
                    e.addFilter({ id: "Extension", params: { extID: that.get_options().extensionId } });
                }
                if (that.get_options().isList != null) {
                    e.addFilter({ id: "ListBased", params: { isList: 1 } });
                }
            },
            dblclick: function (e) {
                that.close({
                    command: "ok",
                    data: that.getSelections()
                });
            }
        });

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

    },

    getSelections: function () {

        var selections = this._grid.getSelections().toArray();
        var result = [];

        _.each(selections, function (item) {
            result[result.length] = {
                id: item.ObjectId,
                label: item.ColName
            }
        });

        return result;

    }

});

PD.Manage.Modules.FormFieldFinderDialog.open =
function (options) {
    return new PD.Manage.Modules.FormFieldFinderDialog(options);
};