/// class PD.Manage.Modules.UserBatchEditCommand

PD.Manage.Modules.UserBatchEditCommand =
PD.Commands.Command.extend({

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

    setDefaults: function () {
        this.props("id", "user/bulk-edit");
        this.props("enabled", true);
        this.props("icon", PD.Path.to16x16("pencil.png"));
        this.props("label", "Bulk Edit");
        this.props("tooltip", "Bulk Edit properties of the selected Users.");
        this.state("users", null);
    },

    setState: function (state) {
        this._super(state);
        this.props("enabled", this._isEnabled());
    },

    execute: function (e) {
        this._super(e);

        var users = this.state("users");

        if (_.isArray(users)) {
            this._showDialog(_.map(users, "id"));
        }

    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _isEnabled: function () {
        return _.isArray(this.state("users"))
            && this.state("users").length > 1;
    },

    _showDialog: function (ids) {

        var that = this;

        PD.Manage.Modules.UserBatchEditDialog.open({
            callbacks: {
                ok: function (sender, e) {
                    that._startTask(_.extend(e, {
                        ids: ids
                    }));
                }
            }
        });

    },

    _startTask: function (data) {

        PDJS.Shared.TaskStatusDialog.open({
            title: "Bulk Edit | Users",
            taskStart: (e) => {

                PD.ajax({
                    type: "POST",
                    url: PD.Path.resolve("~/Manage/Users/BulkEdit"),
                    data: data,
                    success: (e2) => {
                        e.startPolling(e2);
                    },
                    error: (e2) => {
                        e.abort(e2.responseJSON);
                        e2.errorHandled = true;
                    }
                });

            },
            taskSuccess: (e) => {
                PD.context
                    .get_default()
                    .get_events()
                    .publish("grid/refresh");
            }
        });

    }

});