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

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: ../login.php');
    exit();
}

$user_role = $_SESSION['user_role'];
$user_name = $_SESSION['user_name'];

// Check if assignment ID is provided
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header('Location: assignments.php');
    exit();
}

$assignment_id = $_GET['id'];

// Fetch assignment details
try {
    $stmt = $pdo->prepare("
        SELECT a.*, c.name as class_name, c.section as class_section, s.name as subject_name 
        FROM assignments a
        JOIN classes c ON a.class_id = c.id
        JOIN subjects s ON a.subject_id = s.id
        WHERE a.id = ?
    ");
    $stmt->execute([$assignment_id]);
    $assignment = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$assignment) {
        header('Location: assignments.php');
        exit();
    }
    
    // Fetch student's submission for this assignment
    $stmt = $pdo->prepare("
        SELECT asub.*, s.first_name, s.last_name, s.roll_number
        FROM assignment_submissions asub
        JOIN students s ON asub.student_id = s.id
        WHERE asub.assignment_id = ? AND asub.student_id = (SELECT id FROM students WHERE user_id = ?)
    ");
    $stmt->execute([$assignment_id, $_SESSION['user_id']]);
    $submission = $stmt->fetch(PDO::FETCH_ASSOC);
    
} catch (Exception $e) {
    error_log("Error fetching assignment results: " . $e->getMessage());
    $error_message = "Error loading results: " . $e->getMessage();
}
?>

<!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">
    <link href="../assets/css/sidebar.css" rel="stylesheet">
    <style>
        .result-card {
            border-radius: 15px;
            overflow: hidden;
            transition: all 0.3s ease;
            margin-bottom: 20px;
            border: 1px solid #e0e0e0;
        }
        
        .result-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }
        
        .result-header {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            padding: 20px;
        }
        
        .result-body {
            padding: 20px;
        }
        
        .status-badge {
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: bold;
        }
        
        .status-pending {
            background-color: #fff3cd;
            color: #856404;
        }
        
        .status-submitted {
            background-color: #d4edda;
            color: #155724;
        }
        
        .status-graded {
            background-color: #d1ecf1;
            color: #0c5460;
        }
    </style>
    <link href="../assets/css/style.css" rel="stylesheet">
</head>
<body>
    <div class="sidebar-container">
        <?php echo renderSidebar('online_learning/assignments.php', $user_role, $user_name); ?>
        
        <div class="main-content">
            <!-- Content Header -->
            <div class="content-header">
                <h1 class="content-title">
                    <i class="fas fa-chart-bar me-2"></i>
                    অ্যাসাইনমেন্ট ফলাফল
                </h1>
                <p class="content-breadcrumb">
                    <i class="fas fa-home me-1"></i>
                    হোম / অনলাইন শিক্ষা / অ্যাসাইনমেন্ট / ফলাফল
                </p>
            </div>
            
            <!-- Content Body -->
            <div class="content-body">
                <?php if (isset($error_message)): ?>
                    <div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
                <?php endif; ?>
                
                <div class="row">
                    <div class="col-12">
                        <div class="card result-card">
                            <div class="result-header">
                                <h2><?php echo htmlspecialchars($assignment['title']); ?></h2>
                                <p><?php echo htmlspecialchars($assignment['subject_name']); ?> - <?php echo htmlspecialchars($assignment['class_name'] . ' - ' . $assignment['class_section']); ?></p>
                                <p>জমা দেওয়ার শেষ তারিখ: <?php echo date('d F, Y h:i A', strtotime($assignment['due_date'])); ?></p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header bg-primary text-white">
                                <h5 class="mb-0">
                                    <i class="fas fa-file-alt me-2"></i>
                                    আপনার জমা
                                </h5>
                            </div>
                            <div class="card-body">
                                <?php if ($submission): ?>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <p><strong>জমা দেওয়ার তারিখ:</strong> <?php echo date('d F, Y h:i A', strtotime($submission['created_at'])); ?></p>
                                            <p><strong>অবস্থা:</strong>
                                                <?php 
                                                $status_text = '';
                                                $status_class = '';
                                                switch ($submission['status']) {
                                                    case 'submitted':
                                                        $status_text = 'জমা দেওয়া হয়েছে';
                                                        $status_class = 'status-submitted';
                                                        break;
                                                    case 'graded':
                                                        $status_text = 'মূল্যায়ন করা হয়েছে';
                                                        $status_class = 'status-graded';
                                                        break;
                                                    default:
                                                        $status_text = 'চলছে';
                                                        $status_class = 'status-pending';
                                                }
                                                ?>
                                                <span class="status-badge <?php echo $status_class; ?>"><?php echo $status_text; ?></span>
                                            </p>
                                        </div>
                                        <div class="col-md-6">
                                            <?php if ($submission['file_upload_id']): ?>
                                                <p><strong>জমা দেওয়া ফাইল:</strong>
                                                    <a href="../uploads/download.php?id=<?php echo $submission['file_upload_id']; ?>" class="btn btn-sm btn-outline-primary">
                                                        <i class="fas fa-download"></i> ডাউনলোড
                                                    </a>
                                                </p>
                                            <?php endif; ?>
                                            <?php if ($submission['grade']): ?>
                                                <p><strong>প্রাপ্ত নম্বর:</strong> <?php echo htmlspecialchars($submission['grade']); ?> / <?php echo htmlspecialchars($assignment['max_marks']); ?></p>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                    <?php if ($submission['feedback']): ?>
                                        <div class="mt-3">
                                            <h6>প্রতিক্রিয়া:</h6>
                                            <p><?php echo htmlspecialchars($submission['feedback']); ?></p>
                                        </div>
                                    <?php elseif ($submission['status'] === 'graded' && !$submission['feedback']): ?>
                                        <div class="mt-3">
                                            <h6>প্রতিক্রিয়া:</h6>
                                            <p class="text-muted">প্রতিক্রিয়া পাওয়া যায়নি।</p>
                                        </div>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <div class="text-center py-5">
                                        <i class="fas fa-file-alt fa-3x mb-3 text-muted"></i>
                                        <h4>আপনি এখনও এই অ্যাসাইনমেন্টটি জমা দেননি</h4>
                                        <p class="text-muted">অ্যাসাইনমেন্ট জমা দেওয়ার শেষ তারিখের আগে জমা দিন।</p>
                                        <a href="assignments.php" class="btn btn-primary">অ্যাসাইনমেন্ট পেজে ফিরে যান</a>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="mt-3">
                    <a href="assignments.php" class="btn btn-secondary">
                        <i class="fas fa-arrow-left me-2"></i>
                        অ্যাসাইনমেন্ট তালিকায় ফিরে যান
                    </a>
                </div>
            </div>
        </div>
    </div>

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