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

/**
 * @var \PleskExt\PanelMigrator\Backend\Report $report
 */
$report = $this->report;

$affectedObjects = [];
foreach ($report->getAllIssues() as $issueNum => $issue) {
    $affectedObjects[$issueNum] = $issue->getAffectedObjects();
}
echo $this->popupPreChecks;

$this->passVariablesToJavaScript([
    'URL_START_MIGRATION' => $this->urlStartMigration,
    'URL_REFRESH_PRE_MIGRATION_CHECKS' => $this->urlRefreshPreMigrationChecks,
    'URL_DISCARD_PRE_MIGRATION_CHECKS' => $this->urlDiscardPreMigrationChecks,
    'URL_MIGRATION_STATUS' => $this->urlMigrationStatus,
    'URL_LIST_OF_SUBSCRIPTIONS' => $this->urlListOfSubscriptions,
    'AFFECTED_OBJECTS' => $affectedObjects,
    'MAX_AFFECTED_OBJECTS_INLINE' => 3,
    'TASK_ID' => $this->taskId,
    'REDIRECT_TO_LIST_OF_SUBSCRIPTIONS' => $this->redirectToListOfSubscriptions,
]);
$this->includeMigratorJavaScript('locale.js');
$this->addJavaScriptLocaleKeys([
    'preMigrationResultsTitleAffectedObjects',
    'preMigrationResultsTitleAffectedSubscriptions',
    'preMigrationResultsTitleAffectedMssqlServers',
    'preMigrationResultsDescriptionAffectedObjects',
    'preMigrationResultsDescriptionAffectedSubscriptions',
    'preMigrationResultsDescriptionAffectedMssqlServers',
    'preMigrationResultsAffectedObjectButtonClose',
    'preMigrationResultsButtonContinueHint',
    'preMigrationResultsButtonRefreshHint',
    'preMigrationResultsButtonCloseHint',
    'preMigrationResultsButtonDiscardHint',
    'waitButtonTitle',
]);
$this->includeMigratorJavaScript('pre-migration-check-results.js');
$this->includeMigratorCss('pre-migration-check-results.css');

function severityIcon($severity) {
    if ($severity == \PleskExt\PanelMigrator\Backend\Issue::SEVERITY_ERROR) {
        return 'subscription-status-error.png';
    } else if ($severity == \PleskExt\PanelMigrator\Backend\Issue::SEVERITY_WARNING) {
        return 'subscription-status-warning.png';
    } else {
        return 'subscription-status-success.png';
    }
}

$issues = $report->getAllIssues();

?>
<?php if (count($issues) > 0) { ?>
    <table>
    <?php foreach ($report->getAllIssues() as $issueNum => $issue) { ?>
        <tr class="<?php echo $issueNum % 2 ? 'odd' : 'even';?>">
            <td style="padding: 6px; 8px;">
                <div style="font-weight: bold"><?php echo htmlspecialchars($issue->getReportPathText()); ?></div>
                <div class="problem-text">
                    <img src="<?php echo $this->migratorImage(severityIcon($issue->getSeverity())); ?>" style="width: 1em; height: 1em;">
                    <?php echo nl2br(htmlspecialchars($issue->getProblemText())); ?>
                </div>
                <?php if (!empty($issue->getAffectedObjects())) {?>
                    <div class="affected-objects" style="display: none;">
                        <div class="issue-num" style="display: none"><?php echo $issueNum; ?></div>
                    </div>
                <?php } ?>
                <?php if ($issue->getSolutionText()) { ?>
                    <div class="solution-text"><?php echo nl2br(htmlspecialchars($issue->getSolutionText())); ?></div>
                <?php } ?>
                <?php if ($issue->getSoltionDownloadRpcAgent()) { ?>
                    <div class="solution-download-rpc-agent">
                        <img src="<?php echo $this->migratorImage('download-rpc-agent.png');   ?>" style="width: 16px; height 16px;">&nbsp;
                        <a href="<?php echo $this->urlDownloadRpcAgent; ?>"><?php echo $this->lmsg('downloadRpcAgent'); ?></a>
                    </div>
                <?php } ?>
            </td>
        </tr>
    <?php } ?>
    </table>
<?php } else { ?>
    <?php echo $this->lmsg('preMigrationResultsNoIssues'); ?>
<?php } ?>
<div class="btns-box">
    <span class="btn action">
        <button name="continue" type="button" id="pre-checks-continue"><?php echo $this->lmsg('preMigrationResultsButtonContinue'); ?></button>
    </span>
    <span class="btn">
        <button name="refresh" type="button" id="pre-checks-refresh"><?php echo $this->lmsg('preMigrationResultsButtonRefresh'); ?></button>
    </span>
    <span class="btn">
        <button name="close" type="button" id="pre-checks-close"><?php echo $this->lmsg('preMigrationResultsButtonClose'); ?></button>
    </span>
    <span class="btn">
        <button name="discard" type="button" id="pre-checks-discard"><?php echo $this->lmsg('preMigrationResultsButtonDiscard'); ?></button>
    </span>
</div>
