<?php
/**
 * View — Demo Oracle : SQL Monitor natif.
 *
 * Le rapport le plus parlant de la demonstration : Oracle facture le
 * Real-Time SQL Monitoring au titre du Tuning Pack, et cette page le
 * reconstitue a partir de vues qui ne relevent d'aucune licence.
 *
 * Les plafonds sont ANNONCES, pas dissimules — et ils sont appliques cote
 * serveur par DemoOracleSqlMonFilters::parse(), pas ici.
 *
 * Variables injectees par View::display() :
 *   $baseUrl, $fetchError, $sourceInfo, $filters, $running, $settings
 */
$f       = $filters;
$hasExec = $f['has_exec'];
?>

<div class="page-header" style="display:flex;align-items:center;flex-wrap:wrap;gap:.75rem">
    <h1 style="margin:0">📑 <?= t('demo_oracle.reports_sqlmon') ?></h1>
</div>

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

<div id="sqlmon-notice" class="alert alert-warning" style="display:none"></div>

<?php if ($hasExec): ?>

    <div class="card" style="margin-bottom:1rem">
        <div id="sqlmon-source" style="margin-bottom:.6rem"></div>
        <div id="sqlmon-head"></div>
        <div id="sqlmon-totals"></div>

        <div style="display:flex;flex-wrap:wrap;gap:1rem;align-items:center;
                    margin-top:.8rem;padding-top:.6rem;border-top:1px solid var(--color-border)">
            <a href="<?= htmlspecialchars($baseUrl) ?>" class="btn btn-sm btn-secondary">
                ← <?= t('demo_oracle.sqlmon_exec_list') ?>
            </a>
            <span id="sqlmon-countdown-box" style="font-size:.82rem;color:var(--color-text-muted)">
                <?= t('demo_oracle.sqlmon_refresh_in') ?>
                <strong id="sqlmon-countdown">—</strong>s
                · <?= t('demo_oracle.sqlmon_updated') ?> <strong id="sqlmon-updated">—</strong>
                <?= DemoOracleDemo::badge(t('demo_oracle.sqlmon_refresh_hint')) ?>
            </span>
            <span id="sqlmon-samples" style="font-size:.82rem;color:var(--color-text-muted)"></span>
            <span id="sqlmon-finished" style="display:none;font-size:.82rem">
                <span style="color:var(--color-text-muted)"></span>
                <button type="button" class="btn btn-sm btn-secondary"
                        onclick="sqlmonRestart()" style="margin-left:.5rem">
                    <?= t('demo_oracle.sqlmon_restart') ?>
                </button>
            </span>
        </div>
    </div>

    <h2 style="font-size:1rem;margin:1rem 0 .5rem">
        <?= t('demo_oracle.sqlmon_plan_title') ?>
        <?= DemoOracleDemo::badge(t('demo_oracle.sqlmon_plan_hint')) ?>
    </h2>
    <div class="card" style="padding:0;overflow-x:auto">
        <div id="sqlmon-plan" style="padding:.5rem"></div>
    </div>

    <h2 style="font-size:1rem;margin:1rem 0 .5rem"><?= t('demo_oracle.sqlmon_longops_title') ?></h2>
    <div class="card" style="padding:.5rem;overflow-x:auto">
        <div id="sqlmon-longops"></div>
    </div>

<?php else: ?>

    <div class="card" style="margin-bottom:1rem">
        <div id="sqlmon-source"></div>
        <p style="margin:.6rem 0 0;font-size:.85rem;color:var(--color-text-muted)">
            <?= t('demo_oracle.sqlmon_exec_pick') ?>
        </p>
        <p style="margin:.4rem 0 0;font-size:.85rem;color:var(--color-text-muted)">
            <?= t('demo_oracle.sqlmon_running_only') ?>
        </p>
    </div>

    <form method="get" action="<?= htmlspecialchars(DEMO_ORACLE_BASE . '/report.php') ?>"
          class="card" style="margin-bottom:1rem">
        <input type="hidden" name="section" value="reports/sqlmon">
        <div style="display:flex;flex-wrap:wrap;gap:.75rem;align-items:end">
            <div class="form-group" style="margin:0;flex:1 1 200px">
                <label for="sqlmon_filter" style="font-size:.82rem;color:var(--color-text-muted)">
                    SQL_ID
                </label>
                <input type="text" id="sqlmon_filter" name="sql_id"
                       value="<?= htmlspecialchars($f['sql_id']) ?>"
                       maxlength="13" pattern="[A-Za-z0-9]{1,13}">
            </div>
            <div class="form-actions" style="margin:0">
                <button type="submit" class="btn btn-primary"><?= t('demo_oracle.sqlmon_filter') ?></button>
            </div>
        </div>
    </form>

    <h2 style="font-size:1rem;margin:1rem 0 .5rem">
        ⏱ <?= t('demo_oracle.sqlmon_exec_list') ?>
        <span style="font-size:.8rem;font-weight:400;color:var(--color-text-muted)">
            · <?= t('demo_oracle.sqlmon_updated') ?> <strong id="sqlmon-updated">—</strong>
        </span>
        <?= DemoOracleDemo::badge(t('demo_oracle.sqlmon_limit_hint')) ?>
    </h2>
    <div class="card" style="padding:.5rem;overflow-x:auto">
        <div id="sqlmon-execs">
            <p style="margin:0;color:var(--color-text-muted)"><?= t('demo_oracle.sqlmon_exec_loading') ?></p>
        </div>
    </div>

