/// class PD.Manage.Services.Entities.EntityManagementService

PD.Manage.Services.Entities.EntityManagementService =
Class.extend({

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

    _viewGrid: null,

    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (viewGrid) {
        this._viewGrid = viewGrid;
    },

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

    showChangeStatusDialog: function (ids) {
        
        var that = this;

        PD.Web.UI.RadioDialog.warn({
            title: "Change Status",
            message: "What status would you like to assign to the selected entities?",
            ok: function (e) {
                e.close();
                that.changeStatus("Change Status", ids, e.value);
            },
            radios: [
                { value: 1, label: "<b>Set to Active</b><br/>Entities that are set to an \"active\" state will be made available to end users of the system, subject to security settings." },
                { value: 2, label: "<b>Set to Inactive</b><br/>Entities that are set to an \"inactive\" state will no longer be accessible to end users of the system and will only be accessible to users granted manage permissions to the entity type." },
            ]
        });

    },

    sendToRecycleBin: function (ids) {

        var that = this;

        PD.Web.UI.MessageDialog.warn({
            title: "Send to Recycle Bin",
            message: "Send the selected entities to the Recycle Bin?",
            ok: function (e) {
                e.close();
                that.changeStatus("Send to Recycle Bin", ids, 4);
            }
        });

    },
    
    //===================================
    // PRIVATE METHODS
    //===================================

    changeStatus: function (title, ids, status) {

        var grid = this._viewGrid;

        PD.Shared.Dialogs.TaskStatus.open({
            config: {
                title: title
            },
            showDetail: true,
            taskStart: function (args) {

                PD.ajax({
                    type: "POST",
                    url: PD.Path.resolve("~/Manage/EntityStatus/Change"),
                    data: {
                        ids: ids,
                        status: status
                    },
                    success: function (e) {
                        args.startPolling(e);
                    },
                    error: function (e) {
                        args.abort(e.responseJSON);
                        e.errorHandled = true;
                    }
                });

            },
            taskSuccess: function () {
                grid.refresh({
                    clear: status === 4
                });
            }
        });

    },

});