<?php
/**
 * View: Demo Oracle — calcul des statistiques.
 *
 * Portage de modules/oracle/views/report/config_stats.php. Les trois commandes
 * du module complet (modifier une preference, planifier un calcul, basculer la
 * tache automatique) restent affichees, desactivees : le visiteur voit ce que
 * fait la version complete. La fenetre d'edition et le JavaScript qui postait
 * les actions ont ete retires — il n'y a rien a poster.
 *
 * View — Config DBMS_STATS.
 *
 * @var string|null $fetchError
 * @var array       $prefs
 * @var array       $prefNames
 * @var array       $sysStats
 * @var array       $schemaStats
 * @var array       $dictStats
 * @var array       $autoTask
 * @var array       $autoHistory
 * @var object      $settings
 */
$thS = 'text-align:left;padding:.3rem .5rem';
$tdS = 'padding:.3rem .5rem';
$tdM = 'padding:.3rem .5rem;font-family:monospace;font-size:.76rem';
$tdR = 'padding:.3rem .5rem;text-align:right';
$hd  = 'background:var(--color-input-bg,#f8f8f8);border-bottom:1px solid var(--color-border)';
$br  = 'border-bottom:1px solid var(--color-border)';
?>

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

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

<!-- ═══ Section 1 : Préférences globales DBMS_STATS ══════════════════════ -->
<?php if (!empty($prefs)): ?>
<h2 style="font-size:.95rem;margin:.75rem 0 .35rem">⚙ <?= t('demo_oracle.cfgstats_prefs') ?></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="<?= $hd ?>">
        <th style="<?= $thS ?>"><?= t('demo_oracle.cfgstats_pref_name') ?></th>
        <th style="<?= $thS ?>"><?= t('demo_oracle.cfgstats_pref_value') ?></th>
        <th style="<?= $thS ?>;width:60px"></th>
    </tr></thead>
    <tbody>
    <?php foreach ($prefs as $pn => $pv): ?>
    <tr style="<?= $br ?>">
        <td style="<?= $tdS ?>;font-weight:600"><?= htmlspecialchars($pn) ?></td>
        <td style="<?= $tdM ?>"><?= htmlspecialchars($pv) ?></td>
        <td style="<?= $tdS ?>;text-align:center">
            <?php /* Le crayon ouvre, dans le module complet, la fenetre qui
                     appelle DBMS_STATS.SET_GLOBAL_PREFS. Il reste affiche ici
                     pour dire que la fonction existe. */ ?>
            <button type="button" class="btn btn-sm btn-secondary"<?= DemoOracleDemo::disabled() ?>>&#9998;</button>
        </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- ═══ Section 2 : Statistiques par schéma ══════════════════════════════ -->
