<?php
// gestion_compagnies/logs_agence.php
// Page pour sélectionner une agence et afficher les logs d'opérations et de backup/restauration

session_start();
@date_default_timezone_set('Africa/Casablanca');
require_once __DIR__.'/config/connexion.php';
require_once __DIR__.'/config/auth.php';
gc_require_login();

// Récupérer la liste des compagnies/agences
$compagnies = [];
try {
    $stmt = $gc_pdo->query('SELECT compagnie_ref, compagnie_name FROM compagnies_table ORDER BY compagnie_name');
    $compagnies = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(Exception $e) {}
$selected = isset($_GET['compagnie_ref']) ? $_GET['compagnie_ref'] : '';
$logs = [];
$backupLogs = [];
if ($selected) {
    $logFile = __DIR__.'/../data/'.$selected.'/journal.log';
    if (file_exists($logFile)) {
        $lines = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        $logs = array_reverse(array_slice($lines, -100));
    }
    $backupLogFile = __DIR__.'/../backup/'.$selected.'_backup.log';
    if (file_exists($backupLogFile)) {
        $backupLogs = array_reverse(array_slice(file($backupLogFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES), -100));
    }
}
?><!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="utf-8">
    <title>Logs agence / compagnie</title>
    <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../vendor/fontawesome-free/css/all.min.css">
    <link rel="stylesheet" href="../css/ruang-admin.min.css">
</head>
<body id="page-top">
<div id="wrapper">
    <?php include __DIR__.'/sidebar.php'; ?>
    <div id="content-wrapper" class="d-flex flex-column">
        <div id="content">
            <?php include __DIR__.'/topbar.php'; ?>
            <div class="container-fluid">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h3 class="mb-0"><i class="fas fa-clipboard-list text-primary"></i> Logs agence / compagnie</h3>
                </div>
                <form method="get" class="mb-4 row align-items-end">
                    <div class="col-md-4">
                        <label for="compagnie_ref">Sélectionner une agence :</label>
                        <select name="compagnie_ref" id="compagnie_ref" class="form-control">
                            <option value="">-- Choisir --</option>
                            <?php foreach($compagnies as $c): ?>
                                <option value="<?=htmlspecialchars($c['compagnie_ref'])?>" <?=($selected==$c['compagnie_ref'])?'selected':''?>><?=htmlspecialchars($c['compagnie_name']??$c['compagnie_ref'])?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-primary">Afficher</button>
                    </div>
                </form>
                <?php if($selected): ?>
                    <div class="row">
                        <div class="col-lg-6 mb-4">
                            <div class="card shadow mb-4">
                                <div class="card-header py-3"><h6 class="m-0 font-weight-bold text-primary">Dernières opérations (journal.log)</h6></div>
                                <div class="card-body" style="max-height:350px;overflow:auto;">
                                    <pre style="background:none;border:none;padding:0;margin:0;"><?php echo $logs ? htmlspecialchars(implode("\n", $logs)) : 'Aucune opération trouvée.'; ?></pre>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-6 mb-4">
                            <div class="card shadow mb-4">
                                <div class="card-header py-3"><h6 class="m-0 font-weight-bold text-primary">Logs backup/restauration</h6></div>
                                <div class="card-body" style="max-height:350px;overflow:auto;">
                                    <pre style="background:none;border:none;padding:0;margin:0;"><?php echo $backupLogs ? htmlspecialchars(implode("\n", $backupLogs)) : 'Aucun log backup/restauration.'; ?></pre>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
                <p class="text-center text-muted small mb-4">&copy; <?php echo date('Y'); ?> Logs agence</p>
            </div>
        </div>
        <footer class="sticky-footer bg-white">
            <div class="container my-auto">
                <div class="text-center my-auto small">&copy; <?php echo date('Y'); ?> Backups</div>
            </div>
        </footer>
    </div>
</div>
<script src="../vendor/jquery/jquery.min.js"></script>
<script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="../vendor/jquery-easing/jquery.easing.min.js"></script>
<script src="../js/ruang-admin.min.js"></script>
</body>
</html>
