<?php
/**
 * View: Demo Oracle — activite temps reel.
 *
 * Portage de modules/oracle/views/report/realtime.php. La chronologie, les
 * dix panneaux Top N et le filtre croise sont identiques. Les quatre
 * controles de filtrage sont plafonnes ; ils restent affiches, desactives,
 * pour que le visiteur voie ce que la version complete permet de regler.
 *
 * Variables : $baseUrl, $window, $windows, $dim, $dimensions, $services,
 *             $dataSource, $hasSource, $settings
 */
?>

<div class="page-header" style="display:flex;align-items:center;gap:.5rem;flex-wrap:wrap">
    <h1 style="margin:0">Activit&eacute; temps r&eacute;el</h1>
    <?php if ($dataSource === 'bash_snapper'): ?>
    <span class="badge badge-warning" style="font-size:.72rem">sw$bash_snapper</span>
    <?php endif; ?>
</div>

<?php if (!$hasSource): ?>
<div class="card">
    <div class="alert alert-warning" style="margin:0">
        &#x1f512; <?= t('demo_oracle.ash_no_license') ?>
        <?php /* Le module complet propose ici, aux profils DBA, un lien
                 vers l'installation de sw$bash_snapper. La demonstration
                 ne sait rien deployer : elle ne l'affiche pas. */ ?>
    </div>
</div>
<?php return; endif; ?>

<!-- Controls bar -->
<div class="card" style="padding:.65rem 1rem;margin-bottom:.75rem">
    <div style="display:flex;align-items:center;gap:1.25rem;flex-wrap:wrap;row-gap:.5rem">

        <!-- Fen&ecirc;tre -->
        <div style="display:flex;align-items:center;gap:.4rem">
            <span style="font-size:.82rem;color:var(--color-text-muted)">Fen&ecirc;tre&nbsp;:</span>
            <?php foreach ($windows as $w): $on = ($window === $w); ?>
            <button class="btn btn-sm <?= $on ? 'btn-primary' : 'btn-secondary' ?>"
                    <?= $on ? '' : DemoOracleDemo::disabled() ?>><?= $w ?>&nbsp;min</button>
            <?php endforeach; ?>
            <?= DemoOracleDemo::badge(t('demo_oracle.rt_window_hint')) ?>
        </div>

        <!-- Dimension -->
        <div style="display:flex;align-items:center;gap:.4rem">
            <label for="ash-dim" style="font-size:.82rem;color:var(--color-text-muted)">Dimension&nbsp;:</label>
            <select id="ash-dim" class="form-control form-control-sm" style="min-width:170px"
                    onchange="ashSetDim(this.value)">
                <?php foreach ($dimensions as $k => $d): ?>
                <option value="<?= $k ?>"<?= $dim === $k ? ' selected' : '' ?><?= $d['demo'] ? '' : ' disabled' ?>>
                    <?= htmlspecialchars(t($d['label'])) ?><?= $d['demo'] ? '' : ' &#128274;' ?>
                </option>
                <?php endforeach; ?>
            </select>
        </div>

        <!-- Service -->
        <?php if (!empty($services)): ?>
        <div style="display:flex;align-items:center;gap:.4rem">
            <label for="ash-svc" style="font-size:.82rem;color:var(--color-text-muted)">Service&nbsp;:</label>
            <select id="ash-svc" class="form-control form-control-sm" style="min-width:160px"<?= DemoOracleDemo::disabled() ?>>
                <option value="0" selected><?= t('demo_oracle.rt_all_services') ?></option>
                <?php foreach ($services as $hash => $name): ?>
                <option value="<?= htmlspecialchars((string)$hash) ?>" disabled><?= htmlspecialchars($name) ?></option>
                <?php endforeach; ?>
            </select>
            <?= DemoOracleDemo::badge() ?>
        </div>
        <?php endif; ?>

        <!-- &Eacute;tat sessions -->
        <div style="display:flex;align-items:center;gap:.4rem">
            <span style="font-size:.82rem;color:var(--color-text-muted)">Sessions&nbsp;:</span>
            <button class="btn btn-sm btn-primary"<?= DemoOracleDemo::disabled() ?>><?= t('demo_oracle.rt_state_active') ?></button>
            <button class="btn btn-sm btn-secondary"<?= DemoOracleDemo::disabled() ?>><?= t('demo_oracle.rt_state_all') ?></button>
            <button class="btn btn-sm btn-secondary"<?= DemoOracleDemo::disabled() ?>><?= t('demo_oracle.rt_state_inactive') ?></button>
            <?= DemoOracleDemo::badge() ?>
        </div>

        <!-- Status -->
        <div style="margin-left:auto;display:flex;align-items:center;gap:1rem;font-size:.8rem;color:var(--color-text-muted)">
            <span>Mise &agrave; jour&nbsp;: <strong id="ash-updated">&mdash;</strong></span>
            <span>Refresh&nbsp;: <strong id="ash-countdown">10</strong>s</span>
            <span id="ash-cache-badge"></span>
        </div>

    </div>
