<?php
/**
 * Vue du rapport de restauration
 */

// Inclure le header
include __DIR__ . '/../../includes/header_dynamic.php';
?>

<div class="container-fluid">
    <!-- En-tête de la page -->
    <div class="page-header">
        <div class="row align-items-center">
            <div class="col-md-8">
                <h1 class="page-title">
                    <i class="fas fa-utensils text-warning"></i>
                    <?= $pageTitle ?>
                </h1>
                <p class="page-subtitle text-muted">
                    Analyse détaillée des revenus et performances du restaurant
                </p>
            </div>
            <div class="col-md-4 text-end">
                <a href="dashboard.php" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> Retour au Dashboard
                </a>
            </div>
        </div>
    </div>

    <!-- Filtres de rapport -->
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="card-title mb-0">
                <i class="fas fa-filter"></i> Filtres de Rapport
            </h5>
        </div>
        <div class="card-body">
            <form method="GET" action="reports.php" class="row g-3">
                <input type="hidden" name="report" value="restaurant">
                
                <div class="col-md-3">
                    <label for="date_from" class="form-label">Date de début</label>
                    <input type="date" class="form-control" id="date_from" name="date_from" 
                           value="<?= htmlspecialchars($filters['date_from']) ?>">
                </div>
                
                <div class="col-md-3">
                    <label for="date_to" class="form-label">Date de fin</label>
                    <input type="date" class="form-control" id="date_to" name="date_to" 
                           value="<?= htmlspecialchars($filters['date_to']) ?>">
                </div>
                
                <div class="col-md-2">
                    <label for="period" class="form-label">Période</label>
                    <select class="form-select" id="period" name="period">
                        <option value="daily" <?= $filters['period'] == 'daily' ? 'selected' : '' ?>>Journalier</option>
                        <option value="monthly" <?= $filters['period'] == 'monthly' ? 'selected' : '' ?>>Mensuel</option>
                        <option value="yearly" <?= $filters['period'] == 'yearly' ? 'selected' : '' ?>>Annuel</option>
                    </select>
                </div>
                
                <div class="col-md-2">
                    <label for="chart_type" class="form-label">Type de graphique</label>
                    <select class="form-select" id="chart_type" name="chart_type">
                        <option value="line" <?= $filters['chart_type'] == 'line' ? 'selected' : '' ?>>Ligne</option>
                        <option value="bar" <?= $filters['chart_type'] == 'bar' ? 'selected' : '' ?>>Barres</option>
                        <option value="pie" <?= $filters['chart_type'] == 'pie' ? 'selected' : '' ?>>Secteurs</option>
                    </select>
                </div>
                
                <div class="col-md-2">
                    <label class="form-label">&nbsp;</label>
                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search"></i> Analyser
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Statistiques principales -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card stat-card bg-warning text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between">
                        <div>
                            <h6 class="card-title">Factures Total</h6>
                            <h3 class="mb-0"><?= number_format($restaurantStats['total_invoices'] ?? 0, 0, ',', ' ') ?></h3>
                        </div>
                        <div class="align-self-center">
                            <i class="fas fa-receipt fa-2x"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card stat-card bg-success text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between">
                        <div>
                            <h6 class="card-title">Chiffre d'Affaires</h6>
                            <h3 class="mb-0"><?= number_format($restaurantStats['total_revenue'] ?? 0, 0, ',', ' ') ?> FCFA</h3>
                        </div>
                        <div class="align-self-center">
                            <i class="fas fa-money-bill-wave fa-2x"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card stat-card bg-info text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between">
                        <div>
                            <h6 class="card-title">Moyenne par Facture</h6>
                            <h3 class="mb-0"><?= number_format($restaurantStats['avg_invoice_amount'] ?? 0, 0, ',', ' ') ?> FCFA</h3>
                        </div>
                        <div class="align-self-center">
                            <i class="fas fa-calculator fa-2x"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-3">
            <div class="card stat-card bg-primary text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between">
                        <div>
                            <h6 class="card-title">Clients Uniques</h6>
                            <h3 class="mb-0"><?= number_format($restaurantStats['unique_clients'] ?? 0, 0, ',', ' ') ?></h3>
                        </div>
                        <div class="align-self-center">
                            <i class="fas fa-users fa-2x"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Graphiques -->
    <div class="row mb-4">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-chart-line"></i> Évolution du Chiffre d'Affaires Restaurant
                    </h5>
                </div>
                <div class="card-body">
                    <canvas id="restaurantChart" height="100"></canvas>
                </div>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-chart-pie"></i> Répartition par Catégorie
                    </h5>
                </div>
                <div class="card-body">
                    <canvas id="categoryChart" height="200"></canvas>
                </div>
            </div>
        </div>
    </div>

    <!-- Tableaux de données -->
    <div class="row">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-trophy"></i> Top Articles du Menu
                    </h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Article</th>
                                    <th>Quantité</th>
                                    <th>Revenus</th>
                                    <th>Factures</th>
                                    <th>Prix Moy.</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($topItems as $item): ?>
                                <tr>
                                    <td>
                                        <span class="badge bg-warning">
                                            <?= htmlspecialchars($item['item_name']) ?>
                                        </span>
                                    </td>
                                    <td><?= number_format($item['total_quantity'], 0, ',', ' ') ?></td>
                                    <td class="fw-bold">
                                        <?= number_format($item['total_revenue'], 0, ',', ' ') ?> FCFA
                                    </td>
                                    <td><?= $item['invoices_count'] ?></td>
                                    <td><?= number_format($item['avg_price'], 0, ',', ' ') ?> FCFA</td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-info-circle"></i> Détails Statistiques
                    </h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-12 mb-3">
                            <p><strong>Facture Min:</strong><br>
                            <span class="text-success"><?= number_format($restaurantStats['min_invoice_amount'] ?? 0, 0, ',', ' ') ?> FCFA</span></p>
                        </div>
                        <div class="col-12 mb-3">
                            <p><strong>Facture Max:</strong><br>
                            <span class="text-danger"><?= number_format($restaurantStats['max_invoice_amount'] ?? 0, 0, ',', ' ') ?> FCFA</span></p>
                        </div>
                        <div class="col-12 mb-3">
                            <p><strong>Serveurs Actifs:</strong><br>
                            <span class="text-info"><?= $restaurantStats['waiters_count'] ?? 0 ?></span></p>
                        </div>
                        <div class="col-12">
                            <p><strong>Période:</strong><br>
                            <span class="text-muted"><?= date('d/m/Y', strtotime($filters['date_from'])) ?> - <?= date('d/m/Y', strtotime($filters['date_to'])) ?></span></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Scripts pour les graphiques -->
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Données du graphique principal
    const restaurantData = {
        labels: <?= json_encode($restaurantData['labels']) ?>,
        datasets: [{
            label: 'Nombre de Factures',
            data: <?= json_encode($restaurantData['invoices']) ?>,
            borderColor: 'rgb(255, 193, 7)',
            backgroundColor: 'rgba(255, 193, 7, 0.2)',
            tension: 0.1,
            fill: true
        }, {
            label: 'Chiffre d\'Affaires (FCFA)',
            data: <?= json_encode($restaurantData['revenue']) ?>,
            borderColor: 'rgb(40, 167, 69)',
            backgroundColor: 'rgba(40, 167, 69, 0.2)',
            tension: 0.1,
            yAxisID: 'y1'
        }]
    };

    // Configuration du graphique principal
    const restaurantConfig = {
        type: '<?= $filters['chart_type'] ?>',
        data: restaurantData,
        options: {
            responsive: true,
            maintainAspectRatio: false,
            interaction: {
                mode: 'index',
                intersect: false,
            },
            scales: {
                y: {
                    type: 'linear',
                    display: true,
                    position: 'left',
                    title: {
                        display: true,
                        text: 'Nombre de Factures'
                    }
                },
                y1: {
                    type: 'linear',
                    display: true,
                    position: 'right',
                    title: {
                        display: true,
                        text: 'Chiffre d\'Affaires (FCFA)'
                    },
                    grid: {
                        drawOnChartArea: false,
                    },
                }
            },
            plugins: {
                legend: {
                    position: 'top',
                },
                title: {
                    display: true,
                    text: 'Évolution du Chiffre d\'Affaires Restaurant'
                }
            }
        }
    };

    // Créer le graphique principal
    const restaurantCtx = document.getElementById('restaurantChart').getContext('2d');
    new Chart(restaurantCtx, restaurantConfig);

    // Données du graphique des catégories
    const categoryData = {
        labels: <?= json_encode(array_column($categoryStats, 'category')) ?>,
        datasets: [{
            data: <?= json_encode(array_column($categoryStats, 'total_revenue')) ?>,
            backgroundColor: [
                '#FF6384',
                '#36A2EB',
                '#FFCE56',
                '#4BC0C0',
                '#9966FF',
                '#FF9F40'
            ]
        }]
    };

    // Configuration du graphique des catégories
    const categoryConfig = {
        type: 'doughnut',
        data: categoryData,
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    position: 'bottom',
                },
                title: {
                    display: true,
                    text: 'Répartition par Catégorie'
                }
            }
        }
    };

    // Créer le graphique des catégories
    const categoryCtx = document.getElementById('categoryChart').getContext('2d');
    new Chart(categoryCtx, categoryConfig);
});
</script>

<?php
// Inclure le footer
include __DIR__ . '/../../includes/footer.php';
?>

