<?php																			
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
include "../include/config.php";
$userlogin = $_SESSION['studentid'];

if (isset($_SESSION['studentid'])) {
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Library Report</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;500;600&display=swap" rel="stylesheet">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <meta name="theme-color" content="var(--primary)">
    
    <style>
        :root {
            --primary-color: #1a73e8;
            --secondary-color: #2c3e50;
            --success-color: #0f9d58;
            --warning-color: #f4b400;
            --danger-color: #db4437;
            --light-color: #f8f9fa;
            --dark-color: #343a40;
        }
        
        body {
            font-family: 'Montserrat', sans-serif;
            background-color: #f5f7fa;
            color: #333;
            padding: 0;
            margin: 0;
            font-size: 14px;
        }
        
        .navbar-custom {
            background-color: var(--secondary-color);
        }
        
        .content {
            margin-left: 210px;
            padding: 20px;
        }
        
        @media (max-width: 992px) {
            .content {
                margin-left: 0;
            }
        }
        
        .card {
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.08);
            border: none;
            margin-bottom: 20px;
        }
        
        .card-header {
            background: var(--primary-color);
            color: white;
            border-radius: 12px 12px 0 0 !important;
            padding: 15px 20px;
        }
        
        .table-container {
            border-radius: 8px;
            overflow: hidden;
            margin-bottom: 30px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        
        .library-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 0;
        }
        
        .library-table th {
            background-color: #e9ecef;
            color: var(--secondary-color);
            font-weight: 600;
            padding: 12px 15px;
            text-align: left;
            border-bottom: 2px solid #dee2e6;
        }
        
        .library-table td {
            padding: 12px 15px;
            border-bottom: 1px solid #eaeaea;
            vertical-align: middle;
        }
        
        .library-table tr:last-child td {
            border-bottom: none;
        }
        
        .library-table tr:hover {
            background-color: #f8f9fa;
        }
        
        .status-badge {
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: 600;
        }
        
        .status-returned {
            background-color: rgba(15, 157, 88, 0.15);
            color: var(--success-color);
        }
        
        .status-borrowed {
            background-color: rgba(219, 68, 55, 0.15);
            color: var(--danger-color);
        }
        
        .status-overdue {
            background-color: rgba(244, 180, 0, 0.15);
            color: var(--warning-color);
        }
        
        .fine-amount {
            font-weight: 600;
            color: var(--danger-color);
        }
        
        .no-results {
            text-align: center;
            padding: 40px;
            color: #6c757d;
            font-style: italic;
        }
        
        .stats-box {
            background-color: #f8f9fa;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 20px;
            border-left: 4px solid var(--primary-color);
            text-align: center;
        }
        
        .stats-box h6 {
            color: #6c757d;
            font-size: 0.9rem;
            margin-bottom: 5px;
        }
        
        .stats-box h3 {
            margin: 0;
            font-weight: 700;
        }
        
        .term-stats {
            background-color: #f8f9fa;
            padding: 10px 15px;
            border-bottom: 1px solid #eaeaea;
            font-size: 0.9rem;
        }
        
        .term-stats span {
            margin-right: 15px;
        }
        
        .term-stats .badge {
            font-size: 0.8rem;
            padding: 4px 8px;
        }
        
        .book-cover {
            width: 40px;
            height: 60px;
            background-color: #e9ecef;
            border-radius: 4px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #6c757d;
            font-size: 20px;
        }
        
        @media (max-width: 768px) {
            .content {
                padding: 15px;
            }
            
            .library-table {
                font-size: 0.85rem;
            }
            
            .library-table th, 
            .library-table td {
                padding: 8px 10px;
            }
            
            .stats-box {
                padding: 10px;
                margin-bottom: 15px;
            }
            
            .stats-box h3 {
                font-size: 1.5rem;
            }
            
            .term-stats span {
                display: block;
                margin-bottom: 5px;
            }
        }
    </style>
