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

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

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

    _uploadInput: null,

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

    init: function (options) {

        this._super($.extend(true, {
            config: {
                resizable: false,
                height: "110px",
                width: "400px",
                title: "Import Configuration"
            }
        }, options));

    },

    //===================================
    // 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 () {
        this._init_uploadInput();
    },

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

    okButton_onClick: function () {
        this._importConfig();
    },

    cancelButton_onClick: function () {
        this.close();
    },

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

    template: function (e) {

        return [
            "<div class='-pd-system-settings-import-dialog'>",
                "<div class='-pd-dialog-content'>",
                    "<div>",
                        "<div style='margin-bottom:5px;font-size:12px;'>Configuration File:</div>",
                        "<div data-id='import-file' style='margin-bottom: 10px;'></div>",
                    "</div>",
                "</div>",
                "<div class='-pd-dialog-button-deck'>",
                    "<button class='-pd-button' data-id='ok'>OK</button>",
                    "<button class='-pd-button' data-id='cancel'>Cancel</button>",
                "</div>",
            "</div>"
        ];

    },

    _init_uploadInput: function () {

        var that = this;

        this._uploadInput = new PD.Web.UI.Controls.UploadInput({
            insertAt: "[data-id=import-file]",
            uploadStart: function (e) {
                that.get_okButton().prop("disabled", true);
            },
            uploadComplete: function (e) {
                that.get_okButton().prop("disabled", false);
            }
        });

        this.get_children().add(this._uploadInput);

    },

    _importConfig: function () {

        var uploadId = this._uploadInput.get_uploadId()

        this.get_okButton().prop("disabled", true);

        this.close();

        PDJS.Shared.TaskStatusDialog.open({
            title: "Export | Configuration",
            taskStart: (e) => {

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

            },
            taskSuccess: (e) => {
                PD.context
                    .get_default()
                    .get_events()
                    .publish("system-setting/edit");
            }
        });

    }

});

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