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

use Plesk\MigrationLibV8\Utils\StringUtils;
use PleskExt\PanelMigrator\AdvancedMode;

echo $this->renderTabs($this->tabs);
echo $this->serverInfoToolbar;
echo $this->progressStatus;
echo $this->popupResync;
echo $this->popupStatusDetails;
echo $this->popupPreChecks;
$this->passVariablesToJavaScript([
    // initial set of data
    'MIGRATION_STATUS_DATA' => $this->migrationStatusData,
    // URL to update migration status data
    'URL_GET_MIGRATION_STATUS' => $this->urlGetMigrationStatus,
    // URL to start queue runner
    'URL_RUN_QUEUE' => $this->urlRunQueue,
    // Period between queue runner starts
    'RUN_QUEUE_PERIOD' => 60 * 1000,
]);
$this->includeMigratorJavaScript('migration-status.js');
$this->includeMigratorCss('migration-status.css');
$this->includeMigratorJavaScript('run-queue-periodically.js');

function printStatusImages($viewScript)
{
    echo <<<EOT
        <img 
            class="migration-status-status-image image-status-success" 
            src="{$viewScript->migratorImage('subscription-status-success.png')}"
            style="display: none;"
        >
        <img 
            class="migration-status-status-image image-status-warning" 
            src="{$viewScript->migratorImage('subscription-status-warning.png')}"
            style="display: none;"
        >
        <img 
            class="migration-status-status-image image-status-failure" 
            src="{$viewScript->migratorImage('subscription-status-error.png')}"
            style="display: none;"
        >
EOT;
}

$this->addJavaScriptLocaleKeys([
    'issuesPopupTitleSubscription',
    'issuesPopupTitleReseller',
    'issuesPopupTitleCustomer',
    'issuesPopupTitleOverall'
]);
?>

<!-- Queue column: subscriptions in progress and queued subscriptions-->
<div class="migration-status-block-queue">
    <div class="migration-status-block-header">
        <?php echo $this->lmsg('migrationQueue'); ?> -
        <img
            src="<?php echo $this->migratorImage('subscription-status-queue.png'); ?>"
            class="migration-status-status-image"
        >&nbsp;
        <span id="queue-count">0</span>
    </div>
    <div>
        <div id="overall-status" style="display: none;">
            <img
                class="migration-status-status-image image-status-in-progress"
                src="<?php echo $this->migratorImage('subscription-status-in-progress.gif'); ?>"
            >
            <span><?php echo $this->lmsg('overallProgress'); ?></span>
            <span> - </span>
            <span id="overall-status-text"></span>
        </div>
        <?php foreach ($this->progressSubscriptions as $subscriptionProgress) { ?>
            <div class="subscription-in-progress" style="display: none;">
                <div>
                    <img
                        class="migration-status-status-image image-status-in-progress"
                        src="<?php echo $this->migratorImage('subscription-status-in-progress.gif'); ?>"
                        style="display: none;"
                    >
                    <img
                        class="migration-status-status-image image-status-on-hold"
                        src="<?php echo $this->migratorImage('subscription-status-queue.png'); ?>"
                        style="display: none;"
                    >
                    <span class="subscription-name" style="display: none;">
                        <?php echo htmlspecialchars($subscriptionProgress->getSubscriptionName()); ?>
                    </span>
                    <span class="subscription-name-normalized" style="display: none;">
                        <?php
                        echo htmlspecialchars(
                            StringUtils::normalizeDomainName(
                                $subscriptionProgress->getSubscriptionName()
                            )
                        );
                        ?>
                    </span>
                    <span class="subscription-name-plain">
                        <?php echo htmlspecialchars($subscriptionProgress->getSubscriptionName()); ?>
                    </span>
                    <span class="subscription-name-link" style="display: none;">
                        <a href=""><?php echo htmlspecialchars($subscriptionProgress->getSubscriptionName()); ?></a>
                    </span>

                    <span class="status-separator"> - </span>
                    <span class="status-text"><?php echo htmlspecialchars($subscriptionProgress->getAction() ?: ''); ?></span>
                </div>
                <?php if (AdvancedMode::isEnabled()) { ?>
                    <div class="details-text-container" style="padding-left: 50px; display: none;">
                        <img
                            class="migration-status-status-image image-status-in-progress"
                            src="<?php echo $this->migratorImage('subscription-status-in-progress-details.gif'); ?>"
                        >
                        <span class="details-text" style="color: #707070">
                            <?php echo htmlspecialchars($subscriptionProgress->getDetails()); ?>
                        </span>
                    </div>
                <?php } ?>
            </div>
        <?php } ?>
    </div>
    <div id="migration-status-block-pre-checks">
        <div class="migration-status-block-header"><?php echo $this->lmsg('preChecks'); ?></div>
        <?php foreach ($this->preCheckTasks as $taskId => $taskInfo) { ?>
            <div style="display: none;" class="migration-status-pre-check-task">
                <div style="display: none;" class="task-id"><?php echo htmlspecialchars($taskId); ?></div>
                <div><a href="javascript:;">Pre-migration checks (<?php echo htmlspecialchars($taskInfo['date']); ?>)</a></div>
            </div>
        <?php } ?>
    </div>
