<?php
// Copyright 1999-2017. Plesk International GmbH. All Rights Reserved.

use Plesk\MigrationLibV8\Plesk\PleskFunctions;
use PleskExt\PanelMigrator\AdvancedMode;

if (pm_ProductInfo::isWindows()) {
    $this->passVariablesToJavaScript([
        'MESSAGE_LABEL_BUILT_IN_ADMIN_LOGIN' => $this->lmsg('newMigrationFormBuiltInAdminLogin'),
        'MESSAGE_LABEL_BUILT_IN_ADMIN_PASSWORD' => $this->lmsg('newMigrationFormBuiltInAdminPassword'),
        'MESSAGE_LABEL_HELM_ADMIN_LOGIN' => $this->lmsg('newMigrationFormHelmAdminLogin'),
        'MESSAGE_LABEL_HELM_ADMIN_PASSWORD' => $this->lmsg('newMigrationFormHelmAdminPassword'),
        'MESSAGE_DOWNLOAD_RPC_AGENT' => $this->lmsg('downloadRpcAgent'),
        'URL_DOWNLOAD_RPC_AGENT' => $this->urlDownloadRpcAgent,
    ]);
    $this->includeMigratorJavaScript('new-migration-windows.js');
    if (PleskFunctions::hasRemoteSmarterMail()) {
        $this->includeMigratorJavaScript('new-migration-remote-smarter-mail.js');
    }
} else {
    $this->includeMigratorJavaScript('new-migration-unix.js');
}

if (AdvancedMode::isEnabled()) {
    $this->includeMigratorJavaScript('new-migration-custom.js');
}

$this->passVariablesToJavaScript([
    'URL_SELECT_OBJECTS_TO_MIGRATE' => $this->urlSelectObjectsToMigrate,
]);
$this->includeMigratorCss('image-selector.css');
?>
<?php echo $this->form ?>
<?php echo $this->popupFetchSource ?>
<script>
    //<![CDATA[
    Jsw.onReady(function() {
        // Hack to make popup after form is submitted and processed at controller side:
        // redefine _onSuccess function, which will display popup and call the original _onSuccess
        // function.
        var formComponent = Jsw.getComponent('new-migration-form');
        var oldFunction = formComponent._onSuccess;
        formComponent._onSuccess = function(req) {
            var response = req.responseText.evalJSON();
            var result = oldFunction.apply(this, arguments);
            if (response.session) {
                // Fill session field, so new session is not created on new form submit (for example in case of error).
                // This field should be handled by controller code.
                var elemSessionField = $('session');
                elemSessionField.value = response.session;

                if (response.success) {
                    // Show progress popup dialog.
                    showFetchSourcePopup(
                        response.session,
                        // On success function
                        function () {
                            Jsw.redirect(URL_SELECT_OBJECTS_TO_MIGRATE + '/session/' + response.session);
                        },
                        // On error function
                        function (errorId, errorMessage) {
                            // Add error message as status message to the top of the form, so user will still
                            // see the error message after popup is closed.
                            var message = formatStr(errorMessage);

                            if (errorId && errorId.indexOf('RPC_AGENT_') === 0) {
                                message += (
                                    '<div style="padding-top: 15px;">' +
                                        '<a href="' + URL_DOWNLOAD_RPC_AGENT + '">' +
                                            MESSAGE_DOWNLOAD_RPC_AGENT +
                                        '</a>' +
                                    '<div>'
                                );
                            }

                            Jsw.addStatusMessage('error', message);
                        }
                    );
                }
            }
            return result;
        }
    });
    //]]>
</script>
