<?php
/**
 * View: Demo Oracle — parametres de la base.
 *
 * Portage de modules/oracle/views/report/config.php. Les filtres et le tri
 * fonctionnent a l'identique ; la colonne d'actions et la fenetre d'edition
 * en ligne ont disparu, le module n'ayant aucun gestionnaire a appeler.
 *
 * Variables : $fetchError, $rows, $nlsRows, $dbInfo, $instances, $typeLabels
 */
?>

<div class="page-header" style="margin-bottom:.35rem">
    <h1 style="margin:0"><?= t('demo_oracle.config_params') ?></h1>
</div>

<?php
/* ── Section 1 : Identité de la base + instances ─────────────────────────
 *   Données tirées de V$DATABASE (1 ligne) et GV$INSTANCE (N lignes en RAC).
 *   Affichée systématiquement, indépendamment du succès de gv$parameter.   */
if ($dbInfo !== null || !empty($instances)):
?>
<div class="card" style="margin-bottom:.6rem;padding:.6rem .75rem">
    <h2 style="margin:0 0 .5rem;font-size:1rem">
        🗄 <?= t('demo_oracle.config_dbinfo_title') ?>
    </h2>

    <?php if ($dbInfo !== null): ?>
    <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(220px,1fr));gap:.2rem .9rem;font-size:.82rem">
        <?php
        $kv = function (string $label, $value) {
            $value = $value === null || $value === '' ? '—' : (string)$value;
            echo '<div><span style="color:var(--color-text-muted)">'
                . htmlspecialchars($label) . '</span> : <strong>'
                . htmlspecialchars($value) . '</strong></div>';
        };
        $kv('db_name',          $dbInfo['NAME']             ?? '');
        $kv('db_unique_name',   $dbInfo['DB_UNIQUE_NAME']   ?? '');
        $kv('dbid',             $dbInfo['DBID']             ?? '');
        $kv('created',          $dbInfo['CREATED']          ?? '');
        $kv('open_mode',        $dbInfo['OPEN_MODE']        ?? '');
        $kv('log_mode',         $dbInfo['LOG_MODE']         ?? '');
        $kv('database_role',    $dbInfo['DATABASE_ROLE']    ?? '');
        $kv('protection_mode',  $dbInfo['PROTECTION_MODE']  ?? '');
        $kv('protection_level', $dbInfo['PROTECTION_LEVEL'] ?? '');
        $kv('force_logging',    $dbInfo['FORCE_LOGGING']    ?? '');
        $kv('flashback_on',     $dbInfo['FLASHBACK_ON']     ?? '');
        $kv('controlfile_type', $dbInfo['CONTROLFILE_TYPE'] ?? '');
        $kv('cdb',              $dbInfo['CDB']              ?? '');
        $kv('platform_name',    $dbInfo['PLATFORM_NAME']    ?? '');
        $kv('current_scn',      $dbInfo['CURRENT_SCN']      ?? '');
        $kv('switchover_status',$dbInfo['SWITCHOVER_STATUS'] ?? '');
        ?>
    </div>
    <?php endif; ?>

    <?php if (!empty($instances)): ?>
    <h3 style="margin:.85rem 0 .35rem;font-size:.9rem;color:var(--color-text-muted)">
        <?= t('demo_oracle.config_instances_title') ?> (<?= count($instances) ?>)
    </h3>
    <div style="overflow-x:auto">
    <table class="data-table" style="width:100%;border-collapse:collapse;font-size:.78rem">
        <thead>
            <tr style="background:var(--color-input-bg,#f8f8f8);border-bottom:1px solid var(--color-border)">
                <th style="text-align:left;padding:.25rem .45rem">#</th>
                <th style="text-align:left;padding:.25rem .45rem">instance_name</th>
                <th style="text-align:left;padding:.25rem .45rem">host_name</th>
                <th style="text-align:left;padding:.25rem .45rem">version</th>
                <th style="text-align:left;padding:.25rem .45rem">edition</th>
                <th style="text-align:left;padding:.25rem .45rem">startup_time</th>
                <th style="text-align:left;padding:.25rem .45rem">status</th>
                <th style="text-align:left;padding:.25rem .45rem">database_status</th>
                <th style="text-align:left;padding:.25rem .45rem">role</th>
                <th style="text-align:left;padding:.25rem .45rem">active_state</th>
                <th style="text-align:left;padding:.25rem .45rem">archiver</th>
                <th style="text-align:left;padding:.25rem .45rem">logins</th>
                <th style="text-align:right;padding:.25rem .45rem">thread#</th>
                <th style="text-align:left;padding:.25rem .45rem">database_type</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($instances as $i):
            $statusOk = strcasecmp($i['STATUS'] ?? '', 'OPEN') === 0;
        ?>
            <tr style="border-bottom:1px solid var(--color-border)">
                <td style="padding:.25rem .45rem"><?= htmlspecialchars($i['INSTANCE_NUMBER'] ?? '') ?></td>
                <td style="padding:.25rem .45rem"><code><?= htmlspecialchars($i['INSTANCE_NAME'] ?? '') ?></code></td>
                <td style="padding:.25rem .45rem"><?= htmlspecialchars($i['HOST_NAME'] ?? '') ?></td>
                <td style="padding:.25rem .45rem"><?= htmlspecialchars($i['VERSION'] ?? '') ?></td>
                <td style="padding:.25rem .45rem"><?= htmlspecialchars($i['EDITION'] ?? '') ?></td>
                <td style="padding:.25rem .45rem;font-family:monospace;font-size:.72rem"><?= htmlspecialchars($i['STARTUP_TIME'] ?? '') ?></td>
                <td style="padding:.25rem .45rem">
                    <span class="badge <?= $statusOk ? 'badge-success' : 'badge-warning' ?>" style="font-size:.7rem">
                        <?= htmlspecialchars($i['STATUS'] ?? '—') ?>
                    </span>
                </td>
                <td style="padding:.25rem .45rem"><?= htmlspecialchars($i['DATABASE_STATUS'] ?? '') ?></td>
                <td style="padding:.25rem .45rem"><?= htmlspecialchars($i['INSTANCE_ROLE'] ?? '') ?></td>
                <td style="padding:.25rem .45rem"><?= htmlspecialchars($i['ACTIVE_STATE'] ?? '') ?></td>
                <td style="padding:.25rem .45rem"><?= htmlspecialchars($i['ARCHIVER'] ?? '') ?></td>
                <td style="padding:.25rem .45rem"><?= htmlspecialchars($i['LOGINS'] ?? '') ?></td>
                <td style="padding:.25rem .45rem;text-align:right"><?= htmlspecialchars($i['THREAD#'] ?? '') ?></td>
                <td style="padding:.25rem .45rem"><?= htmlspecialchars($i['DATABASE_TYPE'] ?? '') ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php if ($fetchError): ?>
