<?php
/**
 * View: Demo Oracle — rapport ASH detaille.
 *
 * Portage de modules/oracle/views/report/ash_detail.php. Le graphique empile,
 * les six panneaux Top N et le filtrage croise sont identiques.
 *
 * Trois controles sont plafonnes, et restent affiches pour le dire :
 * l'amplitude (rognee a $maxHours heures cote serveur), le choix de dimension
 * (deux sur huit) et le filtre de service (fige sur « tous »).
 *
 * Les libelles passent directement par t() : le module complet les prepare
 * dans un tableau $i18nLabels cote controleur, ce qui obligeait a le tenir a
 * jour a chaque libelle ajoute.
 *
 * Variables : $fetchError, $hasSource, $dataSource, $dtBegin, $dtEnd,
 *             $clamped, $maxHours, $bucketSecs, $allBuckets, $dimensions,
 *             $dim, $baseUrl, $cdnD3Js
 */
?>

<div class="page-header" style="display:flex;align-items:center;flex-wrap:wrap;gap:.75rem;margin-bottom:.35rem">
    <h1 style="margin:0;flex-shrink:0"><?= htmlspecialchars(t('demo_oracle.ash_detail_title')) ?></h1>
    <?php if ($dataSource === 'bash_snapper'): ?>
    <span class="badge badge-warning" style="font-size:.72rem">sw$bash_snapper</span>
    <?php endif; ?>
    <?php if (!$fetchError && $hasSource): ?>
    <form method="GET" style="display:flex;align-items:center;gap:.4rem;flex-wrap:wrap;margin:0">
        <?php /* Le routeur du module identifie la page par ?section= : le
                 formulaire doit la reconduire, sinon « Appliquer » renverrait
                 sur la vue d'ensemble. */ ?>
        <input type="hidden" name="section" value="history/detail">
        <input type="hidden" name="dim" value="<?= htmlspecialchars($dim) ?>">
        <label style="font-size:.8rem;color:var(--color-text-muted)"><?= htmlspecialchars(t('demo_oracle.ash_detail_from')) ?></label>
        <input type="datetime-local" name="dt_begin"
               value="<?= htmlspecialchars(str_replace(' ', 'T', $dtBegin)) ?>"
               style="font-size:.82rem">
        <label style="font-size:.8rem;color:var(--color-text-muted)"><?= htmlspecialchars(t('demo_oracle.ash_detail_to')) ?></label>
        <input type="datetime-local" name="dt_end"
               value="<?= htmlspecialchars(str_replace(' ', 'T', $dtEnd)) ?>"
               style="font-size:.82rem">
        <button type="submit" class="btn btn-primary btn-sm"><?= htmlspecialchars(t('demo_oracle.ash_detail_apply')) ?></button>
        <?= DemoOracleDemo::badge(t('demo_oracle.ash_window_hint', ['hours' => (string)$maxHours])) ?>
    </form>
    <?php endif; ?>
</div>

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

