<?php
session_start();
error_reporting(0);
include('includes/dbconnection.php');
$student_id = $_GET['student_id'] ?? '';
$query = mysqli_query($con, "SELECT * FROM students WHERE id = '$student_id'");
$student = mysqli_fetch_assoc($query);

$studentImage = "studentimages/" . htmlspecialchars($student['studentImage']);

use Dotenv\Dotenv;

require_once 'vendor/autoload.php';
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$school_name = $_ENV['SCHOOL_NAME'];
$logo = $_ENV['WEBSITE_LOGO'];
$address = $_ENV['SCHOOL_ADDRESS'];
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php @include("includes/head.php"); ?>
    <style>
        @media print {
            .no-print {
                display: none;
            }
        }

        body {
            font-family: 'Segoe UI', sans-serif;
            background-color: #f9f9f9;
        }

        .profile-wrapper {
            max-width: 1000px;
            margin: auto;
            background: #fff;
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 40px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .school-header {
            text-align: center;
            border-bottom: 1px solid #ccc;
            margin-bottom: 30px;
            padding-bottom: 15px;
        }

        .card-header {
            margin-top: 5px;
            margin-bottom: 20px;
        }

        .school-header h1 {
            margin: 0;
            font-size: 26px;
            color: #2c3e50;
        }

        .profile-header {
            display: flex;
            align-items: center;
            gap: 20px;
            margin-bottom: 30px;
        }

        .profile-photo {
            width: 70px;
            height: 70px;
            border-radius: 30%;
            object-fit: cover;
            border: 2px solid #2c3e50;
        }

        .student-name {
            font-size: 18px;
            font-weight: bold;
            color: #2c3e50;
        }

        .student-class {
            font-size: 15px;
            margin-top: 5px;
            font-weight: bold;
            color: #7f8c8d;
        }

        .profile-info {
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 15px 10px;
        }

        .info-group {
            display: flex;
        }

        .info-label {
            font-weight: 600;
            color: #555;
        }

        .info-value {
            margin-left: 10px;
        }

        .print-button {
            margin: 30px auto 0;
            display: block;
            padding: 10px 25px;
            background-color: #0069d9;
            color: #fff;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            cursor: pointer;
        }

        .print-button:hover {
            background-color: #0053ba;
        }
    </style>
</head>

<body>
    <div class="profile-wrapper">
        <div class="school-header">
            <img src="<?php echo $logo; ?>" width="70" height="70" alt="Logo">
            <h1><strong><?php echo strtoupper($school_name); ?></strong></h1>
            <?php echo $address; ?>
        </div>
        <div class="card-header bg-primary text-white">
            <h5 style="text-align: center;font-weight:bold;"> STUDENT PROFILE</h5>
        </div>

        <div class="profile-header">
            <img src="<?php echo htmlentities($studentImage); ?>" alt="Student Photo" class="profile-photo">
            <div>
                <div class="student-name"><?php echo $student['studentName'] ?? 'N/A'; ?></div>
                <div class="student-class">Admission No: <?php echo $student['studentno'] ?? 'N/A'; ?></div>
            </div>
        </div>

        <div class="card-header bg-primary text-white">
            <h5><i class="fas fa-user-graduate"></i> Student Details</h5>
        </div>
        <div class="profile-info">
            <?php
            $fields = [
                'class' => 'Class',
                'section' => 'Arm',
                'stream' => 'School Type',
                'scholarship' => 'Scholarship Status',
                'email' => 'Email',
                'gender' => 'Gender',
                'age' => 'Date of Birth',
                'contactno' => 'Contact',
                'nationality' => 'Nationality',
                'state' => 'State',
                'village' => 'L.G.A',
                'language' => 'Language',
                'religion' => 'Religion'
            ];
            foreach ($fields as $key => $label) {
                $value = $student[$key] ?? 'N/A';
                if ($key === 'status') $value = ($student[$key] == 1) ? 'Active' : 'Inactive';
                echo "<div class='info-group'><div class='info-label'>{$label}:</div><div class='info-value'>{$value}</div></div>";
            }
            ?>
        </div>

        <div class="card-header bg-primary text-white">
            <h5><i class="fas fa-user-tie"></i> Parent Details</h5>
        </div>
        <div class="profile-info">
            <?php
            $parent_fields = [
                'parentName' => 'Parent Name',
                'relation' => 'Relationship',
                'occupation' => 'Occupation',
                'email' => 'Email',
                'nextphone' => 'Contact 2',
                'country' => 'Home Address',
                'district' => 'City',
                'village' => 'L.G.A',
                'work_address' => 'Work Address'
            ];
            foreach ($parent_fields as $key => $label) {
                $value = $student[$key] ?? 'N/A';
                echo "<div class='info-group'><div class='info-label'>{$label}:</div><div class='info-value'>{$value}</div></div>";
            }
            ?>
        </div>

        <div class="card-header bg-primary text-white">
            <h5><i class="fas fa-door-open"></i> Admission Details</h5>
        </div>
        <div class="profile-info">
            <?php
            $admission_fields = [
                'studentno' => 'Admission No',
                'studentRegNo' => 'Registration No',
                'adclass' => 'Admitted Class',
                'academic_session' => 'Session Admitted',
                'admission_term' => 'Term Admitted',
                'stream' => 'School Type',
                'Scholarship' => 'Scholarship Status',
                'postingDate' => 'Profile Created'
            ];
            foreach ($admission_fields as $key => $label) {
                $value = $student[$key] ?? 'N/A';
                echo "<div class='info-group'><div class='info-label'>{$label}:</div><div class='info-value'>{$value}</div></div>";
            }
            ?>
        </div>

        <div class="card-header bg-primary text-white">
            <h5><i class="fas fa-bed"></i> Medical & Boarding Details</h5>
        </div>
        <div class="profile-info">
            <?php
            $medical_fields = [
                'physical_peculiarity' => 'Physical Peculiarity',
                'medical_history' => 'Medical History',
                'doctor_phone' => 'Doctor\'s Phone',
                'doctor_address' => 'Doctor\'s Address',
                'hospital_name' => 'Hospital Name',
                'hospital_address' => 'Hospital Address',
                'boarding_experience' => 'Boarding Experience',
                'ifyes' => 'If Yes, Explain'
            ];
            foreach ($medical_fields as $key => $label) {
                $value = $student[$key] ?? 'N/A';
                echo "<div class='info-group'><div class='info-label'>{$label}:</div><div class='info-value'>{$value}</div></div>";
            }
            ?>
        </div>
    </div>

    <button class="print-button no-print" onclick="window.print()">Print Profile</button>
</body>

</html>