<?php
require_once 'config.php';
requireLogin();

// Get user statistics
$stats = [];
if ($_SESSION['role'] === 'siswa') {
    $stmt = $pdo->prepare("SELECT COUNT(*) as total_tasks FROM tasks WHERE student_id = ?");
    $stmt->execute([$_SESSION['user_id']]);
    $stats['total_tasks'] = $stmt->fetchColumn();
    
    $stmt = $pdo->prepare("SELECT COUNT(*) as graded_tasks FROM tasks WHERE student_id = ? AND grade IS NOT NULL");
    $stmt->execute([$_SESSION['user_id']]);
    $stats['graded_tasks'] = $stmt->fetchColumn();
} elseif ($_SESSION['role'] === 'guru') {
    $stmt = $pdo->prepare("SELECT COUNT(*) as total_tasks FROM tasks t JOIN users u ON t.student_id = u.id WHERE t.subject = ?");
    $stmt->execute([$_SESSION['subject']]);
    $stats['total_tasks'] = $stmt->fetchColumn();
    
    $stmt = $pdo->prepare("SELECT COUNT(*) as pending_tasks FROM tasks t JOIN users u ON t.student_id = u.id WHERE t.subject = ? AND t.grade IS NULL");
    $stmt->execute([$_SESSION['subject']]);
    $stats['pending_tasks'] = $stmt->fetchColumn();
} else {
    $stmt = $pdo->prepare("SELECT COUNT(*) as total_users FROM users WHERE role != 'admin'");
    $stmt->execute();
    $stats['total_users'] = $stmt->fetchColumn();
    
    $stmt = $pdo->prepare("SELECT COUNT(*) as total_tasks FROM tasks");
    $stmt->execute();
    $stats['total_tasks'] = $stmt->fetchColumn();
}

// Handle success/error messages
$success = $_GET['success'] ?? '';
$error = $_GET['error'] ?? '';
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - Sistem Pengumpulan Tugas</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .card-hover { transition: all 0.3s ease; }
        .card-hover:hover { transform: translateY(-5px); box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1); }
    </style>