<div class="alert alert-error" role="alert"><?= htmlspecialchars($fetchError) ?></div>
<?php elseif (empty($rows)): ?>
<div class="alert alert-info"><?= t('demo_oracle.no_params') ?></div>
<?php else: ?>

<!-- ── Filter bar ─────────────────────────────────────────────────────────── -->
<div class="card cfg-filter-card">
    <div class="cfg-filter-bar">
        <label class="cfg-filter-check">
            <input type="checkbox" id="f-isbasic" checked>
            <?= t('demo_oracle.filter_isbasic') ?>
        </label>
        <label class="cfg-filter-check">
            <input type="checkbox" id="f-ismodified" checked>
            <?= t('demo_oracle.filter_modified') ?>
        </label>
        <span class="cfg-filter-sep"></span>
        <span class="cfg-filter-label"><?= t('demo_oracle.filter_mod_label') ?> :</span>
        <label class="cfg-filter-check">
            <input type="checkbox" class="f-mode" value="ses" checked>
            <?= t('demo_oracle.filter_mod_session') ?>
        </label>
        <label class="cfg-filter-check">
            <input type="checkbox" class="f-mode" value="sys" checked>
            <?= t('demo_oracle.filter_mod_system') ?>
        </label>
        <label class="cfg-filter-check">
            <input type="checkbox" class="f-mode" value="pdb" checked>
            <?= t('demo_oracle.filter_mod_pdb') ?>
        </label>
        <label class="cfg-filter-check">
            <input type="checkbox" class="f-mode" value="inst" checked>
            <?= t('demo_oracle.filter_mod_instance') ?>
        </label>
        <span class="cfg-filter-sep"></span>
        <select id="f-type" class="cfg-filter-select">
            <option value=""><?= t('demo_oracle.filter_all_types') ?></option>
            <?php foreach ($typeLabels as $num => $label): ?>
            <option value="<?= $num ?>"><?= htmlspecialchars($label) ?></option>
            <?php endforeach; ?>
        </select>
        <input type="text" id="f-name" class="cfg-filter-input"
               placeholder="<?= htmlspecialchars(t('demo_oracle.filter_name_placeholder')) ?>">
        <button type="button" id="f-reset-btn" class="btn btn-secondary btn-sm">
            <?= t('demo_oracle.filter_reset') ?>
        </button>
        <span id="f-count" class="cfg-filter-count"></span>
    </div>
    <?php /* Le module complet rend la colonne « valeur » editable et ajoute une
             colonne d'actions (remise par defaut). La demonstration l'annonce
             ici plutot que de laisser croire a un oubli. */ ?>
    <div style="margin-top:.45rem;display:flex;align-items:center;gap:.5rem;flex-wrap:wrap">
        <button type="button" class="btn btn-secondary btn-sm"<?= DemoOracleDemo::disabled() ?>>
            <?= t('demo_oracle.cfg_edit_param') ?>
        </button>
        <?= DemoOracleDemo::badge(t('demo_oracle.cfg_edit_hint')) ?>
    </div>
