<?php
/**
 * View: admin/batch — Batchs / Ordonnancement.
 *
 * Variables injectées :
 *   $pageTitle, $error, $success, $isWindows, $hasShell, $jobs, $cronLine,
 *   $schtasksLine, $installed, $currentCrontab, $marker, $ashRuns, $rmanRuns,
 *   $backups, $appLog, $csrfToken
 */
$pageTitle = t('batch.title');
include WEBROOT_PATH . '/includes/header.php';

$fmtSize = function (int $b): string {
    if ($b >= 1048576) return round($b / 1048576, 1) . ' Mo';
    if ($b >= 1024)    return round($b / 1024, 1) . ' Ko';
    return $b . ' o';
};
?>
<div class="admin-layout">
<?php include WEBROOT_PATH . '/includes/admin_sidebar.php'; ?>
<div class="admin-content">

<div class="page-header">
    <h1>⏲ <?= t('batch.title') ?></h1>
</div>

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

<?php if (!$hasShell): ?>
<div class="alert alert-warning"><?= t('batch.shell_unavailable') ?></div>
<?php endif; ?>

<!-- Planning proposé -->
<div class="card" style="margin-bottom:1.2rem">
    <h2><?= t('batch.schedule_title') ?></h2>
    <p style="color:var(--color-text-muted);font-size:.85rem;margin-top:0"><?= t('batch.schedule_intro') ?></p>
    <div style="overflow-x:auto">
    <table class="data-table" style="font-size:.82rem">
        <thead><tr>
            <th><?= t('batch.col_job') ?></th>
            <th><?= t('batch.col_schedule') ?></th>
            <th><?= t('batch.col_command') ?></th>
            <th><?= t('batch.col_state') ?></th>
        </tr></thead>
        <tbody>
        <?php foreach ($jobs as $j): ?>
            <tr>
                <td><strong><?= t('batch.job_' . $j['key']) ?></strong></td>
                <td><code><?= htmlspecialchars($j['cron']) ?></code></td>
                <td><code style="font-size:.78em"><?= htmlspecialchars($j['script'] . ' ' . $j['args']) ?></code></td>
                <td>
                    <?php if ($installed[$j['key']]): ?>
                    <span class="badge badge-success"><?= t('batch.state_active') ?></span>
                    <?php else: ?>
                    <span class="badge" style="background:#6b7280;color:#fff"><?= t('batch.state_inactive') ?></span>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    </div>

    <?php if (!$isWindows && $hasShell): ?>
    <div style="display:flex;gap:.5rem;margin-top:.8rem;flex-wrap:wrap">
        <form method="post" style="display:inline"
              onsubmit="return confirm('<?= htmlspecialchars(t('batch.confirm_install')) ?>')">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">
            <input type="hidden" name="action" value="install">
            <button type="submit" class="btn btn-primary">▶ <?= t('batch.install') ?></button>
        </form>
        <form method="post" style="display:inline"
              onsubmit="return confirm('<?= htmlspecialchars(t('batch.confirm_uninstall')) ?>')">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">
            <input type="hidden" name="action" value="uninstall">
            <button type="submit" class="btn btn-danger">⏹ <?= t('batch.uninstall') ?></button>
        </form>
    </div>
    <p style="color:var(--color-text-muted);font-size:.78rem;margin-bottom:0"><?= t('batch.apache_note') ?></p>
    <?php endif; ?>
</div>

<?php if (!$isWindows): ?>
<!-- Crontab courante -->
<div class="card" style="margin-bottom:1.2rem">
    <h2><?= t('batch.crontab_title') ?></h2>
    <?php if (trim($currentCrontab) === ''): ?>
    <p style="color:var(--color-text-muted)"><?= t('batch.crontab_empty') ?></p>
    <?php else: ?>
    <pre style="background:var(--color-bg-alt,#f5f5f5);padding:.7rem;border-radius:4px;overflow-x:auto;font-size:.78rem;white-space:pre-wrap"><?= htmlspecialchars($currentCrontab) ?></pre>
    <?php endif; ?>
</div>
<?php endif; ?>

<!-- Windows (informatif) -->
<div class="card" style="margin-bottom:1.2rem">
    <h2><?= t('batch.windows_title') ?></h2>
    <p style="color:var(--color-text-muted);font-size:.85rem;margin-top:0"><?= t('batch.windows_intro') ?></p>
    <pre style="background:var(--color-bg-alt,#f5f5f5);padding:.7rem;border-radius:4px;overflow-x:auto;font-size:.74rem"><?php
        foreach ($jobs as $j) echo htmlspecialchars($schtasksLine($j)) . "\n";
    ?></pre>