<?php endif; ?>

<script>
window.DemoOracleReport = window.DemoOracleReport || {};
window.DemoOracleReport.sqlmon = {
    baseUrl:   <?= json_encode($baseUrl) ?>,
    sqlId:     <?= json_encode($f['sql_id']) ?>,
    execId:    <?= json_encode((string)($f['exec_id'] ?? '')) ?>,
    execStart: <?= json_encode($f['exec_start']) ?>,
    phv:       <?= json_encode($f['phv']) ?>,
    instId:    <?= json_encode($f['inst_id']) ?>,
    hasExec:   <?= $hasExec ? 'true' : 'false' ?>,
    refreshMs: <?= (int)$f['refresh'] * 1000 ?>,
    T: {
        src_session:           <?= json_encode(t('demo_oracle.sqlmon_src_session')) ?>,
        src_hint_session:      <?= json_encode(t('demo_oracle.sqlmon_src_hint_session')) ?>,
        licensed_note:         <?= json_encode(t('demo_oracle.sqlmon_licensed_note')) ?>,
        activity_partial:      <?= json_encode(t('demo_oracle.sqlmon_activity_partial')) ?>,
        activity_unattributed: <?= json_encode(t('demo_oracle.sqlmon_activity_unattributed')) ?>,
        counter_rtsm_only:     <?= json_encode(t('demo_oracle.sqlmon_counter_rtsm_only')) ?>,
        status_deduced:        <?= json_encode(t('demo_oracle.sqlmon_status_deduced')) ?>,
        exec_running:          <?= json_encode(t('demo_oracle.sqlmon_exec_running')) ?>,
        exec_done:             <?= json_encode(t('demo_oracle.sqlmon_exec_done')) ?>,
        exec_none:             <?= json_encode(t('demo_oracle.sqlmon_exec_none')) ?>,
        exec_sessions:         <?= json_encode(t('demo_oracle.sqlmon_exec_sessions')) ?>,
        exec_open:             <?= json_encode(t('demo_oracle.sqlmon_exec_open')) ?>,
        exec_begin:            <?= json_encode(t('demo_oracle.sqlmon_exec_begin')) ?>,
        exec_status:           <?= json_encode(t('demo_oracle.sqlmon_exec_status')) ?>,
        col_line:              <?= json_encode(t('demo_oracle.sqlmon_col_line')) ?>,
        col_operation:         <?= json_encode(t('demo_oracle.sqlmon_col_operation')) ?>,
        col_object:            <?= json_encode(t('demo_oracle.sqlmon_col_object')) ?>,
        col_est_rows:          <?= json_encode(t('demo_oracle.sqlmon_col_est_rows')) ?>,
        col_act_rows:          <?= json_encode(t('demo_oracle.sqlmon_col_act_rows')) ?>,
        col_starts:            <?= json_encode(t('demo_oracle.sqlmon_col_starts')) ?>,
        col_mem:               <?= json_encode(t('demo_oracle.sqlmon_col_mem')) ?>,
        col_temp:              <?= json_encode(t('demo_oracle.sqlmon_col_temp')) ?>,
        col_activity:          <?= json_encode(t('demo_oracle.sqlmon_col_activity')) ?>,
        plan_none:             <?= json_encode(t('demo_oracle.sqlmon_plan_none')) ?>,
        plan_truncated:        <?= json_encode(t('demo_oracle.sqlmon_plan_truncated')) ?>,
        longops_none:          <?= json_encode(t('demo_oracle.sqlmon_longops_none')) ?>,
        longops_eta:           <?= json_encode(t('demo_oracle.sqlmon_longops_eta')) ?>,
        tot_elapsed:           <?= json_encode(t('demo_oracle.sqlmon_tot_elapsed')) ?>,
        tot_cpu:               <?= json_encode(t('demo_oracle.sqlmon_tot_cpu')) ?>,
        tot_io:                <?= json_encode(t('demo_oracle.sqlmon_tot_io')) ?>,
        tot_lio:               <?= json_encode(t('demo_oracle.sqlmon_tot_lio')) ?>,
        tot_pio:               <?= json_encode(t('demo_oracle.sqlmon_tot_pio')) ?>,
        tot_read:              <?= json_encode(t('demo_oracle.sqlmon_tot_read')) ?>,
        scope_exec:            <?= json_encode(t('demo_oracle.sqlmon_scope_exec')) ?>,
        scope_delta:           <?= json_encode(t('demo_oracle.sqlmon_scope_delta')) ?>,
        samples:               <?= json_encode(t('demo_oracle.sqlmon_samples')) ?>,
        finished:              <?= json_encode(t('demo_oracle.sqlmon_finished')) ?>,
        abandoned:             <?= json_encode(t('demo_oracle.sqlmon_abandoned')) ?>
    }
};
</script>
<script src="/modules/demo_oracle/assets/js/report-sqlmon.js?v=<?= filemtime(__DIR__ . '/../../assets/js/report-sqlmon.js') ?>"></script>