</div>

<!-- ── Parameters table ───────────────────────────────────────────────────── -->
<div class="card" style="padding:0;overflow-x:auto">
<table class="data-table cfg-table" id="cfg-table">
    <thead>
        <tr>
            <th class="sortable" data-col="0"><?= t('demo_oracle.col_instance') ?> <span class="sort-arrow"></span></th>
            <th class="sortable" data-col="1"><?= t('demo_oracle.col_param') ?> <span class="sort-arrow"></span></th>
            <th class="sortable cfg-col-value" data-col="2"><?= t('demo_oracle.col_value') ?> <span class="sort-arrow"></span></th>
            <th class="sortable text-center" data-col="3"><?= t('demo_oracle.col_isdefault') ?> <span class="sort-arrow"></span></th>
            <th class="sortable text-center" data-col="4"><?= t('demo_oracle.col_type') ?> <span class="sort-arrow"></span></th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($rows as $row):
        $isDefault  = strtoupper($row['ISDEFAULT'] ?? '') === 'TRUE';
        $isBasic    = strtoupper($row['ISBASIC']   ?? '') === 'TRUE';
        $isModified = strtoupper($row['ISMODIFIED'] ?? '') !== 'FALSE';
        $sesMod     = strtoupper($row['ISSES_MODIFIABLE']      ?? '') !== 'FALSE';
        $sysMod     = strtoupper($row['ISSYS_MODIFIABLE']      ?? '') !== 'FALSE';
        $pdbMod     = strtoupper($row['ISPDB_MODIFIABLE']      ?? '') !== 'FALSE';
        $instMod    = strtoupper($row['ISINSTANCE_MODIFIABLE'] ?? '') !== 'FALSE';
        $typeNum    = (int)($row['TYPE'] ?? 0);
        $name       = $row['NAME']          ?? '';
        $value      = $row['DISPLAY_VALUE'] ?? '';
        $updateCmt  = $row['UPDATE_COMMENT'] ?? '';
        $description= $row['DESCRIPTION']   ?? '';
        $typeLabel  = $typeLabels[$typeNum]  ?? (string)$typeNum;
    ?>
    <tr
        data-isbasic="<?= $isBasic    ? '1' : '0' ?>"
        data-ismodified="<?= $isModified ? '1' : '0' ?>"
        data-ses="<?= $sesMod  ? '1' : '0' ?>"
        data-sys="<?= $sysMod  ? '1' : '0' ?>"
        data-pdb="<?= $pdbMod  ? '1' : '0' ?>"
        data-inst="<?= $instMod ? '1' : '0' ?>"
        data-type="<?= $typeNum ?>"
        data-name="<?= htmlspecialchars(strtolower($name)) ?>"
    >
        <td><?= htmlspecialchars($row['INAME'] ?? '') ?></td>
        <td>
            <span class="cfg-param-name"
                  title="<?= htmlspecialchars($description) ?>">
                <?= htmlspecialchars($name) ?>
            </span>
        </td>
        <td class="cfg-value-cell"
            data-name="<?= htmlspecialchars($name) ?>"
            data-value="<?= htmlspecialchars($value) ?>"
            data-desc="<?= htmlspecialchars($description) ?>"
            <?= (!$isDefault && $updateCmt !== '') ? 'title="' . htmlspecialchars($updateCmt) . '"' : '' ?>
        >
            <?php if (!$isDefault): ?>
            <span class="cfg-val-modified"><?= htmlspecialchars($value !== '' ? $value : '—') ?></span>
            <?php else: ?>
            <span><?= htmlspecialchars($value !== '' ? $value : '—') ?></span>
            <?php endif; ?>
        </td>
        <td class="text-center">
            <?php if ($isDefault): ?>
            <span class="badge badge-success" style="font-size:.7em">TRUE</span>
            <?php else: ?>
            <span class="badge badge-warning" style="font-size:.7em">FALSE</span>
            <?php endif; ?>
        </td>
        <td class="text-center">
            <span class="cfg-type-badge"><?= htmlspecialchars($typeLabel) ?></span>
        </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>

