<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

$collegePage = 'students';
Auth()->requireRole(['college']);

$db   = DB();
$user = Auth()->getUser();
$id   = intval($_GET['id'] ?? 0);

$student = $db->fetchOne("
    SELECT s.*, c.course_name, c.course_code, c.duration, c.category,
           csi.sub_name, csi.duration_label
    FROM students s
    LEFT JOIN courses c ON s.course_id = c.id
    LEFT JOIN course_sub_items csi ON s.sub_item_id = csi.id
    WHERE s.id = ? AND s.institution_id = ?
", [$id, $user['id']]);

if (!$student) {
    redirectWithMessage('students.php', 'Student not found.', 'danger');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Student Details – <?= APP_SHORT_NAME ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<style>
:root{--primary:#3b0b7c;--secondary:#6c4ab6;}
.college-sidebar{width:250px;background:white;min-height:100vh;position:fixed;top:0;left:0;display:flex;flex-direction:column;box-shadow:3px 0 15px rgba(59,11,124,.08);z-index:1040;transition:transform .3s;}
.sidebar-brand{padding:28px 20px 20px;text-align:center;background:linear-gradient(135deg,var(--primary),var(--secondary));}
.sidebar-logo{width:68px;height:68px;border-radius:50%;border:3px solid rgba(255,255,255,.35);object-fit:cover;margin-bottom:12px;}
.sidebar-college-name{color:white;font-weight:700;font-size:.9rem;line-height:1.3;}
.sidebar-code{color:rgba(255,255,255,.65);font-size:.75rem;margin-top:4px;}
.sidebar-nav{flex:1;padding:16px 12px;overflow-y:auto;}
.sidebar-link{display:flex;align-items:center;gap:12px;padding:11px 16px;border-radius:10px;color:#555;text-decoration:none;font-size:.9rem;font-weight:500;margin-bottom:4px;transition:background .2s,color .2s,transform .15s;}
.sidebar-link:hover{background:#f0ebf8;color:var(--primary);transform:translateX(3px);}
.sidebar-link.active{background:linear-gradient(135deg,var(--primary),var(--secondary));color:white!important;box-shadow:0 4px 12px rgba(59,11,124,.25);}
.sidebar-icon{width:18px;text-align:center;flex-shrink:0;font-size:.95rem;}
.sidebar-footer{padding:12px;border-top:1px solid #f0f0f0;}
.sidebar-logout{color:#dc3545!important;}
.college-main{margin-left:250px;padding:30px;min-height:100vh;background:#f8f9fa;}
@media(max-width:768px){.college-sidebar{transform:translateX(-100%)}.college-main{margin-left:0;padding:15px}}
.info-row{display:flex;padding:10px 0;border-bottom:1px solid #f0f0f0;}
.info-label{width:200px;font-weight:600;color:#555;flex-shrink:0;}
.info-value{color:#222;}
.student-photo{width:130px;height:160px;object-fit:cover;border-radius:10px;border:3px solid #e0d8f0;}
.reg-badge{font-family:monospace;background:linear-gradient(135deg,var(--primary),var(--secondary));color:white;padding:6px 18px;border-radius:20px;font-size:1rem;font-weight:700;letter-spacing:.05em;}
</style>
</head>
<body>
<?php include __DIR__ . '/../includes/college_sidebar.php'; ?>
<div class="college-main">

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <a href="students.php" class="btn btn-outline-secondary btn-sm mb-2">
            <i class="fas fa-arrow-left me-1"></i>Back
        </a>
        <h3 class="fw-bold mb-0">Student Details</h3>
    </div>
    <span class="reg-badge"><?= htmlspecialchars($student['registration_number'] ?? 'N/A') ?></span>
</div>

<div class="row g-4">
    <!-- Photo & Status Card -->
    <div class="col-md-3">
        <div class="card border-0 shadow-sm rounded-4 text-center p-4">
            <?php if (!empty($student['photo_path']) && file_exists('../' . $student['photo_path'])): ?>
                <img src="../<?= htmlspecialchars($student['photo_path']) ?>" class="student-photo mx-auto d-block mb-3" alt="Photo">
            <?php else: ?>
                <div class="student-photo mx-auto d-flex align-items-center justify-content-center mb-3"
                     style="background:#f0ebf8;">
                    <i class="fas fa-user fa-3x text-secondary opacity-50"></i>
                </div>
            <?php endif; ?>
            <h5 class="fw-bold mb-1"><?= htmlspecialchars($student['full_name']) ?></h5>
            <small class="text-muted"><?= htmlspecialchars($student['course_name'] ?? '-') ?></small>
            <div class="mt-3">
                <?php
                $ap = $student['approval_status'] ?? 'pending';
                $cls = match($ap){ 'approved'=>'bg-success','rejected'=>'bg-danger',default=>'bg-warning text-dark'};
                ?>
                <span class="badge <?= $cls ?> fs-6 px-3"><?= ucfirst($ap) ?></span>
            </div>
        </div>
    </div>

    <!-- Details -->
    <div class="col-md-9">
        <div class="card border-0 shadow-sm rounded-4">
            <div class="card-header fw-bold py-3" style="background:linear-gradient(135deg,var(--primary),var(--secondary));color:white;border-radius:15px 15px 0 0;">
                <i class="fas fa-id-card me-2"></i>Admission Details
            </div>
            <div class="card-body p-4">

                <h6 class="text-primary fw-bold mb-3">Course Information</h6>
                <div class="info-row"><span class="info-label">Roll Number</span><span class="info-value fw-bold text-danger"><?= htmlspecialchars($student['roll_number'] ?? 'PENDING APPROVAL') ?></span></div>
                <div class="info-row"><span class="info-label">Course</span><span class="info-value"><?= htmlspecialchars($student['course_name'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Sub Course</span><span class="info-value"><?= htmlspecialchars($student['sub_name'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Duration</span><span class="info-value"><?= htmlspecialchars($student['duration_label'] ?? $student['duration'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Course Code</span><span class="info-value"><?= htmlspecialchars($student['course_code'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Session</span><span class="info-value"><?= htmlspecialchars($student['session_year'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Admission Date</span><span class="info-value"><?= $student['admission_date'] ? date('d M Y', strtotime($student['admission_date'])) : '-' ?></span></div>

                <h6 class="text-primary fw-bold mb-3 mt-4">Student Information</h6>
                <div class="info-row"><span class="info-label">Student Name</span><span class="info-value fw-bold"><?= htmlspecialchars($student['full_name']) ?></span></div>
                <div class="info-row"><span class="info-label">Father's Name</span><span class="info-value"><?= htmlspecialchars($student['father_name'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Mother's Name</span><span class="info-value"><?= htmlspecialchars($student['mother_name'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Date of Birth</span><span class="info-value"><?= $student['date_of_birth'] ? date('d M Y', strtotime($student['date_of_birth'])) : '-' ?></span></div>
                <div class="info-row"><span class="info-label">Mobile</span><span class="info-value"><?= htmlspecialchars($student['mobile'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Email</span><span class="info-value"><?= htmlspecialchars($student['email'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Address</span><span class="info-value"><?= nl2br(htmlspecialchars($student['address'] ?? '-')) ?></span></div>

                <h6 class="text-primary fw-bold mb-3 mt-4">Training Details</h6>
                <div class="info-row"><span class="info-label">Training Center</span><span class="info-value"><?= htmlspecialchars($student['training_center'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Training Period</span><span class="info-value"><?= htmlspecialchars($student['training_period'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Examining Body</span><span class="info-value fw-bold text-primary"><?= htmlspecialchars($student['examining_body'] ?? 'Allied Healthcare Council Of India') ?></span></div>

                <h6 class="text-primary fw-bold mb-3 mt-4">Fee Details</h6>
                <div class="info-row"><span class="info-label">Total Fee</span><span class="info-value fw-bold text-success">₹<?= number_format($student['total_fee'] ?? 0, 2) ?></span></div>
                <div class="info-row"><span class="info-label">Submitted On</span><span class="info-value"><?= date('d M Y, h:i A', strtotime($student['created_at'])) ?></span></div>

            </div>
        </div>
    </div>
</div>

</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
function toggleSidebar(){document.getElementById('collegeSidebar').classList.toggle('open');document.getElementById('sidebarOverlay').classList.toggle('d-none');}
</script>
</body>
</html>