<?php if (!empty($schemaStats)): ?>
<h2 style="font-size:.95rem;margin:.75rem 0 .35rem">📋 <?= t('demo_oracle.cfgstats_schema') ?></h2>
<p style="font-size:.78rem;color:var(--color-text-muted);margin:0 0 .35rem"><?= t('demo_oracle.cfgstats_schema_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:.78rem">
    <thead><tr style="<?= $hd ?>">
        <th style="<?= $thS ?>" rowspan="2"><?= t('demo_oracle.cfgstats_schema_owner') ?></th>
        <th style="text-align:center;padding:.2rem .5rem;border-bottom:1px solid var(--color-border)" colspan="3">Tables</th>
        <th style="text-align:center;padding:.2rem .5rem;border-bottom:1px solid var(--color-border)" colspan="3">Index</th>
        <th style="text-align:center;padding:.2rem .5rem;border-bottom:1px solid var(--color-border)" colspan="3">Part. tables</th>
        <th style="text-align:center;padding:.2rem .5rem;border-bottom:1px solid var(--color-border)" colspan="3">Part. index</th>
    </tr><tr style="<?= $hd ?>">
        <?php for ($i = 0; $i < 4; $i++): ?>
        <th style="<?= $tdM ?>"><?= t('demo_oracle.cfgstats_col_last') ?></th>
        <th style="<?= $tdR ?>;font-size:.72rem">#</th>
        <th style="<?= $tdR ?>;font-size:.72rem">⚠</th>
        <?php endfor; ?>
    </tr></thead>
    <tbody>
    <?php foreach ($schemaStats as $s):
        $types = [
            ['LAST_TABLE','CNT_TABLE','OBS_TABLE'],
            ['LAST_INDEX','CNT_INDEX','OBS_INDEX'],
            ['LAST_TAB_PART','CNT_TAB_PART','OBS_TAB_PART'],
            ['LAST_IDX_PART','CNT_IDX_PART','OBS_IDX_PART'],
        ];
    ?>
    <tr style="<?= $br ?>">
        <td style="<?= $tdS ?>;font-weight:600"><?= htmlspecialchars($s['OWNER'] ?? '') ?></td>
        <?php foreach ($types as [$lk, $ck, $ok]):
            $cnt = (int)($s[$ck] ?? 0);
            $obs = (int)($s[$ok] ?? 0);
            $last = $s[$lk] ?? '';
            $color = $obs > 0 ? 'color:var(--color-warning)' : '';
        ?>
        <td style="<?= $tdM ?>;<?= $color ?>"><?= $last ?: '—' ?></td>
        <td style="<?= $tdR ?>;font-size:.72rem"><?= $cnt ?: '' ?></td>
        <td style="<?= $tdR ?>;font-size:.72rem;<?= $obs > 0 ? 'color:var(--color-danger);font-weight:700' : 'color:var(--color-text-muted)' ?>"><?= $obs ?: '' ?></td>
        <?php endforeach; ?>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- ═══ Planifier un calcul système / dictionnaire ══════════════════════ -->
<div style="margin:.5rem 0 .75rem;display:flex;gap:.6rem;align-items:center;flex-wrap:wrap">
    <button type="button" class="btn btn-sm btn-primary"<?= DemoOracleDemo::disabled() ?>><?= t('demo_oracle.cfgstats_schedule_btn') ?></button>
    <?= DemoOracleDemo::badge() ?>
</div>

<!-- ═══ Section 3 : Statistiques système (AUX_STATS$) ═══════════════════ -->
<?php if (!empty($sysStats)): ?>
<h2 style="font-size:.95rem;margin:.75rem 0 .35rem">🖥 <?= t('demo_oracle.cfgstats_sysstats') ?></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="<?= $hd ?>">
        <th style="<?= $thS ?>">sname</th>
        <th style="<?= $thS ?>">pname</th>
        <th style="<?= $tdR ?>">pval1</th>
        <th style="<?= $thS ?>">pval2</th>
    </tr></thead>
    <tbody>
    <?php foreach ($sysStats as $r): ?>
    <tr style="<?= $br ?>">
        <td style="<?= $tdS ?>"><?= htmlspecialchars($r['SNAME'] ?? '') ?></td>
        <td style="<?= $tdS ?>;font-weight:600"><?= htmlspecialchars($r['PNAME'] ?? '') ?></td>
        <td style="<?= $tdR ?>;font-family:monospace"><?= htmlspecialchars($r['PVAL1'] ?? '') ?></td>
        <td style="<?= $tdM ?>"><?= htmlspecialchars($r['PVAL2'] ?? '') ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- ═══ Section 4 : Statistiques dictionnaire ════════════════════════════ -->
<?php if (!empty($dictStats)): ?>
<h2 style="font-size:.95rem;margin:.75rem 0 .35rem">📖 <?= t('demo_oracle.cfgstats_dict') ?></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="<?= $hd ?>">
        <th style="<?= $thS ?>">Table</th>
        <th style="<?= $thS ?>"><?= t('demo_oracle.cfgstats_col_last') ?></th>
        <th style="<?= $tdR ?>">num_rows</th>
    </tr></thead>
    <tbody>
    <?php foreach ($dictStats as $r): ?>
    <tr style="<?= $br ?>">
        <td style="<?= $tdS ?>;font-weight:600;font-family:monospace"><?= htmlspecialchars($r['TABLE_NAME'] ?? '') ?></td>
        <td style="<?= $tdM ?>"><?= htmlspecialchars($r['LAST_ANALYZED'] ?? '—') ?></td>
        <td style="<?= $tdR ?>;font-family:monospace"><?= $r['NUM_ROWS'] !== null ? number_format((float)$r['NUM_ROWS'], 0, '.', ' ') : '—' ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- ═══ Section 5 : Tâche automatique ════════════════════════════════════ -->
<?php if (!empty($autoTask)): ?>
<?php $atEnabled = (($autoTask['STATUS'] ?? '') === 'ENABLED'); ?>
<h2 style="font-size:.95rem;margin:.75rem 0 .35rem">🤖 <?= t('demo_oracle.cfgstats_autotask') ?></h2>
<div class="card" style="padding:.55rem .7rem;margin-bottom:.5rem;display:flex;align-items:center;gap:.75rem;flex-wrap:wrap">
    <span style="font-size:.85rem;font-weight:600"><?= htmlspecialchars($autoTask['CLIENT_NAME'] ?? '') ?></span>
    <span class="badge <?= $atEnabled ? 'badge-success' : 'badge-danger' ?>"><?= $atEnabled ? 'ENABLED' : 'DISABLED' ?></span>
    <?php if (!empty($autoTask['WINDOW_GROUP'])): ?>
    <span style="font-size:.78rem;color:var(--color-text-muted)">Window: <?= htmlspecialchars($autoTask['WINDOW_GROUP']) ?></span>
    <?php endif; ?>
    <span style="margin-left:auto;display:inline-flex;align-items:center;gap:.5rem">
        <button type="button" class="btn btn-sm <?= $atEnabled ? 'btn-danger' : 'btn-primary' ?>"<?= DemoOracleDemo::disabled() ?>>
            <?= $atEnabled ? t('demo_oracle.cfgstats_disable') : t('demo_oracle.cfgstats_enable') ?>
        </button>
        <?= DemoOracleDemo::badge() ?>
    </span>
</div>
<?php endif; ?>

<?php if (!empty($autoHistory)): ?>
<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="<?= $hd ?>">
        <th style="<?= $thS ?>">window</th>
        <th style="<?= $thS ?>"><?= t('demo_oracle.cfgstats_col_last') ?></th>
        <th style="<?= $thS ?>"><?= t('demo_oracle.bksum_end') ?></th>
        <th style="<?= $thS ?>">duration</th>
        <th style="<?= $thS ?>">status</th>
        <th style="<?= $thS ?>">info</th>
    </tr></thead>
    <tbody>
    <?php foreach ($autoHistory as $h):
        $st = (string)($h['JOB_STATUS'] ?? '');
        $ok = stripos($st, 'SUCCEEDED') !== false;
    ?>
    <tr style="<?= $br ?>">
        <td style="<?= $tdS ?>"><?= htmlspecialchars($h['WINDOW_NAME'] ?? '') ?></td>
        <td style="<?= $tdM ?>"><?= htmlspecialchars($h['WINDOW_START'] ?? '') ?></td>
        <td style="<?= $tdM ?>"><?= htmlspecialchars($h['WINDOW_END']   ?? '') ?></td>
        <td style="<?= $tdS ?>"><?= htmlspecialchars($h['WINDOW_DURATION'] ?? '') ?></td>
        <td style="<?= $tdS ?>"><span class="badge <?= $ok ? 'badge-success' : 'badge-warning' ?>" style="font-size:.7rem"><?= htmlspecialchars($st) ?></span></td>
        <td style="<?= $tdS ?>;font-size:.74rem;max-width:300px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"><?= htmlspecialchars($h['JOB_INFO'] ?? '') ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

