<?php 
ini_set('display_errors', 1); error_reporting(E_ALL);
require_once '../db_connect.php'; 
include 'includes/header.php'; 
include 'includes/sidebar.php'; 

if (!isset($_GET['id'])) { echo "<script>window.location.href='students.php';</script>"; exit; }

try {
    // নতুন ডাটাবেস লজিক অনুযায়ী Enrollments টেবিল থেকে সব কোর্সের নাম আনা হচ্ছে
    $stmt = $pdo->prepare("
        SELECT s.*, 
               GROUP_CONCAT(c.title SEPARATOR '<br>') as enrolled_courses 
        FROM students s 
        LEFT JOIN enrollments e ON s.id = e.student_id 
        LEFT JOIN courses c ON e.course_id = c.id 
        WHERE s.id = ? 
        GROUP BY s.id
    ");
    $stmt->execute([$_GET['id']]);
    $student = $stmt->fetch(PDO::FETCH_ASSOC);
} catch(Exception $e) { die("Error: " . $e->getMessage()); }

if (!$student) { 
    die("<div class='main-content p-5'><div class='alert alert-danger fw-bold text-center border-0 shadow-sm'>Student not found in database!</div></div>"); 
}
?>

<style>
    /* Premium Theme Colors */
    :root { --theme-green: #064e3b; --theme-dark: #022c22; --theme-gold: #d4af37; --bg-soft: #f8fafc; }
    
    .main-content { background-color: var(--bg-soft); min-height: 100vh; }
    .dashboard-header { background: #fff; padding: 20px 30px; border-radius: 15px; box-shadow: 0 5px 20px rgba(0,0,0,0.03); margin-bottom: 25px; border-left: 6px solid var(--theme-green); }
    
    /* Card Styles */
    .custom-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 18px; box-shadow: 0 10px 40px rgba(0,0,0,0.03); padding: 25px; margin-bottom: 25px; position: relative; overflow: hidden; }
    .card-title-border { font-weight: 800; color: var(--theme-dark); border-bottom: 2px solid var(--bg-soft); padding-bottom: 15px; margin-bottom: 20px; display: flex; align-items: center; }
    
    /* Info Grid */
    .info-label { font-size: 11px; text-transform: uppercase; font-weight: 800; color: #94a3b8; letter-spacing: 0.5px; margin-bottom: 2px; }
    .info-value { font-size: 15px; font-weight: 600; color: var(--theme-dark); word-break: break-word; }
    .info-group { margin-bottom: 20px; }
    
    /* Status Badges */
    .status-pill { padding: 4px 12px; border-radius: 50px; font-size: 11px; font-weight: 800; text-transform: uppercase; }
    .pill-active { background: #ecfdf5; color: #059669; border: 1px solid #d1fae5; }
    .pill-trial { background: #fffbeb; color: #d97706; border: 1px solid #fef3c7; }
    
    .course-badge { background: rgba(6, 78, 59, 0.05); border: 1px solid rgba(6, 78, 59, 0.1); color: var(--theme-green); font-size: 12px; padding: 6px 15px; border-radius: 8px; font-weight: 800; display: inline-block; margin-bottom: 5px; width: 100%; text-align: left;}

    .fade-up { animation: fadeInUp 0.5s ease-out forwards; }
    @keyframes fadeInUp { 0% { opacity: 0; transform: translateY(20px); } 100% { opacity: 1; transform: translateY(0); } }

    /* --- 📱 Mobile Responsive Fixes --- */
    @media (max-width: 768px) {
        .main-content { padding: 15px !important; margin-left: 0 !important; }
        .dashboard-header { flex-direction: column; align-items: flex-start !important; gap: 15px; padding: 15px 20px; }
        .dashboard-header .d-flex { width: 100%; align-items: center; }
        .dashboard-header > a.btn { width: 100%; justify-content: center; } 
        #sidebarToggle { width: 42px !important; height: 42px !important; padding: 0; display: inline-flex !important; align-items: center; justify-content: center; flex-shrink: 0; border-radius: 8px;}
    }
</style>

<div class="main-content p-4 fade-up">
    
    <div class="dashboard-header d-flex justify-content-between align-items-center">
        <div class="d-flex align-items-center">
            <button class="btn btn-light border d-md-none me-3 shadow-sm" id="sidebarToggle">
                <i class="fas fa-bars text-dark"></i>
            </button>
            <h4 class="mb-0 fw-bold" style="color: var(--theme-green);">
                <i class="fas fa-id-card me-2" style="color: var(--theme-gold);"></i> Detailed Profile
            </h4>
        </div>
        <a href="students.php" class="btn btn-light border px-4 py-2 rounded-pill fw-bold shadow-sm text-dark text-decoration-none mt-3 mt-md-0">
            <i class="fas fa-arrow-left me-2"></i> Student List
        </a>
    </div>
    
    <div class="row g-4">
        <div class="col-lg-4">
            <div class="custom-card text-center" style="border-top: 5px solid var(--theme-gold);">
                <div class="position-absolute top-0 end-0 p-3">
                    <span class="status-pill <?= $student['status'] == 'active' ? 'pill-active' : 'bg-light text-muted' ?>">
                        <?= $student['status'] ?>
                    </span>
                </div>

                <div style="width: 120px; height: 120px; border-radius: 50%; overflow: hidden; margin: 10px auto 20px; border: 4px solid var(--bg-soft); box-shadow: 0 8px 20px rgba(0,0,0,0.05);">
                    <?php if(!empty($student['profile_photo'])): ?>
                        <img src="../<?= htmlspecialchars($student['profile_photo']) ?>" style="width:100%; height:100%; object-fit:cover;">
                    <?php else: ?>
                        <div style="width:100%; height:100%; background: rgba(212, 175, 55, 0.1); color: var(--theme-green); font-size: 45px; font-weight: 800; display: flex; align-items: center; justify-content: center;">
                            <?= strtoupper(substr($student['name'], 0, 1)) ?>
                        </div>
                    <?php endif; ?>
                </div>

                <h4 class="fw-bold text-dark mb-1"><?= htmlspecialchars($student['name']) ?></h4>
                <p class="text-muted fw-bold small mb-4">ID: #<?= htmlspecialchars($student['admission_id'] ?? $student['id']) ?></p>
                
                <div class="text-start border-top pt-3">
                    <div class="info-label mb-2">Enrolled Courses</div>
                    <?php 
                    if (!empty($student['enrolled_courses'])) {
                        $courses_arr = explode('<br>', $student['enrolled_courses']);
                        foreach($courses_arr as $c_name) {
                            echo "<div class='course-badge'><i class='fas fa-graduation-cap me-2' style='color: var(--theme-gold);'></i> " . htmlspecialchars($c_name) . "</div>";
                        }
                    } else {
                        echo "<div class='badge bg-light text-danger border w-100 py-2'>Not Enrolled in any course</div>";
                    }
                    ?>
                </div>

                <div class="text-start mt-4">
                    <div class="detail-item py-2 border-bottom d-flex justify-content-between">
                        <span class="text-muted small fw-bold uppercase">Type</span>
                        <span class="fw-bold text-dark small"><?= $student['admission_type'] ?? 'Direct' ?></span>
                    </div>
                    <div class="detail-item py-2 border-bottom d-flex justify-content-between">
                        <span class="text-muted small fw-bold uppercase">Trial Mode</span>
                        <span class="status-pill <?= ($student['is_trial'] ?? 0) ? 'pill-trial' : 'bg-light text-success' ?> small" style="font-size: 9px;">
                            <?= ($student['is_trial'] ?? 0) ? 'Trial Account' : 'Full Admission' ?>
                        </span>
                    </div>
                    <div class="detail-item py-2 d-flex justify-content-between">
                        <span class="text-muted small fw-bold uppercase">Joined</span>
                        <span class="fw-bold text-dark small"><?= date('d M, Y', strtotime($student['created_at'])) ?></span>
                    </div>
                </div>
            </div>

            <div class="custom-card" style="border-left: 5px solid #ef4444;">
                <h6 class="card-title-border" style="font-size: 14px;"><i class="fas fa-phone-alt me-2 text-danger"></i> Emergency Contact</h6>
                <div class="info-group">
                    <div class="info-label">Guardian Phone</div>
                    <div class="info-value text-danger"><?= htmlspecialchars($student['guardian_phone'] ?? 'Not Provided') ?></div>
                </div>
            </div>
        </div>

        <div class="col-lg-8">
            <div class="custom-card">
                <h5 class="card-title-border"><i class="fas fa-user-circle me-2" style="color: var(--theme-gold);"></i> Personal Information</h5>
                <div class="row">
                    <div class="col-md-6 info-group">
                        <div class="info-label">Full Name</div>
                        <div class="info-value"><?= htmlspecialchars($student['name']) ?></div>
                    </div>
                    <div class="col-md-6 info-group">
                        <div class="info-label">Email Address</div>
                        <div class="info-value"><?= htmlspecialchars($student['email']) ?></div>
                    </div>
                    <div class="col-md-6 info-group">
                        <div class="info-label">Student Phone</div>
                        <div class="info-value"><?= htmlspecialchars($student['phone']) ?></div>
                    </div>
                    <div class="col-md-6 info-group">
                        <div class="info-label">Father / Husband Name</div>
                        <div class="info-value"><?= htmlspecialchars($student['father_husband_name'] ?? 'N/A') ?></div>
                    </div>
                    <div class="col-md-6 info-group">
                        <div class="info-label">Mother's Name</div>
                        <div class="info-value"><?= htmlspecialchars($student['mother_name'] ?? 'N/A') ?></div>
                    </div>
                </div>
            </div>

            <div class="custom-card">
                <h5 class="card-title-border"><i class="fas fa-book-open me-2" style="color: var(--theme-gold);"></i> Education Background</h5>
                <div class="row text-center">
                    <div class="col-md-6">
                        <div class="p-3 bg-light rounded-4 border mb-3 mb-md-0">
                            <div class="info-label">SSC / Equivalent</div>
                            <div class="info-value mt-1"><?= htmlspecialchars($student['edu_ssc'] ?? 'N/A') ?></div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="p-3 bg-light rounded-4 border">
                            <div class="info-label">HSC / Equivalent</div>
                            <div class="info-value mt-1"><?= htmlspecialchars($student['edu_hsc'] ?? 'N/A') ?></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="custom-card">
                <h5 class="card-title-border"><i class="fas fa-map-marker-alt me-2" style="color: var(--theme-gold);"></i> Address Details</h5>
                <div class="row">
                    <div class="col-md-6 info-group">
                        <div class="info-label">Present Address</div>
                        <div class="info-value fw-medium" style="font-size: 13px; line-height: 1.6;">
                            <?= nl2br(htmlspecialchars($student['present_address'] ?? 'N/A')) ?>
                        </div>
                    </div>
                    <div class="col-md-6 info-group">
                        <div class="info-label">Permanent Address</div>
                        <div class="info-value fw-medium" style="font-size: 13px; line-height: 1.6;">
                            <?= nl2br(htmlspecialchars($student['permanent_address'] ?? 'N/A')) ?>
                        </div>
                    </div>
                </div>
            </div>

            <div class="alert mt-2 fw-bold border-0 d-flex align-items-center" style="background: rgba(6, 78, 59, 0.05); color: var(--theme-green); border-left: 4px solid var(--theme-gold) !important; border-radius: 12px;">
                <i class="fas fa-info-circle me-3 fa-lg" style="color: var(--theme-gold);"></i> 
                <div style="font-size: 13px;">This student's academic progress tracking and exam history will be available here soon.</div>
            </div>
        </div>
    </div>
</div>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        const toggleBtn = document.getElementById('sidebarToggle');
        if (toggleBtn) {
            toggleBtn.addEventListener('click', function(e) {
                e.preventDefault();
                const sidebar = document.querySelector('.sidebar');
                if (sidebar) { sidebar.classList.toggle('active'); }
            });
        }
    });
</script>

<?php include 'includes/footer.php'; ?>