<?php require_once 'connect.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Library Reports - BookTracker</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
            font-family: 'Libre Baskerville', serif;
        }
        .report-section {
            background: white;
            border-radius: 8px;
            margin: 2rem 0;
            padding: 2rem;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        h1, h2 {
            color: #2C4A52;
            border-bottom: 2px solid #D4AA7D;
            padding-bottom: 0.5rem;
            margin-bottom: 1.5rem;
        }
        .table-hover tbody tr:hover {
            background-color: #f5f5f5;
        }
        .table thead th {
            background-color: #2C4A52;
            color: white;
        }
        .stat-number {
            font-weight: 600;
            color: #D4AA7D;
        }
        .nav {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
        }
        .nav a {
            color: #2C4A52;
            text-decoration: none;
            padding: 10px 15px;
            border: 1px solid #2C4A52;
            border-radius: 5px;
        }
        .nav a:hover {
            background-color: #2C4A52;
            color: white;
        }
    </style>
</head>
<body>
    <div class="container-fluid py-4">
        <header class="mb-5 text-center">
            <h1 class="display-4" style="color: #2C4A52;">BookTracker Analytics</h1>
            <div class="nav justify-content-center">
                <a href="#member-stats">Member Statistics</a>
                <a href="#book-stats">Book Statistics</a>
                <a href="#genre-trends">Genre Trends</a>
            </div>
        </header>

        <!-- Member Statistics Section -->
        <div class="report-section" id="member-stats">
            <h2>Member Loan Statistics (<?php echo date('Y'); ?>)</h2>
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>Member</th>
                            <th>Status</th>
                            <th>Month</th>
                            <th>Total Loans</th>
                            <th>Current Loans</th>
                            <th>Total Fines</th>
                            <th>Fines Amount</th>
                            <th>Paid Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        try {
                            $stmt = $conn->query("SELECT * FROM get_member_history()");
                            $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
                            
                            if(count($results) > 0) {
                                foreach($results as $row) {
                                    $startDate = new DateTime($row['startdate']);
                                    $formattedDate = $startDate->format('F');
                                    echo "<tr>
                                            <td>{$row['membername']}</td>
                                            <td>{$row['membershipstatus']}</td>
                                            <td>{$formattedDate}</td>
                                            <td class='stat-number'>{$row['totalloans']}</td>
                                            <td class='stat-number'>{$row['currentloans']}</td>
                                            <td class='stat-number'>{$row['totalfines']}</td>
                                            <td class='stat-number'>$".number_format($row['totalfineamount'], 2)."</td>
                                            <td class='stat-number'>$".number_format($row['totalpayments'], 2)."</td>
                                          </tr>";
                                }
                            } else {
                                echo "<tr><td colspan='8'>No member statistics available for " . date('Y') . ".</td></tr>";
                            }
                        } catch(PDOException $e) {
                            echo "<tr><td colspan='8'>Error loading member statistics: " . $e->getMessage() . "</td></tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Book Statistics Section -->
        <div class="report-section" id="book-stats">
            <h2>Book Loan Statistics</h2>
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>Title</th>
                            <th>Avg Days</th>
                            <th>Min Days</th>
                            <th>Max Days</th>
                            <th>Total Loans</th>
                            <th>Loans with Fines</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        try {
                            $stmt = $conn->query("SELECT * FROM get_book_loan_stats()");
                            $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
                            
                            if(count($results) > 0) {
                                foreach($results as $row) {
                                    echo "<tr>
                                            <td>{$row['title']}</td>
                                            <td class='stat-number'>".number_format($row['avgdaystoreturn'], 1)."</td>
                                            <td class='stat-number'>{$row['mindaystoreturn']}</td>
                                            <td class='stat-number'>{$row['maxdaystoreturn']}</td>
                                            <td class='stat-number'>{$row['totalloans']}</td>
                                            <td class='stat-number'>{$row['loanswithfines']}</td>
                                          </tr>";
                                }
                            } else {
                                echo "<tr><td colspan='6'>No book statistics found.</td></tr>";
                            }
                        } catch(PDOException $e) {
                            echo "<tr><td colspan='6'>Error loading book statistics: " . $e->getMessage() . "</td></tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Genre Trends Section -->
        <div class="report-section" id="genre-trends">
            <h2>Genre Trends Analysis - Last 3 Quarters</h2>
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>Genre</th>
                            <th>Quarter</th>
                            <th>Loans</th>
                            <th>Average</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        try {
                            $stmt = $conn->query("SELECT * FROM get_genre_loan_stats()");
                            $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
                            
                            if(count($results) > 0) {
                                foreach($results as $row) {
                                    $period = new DateTime($row['period']);
                                    $formattedPeriod = 'Q' . ceil($period->format('n')/3) . ' ' . $period->format('Y');
                                    echo "<tr>
                                            <td>{$row['genre']}</td>
                                            <td>{$formattedPeriod}</td>
                                            <td class='stat-number'>{$row['loancount']}</td>
                                            <td class='stat-number'>".number_format($row['movingaverage'], 2)."</td>
                                          </tr>";
                                }
                            } else {
                                echo "<tr><td colspan='4'>No genre trends data available for the last 3 quarters.</td></tr>";
                            }
                        } catch(PDOException $e) {
                            echo "<tr><td colspan='4'>Error loading genre trends: " . $e->getMessage() . "</td></tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>