<?php
/**
 * View — Dashboard Exports / Imports / Tables externes.
 *
 * @var string|null $fetchError
 * @var array       $dpJobs
 * @var array       $dpLongOps
 * @var array       $dirs
 * @var array       $extTables
 * @var array       $locsByTable
 */
?>

<div class="page-header"><h1 style="margin:0;font-size:1.05rem">📦 <?= t('demo_oracle.dash_exports_title') ?></h1></div>
<p style="font-size:.85rem;color:var(--color-text-muted);margin:.25rem 0 .75rem">
    <?= t('demo_oracle.dash_exports_hint') ?>
</p>

<?php if ($fetchError): ?>
<div class="alert alert-error"><?= htmlspecialchars($fetchError) ?></div>
<?php endif; ?>

<!-- Data Pump jobs -->
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.dash_exports_dp_jobs') ?> (DBA_DATAPUMP_JOBS)
</h2>
<?php if (empty($dpJobs)): ?>
<div class="card" style="margin-bottom:.75rem"><p style="margin:0;color:var(--color-text-muted)"><?= t('demo_oracle.dash_exports_no_dp') ?></p></div>
<?php else: ?>
<div class="card" style="padding:0;overflow-x:auto;margin-bottom:.75rem">
<table class="data-table" style="width:100%;border-collapse:collapse;font-size:.82rem">
    <thead>
        <tr style="background:var(--color-input-bg,#f8f8f8);border-bottom:1px solid var(--color-border)">
            <th style="text-align:left;padding:.3rem .5rem">owner</th>
            <th style="text-align:left;padding:.3rem .5rem">job_name</th>
            <th style="text-align:left;padding:.3rem .5rem">operation</th>
            <th style="text-align:left;padding:.3rem .5rem">job_mode</th>
            <th style="text-align:left;padding:.3rem .5rem">state</th>
            <th style="text-align:right;padding:.3rem .5rem">degree</th>
            <th style="text-align:right;padding:.3rem .5rem">attached</th>
            <th style="text-align:right;padding:.3rem .5rem">dp_sessions</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($dpJobs as $j):
        $state = (string)($j['STATE'] ?? '');
        $stateColor = match (true) {
            $state === 'EXECUTING'                    => 'var(--color-primary)',
            in_array($state, ['DEFINING','IDLING','NOT RUNNING'], true) => 'var(--color-text-muted)',
            default                                   => 'var(--color-warning)',
        };
    ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem"><code><?= htmlspecialchars($j['OWNER_NAME'] ?? '') ?></code></td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.78rem"><?= htmlspecialchars($j['JOB_NAME']  ?? '') ?></td>
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($j['OPERATION'] ?? '') ?></td>
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($j['JOB_MODE']  ?? '') ?></td>
            <td style="padding:.3rem .5rem;color:<?= $stateColor ?>;font-weight:600"><?= htmlspecialchars($state) ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($j['DEGREE']            ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($j['ATTACHED_SESSIONS'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($j['DATAPUMP_SESSIONS'] ?? '') ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- Data Pump long ops -->
<?php if (!empty($dpLongOps)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.dash_exports_long_ops') ?> (GV$SESSION_LONGOPS)
</h2>
<div class="card" style="padding:0;overflow-x:auto;margin-bottom:.75rem">
<table class="data-table" style="width:100%;border-collapse:collapse;font-size:.82rem">
    <thead>
        <tr style="background:var(--color-input-bg,#f8f8f8);border-bottom:1px solid var(--color-border)">
            <th style="text-align:left;padding:.3rem .5rem">sid</th>
            <th style="text-align:left;padding:.3rem .5rem">user</th>
            <th style="text-align:left;padding:.3rem .5rem">opname</th>
            <th style="text-align:left;padding:.3rem .5rem">target</th>
            <th style="text-align:right;padding:.3rem .5rem">progress</th>
            <th style="text-align:left;padding:.3rem .5rem">start_time</th>
            <th style="text-align:right;padding:.3rem .5rem">elapsed (s)</th>
            <th style="text-align:right;padding:.3rem .5rem">remaining (s)</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($dpLongOps as $lo):
        $total = (float)($lo['TOTALWORK'] ?? 0);
        $sofar = (float)($lo['SOFAR']     ?? 0);
        $pct   = $total > 0 ? round($sofar * 100 / $total, 1) : 0;
    ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem"><code><?= (int)$lo['INST_ID'] ?>:<?= (int)$lo['SID'] ?>,<?= (int)$lo['SERIAL_NUM'] ?></code></td>
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($lo['USERNAME'] ?? '') ?></td>
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($lo['OPNAME']   ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-size:.76rem"><?= htmlspecialchars($lo['TARGET']  ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= $pct ?> %</td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.76rem"><?= htmlspecialchars($lo['START_TIME']      ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= (int)($lo['ELAPSED_SECONDS'] ?? 0) ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= (int)($lo['TIME_REMAINING']  ?? 0) ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- Directories -->
<?php if (!empty($dirs)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.dash_exports_dirs') ?> (DBA_DIRECTORIES, <?= count($dirs) ?>)
</h2>
<div class="card" style="padding:0;overflow-x:auto;margin-bottom:.75rem">
<table class="data-table" style="width:100%;border-collapse:collapse;font-size:.82rem">
    <thead>
        <tr style="background:var(--color-input-bg,#f8f8f8);border-bottom:1px solid var(--color-border)">
            <th style="text-align:left;padding:.3rem .5rem">directory_name</th>
            <th style="text-align:left;padding:.3rem .5rem">owner</th>
            <th style="text-align:left;padding:.3rem .5rem">directory_path</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($dirs as $d): ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem"><strong><code><?= htmlspecialchars($d['DIRECTORY_NAME'] ?? '') ?></code></strong></td>
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($d['OWNER']          ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.76rem"><?= htmlspecialchars($d['DIRECTORY_PATH'] ?? '') ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- External tables -->
<?php if (!empty($extTables)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.dash_exports_extables') ?> (DBA_EXTERNAL_TABLES, <?= count($extTables) ?>)
</h2>
<p style="font-size:.78rem;color:var(--color-text-muted);margin:0 0 .35rem">
    <?= t('demo_oracle.dash_exports_extables_hint') ?>
</p>
<div class="card" style="padding:0;overflow-x:auto;margin-bottom:.75rem">
<table class="data-table" style="width:100%;border-collapse:collapse;font-size:.82rem">
    <thead>
        <tr style="background:var(--color-input-bg,#f8f8f8);border-bottom:1px solid var(--color-border)">
            <th style="text-align:left;padding:.3rem .5rem">owner</th>
            <th style="text-align:left;padding:.3rem .5rem">table_name</th>
            <th style="text-align:left;padding:.3rem .5rem">type_name</th>
            <th style="text-align:left;padding:.3rem .5rem">default_directory</th>
            <th style="text-align:left;padding:.3rem .5rem">access_type</th>
            <th style="text-align:right;padding:.3rem .5rem">reject_limit</th>
            <th style="text-align:left;padding:.3rem .5rem">locations</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($extTables as $et):
        $k = ($et['OWNER'] ?? '') . '|' . ($et['TABLE_NAME'] ?? '');
        $locs = $locsByTable[$k] ?? [];
    ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem"><code><?= htmlspecialchars($et['OWNER']      ?? '') ?></code></td>
            <td style="padding:.3rem .5rem">
                <?php /* Le module complet fait de ce nom un lien vers le detail
                         de l'objet — statistiques, colonnes, index, DDL. Cette
                         page ne fait pas partie de la demonstration : le nom
                         reste du texte. */ ?>
                <strong><?= htmlspecialchars($et['TABLE_NAME'] ?? '') ?></strong>
            </td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.76rem">
                <?= htmlspecialchars(($et['TYPE_OWNER'] ?? '') . '.' . ($et['TYPE_NAME'] ?? '')) ?>
            </td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.76rem">
                <?= htmlspecialchars(($et['DEFAULT_DIRECTORY_OWNER'] ?? '') . '.' . ($et['DEFAULT_DIRECTORY_NAME'] ?? '')) ?>
            </td>
            <td style="padding:.3rem .5rem;font-size:.76rem;max-width:280px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"><?= htmlspecialchars($et['ACCESS_TYPE'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($et['REJECT_LIMIT'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.74rem">
                <?php foreach ($locs as $i => $loc): ?>
                <?php if ($i > 0): ?><br><?php endif; ?>
                <?= htmlspecialchars(($loc['DIRECTORY_NAME'] ?? '') . ':' . ($loc['LOCATION'] ?? '')) ?>
                <?php endforeach; ?>
            </td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>