</div>

<!-- Timeline chart -->
<div class="card" style="padding:1rem;margin-bottom:.75rem">
    <h2 style="font-size:.95rem;margin:0 0 .75rem">Sessions par minute</h2>
    <div style="position:relative;height:240px">
        <canvas id="ash-timeline"></canvas>
    </div>
</div>

<!-- Active cross-filter chip -->
<div id="ash-xf-bar" style="display:none;margin-bottom:.5rem;padding:.4rem .75rem;background:var(--color-primary-light,#e8f0f7);border-radius:4px;border:1px solid var(--color-primary,#4e79a7);align-items:center;gap:.75rem;font-size:.82rem">
    <span style="color:var(--color-text-muted)">Filtre actif&nbsp;:</span>
    <strong id="ash-xf-label"></strong>
    <button class="btn btn-sm btn-secondary" style="padding:0 .45rem;font-size:.78rem;line-height:1.5" onclick="ashClearXFilter()">&#x2715; Effacer</button>
</div>

<!-- Top N panels — responsive grid -->
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(300px,1fr));gap:.75rem;margin-top:.75rem">

    <div class="card" style="padding:1rem">
        <h2 style="font-size:.9rem;margin:0 0 .6rem">Top SQL <small style="font-weight:400;color:var(--color-text-muted);font-size:.76rem">(clic&nbsp;=&nbsp;filtre, clic droit&nbsp;=&nbsp;d&eacute;tail)</small></h2>
        <div id="ash-top-sql"></div>
    </div>

    <div class="card" style="padding:1rem">
        <h2 style="font-size:.9rem;margin:0 0 .6rem">Top &eacute;v&eacute;nements d'attente <small style="font-weight:400;color:var(--color-text-muted);font-size:.76rem">(clic&nbsp;=&nbsp;filtre)</small></h2>
        <div id="ash-top-events"></div>
    </div>

    <div class="card" style="padding:1rem">
        <h2 style="font-size:.9rem;margin:0 0 .6rem">Top modules <small style="font-weight:400;color:var(--color-text-muted);font-size:.76rem">(clic&nbsp;=&nbsp;filtre)</small></h2>
        <div id="ash-top-modules"></div>
    </div>

    <div class="card" style="padding:1rem">
        <h2 style="font-size:.9rem;margin:0 0 .6rem">Top actions <small style="font-weight:400;color:var(--color-text-muted);font-size:.76rem">(clic&nbsp;=&nbsp;filtre)</small></h2>
        <div id="ash-top-actions"></div>
    </div>

    <div class="card" style="padding:1rem">
        <h2 style="font-size:.9rem;margin:0 0 .6rem">Top services</h2>
        <div id="ash-top-services"></div>
    </div>

    <div class="card" style="padding:1rem">
        <h2 style="font-size:.9rem;margin:0 0 .6rem">Top serveurs clients <small style="font-weight:400;color:var(--color-text-muted);font-size:.76rem">(clic&nbsp;=&nbsp;filtre)</small></h2>
        <div id="ash-top-machines"></div>
    </div>

    <div class="card" style="padding:1rem">
        <h2 style="font-size:.9rem;margin:0 0 .6rem">Top objets</h2>
        <div id="ash-top-objects"></div>
    </div>

    <div class="card" style="padding:1rem">
        <h2 style="font-size:.9rem;margin:0 0 .6rem">Top types d'objets</h2>
        <div id="ash-top-object-types"></div>
    </div>

    <div class="card" style="padding:1rem">
        <h2 style="font-size:.9rem;margin:0 0 .6rem">Top instances (RAC) <small style="font-weight:400;color:var(--color-text-muted);font-size:.76rem">(clic&nbsp;=&nbsp;filtre)</small></h2>
        <div id="ash-top-instances"></div>
    </div>

    <div class="card" id="ash-top-pdbs-card" style="padding:1rem;display:none">
        <h2 style="font-size:.9rem;margin:0 0 .6rem">Top PDB</h2>
        <div id="ash-top-pdbs"></div>
    </div>

</div>

<!-- PHP-injected config for external JS -->
<script>
window.DemoOracleReport = window.DemoOracleReport || {};
window.DemoOracleReport.realtime = {
    baseUrl:      <?= json_encode($baseUrl) ?>,
    sqlDetailUrl: <?= json_encode($sqlDetailUrl) ?>,
    fullOnly:     <?= json_encode(t('demo_oracle.full_only')) ?>,
    window:     <?= (int)$window ?>,
    dim:        <?= json_encode($dim) ?>,
    cdnD3:      <?= json_encode($settings->get('cdn_d3_js', '')) ?>,
    cdnChartJs: <?= json_encode($settings->get('cdn_chartjs_js', '')) ?>
};
</script>
<script src="/modules/demo_oracle/assets/js/report-realtime.js?v=<?= filemtime(__DIR__ . '/../../assets/js/report-realtime.js') ?>"></script>
