<?php
session_start();
include 'db_connection.php';

// Check if the student is logged in
if (!isset($_SESSION['student_id'])) {
    echo "You need to login first.";
    exit;
}

// Retrieve student_id from session
$student_id = $_SESSION['student_id'];

// Fetch student personal information
$sql_personal = "SELECT name, dob, email, phone, address, class_id FROM students WHERE student_id = ?";
$stmt_personal = $conn->prepare($sql_personal);
$stmt_personal->bind_param("i", $student_id);
$stmt_personal->execute();
$result_personal = $stmt_personal->get_result();
$personal_info = $result_personal->fetch_assoc();

// Fetch class information
$class_id = $personal_info['class_id'];
$sql_class = "SELECT class_name FROM classes WHERE class_id = ?";
$stmt_class = $conn->prepare($sql_class);
$stmt_class->bind_param("i", $class_id);
$stmt_class->execute();
$result_class = $stmt_class->get_result();
$class_info = $result_class->fetch_assoc();

// Fetch fee status
$sql_fees = "SELECT fees_paid, fees_outstanding FROM students WHERE student_id = ?";
$stmt_fees = $conn->prepare($sql_fees);
$stmt_fees->bind_param("i", $student_id);
$stmt_fees->execute();
$result_fees = $stmt_fees->get_result();
$fee_info = $result_fees->fetch_assoc();

// Fetch payment history
$sql_payment_history = "SELECT amount, date FROM payments WHERE student_id = ? ORDER BY date DESC";
$stmt_payment_history = $conn->prepare($sql_payment_history);
$stmt_payment_history->bind_param("i", $student_id);
$stmt_payment_history->execute();
$result_payment_history = $stmt_payment_history->get_result();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student Profile</title>
    <link rel="stylesheet" href="styles.css"> <!-- Link your CSS file here -->
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 80%;
            margin: 0 auto;
            background: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }
        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #333;
        }
        .profile-section {
            margin-bottom: 30px;
        }
        .profile-section h3 {
            margin-bottom: 10px;
            color: #555;
        }
        .profile-section p {
            margin: 5px 0;
            color: #666;
        }
        .payment-history {
            margin-top: 20px;
        }
        .payment-history table {
            width: 100%;
            border-collapse: collapse;
        }
        .payment-history th, .payment-history td {
            padding: 10px;
            border: 1px solid #ddd;
            text-align: left;
        }
        .payment-history th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
    <?php include 'header.php'; ?>
    <div class="container">
        <h2>Student Profile</h2>
        
        <div class="profile-section">
            <h3>Personal Information</h3>
            <p><strong>Name:</strong> <?php echo $personal_info['name']; ?></p>
            <p><strong>Date of Birth:</strong> <?php echo $personal_info['dob']; ?></p>
            <p><strong>Email:</strong> <?php echo $personal_info['email']; ?></p>
            <p><strong>Phone:</strong> <?php echo $personal_info['phone']; ?></p>
            <p><strong>Address:</strong> <?php echo $personal_info['address']; ?></p>
        </div>

        <div class="profile-section">
            <h3>Academic Information</h3>
            <p><strong>Class:</strong> <?php echo $class_info['class_name']; ?></p>
        </div>

        <div class="profile-section">
            <h3>Fee Status</h3>
            <p><strong>Total Fees Paid:</strong> <?php echo $fee_info['fees_paid']; ?></p>
            <p><strong>Outstanding Fees:</strong> <?php echo $fee_info['fees_outstanding']; ?></p>
        </div>

        <div class="profile-section payment-history">
            <h3>Payment History</h3>
            <table>
                <tr>
                    <th>Amount</th>
                    <th>Payment Date</th>
                </tr>
                <?php
                if ($result_payment_history->num_rows > 0) {
                    while ($row_payment = $result_payment_history->fetch_assoc()) {
                        echo "<tr>";
                        echo "<td>" . $row_payment['amount'] . "</td>";
                        echo "<td>" . $row_payment['payment_date'] . "</td>";
                        echo "</tr>";
                    }
                } else {
                    echo "<tr><td colspan='2'>No payment history available.</td></tr>";
                }
                ?>
            </table>
        </div>
    </div>
</body>
</html>

<?php
// Close the prepared statements and connection
$stmt_personal->close();
$stmt_class->close();
$stmt_fees->close();
$stmt_payment_history->close();
$conn->close();
?>