</div>

<!-- Results column -->
<div class="migration-status-block-results">
    <!-- Overall results -->
    <div id="migration-status-block-results-overall">
        <div class="migration-status-block-header"><?php echo $this->lmsg('resultsTitleOverall'); ?></div>
        <div>
            <div class="overallStatus">
                <img
                    src="<?php echo $this->migratorImage('server-info-toolbar-subscriptions.png'); ?>"
                    class="migration-status-status-image"
                >
                <span>Overall</span>
                <span><a class="action-details" href="javascript:;">[<?php echo $this->lmsg('actionDetails'); ?>]</a></span>
            </div>
        </div>
    </div>

    <!-- Per-subscription results -->
    <div id="migration-status-block-results-subscription">
        <div class="migration-status-block-header">
            <?php echo $this->lmsg('resultsTitleSubscriptions'); ?> -
            <img
                src="<?php echo $this->migratorImage('subscription-status-success.png'); ?>"
                class="migration-status-status-image"
            >
            <span id="success-count">0</span>
            <img
                src="<?php echo $this->migratorImage('subscription-status-warning.png'); ?>"
                class="migration-status-status-image"
            >
            <span id="warning-count">0</span>
            <img
                src="<?php echo $this->migratorImage('subscription-status-error.png'); ?>"
                class="migration-status-status-image"
            >
            <span id="failed-count">0</span>
        </div>
        <div>
            <?php foreach ($this->progressSubscriptions as $subscriptionProgress) { ?>
                <div class="subscription-finished" style="display: none;">
                    <?php printStatusImages($this) ?>
                    <span class="subscription-name" style="display: none;">
                        <?php echo htmlspecialchars($subscriptionProgress->getSubscriptionName()); ?>
                    </span>
                    <span class="subscription-name-normalized" style="display: none;">
                        <?php
                            echo htmlspecialchars(
                                StringUtils::normalizeDomainName(
                                    $subscriptionProgress->getSubscriptionName()
                                )
                            );
                        ?>
                    </span>
                    <span class="subscription-name-plain">
                        <?php echo htmlspecialchars($subscriptionProgress->getSubscriptionName()); ?>
                    </span>
                    <span class="subscription-name-link" style="display: none;">
                        <a href=""><?php echo htmlspecialchars($subscriptionProgress->getSubscriptionName()); ?></a>
                    </span>
                    <span>
                        <a class="action-details" href="javascript:;">[<?php echo $this->lmsg('actionDetails'); ?>]</a>
                    </span>
                    <span>
                        <a class="action-resync" href="javascript:;">[<?php echo $this->lmsg('subscriptionActionResync'); ?>]</a>
                    </span>
                </div>
            <?php } ?>
        </div>
    </div>

    <!-- Per-reseller results -->
    <div id="migration-status-block-results-reseller">
        <div class="migration-status-block-header"><?php echo $this->lmsg('resultsTitleResellers') ?></div>
        <div>
            <?php foreach ($this->resellers as $resellerLogin => $resellerInfo) { ?>
                <div class="resellerStatus">
                    <div class="resellerLogin" style="display: none;"><?php echo htmlspecialchars($resellerLogin); ?></div>
                    <img
                        src="<?php echo $this->migratorImage('server-info-toolbar-resellers.png'); ?>"
                        class="migration-status-status-image"
                    >
                    <span class=""><?php echo htmlspecialchars($resellerInfo['contact_name']); ?></span>
                    <span><a class="action-details">[<?php echo $this->lmsg('actionDetails'); ?>]</a></span>
                </div>
            <?php } ?>
        </div>
    </div>

    <!-- Per-customer results -->
    <div id="migration-status-block-results-customer">
        <div class="migration-status-block-header"><?php echo $this->lmsg('resultsTitleCustomers'); ?></div>
        <div>
            <?php foreach ($this->customers as $customerLogin => $customerInfo) { ?>
                <div class="customerStatus">
                    <div class="customerLogin" style="display: none;">
                        <?php echo htmlspecialchars($customerLogin); ?>
                    </div>
                    <img
                        src="<?php echo $this->migratorImage('server-info-toolbar-customers.png'); ?>"
                        class="migration-status-status-image"
                    >
                    <span class=""><?php echo htmlspecialchars($customerInfo['contact_name']); ?></span>
                    <span><a class="action-details">[<?php echo $this->lmsg('actionDetails'); ?>]</a></span>
                </div>
            <?php } ?>
        </div>
    </div>
</div>
&nbsp;