</div>

<!-- Dernières exécutions -->
<div class="card">
    <h2><?= t('batch.logs_title') ?></h2>

    <h3 style="font-size:.9rem;margin-bottom:.3rem"><?= t('batch.logs_rman') ?></h3>
    <?php if (empty($rmanRuns)): ?>
    <p style="color:var(--color-text-muted);font-size:.85rem"><?= t('batch.logs_none') ?></p>
    <?php else: ?>
    <table class="data-table" style="font-size:.8rem;margin-bottom:1rem">
        <thead><tr><th><?= t('cmdb.rman_label') ?></th><th><?= t('batch.col_when') ?></th><th><?= t('batch.col_result') ?></th><th>#</th></tr></thead>
        <tbody>
        <?php foreach ($rmanRuns as $r): ?>
            <tr>
                <td><?= htmlspecialchars((string)$r['label']) ?></td>
                <td><?= htmlspecialchars((string)$r['collected_at']) ?></td>
                <td><?php if ((int)$r['ok'] === 1): ?><span class="badge badge-success">OK</span>
                    <?php else: ?><span class="badge badge-danger" title="<?= htmlspecialchars((string)$r['msg']) ?>"><?= t('batch.failed') ?></span><?php endif; ?></td>
                <td style="text-align:right"><?= (int)$r['db_count'] ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>

    <h3 style="font-size:.9rem;margin-bottom:.3rem"><?= t('batch.logs_ash') ?></h3>
    <?php if (empty($ashRuns)): ?>
    <p style="color:var(--color-text-muted);font-size:.85rem"><?= t('batch.logs_none') ?></p>
    <?php else: ?>
    <table class="data-table" style="font-size:.8rem;margin-bottom:1rem">
        <thead><tr><th><?= t('cmdb.rman_database') ?></th><th><?= t('batch.col_source') ?></th><th><?= t('batch.col_day') ?></th><th><?= t('batch.col_when') ?></th><th><?= t('batch.col_rows') ?></th></tr></thead>
        <tbody>
        <?php foreach ($ashRuns as $r): ?>
            <tr>
                <td><?= htmlspecialchars((string)$r['db_unique_name']) ?></td>
                <td><?= htmlspecialchars((string)$r['source']) ?></td>
                <td><?= htmlspecialchars((string)$r['sample_date']) ?></td>
                <td><?= htmlspecialchars((string)$r['collected_at']) ?></td>
                <td style="text-align:right"><?= (int)$r['row_count'] ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>

    <h3 style="font-size:.9rem;margin-bottom:.3rem"><?= t('batch.logs_backups') ?></h3>
    <?php if (empty($backups)): ?>
    <p style="color:var(--color-text-muted);font-size:.85rem"><?= t('batch.logs_none') ?></p>
    <?php else: ?>
    <table class="data-table" style="font-size:.8rem;margin-bottom:1rem">
        <thead><tr><th><?= t('backup.col_name') ?></th><th style="text-align:right"><?= t('backup.col_size') ?></th><th><?= t('backup.col_date') ?></th></tr></thead>
        <tbody>
        <?php foreach ($backups as $b): ?>
            <tr><td><code><?= htmlspecialchars($b['name']) ?></code></td>
                <td style="text-align:right"><?= $fmtSize((int)$b['size']) ?></td>
                <td><?= htmlspecialchars($b['date']) ?></td></tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>

    <h3 style="font-size:.9rem;margin-bottom:.3rem"><?= t('batch.logs_applog') ?> <code style="font-size:.72em">log/app.log</code></h3>
    <?php if (trim($appLog) === ''): ?>
    <p style="color:var(--color-text-muted);font-size:.85rem"><?= t('batch.logs_none') ?></p>
    <?php else: ?>
    <pre style="background:var(--color-bg-alt,#f5f5f5);padding:.7rem;border-radius:4px;overflow-x:auto;font-size:.74rem;max-height:320px;white-space:pre-wrap"><?= htmlspecialchars($appLog) ?></pre>
    <?php endif; ?>
</div>

</div><!-- .admin-content -->
</div><!-- .admin-layout -->

<?php include WEBROOT_PATH . '/includes/footer.php'; ?>
