<?php
/**
 * View: Demo Oracle — optimisation SQL.
 *
 * Portage de modules/oracle/views/report/config_sqltune.php. Bascule de la
 * tache automatique, edition d'un parametre et suppression d'un profil SQL
 * restent affichees, desactivees. La fenetre d'edition et le JavaScript qui
 * postait les actions ont ete retires.
 *
 * View — Config DBMS_SQLTUNE (Auto SQL Tuning Advisor).
 *
 * @var string|null $fetchError
 * @var array       $autoTask
 * @var array       $params
 * @var array       $profiles
 * @var array       $executions
 * @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.cfgsqlt_title') ?></h1></div>
<p style="font-size:.85rem;color:var(--color-text-muted);margin:.25rem 0 .75rem"><?= t('demo_oracle.cfgsqlt_hint') ?></p>

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

<!-- ═══ Section 1 : Tâche automatique ════════════════════════════════════ -->
<?php if (!empty($autoTask)): ?>
<?php $atEnabled = (($autoTask['STATUS'] ?? '') === 'ENABLED'); ?>
<h2 style="font-size:.95rem;margin:.75rem 0 .35rem">🤖 <?= t('demo_oracle.cfgsqlt_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; ?>

<!-- ═══ Section 2 : Paramètres ═══════════════════════════════════════════ -->
<?php if (!empty($params)): ?>
<h2 style="font-size:.95rem;margin:.75rem 0 .35rem">⚙ <?= t('demo_oracle.cfgsqlt_params') ?></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 ?>">type</th>
        <th style="text-align:center;padding:.3rem .5rem">default</th>
        <th style="<?= $thS ?>">description</th>
        <th style="<?= $thS ?>;width:60px"></th>
    </tr></thead>
    <tbody>
    <?php foreach ($params as $p):
        $isDef = (($p['IS_DEFAULT'] ?? '') === 'TRUE');
    ?>
    <tr style="<?= $br ?>">
        <td style="<?= $tdS ?>;font-weight:600"><?= htmlspecialchars($p['PARAMETER_NAME'] ?? '') ?></td>
        <td style="<?= $tdM ?>"><?= htmlspecialchars($p['PARAMETER_VALUE'] ?? '') ?></td>
        <td style="<?= $tdS ?>;font-size:.74rem;color:var(--color-text-muted)"><?= htmlspecialchars($p['PARAMETER_TYPE'] ?? '') ?></td>
        <td style="text-align:center;<?= $tdS ?>">
            <?= $isDef ? '<span style="color:var(--color-success)">✓</span>' : '<span style="color:var(--color-warning)">✎</span>' ?>
        </td>
        <td style="<?= $tdS ?>;font-size:.74rem;max-width:320px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"
            title="<?= htmlspecialchars($p['DESCRIPTION'] ?? '') ?>"><?= htmlspecialchars($p['DESCRIPTION'] ?? '') ?></td>
        <td style="<?= $tdS ?>;text-align:center">
            <button type="button" class="btn btn-sm btn-secondary"<?= DemoOracleDemo::disabled() ?>>&#9998;</button>
        </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- ═══ Section 3 : SQL Profiles ═════════════════════════════════════════ -->
<?php if (!empty($profiles)): ?>
<h2 style="font-size:.95rem;margin:.75rem 0 .35rem">📌 SQL Profiles (<?= count($profiles) ?>)</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:.78rem">
    <thead><tr style="<?= $hd ?>">
        <th style="<?= $thS ?>">name</th>
        <th style="<?= $thS ?>">category</th>
        <th style="<?= $thS ?>">status</th>
        <th style="text-align:center;padding:.3rem .5rem">force</th>
        <th style="<?= $thS ?>"><?= t('demo_oracle.cfgsqlt_created') ?></th>
        <th style="<?= $thS ?>">sql_text</th>
        <th style="<?= $thS ?>"><?= t('demo_oracle.col_actions') ?></th>
    </tr></thead>
    <tbody>
    <?php foreach ($profiles as $p):
        $ok = (($p['STATUS'] ?? '') === 'ENABLED');
    ?>
    <tr style="<?= $br ?>">
        <td style="<?= $tdS ?>;font-weight:600;font-size:.74rem"><?= htmlspecialchars($p['NAME'] ?? '') ?></td>
        <td style="<?= $tdS ?>"><?= htmlspecialchars($p['CATEGORY'] ?? '') ?></td>
        <td style="<?= $tdS ?>"><span class="badge <?= $ok ? 'badge-success' : 'badge-warning' ?>" style="font-size:.68rem"><?= htmlspecialchars($p['STATUS'] ?? '') ?></span></td>
        <td style="text-align:center;<?= $tdS ?>"><?= ($p['FORCE_MATCHING'] ?? '') === 'YES' ? '✓' : '' ?></td>
        <td style="<?= $tdM ?>"><?= htmlspecialchars($p['CREATED'] ?? '') ?></td>
        <td style="<?= $tdM ?>;max-width:350px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"
            title="<?= htmlspecialchars($p['SQL_TEXT'] ?? '') ?>"><?= htmlspecialchars($p['SQL_TEXT'] ?? '') ?></td>
        <td style="padding:.3rem .5rem">
            <button class="btn btn-sm btn-danger"<?= DemoOracleDemo::disabled(t('demo_oracle.cfgsqlt_drop_profile')) ?>>&#10005;</button>
        </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- ═══ Section 4 : Dernières exécutions ═════════════════════════════════ -->
<?php if (!empty($executions)): ?>
<h2 style="font-size:.95rem;margin:.75rem 0 .35rem">📅 <?= t('demo_oracle.cfgsqlt_executions') ?></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 ?>">execution</th>
        <th style="<?= $thS ?>"><?= t('demo_oracle.bksum_start') ?></th>
        <th style="<?= $thS ?>"><?= t('demo_oracle.bksum_end') ?></th>
        <th style="<?= $thS ?>">status</th>
        <th style="<?= $thS ?>">error</th>
    </tr></thead>
    <tbody>
    <?php foreach ($executions as $e):
        $st = (string)($e['STATUS'] ?? '');
        $ok = stripos($st, 'COMPLETED') !== false;
    ?>
    <tr style="<?= $br ?>">
        <td style="<?= $tdS ?>;font-size:.74rem"><?= htmlspecialchars($e['EXECUTION_NAME'] ?? '') ?></td>
        <td style="<?= $tdM ?>"><?= htmlspecialchars($e['EXEC_START'] ?? '') ?></td>
        <td style="<?= $tdM ?>"><?= htmlspecialchars($e['EXEC_END']   ?? '') ?></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($e['ERROR_MESSAGE'] ?? '') ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- ═══ Section 5 : Historique autotask ══════════════════════════════════ -->
<?php if (!empty($autoHistory)): ?>
<h2 style="font-size:.95rem;margin:.75rem 0 .35rem">📜 <?= t('demo_oracle.cfgsqlt_history') ?></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 ?>">window</th>
        <th style="<?= $thS ?>"><?= t('demo_oracle.bksum_start') ?></th>
        <th style="<?= $thS ?>"><?= t('demo_oracle.bksum_end') ?></th>
        <th style="<?= $thS ?>">duration</th>
        <th style="<?= $thS ?>">status</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>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