<?php if (!empty($nlsRows)): ?>
<!-- ── NLS parameters section ────────────────────────────────────────────── -->
<div class="page-header" style="margin-top:1rem;margin-bottom:.35rem">
    <h2 style="margin:0"><?= t('demo_oracle.nls_params') ?></h2>
</div>

<div class="card cfg-filter-card">
    <div class="cfg-filter-bar">
        <input type="text" id="nls-f-name" class="cfg-filter-input"
               placeholder="<?= htmlspecialchars(t('demo_oracle.nls_filter_placeholder')) ?>">
        <label class="cfg-filter-check">
            <input type="checkbox" id="nls-f-diff">
            <?= t('demo_oracle.nls_filter_diff') ?>
        </label>
        <span id="nls-f-count" class="cfg-filter-count"></span>
    </div>
</div>

<div class="card" style="padding:0;overflow-x:auto">
<table class="data-table cfg-table" id="nls-table">
    <thead>
        <tr>
            <th class="sortable" data-col="0"><?= t('demo_oracle.nls_col_param') ?> <span class="sort-arrow"></span></th>
            <th class="sortable text-center" data-col="1"><?= t('demo_oracle.nls_col_db') ?> <span class="sort-arrow"></span></th>
            <th class="sortable text-center" data-col="2"><?= t('demo_oracle.nls_col_instance') ?> <span class="sort-arrow"></span></th>
            <th class="sortable text-center" data-col="3"><?= t('demo_oracle.nls_col_session') ?> <span class="sort-arrow"></span></th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($nlsRows as $nr):
        $dbval   = $nr['DBVAL']   ?? '—';
        $instval = $nr['INSTVAL'] ?? '—';
        $sessval = $nr['SESSVAL'] ?? '—';
        $instDiff = ($instval !== '—' && $instval !== $dbval);
        $sessDiff = ($sessval !== '—' && $sessval !== $dbval);
        $hasDiff  = $instDiff || $sessDiff;
    ?>
    <tr data-param="<?= htmlspecialchars(strtolower($nr['PARAMETER'] ?? '')) ?>"
        data-diff="<?= $hasDiff ? '1' : '0' ?>">
        <td><?= htmlspecialchars($nr['PARAMETER'] ?? '') ?></td>
        <td class="text-center"><?= htmlspecialchars($dbval) ?></td>
        <td class="text-center <?= $instDiff ? 'nls-diff' : '' ?>">
            <?= htmlspecialchars($instval) ?>
        </td>
        <td class="text-center <?= $sessDiff ? 'nls-diff-sess' : '' ?>">
            <?= htmlspecialchars($sessval) ?>
        </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; // end if nlsRows ?>


<!-- ── PHP data for JS ────────────────────────────────────────────────────── -->
<script>
window.DemoOracleReport = window.DemoOracleReport || {};
window.DemoOracleReport.config = {
    hasNls:  <?= !empty($nlsRows) ? 'true' : 'false' ?>,
    hasRows: <?= !empty($rows) ? 'true' : 'false' ?>
};
</script>
<script src="/modules/demo_oracle/assets/js/report-config.js?v=<?= filemtime(__DIR__ . '/../../assets/js/report-config.js') ?>"></script>

<?php endif; // end if rows ?>