</head>
<body>
    <?php include "../include/nav.php"; ?>

    <div class="content">

        <div class="container-fluid">
            <div class="card ">
                <div class="card-header bg-secondary">
                    <h5 class="mb-0"><i class="fa fa-book me-2"></i>Library Borrowing History</h5>
                    <p class="mb-0 mt-1 small opacity-75">Your complete record of borrowed books</p>
                </div>
                
                <div class="card-body">
                    <?php
                    // Query to get book borrowing history
                    $query = "SELECT `id`, `book_id`, `studentid`, `issue_date`, `due_date`, `return_date`, `status`, `fine_amount`, `created_at` 
                              FROM `book_issues` 
                              WHERE studentid = '$userlogin' 
                              ORDER BY issue_date DESC, created_at DESC";
                    
                    $result = mysqli_query($conn, $query);
                    
                    if (mysqli_num_rows($result) > 0) {
                        $total_books = 0;
                        $total_borrowed = 0;
                        $total_returned = 0;
                        $total_overdue = 0;
                        $total_fines = 0;
                        
                        // Calculate statistics
                        while ($row = mysqli_fetch_assoc($result)) {
                            $total_books++;
                            
                            if ($row['status'] == 'returned') {
                                $total_returned++;
                            } else {
                                $total_borrowed++;
                                
                                // Check if book is overdue
                                $due_date = strtotime($row['due_date']);
                                $current_date = time();
                                
                                if ($current_date > $due_date) {
                                    $total_overdue++;
                                }
                            }
                            
                            if (!empty($row['fine_amount'])) {
                                $total_fines += $row['fine_amount'];
                            }
                        }
                        
                        // Reset pointer to loop through results again
                        mysqli_data_seek($result, 0);
                        
                        // Display statistics
                        echo '<div class="row mb-4">';
                        echo '<div class="col-md-3">';
                        echo '<div class="stats-box">';
                        echo '<h6>Total Books</h6>';
                        echo '<h3>' . $total_books . '</h3>';
                        echo '</div>';
                        echo '</div>';
                        
                        echo '<div class="col-md-3">';
                        echo '<div class="stats-box">';
                        echo '<h6>Currently Borrowed</h6>';
                        echo '<h3>' . $total_borrowed . '</h3>';
                        echo '</div>';
                        echo '</div>';
                        
                        echo '<div class="col-md-3">';
                        echo '<div class="stats-box">';
                        echo '<h6>Overdue Books</h6>';
                        echo '<h3>' . $total_overdue . '</h3>';
                        echo '</div>';
                        echo '</div>';
                        
                      
                        echo '</div>';
                        
                        // Display book borrowing history
                        echo '<div class="table-container">';
                        echo '<table class="library-table">';
                        
                        // Table headers
                        echo '<tr>';
                        echo '<th>Book ID</th>';
                        echo '<th>Issue Date</th>';
                        echo '<th>Due Date</th>';
                        echo '<th>Return Date</th>';
                        echo '<th>Status</th>';
                        echo '</tr>';
                        
                        // Book rows
                        while ($row = mysqli_fetch_assoc($result)) {
                            // Determine status badge
                            $status_class = '';
                            $status_text = '';
                            
                            if ($row['status'] == 'returned') {
                                $status_class = 'status-returned';
                                $status_text = 'Returned';
                            } else {
                                // Check if book is overdue
                                $due_date = strtotime($row['due_date']);
                                $current_date = time();
                                
                                if ($current_date > $due_date) {
                                    $status_class = 'status-overdue';
                                    $status_text = 'Overdue';
                                } else {
                                    $status_class = 'status-borrowed';
                                    $status_text = 'Borrowed';
                                }
                            }
                            
                            echo '<tr>';
                            echo '<td><div class="book-cover"><i class="fa fa-book"></i></div>' . $row['book_id'] . '</td>';
                            echo '<td>' . date('M d, Y', strtotime($row['issue_date'])) . '</td>';
                            echo '<td>' . date('M d, Y', strtotime($row['due_date'])) . '</td>';
                            echo '<td>' . (!empty($row['return_date']) ? date('M d, Y', strtotime($row['return_date'])) : '-') . '</td>';
                            echo '<td><span class="status-badge ' . $status_class . '">' . $status_text . '</span></td>';
                          
                            echo '</tr>';
                        }
                        
                        echo '</table>';
                        echo '</div>';
                    } else {
                        echo '<div class="no-results">';
                        echo '<i class="fa fa-book fa-3x mb-3"></i>';
                        echo '<h5>No borrowing history found</h5>';
                        echo '<p>You haven\'t borrowed any books from the library yet.</p>';
                        echo '</div>';
                    }
                    ?>
                </div>
            </div>
        </div>
    </div>

    <script>
        // Add some interactivity
        document.addEventListener('DOMContentLoaded', function() {
            // Highlight rows on hover
            const rows = document.querySelectorAll('.library-table tr');
            rows.forEach(row => {
                row.addEventListener('mouseenter', function() {
                    this.style.backgroundColor = '#f1f4f7';
                    this.style.transition = 'background-color 0.2s ease';
                });
                
                row.addEventListener('mouseleave', function() {
                    this.style.backgroundColor = '';
                });
            });
            
            // Add click event to show book details (could be expanded)
            const bookRows = document.querySelectorAll('.library-table tr:not(:first-child)');
            bookRows.forEach(row => {
                row.addEventListener('click', function() {
                    // This could open a modal with more book details
                    console.log('Book row clicked');
                });
            });
        });
    </script>
</body>
</html>
<?php
} else {
    header("location:../index.php");
}
?>