<?php
/**
 * IBVTE Admin – View Student Details
 */
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

if (!checkDBConnection()) exit;
Auth()->requireRole(['super_admin', 'admin']);

$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,
           u.institution_name, u.institution_code
    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
    LEFT JOIN users u ON s.institution_id = u.id
    WHERE s.id = ?
", [$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>View 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">
<link href="../assets/css/admin-style.css" rel="stylesheet">
<style>
:root{--primary:#3b0b7c;--secondary:#6c4ab6;}
.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/admin_header.php'; ?>
<div class="container-fluid">
<div class="row">
<?php include __DIR__ . '/../includes/admin_sidebar.php'; ?>
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 py-4">

<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 to List
        </a>
        <h2 class="h3 fw-bold mb-0">Student Profile</h2>
    </div>
    <span class="reg-badge"><?= htmlspecialchars($student['registration_number'] ?? 'N/A') ?></span>
</div>

<div class="row g-4">
    <!-- Left Panel -->
    <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>
            <div class="badge bg-light text-primary mb-3 border"><?= htmlspecialchars($student['institution_code'] ?? 'Direct') ?></div>
            
            <div class="mt-2">
                <?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 py-2 w-100"><?= strtoupper($ap) ?></span>
            </div>

            <?php if ($ap === 'pending'): ?>
            <div class="d-grid gap-2 mt-3">
                <a href="students.php?action=approve&id=<?= $id ?>" class="btn btn-success fw-bold" onclick="return confirm('Approve this student?')">APPROVE</a>
                <a href="students.php?action=reject&id=<?= $id ?>" class="btn btn-outline-danger btn-sm" onclick="return confirm('Reject this student?')">REJECT</a>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Right Panel -->
    <div class="col-md-9">
        <div class="card border-0 shadow-sm rounded-4 mb-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-info-circle me-2"></i>Full Information
            </div>
            <div class="card-body p-4">

                <h6 class="text-primary fw-bold mb-3">Institution & Course</h6>
                <div class="info-row"><span class="info-label">Roll Number</span><span class="info-value fw-bold text-danger"><?= htmlspecialchars($student['roll_number'] ?? 'Not Assigned') ?></span></div>
                <div class="info-row"><span class="info-label">College/Center</span><span class="info-value fw-bold"><?= htmlspecialchars($student['institution_name'] ?? 'Direct') ?> (<?= $student['institution_code'] ?>)</span></div>
                <div class="info-row"><span class="info-label">Main Course</span><span class="info-value"><?= htmlspecialchars($student['course_name'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Sub-Course Type</span><span class="info-value"><?= htmlspecialchars($student['sub_name'] ?? 'Standard') ?></span></div>
                <div class="info-row"><span class="info-label">Applied Duration</span><span class="info-value fw-bold"><?= htmlspecialchars($student['duration_label'] ?? $student['duration'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Session</span><span class="info-value"><?= htmlspecialchars($student['session_year'] ?? '-') ?></span></div>

                <h6 class="text-primary fw-bold mb-3 mt-4">Personal Details</h6>
                <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 Number</span><span class="info-value"><?= htmlspecialchars($student['mobile'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Email ID</span><span class="info-value"><?= htmlspecialchars($student['email'] ?? '-') ?></span></div>
                <div class="info-row"><span class="info-label">Residential Address</span><span class="info-value"><?= nl2br(htmlspecialchars($student['address'] ?? '-')) ?></span></div>

                <h6 class="text-primary fw-bold mb-3 mt-4">Training & Body</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">Financials</h6>
                <div class="info-row"><span class="info-label">Total Course 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 At</span><span class="info-value"><?= date('d M Y, h:i A', strtotime($student['created_at'])) ?></span></div>

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

</main>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
