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

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

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

    _uploadInput: null,

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

    init: function (options) {

        this._super($.extend(true, {
            config: {
                resizable: false,
                height: "150px",
                width: "400px",
                title: "New Job"
            }
        }, 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]");
    },

    get_checkBox: function () {
        return this.get_node().find("INPUT[type=checkbox]");
    },

    //===================================
    // 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 () {

        var that = this,
            isValidateOnly = this.get_checkBox().is(":checked");

        if (!isValidateOnly) {
            this._showConfirmationDialog(function () {
                that._executeJob();
            });
        }
        else {
            this._executeJob();
        }

    },

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

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

    template: function (e) {

        return [
            "<div class='-pd-data-load-new-job-dialog'>",
                "<div class='-pd-dialog-content'>",
                    "<div>",
                        "<div style='margin-bottom:5px;font-size:12px;'>Data Load Spreadsheet:</div>",
                        "<div data-id='import-spreadsheet' style='margin-bottom: 10px;'></div>",
                        "<label class='-pd-checkbox'>",
                            "<input type='checkbox' />",
                            "<div>",
                                "<div class='-pd-checkbox-label'>Validate Only</div>",
                                "When this option is selected, the specified import spreadsheet ",
                                "will be checked for errors, but no data will be imported.",
                            "</div>",
                        "</label>",
                    "</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-spreadsheet]",
            uploadStart: function (e) {
                that.get_okButton().prop("disabled", true);
            },
            uploadComplete: function (e) {
                that.get_okButton().prop("disabled", false);
            }
        });

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

    },

    _executeJob: function () {

        var that = this,
            uploadId = this._uploadInput.get_uploadId(),
            uploadName = this._uploadInput.get_uploadName(),
            isValidateOnly = this.get_checkBox().is(":checked"),
            service = new PD.Manage.Services.DataLoadService();

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

        service.createJob(uploadId, uploadName, isValidateOnly, function (e) {
            that.close({
                taskId: e.taskId
            });
            that._showTaskStatusDialog(e);
        }, function (e) {
            that.get_okButton().prop("disabled", false);
        });

    },

    _showConfirmationDialog: function (callback) {

        PD.Web.UI.MessageDialog.warn({
            title: "Execute Data Load Job?",
            message: "<b>WARNING:</b> You are about to execute a data load job. This may result in the modification of application data.<br/><br/>Do you wish to continue?",
            ok: function (e) {
                e.close();
                callback(e);
            }
        });

    },

    _showTaskStatusDialog: function (e) {

        PD.Shared.Dialogs.TaskStatus.open({
            config: {
                title: "Data Load Job Progress"
            },
            showDetail: true,
            taskStart: function (args) {
                args.startPolling(e);
            },
            close: function (args) {

                PD.context
                    .get_default()
                    .get_events()
                    .publish("jobs/complete", {
                        id: e.taskId
                    });

            }
        });

    }

});

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