<?php
session_start();
require_once '../config/database.php';
require_once '../includes/access_control.php';
require_once '../includes/site_settings.php';

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

// Get site settings
$settings = get_all_settings($pdo);
$site_name = $settings['site_name'] ?? 'প্রতিষ্ঠানের নাম';
$site_address = $settings['site_address'] ?? 'ঠিকানা, ফোন: ০১৭xxxxxxxx';
$logo_image = $settings['logo_image'] ?? '';
$logo_size = isset($settings['logo_size']) ? intval($settings['logo_size']) : 100;
$logo_opacity = isset($settings['logo_opacity']) ? floatval($settings['logo_opacity']) : 0.9;

// Get exam ID from URL
$exam_id = isset($_GET['exam_id']) ? (int)$_GET['exam_id'] : 0;

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

// Get the exam details
$stmt = $pdo->prepare("
    SELECT e.*, c.name as class_name, c.section as class_section, s.name as subject_name
    FROM exams e
    JOIN classes c ON e.class_id = c.id
    JOIN exam_subjects es ON e.id = es.exam_id
    JOIN subjects s ON es.subject_id = s.id
    WHERE e.id = ?
");
$stmt->execute([$exam_id]);
$exam = $stmt->fetch();

if (!$exam) {
    header('Location: create_question_paper.php');
    exit();
}

// Fetch questions for this exam
$stmt = $pdo->prepare("SELECT * FROM exam_questions WHERE exam_id = ? ORDER BY question_order ASC, id ASC");
$stmt->execute([$exam_id]);
$questions = $stmt->fetchAll();

// Group questions by type
$mcq_questions = [];
$short_questions = [];
$long_questions = [];

foreach ($questions as $question) {
    switch ($question['question_type']) {
        case 'mcq':
            $mcq_questions[] = $question;
            break;
        case 'short':
            $short_questions[] = $question;
            break;
        case 'long':
            $long_questions[] = $question;
            break;
    }
}

// Calculate total marks for each section
$mcq_total_marks = 0;
$short_total_marks = 0;
$long_total_marks = 0;

foreach ($mcq_questions as $question) {
    $mcq_total_marks += $question['marks'];
}

foreach ($short_questions as $question) {
    $short_total_marks += $question['marks'];
}

foreach ($long_questions as $question) {
    $long_total_marks += $question['marks'];
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($exam['name']); ?> - প্রশ্নপত্র</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">
    <style>
        @media print {
            .no-print {
                display: none !important;
            }
            
            body {
                font-size: 14px;
                line-height: 1.4;
            }
            
            .question-paper {
                width: 100%;
                max-width: 100%;
                margin: 0;
                padding: 0;
            }
        }
        
        body {
            font-family: 'Nikosh', 'SolaimanLipi', sans-serif;
            background-color: #f8f9fa;
        }
        
        .question-paper {
            background: white;
            max-width: 800px;
            margin: 20px auto;
            padding: 30px;
            border: 1px solid #ddd;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        
        .header-section {
            text-align: center;
            margin-bottom: 30px;
            border-bottom: 2px solid #000;
            padding-bottom: 15px;
        }
        
        .board-logo {
            font-weight: bold;
            font-size: 1.2rem;
            color: #d32f2f;
        }
        
        .board-name {
            font-size: 1.4rem;
            font-weight: bold;
            margin: 5px 0;
        }
        
        .exam-title {
            font-size: 1.3rem;
            font-weight: bold;
            margin: 10px 0;
        }
        
        .exam-details {
            display: flex;
            justify-content: space-between;
            margin: 15px 0;
            font-size: 0.9rem;
        }
        
        .exam-details div {
            flex: 1;
        }
        
        .question-section {
            margin: 20px 0;
        }
        
        .section-header {
            font-weight: bold;
            margin: 15px 0 10px 0;
            padding-bottom: 5px;
            border-bottom: 1px solid #ccc;
        }
        
        .question-item {
            margin-bottom: 15px;
            line-height: 1.6;
        }
        
        .question-text {
            font-weight: 500;
        }
        
        .mcq-options {
            margin-top: 8px;
            padding-left: 20px;
        }
        
        .option-item {
            margin-bottom: 3px;
        }
        
        .marks-indicator {
            float: right;
            font-weight: bold;
        }
        
        .signature-section {
            margin-top: 40px;
            display: flex;
            justify-content: space-between;
        }
        
        .signature-box {
            text-align: center;
            flex: 1;
        }
        
        .signature-line {
            margin-top: 40px;
            border-top: 1px solid #000;
            padding-top: 5px;
        }
        
        .print-button {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 1000;
        }
    </style>
</head>
<body>
    <div class="print-button no-print">
        <button class="btn btn-primary" onclick="window.print()">
            <i class="fas fa-print me-2"></i>প্রিন্ট করুন
        </button>
        <button class="btn btn-secondary" onclick="window.close()">
            <i class="fas fa-times me-2"></i>বন্ধ করুন
        </button>
    </div>
    
    <div class="question-paper">
        <!-- Header Section -->
        <div class="header-section">
            <?php if ($logo_image): ?>
            <div class="text-center mb-3">
                <img src="../<?php echo htmlspecialchars($logo_image); ?>" alt="Logo" class="img-fluid" style="max-height: <?php echo $logo_size; ?>px; opacity: <?php echo $logo_opacity; ?>; max-width: 200px; object-fit: contain;">
            </div>
            <?php endif; ?>
            <div class="board-logo"><?php echo htmlspecialchars($site_name); ?></div>
            <div class="board-name"><?php echo htmlspecialchars($site_address); ?></div>
            <div class="exam-title"><?php echo htmlspecialchars($exam['name']); ?></div>
            
            <div class="exam-details">
                <div><strong>বিষয়:</strong> <?php echo htmlspecialchars($exam['subject_name']); ?></div>
                <div><strong>ক্লাস:</strong> <?php echo htmlspecialchars($exam['class_name'] . ' - ' . $exam['class_section']); ?></div>
                <div><strong>সময়: ৩ ঘন্টা</strong></div>
                <div><strong>পূর্ণমান: <?php echo $exam['total_marks']; ?></strong></div>
            </div>
        </div>
        
        <div style="text-align: center; margin-bottom: 20px;">
            <strong>[ প্রত্যেক বিভাগের সকল প্রশ্নের উত্তর দিতে হবে ]</strong>
        </div>
        
        <!-- MCQ Section -->
        <?php if (!empty($mcq_questions)): ?>
            <div class="question-section">
                <div class="section-header">
                    ক বিভাগ (MCQ) <?php if ($mcq_total_marks > 0): ?><span class="marks-indicator">পূর্ণমান: <?php echo $mcq_total_marks; ?></span><?php endif; ?>
                </div>
                
                <?php foreach ($mcq_questions as $index => $question): ?>
                    <div class="question-item">
                        <div class="question-text">
                            <?php echo ($question['question_order'] ?? ($index + 1)) . '. ' . htmlspecialchars($question['question_text']); ?>
                            <?php if ($question['marks'] > 0): ?><span class="marks-indicator">[<?php echo $question['marks']; ?>]</span><?php endif; ?>
                        </div>
                        
                        <div class="mcq-options">
                            <div class="option-item">ক) <?php echo htmlspecialchars($question['option_a']); ?></div>
                            <div class="option-item">খ) <?php echo htmlspecialchars($question['option_b']); ?></div>
                            <div class="option-item">গ) <?php echo htmlspecialchars($question['option_c']); ?></div>
                            <div class="option-item">ঘ) <?php echo htmlspecialchars($question['option_d']); ?></div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
        
        <!-- Short Questions Section -->
        <?php if (!empty($short_questions)): ?>
            <div class="question-section">
                <div class="section-header">
                    খ বিভাগ (সংক্ষিপ্ত প্রশ্ন) <?php if ($short_total_marks > 0): ?><span class="marks-indicator">পূর্ণমান: <?php echo $short_total_marks; ?></span><?php endif; ?>
                </div>
                
                <?php foreach ($short_questions as $index => $question): ?>
                    <div class="question-item">
                        <div class="question-text">
                            <?php echo ($question['question_order'] ?? ($index + 1 + count($mcq_questions))) . '. ' . htmlspecialchars($question['question_text']); ?>
                            <?php if ($question['marks'] > 0): ?><span class="marks-indicator">[<?php echo $question['marks']; ?>]</span><?php endif; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
        
        <!-- Long Questions Section -->
        <?php if (!empty($long_questions)): ?>
            <div class="question-section">
                <div class="section-header">
                    গ বিভাগ (বিস্তারিত প্রশ্ন) <?php if ($long_total_marks > 0): ?><span class="marks-indicator">পূর্ণমান: <?php echo $long_total_marks; ?></span><?php endif; ?>
                </div>
                
                <?php foreach ($long_questions as $index => $question): ?>
                    <div class="question-item">
                        <div class="question-text">
                            <?php echo ($question['question_order'] ?? ($index + 1 + count($mcq_questions) + count($short_questions))) . '. ' . htmlspecialchars($question['question_text']); ?>
                            <?php if ($question['marks'] > 0): ?><span class="marks-indicator">[<?php echo $question['marks']; ?>]</span><?php endif; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
        
        <!-- Signature Section -->
        <div class="signature-section">
            <div class="signature-box">
                <div class="signature-line">পরীক্ষকের স্বাক্ষর</div>
            </div>
            <div class="signature-box">
                <div class="signature-line">প্রধান শিক্ষকের স্বাক্ষর</div>
            </div>
        </div>
    </div>

    <script>
        // Auto-print when page loads (optional)
        // window.onload = function() {
        //     window.print();
        // };
    </script>
</body>
</html>