</head>
<body class="bg-gray-50 min-h-screen">
    <!-- Navigation -->
    <nav class="bg-white shadow-lg">
        <div class="max-w-7xl mx-auto px-4">
            <div class="flex justify-between items-center py-4">
                <div class="flex items-center">
                    <i class="fas fa-<?php echo $_SESSION['role'] === 'siswa' ? 'user-graduate' : ($_SESSION['role'] === 'guru' ? 'chalkboard-teacher' : 'user-shield'); ?> text-2xl text-blue-600 mr-3"></i>
                    <h1 class="text-xl font-bold text-gray-800">Dashboard <?php echo ucfirst($_SESSION['role']); ?></h1>
                </div>
                <div class="flex items-center space-x-4">
                    <span class="text-gray-600">Selamat datang, <span class="font-medium"><?php echo htmlspecialchars($_SESSION['name']); ?></span></span>
                    <?php if ($_SESSION['class']): ?>
                        <span class="bg-blue-100 text-blue-800 px-3 py-1 rounded-full text-sm"><?php echo $_SESSION['class']; ?></span>
                    <?php endif; ?>
                    <?php if ($_SESSION['subject']): ?>
                        <span class="bg-green-100 text-green-800 px-3 py-1 rounded-full text-sm"><?php echo $_SESSION['subject']; ?></span>
                    <?php endif; ?>
                    <a href="logout.php" class="bg-red-500 text-white px-4 py-2 rounded-lg hover:bg-red-600 transition">
                        <i class="fas fa-sign-out-alt mr-2"></i>Logout
                    </a>
                </div>
            </div>
        </div>
    </nav>

    <div class="max-w-7xl mx-auto p-6">
        <!-- Success/Error Messages -->
        <?php if ($success): ?>
            <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-6">
                <i class="fas fa-check-circle mr-2"></i>
                <?php if ($success === '1'): ?>Tugas berhasil diupload!<?php endif; ?>
            </div>
        <?php endif; ?>
        
        <?php if ($error): ?>
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-6">
                <i class="fas fa-exclamation-circle mr-2"></i>
                <?php if ($error === '1'): ?>Terjadi kesalahan saat upload tugas!<?php endif; ?>
            </div>
        <?php endif; ?>

        <!-- Statistics Cards -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
            <?php if ($_SESSION['role'] === 'siswa'): ?>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-center">
                        <div class="bg-blue-100 p-3 rounded-full">
                            <i class="fas fa-tasks text-2xl text-blue-600"></i>
                        </div>
                        <div class="ml-4">
                            <p class="text-gray-600 text-sm">Total Tugas</p>
                            <p class="text-2xl font-bold text-gray-800"><?php echo $stats['total_tasks']; ?></p>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-center">
                        <div class="bg-green-100 p-3 rounded-full">
                            <i class="fas fa-check-circle text-2xl text-green-600"></i>
                        </div>
                        <div class="ml-4">
                            <p class="text-gray-600 text-sm">Sudah Dinilai</p>
                            <p class="text-2xl font-bold text-gray-800"><?php echo $stats['graded_tasks']; ?></p>
                        </div>
                    </div>
                </div>
            <?php elseif ($_SESSION['role'] === 'guru'): ?>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-center">
                        <div class="bg-blue-100 p-3 rounded-full">
                            <i class="fas fa-tasks text-2xl text-blue-600"></i>
                        </div>
                        <div class="ml-4">
                            <p class="text-gray-600 text-sm">Total Tugas</p>
                            <p class="text-2xl font-bold text-gray-800"><?php echo $stats['total_tasks']; ?></p>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-center">
                        <div class="bg-yellow-100 p-3 rounded-full">
                            <i class="fas fa-clock text-2xl text-yellow-600"></i>
                        </div>
                        <div class="ml-4">
                            <p class="text-gray-600 text-sm">Belum Dinilai</p>
                            <p class="text-2xl font-bold text-gray-800"><?php echo $stats['pending_tasks']; ?></p>
                        </div>
                    </div>
                </div>
            <?php else: ?>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-center">
                        <div class="bg-blue-100 p-3 rounded-full">
                            <i class="fas fa-users text-2xl text-blue-600"></i>
                        </div>
                        <div class="ml-4">
                            <p class="text-gray-600 text-sm">Total Users</p>
                            <p class="text-2xl font-bold text-gray-800"><?php echo $stats['total_users']; ?></p>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="flex items-center">
                        <div class="bg-green-100 p-3 rounded-full">
                            <i class="fas fa-tasks text-2xl text-green-600"></i>
                        </div>
                        <div class="ml-4">
                            <p class="text-gray-600 text-sm">Total Tugas</p>
                            <p class="text-2xl font-bold text-gray-800"><?php echo $stats['total_tasks']; ?></p>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        </div>

        <?php if ($_SESSION['role'] === 'siswa'): ?>
            <!-- Student Dashboard Content -->
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
                <div class="lg:col-span-2 bg-white rounded-xl shadow-lg p-6">
                    <h2 class="text-xl font-bold text-gray-800 mb-6 flex items-center">
                        <i class="fas fa-upload text-blue-600 mr-3"></i>Upload Tugas
                    </h2>
                    <form action="upload_task.php" method="POST" enctype="multipart/form-data" class="space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-2">Mata Pelajaran</label>
                            <select name="subject" class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500" required>
                                <option value="">Pilih Mata Pelajaran</option>
                                <option value="Matematika">Matematika</option>
                                <option value="Bahasa Indonesia">Bahasa Indonesia</option>
                                <option value="Bahasa Inggris">Bahasa Inggris</option>
                                <option value="IPA">IPA</option>
                                <option value="IPS">IPS</option>
                                <option value="Fisika">Fisika</option>
                                <option value="Kimia">Kimia</option>
                                <option value="Biologi">Biologi</option>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-2">Judul Tugas</label>
                            <input type="text" name="title" class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500" placeholder="Masukkan judul tugas" required>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-2">File Tugas</label>
                            <input type="file" name="file" class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500" accept=".pdf,.doc,.docx,.jpg,.png,.zip,.rar">
                            <p class="text-sm text-gray-500 mt-1">Format: PDF, DOC, DOCX, JPG, PNG, ZIP, RAR (Max: 10MB)</p>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-2">Keterangan</label>
                            <textarea name="description" rows="3" class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500" placeholder="Keterangan tambahan (opsional)"></textarea>
                        </div>
                        <button type="submit" class="w-full bg-green-600 text-white py-3 rounded-lg hover:bg-green-700 transition font-medium">
                            <i class="fas fa-paper-plane mr-2"></i>Kirim Tugas
                        </button>
                    </form>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h3 class="text-lg font-bold text-gray-800 mb-4">Tugas Terbaru</h3>
                    <?php
                    $stmt = $pdo->prepare("SELECT * FROM tasks WHERE student_id = ? ORDER BY submit_date DESC LIMIT 5");
                    $stmt->execute([$_SESSION['user_id']]);
                    $recent_tasks = $stmt->fetchAll();
                    
                    if ($recent_tasks): ?>
                        <div class="space-y-3">
                            <?php foreach ($recent_tasks as $task): ?>
                                <div class="border-l-4 border-blue-500 pl-4 py-2">
                                    <h4 class="font-medium text-gray-800"><?php echo htmlspecialchars($task['title']); ?></h4>
                                    <p class="text-sm text-gray-600"><?php echo htmlspecialchars($task['subject']); ?></p>
                                    <p class="text-xs text-gray-500"><?php echo formatDate($task['submit_date']); ?></p>
                                    <?php if ($task['grade']): ?>
                                        <span class="inline-block bg-green-100 text-green-800 px-2 py-1 rounded text-xs mt-1">
                                            Nilai: <?php echo $task['grade']; ?>
                                        </span>
                                    <?php else: ?>
                                        <span class="inline-block bg-yellow-100 text-yellow-800 px-2 py-1 rounded text-xs mt-1">
                                            Belum dinilai
                                        </span>
                                    <?php endif; ?>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php else: ?>
                        <p class="text-gray-500 text-center py-4">Belum ada tugas yang diupload</p>
                    <?php endif; ?>
                </div>
            </div>
            
        <?php elseif ($_SESSION['role'] === 'guru'): ?>
            <!-- Teacher Dashboard Content -->
            <div class="bg-white rounded-xl shadow-lg p-6">
                <h2 class="text-xl font-bold text-gray-800 mb-6 flex items-center">
                    <i class="fas fa-chalkboard-teacher text-green-600 mr-3"></i>Tugas Siswa - <?php echo $_SESSION['subject']; ?>
                </h2>
                
                <?php
                $stmt = $pdo->prepare("
                    SELECT t.*, u.name as student_name, u.class as student_class 
                    FROM tasks t 
                    JOIN users u ON t.student_id = u.id 
                    WHERE t.subject = ? 
                    ORDER BY t.submit_date DESC
                ");
                $stmt->execute([$_SESSION['subject']]);
                $tasks = $stmt->fetchAll();
                
                if ($tasks): ?>
                    <div class="overflow-x-auto">
                        <table class="min-w-full table-auto">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Siswa</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Kelas</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Judul</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Tanggal</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Aksi</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                <?php foreach ($tasks as $task): ?>
                                    <tr class="hover:bg-gray-50">
                                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                            <?php echo htmlspecialchars($task['student_name']); ?>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            <?php echo htmlspecialchars($task['student_class']); ?>
                                        </td>
                                        <td class="px-6 py-4 text-sm text-gray-900">
                                            <?php echo htmlspecialchars($task['title']); ?>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            <?php echo formatDate($task['submit_date']); ?>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <?php if ($task['grade']): ?>
                                                <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-green-100 text-green-800">
                                                    Nilai: <?php echo $task['grade']; ?>
                                                </span>
                                            <?php else: ?>
                                                <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-yellow-100 text-yellow-800">
                                                    Belum dinilai
                                                </span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                            <?php if ($task['file_path']): ?>
                                                <a href="<?php echo htmlspecialchars($task['file_path']); ?>" target="_blank" class="text-blue-600 hover:text-blue-900 mr-3">
                                                    <i class="fas fa-download"></i> Download
                                                </a>
                                            <?php endif; ?>
                                            <button class="text-green-600 hover:text-green-900">
                                                <i class="fas fa-edit"></i> Nilai
                                            </button>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php else: ?>
                    <p class="text-gray-500 text-center py-8">Belum ada tugas yang masuk untuk mata pelajaran <?php echo $_SESSION['subject']; ?></p>
                <?php endif; ?>
            </div>
            
        <?php else: ?>
            <!-- Admin Dashboard Content -->
            <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h2 class="text-xl font-bold text-gray-800 mb-6 flex items-center">
                        <i class="fas fa-user-shield text-purple-600 mr-3"></i>Panel Admin
                    </h2>
                    <div class="space-y-4">
                        <a href="import_excel.php" class="block bg-blue-50 hover:bg-blue-100 p-4 rounded-lg transition">
                            <div class="flex items-center">
                                <i class="fas fa-file-excel text-2xl text-blue-600 mr-4"></i>
                                <div>
                                    <h3 class="font-medium text-gray-800">Import Data Excel</h3>
                                    <p class="text-sm text-gray-600">Import data siswa dan guru dari file Excel</p>
                                </div>
                            </div>
                        </a>
                        
                        <a href="user.php" class="block bg-green-50 hover:bg-green-100 p-4 rounded-lg transition">
                            <div class="flex items-center">
                                <i class="fas fa-users text-2xl text-green-600 mr-4"></i>
                                <div>
                                    <h3 class="font-medium text-gray-800">Kelola Users</h3>
                                    <p class="text-sm text-gray-600">Tambah, edit, dan hapus data pengguna</p>
                                </div>
                            </div>
                        </a>
                        
                        <a href="laporan.php" class="block bg-yellow-50 hover:bg-yellow-100 p-4 rounded-lg transition">
                            <div class="flex items-center">
                                <i class="fas fa-chart-bar text-2xl text-yellow-600 mr-4"></i>
                                <div>
                                    <h3 class="font-medium text-gray-800">Laporan</h3>
                                    <p class="text-sm text-gray-600">Lihat statistik dan laporan sistem</p>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6">
                    <h3 class="text-lg font-bold text-gray-800 mb-4">Aktivitas Terbaru</h3>
                    <?php
                    $stmt = $pdo->prepare("
                        SELECT t.*, u.name as student_name 
                        FROM tasks t 
                        JOIN users u ON t.student_id = u.id 
                        ORDER BY t.submit_date DESC 
                        LIMIT 10
                    ");
                    $stmt->execute();
                    $recent_activities = $stmt->fetchAll();
                    
                    if ($recent_activities): ?>
                        <div class="space-y-3">
                            <?php foreach ($recent_activities as $activity): ?>
                                <div class="border-l-4 border-purple-500 pl-4 py-2">
                                    <h4 class="font-medium text-gray-800"><?php echo htmlspecialchars($activity['student_name']); ?></h4>
                                    <p class="text-sm text-gray-600"><?php echo htmlspecialchars($activity['title']); ?> - <?php echo htmlspecialchars($activity['subject']); ?></p>
                                    <p class="text-xs text-gray-500"><?php echo formatDate($activity['submit_date']); ?></p>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php else: ?>
                        <p class="text-gray-500 text-center py-4">Belum ada aktivitas</p>
                    <?php endif; ?>
                </div>
            </div>
        <?php endif; ?>
    </div>
</body>
</html>