<?php elseif (!$hasSource): ?>
<div class="card">
    <div class="alert alert-warning" style="margin:0">
        <?= htmlspecialchars(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 else: ?>

<!-- ══════════════════════════════════════════════════════════
     Selector bar — dimension, service filter, wait class filter
══════════════════════════════════════════════════════════ -->
<div class="card" style="padding:.55rem 1rem;margin-bottom:.5rem">
    <div style="display:flex;align-items:center;gap:1.25rem;flex-wrap:wrap;row-gap:.5rem">

        <!-- Dimension selector -->
        <div style="display:flex;align-items:center;gap:.4rem">
            <label for="ash-dim-select" style="font-size:.82rem;color:var(--color-text-muted)"><?= htmlspecialchars(t('demo_oracle.ash_dimension')) ?>&nbsp;:</label>
            <select id="ash-dim-select" class="form-control form-control-sm" style="min-width:170px">
                <?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 filter -->
        <div style="display:flex;align-items:center;gap:.4rem">
            <label for="ash-svc-filter" style="font-size:.82rem;color:var(--color-text-muted)"><?= htmlspecialchars(t('demo_oracle.ash_filter_service')) ?>&nbsp;:</label>
            <select id="ash-svc-filter" class="form-control form-control-sm" style="min-width:160px"<?= DemoOracleDemo::disabled() ?>>
                <option value="" selected><?= htmlspecialchars(t('demo_oracle.ash_filter_all_services')) ?></option>
            </select>
            <?= DemoOracleDemo::badge() ?>
        </div>

        <!-- Wait class filter -->
        <div style="display:flex;align-items:center;gap:.4rem">
            <label for="ash-wc-filter" style="font-size:.82rem;color:var(--color-text-muted)"><?= htmlspecialchars(t('demo_oracle.ash_filter_wait_class')) ?>&nbsp;:</label>
            <select id="ash-wc-filter" class="form-control form-control-sm" style="min-width:160px">
                <option value=""><?= htmlspecialchars(t('demo_oracle.ash_filter_all_classes')) ?></option>
            </select>
        </div>

    </div>
</div>

<!-- ══════════════════════════════════════════════════════════
     Sessions actives par classe d'attente (par minute)
══════════════════════════════════════════════════════════ -->
<div class="card">
    <div style="display:flex;align-items:center;gap:.75rem;margin-bottom:.25rem">
        <h2 style="margin:0;flex-shrink:0;font-size:1rem"><?= htmlspecialchars(t('demo_oracle.ash_chart_title')) ?></h2>
        <div id="chart-ash-legend" style="flex:1;min-width:0;overflow-x:auto"></div>
    </div>
    <div style="overflow-x:auto"><div id="chart-ash-detail"></div></div>
</div>

<!-- ══════════════════════════════════════════════════════════
     Top consommateurs — controlled by dimension selector
══════════════════════════════════════════════════════════ -->
<div class="card" style="margin-top:.4rem">
    <div style="display:flex;align-items:center;gap:.75rem;margin-bottom:.25rem">
        <h2 style="margin:0" id="topn-title"><?= htmlspecialchars(t('demo_oracle.ash_topn_title')) ?></h2>
        <div id="topn-global-reset"></div>
    </div>

    <div style="display:grid;grid-template-columns:repeat(6,1fr);gap:.6rem">
        <div>
            <div style="display:flex;align-items:center;gap:.4rem;margin-bottom:.15rem">
                <h3 style="font-size:.82rem;font-weight:600;margin:0">SQL</h3>
                <span id="reset-sql"></span>
            </div>
            <div id="chart-top-sql"></div>
        </div>
        <div>
            <div style="display:flex;align-items:center;gap:.4rem;margin-bottom:.15rem">
                <h3 style="font-size:.82rem;font-weight:600;margin:0">PHV</h3>
                <span id="reset-phv"></span>
            </div>
            <div id="chart-top-phv"></div>
        </div>
        <div>
            <div style="display:flex;align-items:center;gap:.4rem;margin-bottom:.15rem">
                <h3 style="font-size:.82rem;font-weight:600;margin:0">FMS</h3>
                <span id="reset-fms"></span>
            </div>
            <div id="chart-top-fms"></div>
        </div>
        <div>
            <div style="display:flex;align-items:center;gap:.4rem;margin-bottom:.15rem">
                <h3 style="font-size:.82rem;font-weight:600;margin:0">Wait Event</h3>
                <span id="reset-event"></span>
            </div>
            <div id="chart-top-event"></div>
        </div>
        <div>
            <div style="display:flex;align-items:center;gap:.4rem;margin-bottom:.15rem">
                <h3 style="font-size:.82rem;font-weight:600;margin:0">Module</h3>
                <span id="reset-module"></span>
            </div>
            <div id="chart-top-module"></div>
        </div>
        <div>
            <div style="display:flex;align-items:center;gap:.4rem;margin-bottom:.15rem">
                <h3 style="font-size:.82rem;font-weight:600;margin:0">Action</h3>
                <span id="reset-action"></span>
            </div>
            <div id="chart-top-action"></div>
        </div>
        <div>
            <div style="display:flex;align-items:center;gap:.4rem;margin-bottom:.15rem">
                <h3 style="font-size:.82rem;font-weight:600;margin:0">Service</h3>
                <span id="reset-service"></span>
            </div>
            <div id="chart-top-service"></div>
        </div>
        <div>
            <div style="display:flex;align-items:center;gap:.4rem;margin-bottom:.15rem">
                <h3 style="font-size:.82rem;font-weight:600;margin:0">Client</h3>
                <span id="reset-machine"></span>
            </div>
            <div id="chart-top-machine"></div>
        </div>
        <div>
            <div style="display:flex;align-items:center;gap:.4rem;margin-bottom:.15rem">
                <h3 style="font-size:.82rem;font-weight:600;margin:0">Objet</h3>
                <span id="reset-objname"></span>
            </div>
            <div id="chart-top-objname"></div>
        </div>
        <div>
            <div style="display:flex;align-items:center;gap:.4rem;margin-bottom:.15rem">
                <h3 style="font-size:.82rem;font-weight:600;margin:0">Type objet</h3>
                <span id="reset-objtype"></span>
            </div>
            <div id="chart-top-objtype"></div>
        </div>
        <div>
            <div style="display:flex;align-items:center;gap:.4rem;margin-bottom:.15rem">
                <h3 style="font-size:.82rem;font-weight:600;margin:0">PL/SQL entr&eacute;e</h3>
                <span id="reset-pentry"></span>
            </div>
            <div id="chart-top-pentry"></div>
        </div>
        <div>
            <div style="display:flex;align-items:center;gap:.4rem;margin-bottom:.15rem">
                <h3 style="font-size:.82rem;font-weight:600;margin:0">PL/SQL objet</h3>
                <span id="reset-pobject"></span>
            </div>
            <div id="chart-top-pobject"></div>
        </div>
    </div>
</div>

<?php endif; // fin du bloc « une source ASH est disponible » ?>

<!-- ── CDN : D3 ───────────────────────────────────────────────────────────── -->
<script src="<?= htmlspecialchars($cdnD3Js) ?>"></script>

<!-- ── PHP config injected for external JS ────────────────────────────────── -->
<script>
window.DemoOracleReport = window.DemoOracleReport || {};
window.DemoOracleReport.ashDetail = {
    baseUrl:    <?= json_encode($baseUrl) ?>,
    dtBegin:    <?= json_encode($dtBegin) ?>,
    dtEnd:      <?= json_encode($dtEnd) ?>,
    hasSource:  <?= json_encode($hasSource) ?>,
    bucketSecs: <?= (int)$bucketSecs ?>,
    dataSource: <?= json_encode($dataSource) ?>,
    allBuckets: <?= json_encode($allBuckets) ?>,
    yLabel:     <?= json_encode(t('demo_oracle.ash_y_label')) ?>,
    noLicenseMsg: <?= json_encode(t('demo_oracle.ash_no_license')) ?>,
    sqlDetailUrl: <?= json_encode(demo_oracle_url('report', ['section' => 'sql/detail'])) ?>,
    fullOnly:     <?= json_encode(t('demo_oracle.full_only')) ?>,
    topnTitle:  <?= json_encode(t('demo_oracle.ash_topn_title')) ?>,
    dimLabels: {
        sql_id:       <?= json_encode(t('demo_oracle.ash_dim_sql')) ?>,
        wait_event:   <?= json_encode(t('demo_oracle.ash_dim_event')) ?>,
        module:       <?= json_encode(t('demo_oracle.ash_dim_module')) ?>,
        service_name: <?= json_encode(t('demo_oracle.ash_dim_service')) ?>,
        machine:      <?= json_encode(t('demo_oracle.ash_dim_machine')) ?>,
        plsql_entry:  <?= json_encode(t('demo_oracle.ash_dim_plsql')) ?>,
        object_name:  <?= json_encode(t('demo_oracle.ash_dim_object')) ?>,
        category:     <?= json_encode(t('demo_oracle.ash_dim_category')) ?>
    },
    lblAllServices: <?= json_encode(t('demo_oracle.ash_filter_all_services')) ?>,
    lblAllClasses:  <?= json_encode(t('demo_oracle.ash_filter_all_classes')) ?>
};
</script>
<script src="/modules/demo_oracle/assets/js/report-ash-detail.js?v=<?= filemtime(__DIR__ . '/../../assets/js/report-ash-detail.js') ?>"></script>
