<?php
/**
 * Parent Portal - Student Subjects
 */

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/../config/database.php';

// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Check if user is logged in as parent
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'parent') {
    header('Location: ../parent_login.php');
    exit;
}

// Get parent information
$stmt = $pdo->prepare("SELECT * FROM parents WHERE user_id = ?");
$stmt->execute([$_SESSION['user_id']]);
$parent = $stmt->fetch();

if (!$parent) {
    header('Location: ../parent_register.php');
    exit;
}

// Get associated students
try {
    $stmt = $pdo->prepare("
        SELECT s.*, c.name as class_name
        FROM students s
        LEFT JOIN classes c ON s.class_id = c.id
        WHERE s.id IN (
            SELECT student_id FROM parent_students WHERE parent_id = ?
        ) AND s.status = 'active'
    ");
    $stmt->execute([$parent['id']]);
    $students = $stmt->fetchAll();
} catch (Exception $e) {
    $students = [];
}

// Get selected student
$selected_student_id = $_GET['student_id'] ?? ($students[0]['id'] ?? null);

// Get subjects for selected student
$subjects = [];
if ($selected_student_id) {
    try {
        $stmt = $pdo->prepare("
            SELECT sub.*, t.first_name as teacher_first_name, t.last_name as teacher_last_name
            FROM subjects sub
            LEFT JOIN teachers t ON sub.teacher_id = t.id
            WHERE sub.class_id = (
                SELECT class_id FROM students WHERE id = ?
            )
            ORDER BY sub.name
        ");
        $stmt->execute([$selected_student_id]);
        $subjects = $stmt->fetchAll();
    } catch (Exception $e) {
        $subjects = [];
    }
}
?>

<!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 rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .parent-sidebar {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: white;
        }
        .parent-sidebar .nav-link {
            color: rgba(255,255,255,0.8);
            padding: 15px 20px;
            border-radius: 8px;
            margin: 5px 0;
            text-decoration: none;
            display: block;
        }
        .parent-sidebar .nav-link:hover, .parent-sidebar .nav-link.active {
            background: rgba(255,255,255,0.2);
            color: white;
        }
        .subject-card {
            border-left: 4px solid #17a2b8;
            transition: transform 0.2s;
        }
        .subject-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .subject-icon {
            font-size: 2.5rem;
            color: #17a2b8;
        }
    </style>
    <link href="../assets/css/style.css" rel="stylesheet">
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Parent Sidebar -->
            <nav class="col-md-2 d-none d-md-block parent-sidebar p-3">
                <div class="text-center mb-4">
                    <div class="bg-white rounded-circle d-inline-flex align-items-center justify-content-center mb-3" style="width:80px;height:80px;">
                        <i class="fas fa-user-tie fa-2x text-primary"></i>
                    </div>
                    <h6 class="mb-0"><?= htmlspecialchars($parent['father_name']) ?></h6>
                    <small>অভিভাবক</small>
                </div>
                
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link" href="index.php">
                            <i class="fas fa-home me-2"></i> ড্যাশবোর্ড
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="results.php">
                            <i class="fas fa-chart-line me-2"></i> ফলাফল
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="subjects.php">
                            <i class="fas fa-book me-2"></i> বিষয়সমূহ
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="fees.php">
                            <i class="fas fa-money-bill-wave me-2"></i> ফি পেমেন্ট
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="events.php">
                            <i class="fas fa-calendar-alt me-2"></i> ইভেন্ট
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="attendance.php">
                            <i class="fas fa-clipboard-check me-2"></i> উপস্থিতি
                        </a>
                    </li>
                    <li class="nav-item mt-4">
                        <a class="nav-link text-warning" href="../parent_logout.php">
                            <i class="fas fa-sign-out-alt me-2"></i> লগআউট
                        </a>
                    </li>
                </ul>
            </nav>
            
            <!-- Main Content -->
            <main class="col-md-10 ms-sm-auto px-md-4 py-4">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2 class="h3"><i class="fas fa-book text-info"></i> শিক্ষার্থীর বিষয়সমূহ</h2>
                    <div class="text-muted">
                        <?= date('l, d F Y', strtotime('now')) ?>
                    </div>
                </div>
                
                <?php if (count($students) > 0): ?>
                <!-- Student Selection -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0"><i class="fas fa-filter"></i> শিক্ষার্থী নির্বাচন করুন</h5>
                    </div>
                    <div class="card-body">
                        <form method="GET">
                            <div class="row">
                                <div class="col-md-6">
                                    <select name="student_id" class="form-select" onchange="this.form.submit()">
                                        <?php foreach ($students as $student): ?>
                                            <option value="<?= $student['id'] ?>" <?= $selected_student_id == $student['id'] ? 'selected' : '' ?>>
                                                <?= htmlspecialchars($student['first_name'] . ' ' . $student['last_name']) ?> - 
                                                <?= htmlspecialchars($student['class_name'] ?? 'N/A') ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                
                <?php if ($selected_student_id && count($subjects) > 0): ?>
                <!-- Subjects Grid -->
                <div class="row">
                    <?php foreach ($subjects as $subject): ?>
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card subject-card h-100">
                            <div class="card-body">
                                <div class="d-flex align-items-center mb-3">
                                    <div class="subject-icon me-3">
                                        <i class="fas fa-<?= getSubjectIcon($subject['name']) ?>"></i>
                                    </div>
                                    <div>
                                        <h5 class="card-title mb-1"><?= htmlspecialchars($subject['name']) ?></h5>
                                        <p class="text-muted mb-0 small">কোড: <?= htmlspecialchars($subject['code']) ?></p>
                                    </div>
                                </div>
                                
                                <div class="mb-3">
                                    <p class="card-text">
                                        <strong>পূর্ণ নম্বর:</strong> <?= $subject['total_marks'] ?? 'N/A' ?><br>
                                        <strong>পাস মার্ক:</strong> <?= $subject['pass_marks'] ?? 'N/A' ?>
                                    </p>
                                </div>
                                
                                <?php if (!empty($subject['teacher_first_name'])): ?>
                                <div class="border-top pt-3">
                                    <p class="mb-1"><strong>শিক্ষক:</strong></p>
                                    <p class="text-muted mb-0">
                                        <?= htmlspecialchars($subject['teacher_first_name'] . ' ' . $subject['teacher_last_name']) ?>
                                    </p>
                                </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
                
                <!-- Summary -->
                <div class="card mt-4">
                    <div class="card-header">
                        <h5 class="mb-0"><i class="fas fa-info-circle"></i> বিষয় সংক্রান্ত তথ্য</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-4 text-center">
                                <h3 class="text-primary"><?= count($subjects) ?></h3>
                                <p class="text-muted">মোট বিষয়</p>
                            </div>
                            <div class="col-md-4 text-center">
                                <h3 class="text-success">
                                    <?= array_sum(array_column($subjects, 'total_marks')) ?>
                                </h3>
                                <p class="text-muted">মোট পূর্ণ নম্বর</p>
                            </div>
                            <div class="col-md-4 text-center">
                                <h3 class="text-info">
                                    <?= round(array_sum(array_column($subjects, 'pass_marks')) / count($subjects)) ?>
                                </h3>
                                <p class="text-muted">গড় পাস মার্ক</p>
                            </div>
                        </div>
                    </div>
                </div>
                <?php else: ?>
                <div class="alert alert-info">
                    <h5><i class="fas fa-info-circle"></i> বিষয় তথ্য পাওয়া যায়নি</h5>
                    <p>এই শিক্ষার্থীর জন্য কোনো বিষয় নির্ধারণ করা হয়নি বা তথ্য আপডেট করা হয়নি।</p>
                </div>
                <?php endif; ?>
                
                <?php else: ?>
                <div class="alert alert-info">
                    <h5><i class="fas fa-info-circle"></i> কোনো শিক্ষার্থী খুঁজে পাওয়া যায়নি</h5>
                    <p>সিস্টেমে আপনার শিক্ষার্থীর একাউন্ট অ্যাকটিভ করতে স্কুল অফিসে যোগাযোগ করুন।</p>
                </div>
                <?php endif; ?>
            </main>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

<?php
function getSubjectIcon($subject_name) {
    $subject_name = strtolower($subject_name);
    
    if (strpos($subject_name, 'math') !== false || strpos($subject_name, 'গণিত') !== false) {
        return 'calculator';
    } elseif (strpos($subject_name, 'english') !== false || strpos($subject_name, 'ইংরেজি') !== false) {
        return 'language';
    } elseif (strpos($subject_name, 'bangla') !== false || strpos($subject_name, 'বাংলা') !== false) {
        return 'book';
    } elseif (strpos($subject_name, 'science') !== false || strpos($subject_name, 'বিজ্ঞান') !== false) {
        return 'flask';
    } elseif (strpos($subject_name, 'history') !== false || strpos($subject_name, 'ইতিহাস') !== false) {
        return 'landmark';
    } elseif (strpos($subject_name, 'geography') !== false || strpos($subject_name, 'ভূগোল') !== false) {
        return 'globe-americas';
    } else {
        return 'book';
    }
}
?>