<?php
session_start();

// Include access control functions
require_once '../includes/access_control.php';
require_once '../config/database.php';

// Check if user is logged in and is a teacher
require_teacher();

$user_role = $_SESSION['user_role'];
$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name'] ?? $_SESSION['username'] ?? 'শিক্ষক';

// Only teachers can access this page
if ($user_role !== 'teacher') {
    header('Location: ../index.php');
    exit();
}

// Get teacher ID
$teacher_id = null;
$stmt = $pdo->prepare("SELECT id FROM teachers WHERE user_id = ?");
$stmt->execute([$user_id]);
$teacher = $stmt->fetch();
if ($teacher) {
    $teacher_id = $teacher['id'];
} else {
    // Redirect if not a valid teacher
    header('Location: dashboard.php');
    exit();
}

// Get student ID from URL
$student_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($student_id <= 0) {
    header('Location: my_students.php');
    exit();
}

// Get student data with class and session info
$stmt = $pdo->prepare("
    SELECT s.*, c.name as class_name, c.section as class_section, ses.session_name
    FROM students s
    LEFT JOIN classes c ON s.class_id = c.id
    LEFT JOIN academic_sessions ses ON s.session = ses.session_name
    WHERE s.id = ?
");
$stmt->execute([$student_id]);
$student = $stmt->fetch();

if (!$student) {
    header('Location: my_students.php');
    exit();
}

// Check if teacher has access to this student (student must be in teacher's assigned classes)
if ($teacher_id && !teacher_has_student_access($teacher_id, $student_id)) {
    header('Location: my_students.php');
    exit();
}

// Get student's subjects
$stmt = $pdo->prepare("
    SELECT s.name, s.code
    FROM subjects s
    JOIN student_subjects ss ON s.id = ss.subject_id
    WHERE ss.student_id = ?
    ORDER BY s.name
");
$stmt->execute([$student_id]);
$subjects = $stmt->fetchAll();

// Get student's group info
$group = null;
if (!empty($student['group_name'])) {
    $stmt = $pdo->prepare("SELECT * FROM student_groups WHERE group_name = ?");
    $stmt->execute([$student['group_name']]);
    $group = $stmt->fetch();
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ছাত্র-ছাত্রী প্রোফাইল - স্কুল ম্যানেজমেন্ট সিস্টেম</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/jessoreboard-theme.css" rel="stylesheet">
    <style>
        .student-photo {
            width: 200px;
            height: 200px;
            object-fit: cover;
            border: 5px solid #dee2e6;
        }
        .no-photo {
            width: 200px;
            height: 200px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #e9ecef;
            border: 5px solid #dee2e6;
            font-size: 5rem;
            color: #6c757d;
        }
        .info-card {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="<?php echo ($user_role == 'teacher') ? 'dashboard.php' : '../index.php'; ?>">
                <i class="fas fa-graduation-cap"></i> স্কুল ম্যানেজমেন্ট সিস্টেম
            </a>
            <div class="navbar-nav ms-auto">
                <a class="nav-link" href="<?php echo ($user_role == 'teacher') ? 'my_students.php' : '../students/list.php'; ?>">
                    <i class="fas fa-arrow-left"></i> ছাত্র-ছাত্রী তালিকা
                </a>
            </div>
        </div>
    </nav>

    <div class="container-fluid mt-4">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5><i class="fas fa-user-graduate"></i> ছাত্র-ছাত্রী প্রোফাইল</h5>
                        <div>
                            <a href="edit_student.php?id=<?php echo $student['id']; ?>" class="btn btn-warning btn-sm">
                                <i class="fas fa-edit"></i> সম্পাদনা
                            </a>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <!-- Student Photo -->
                            <div class="col-md-4 text-center mb-4">
                                <?php if (!empty($student['photo']) && file_exists('../' . $student['photo'])): ?>
                                    <img src="../<?php echo $student['photo']; ?>" alt="ছাত্র/ছাত্রীর ছবি" class="student-photo rounded">
                                <?php else: ?>
                                    <div class="no-photo rounded">
                                        <i class="fas fa-user"></i>
                                    </div>
                                <?php endif; ?>
                                <h4 class="mt-3"><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></h4>
                                <p class="text-muted">ছাত্র/ছাত্রী আইডি: <?php echo htmlspecialchars($student['student_id']); ?></p>
                            </div>

                            <!-- Student Information -->
                            <div class="col-md-8">
                                <!-- Basic Information -->
                                <div class="info-card">
                                    <h6><i class="fas fa-user"></i> মৌলিক তথ্য</h6>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <p><strong>পিতার নাম:</strong> <?php echo htmlspecialchars($student['father_name'] ?: '-'); ?></p>
                                            <p><strong>মাতার নাম:</strong> <?php echo htmlspecialchars($student['mother_name'] ?: '-'); ?></p>
                                            <p><strong>জন্ম তারিখ:</strong> <?php echo $student['date_of_birth'] ? date('d-m-Y', strtotime($student['date_of_birth'])) : '-'; ?></p>
                                            <p><strong>ধর্ম:</strong> 
                                                <?php 
                                                switch($student['religion']) {
                                                    case 'islam': echo 'ইসলাম'; break;
                                                    case 'hinduism': echo 'হিন্দু'; break;
                                                    case 'buddhism': echo 'বৌদ্ধ'; break;
                                                    case 'christianity': echo 'খ্রিস্টান'; break;
                                                    case 'other': echo 'অন্যান্য'; break;
                                                    default: echo '-';
                                                }
                                                ?>
                                            </p>
                                        </div>
                                        <div class="col-md-6">
                                            <p><strong>লিঙ্গ:</strong> 
                                                <?php 
                                                switch($student['gender']) {
                                                    case 'male': echo 'পুরুষ'; break;
                                                    case 'female': echo 'মহিলা'; break;
                                                    case 'other': echo 'অন্যান্য'; break;
                                                    default: echo '-';
                                                }
                                                ?>
                                            </p>
                                            <p><strong>রক্তের গ্রুপ:</strong> <?php echo htmlspecialchars($student['blood_group'] ?: '-'); ?></p>
                                            <p><strong>উপবৃত্তি:</strong> <?php echo ($student['scholarship'] == 'yes') ? 'হ্যাঁ' : 'না'; ?></p>
                                            <p><strong>অবস্থা:</strong> 
                                                <?php 
                                                switch($student['status']) {
                                                    case 'active': echo 'সক্রিয়'; break;
                                                    case 'inactive': echo 'নিষ্ক্রিয়'; break;
                                                    case 'graduated': echo 'স্নাতক'; break;
                                                    case 'transferred': echo 'স্থানান্তরিত'; break;
                                                    default: echo '-';
                                                }
                                                ?>
                                            </p>
                                        </div>
                                    </div>
                                </div>

                                <!-- Contact Information -->
                                <div class="info-card">
                                    <h6><i class="fas fa-phone"></i> যোগাযোগের তথ্য</h6>
                                    <div class="row">
                                        <div class="col-md-12">
                                            <table class="table table-bordered">
                                                <tr>
                                                    <td><strong>ইমেইল:</strong> <?php echo htmlspecialchars($student['email'] ?: '-'); ?></td>
                                                    <td><strong>ফোন:</strong> <?php echo htmlspecialchars($student['phone'] ?: '-'); ?></td>
                                                    <td><strong>অভিভাবকের নাম:</strong> <?php echo htmlspecialchars($student['guardian_name'] ?: '-'); ?></td>
                                                    <td><strong>অভিভাবকের ফোন:</strong> <?php echo htmlspecialchars($student['guardian_phone'] ?: '-'); ?></td>
                                                </tr>
                                                <tr>
                                                    <td><strong>অভিভাবকের ইমেইল:</strong> <?php echo htmlspecialchars($student['guardian_email'] ?: '-'); ?></td>
                                                    <td colspan="3"><strong>ঠিকানা:</strong> <?php echo htmlspecialchars($student['address'] ?: '-'); ?></td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                </div>

                                <!-- Academic Information -->
                                <div class="info-card">
                                    <h6><i class="fas fa-graduation-cap"></i> একাডেমিক তথ্য</h6>
                                    <div class="row">
                                        <div class="col-md-12">
                                            <table class="table table-bordered">
                                                <tr>
                                                    <td><strong>ক্লাস:</strong> <?php echo htmlspecialchars($student['class_name'] . ' - ' . $student['class_section']); ?></td>
                                                    <td><strong>সেশন:</strong> <?php echo htmlspecialchars($student['session']); ?></td>
                                                    <td><strong>রোল নম্বর:</strong> <?php echo htmlspecialchars($student['roll_number']); ?></td>
                                                    <td><strong>গ্রুপ:</strong> <?php echo htmlspecialchars($student['group_name'] ?: '-'); ?></td>
                                                </tr>
                                                <tr>
                                                    <td><strong>ভর্তি তারিখ:</strong> <?php echo $student['admission_date'] ? date('d-m-Y', strtotime($student['admission_date'])) : '-'; ?></td>
                                                    <?php if (!empty($student['previous_class'])): ?>
                                                        <td><strong>পূর্ববর্তী শ্রেণি:</strong> <?php echo htmlspecialchars($student['previous_class']); ?></td>
                                                    <?php else: ?>
                                                        <td><strong>পূর্ববর্তী শ্রেণি:</strong> -</td>
                                                    <?php endif; ?>
                                                    <?php if (!empty($student['previous_school'])): ?>
                                                        <td><strong>পূর্ববর্তী স্কুল:</strong> <?php echo htmlspecialchars($student['previous_school']); ?></td>
                                                    <?php else: ?>
                                                        <td><strong>পূর্ববর্তী স্কুল:</strong> -</td>
                                                    <?php endif; ?>
                                                    <td>
                                                        <strong>বিষয়সমূহ:</strong>
                                                        <?php 
                                                        if (!empty($subjects)) {
                                                            $subject_list = [];
                                                            foreach ($subjects as $subject) {
                                                                $subject_list[] = htmlspecialchars($subject['name'] . ' (' . $subject['code'] . ')');
                                                            }
                                                            echo implode(', ', $subject_list);
                                                        } else {
                                                            echo '-';
                                                        }
                                                        ?>
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="text-center mt-4">
                            <a href="my_students.php" class="btn btn-secondary">
                                <i class="fas fa-arrow-left"></i> ছাত্র-ছাত্রী তালিকায় ফিরুন
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

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