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

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

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

// Get user info based on role
$user_info = null;
if ($user_role == 'student') {
    $stmt = $pdo->prepare("SELECT * FROM students WHERE user_id = ?");
    $stmt->execute([$user_id]);
    $user_info = $stmt->fetch();
} elseif ($user_role == 'teacher') {
    $stmt = $pdo->prepare("SELECT * FROM teachers WHERE user_id = ?");
    $stmt->execute([$user_id]);
    $user_info = $stmt->fetch();
}

// Get user's uploaded files
$stmt = $pdo->prepare("
    SELECT fu.*, 
           CASE 
               WHEN fu.uploader_type = 'student' THEN CONCAT(s.first_name, ' ', s.last_name)
               WHEN fu.uploader_type = 'teacher' THEN CONCAT(t.first_name, ' ', t.last_name)
               ELSE 'Admin'
           END as uploader_name,
           c.name as class_name,
           sub.name as subject_name
    FROM file_uploads fu
    LEFT JOIN students s ON fu.uploader_type = 'student' AND fu.uploader_id = s.id
    LEFT JOIN teachers t ON fu.uploader_type = 'teacher' AND fu.uploader_id = t.id
    LEFT JOIN classes c ON fu.target_class_id = c.id
    LEFT JOIN subjects sub ON fu.target_subject_id = sub.id
    WHERE fu.uploader_type = ? AND fu.uploader_id = ? AND fu.status = 'active'
    ORDER BY fu.created_at DESC
");

$uploader_id = $user_info ? $user_info['id'] : 0;
$stmt->execute([$user_role, $uploader_id]);
$my_files = $stmt->fetchAll();

// Get public files if student
$public_files = [];
if ($user_role == 'student' && $user_info) {
    $stmt = $pdo->prepare("
        SELECT fu.*, 
               CONCAT(t.first_name, ' ', t.last_name) as uploader_name,
               c.name as class_name,
               sub.name as subject_name
        FROM file_uploads fu
        JOIN teachers t ON fu.uploader_id = t.id
        LEFT JOIN classes c ON fu.target_class_id = c.id
        LEFT JOIN subjects sub ON fu.target_subject_id = sub.id
        WHERE fu.uploader_type = 'teacher' 
        AND fu.is_public = 1 
        AND fu.status = 'active'
        AND (fu.target_class_id IS NULL OR fu.target_class_id = ?)
        ORDER BY fu.created_at DESC
        LIMIT 20
    ");
    $stmt->execute([$user_info['class_id']]);
    $public_files = $stmt->fetchAll();
}
?>

<!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/style.css" rel="stylesheet">
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="../index.php">
                <i class="fas fa-graduation-cap"></i> স্কুল ম্যানেজমেন্ট সিস্টেম
            </a>
            
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="../index.php">
                            <i class="fas fa-home"></i> হোম
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="index.php">
                            <i class="fas fa-cloud-upload-alt"></i> ফাইল আপলোড
                        </a>
                    </li>
                </ul>
                
                <ul class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button" data-bs-toggle="dropdown">
                            <i class="fas fa-user"></i> <?php echo $_SESSION['user_name']; ?>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="../logout.php">লগআউট</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container-fluid mt-4">
        <div class="row">
            <!-- Sidebar -->
            <div class="col-md-3">
                <div class="card">
                    <div class="card-header">
                        <h5><i class="fas fa-folder"></i> ফাইল ব্যবস্থাপনা</h5>
                    </div>
                    <div class="card-body">
                        <div class="d-grid gap-2">
                            <a href="upload.php" class="btn btn-success">
                                <i class="fas fa-plus"></i> নতুন ফাইল আপলোড
                            </a>
                            
                            <?php if ($user_role == 'teacher'): ?>
                            <a href="assignments.php" class="btn btn-info">
                                <i class="fas fa-tasks"></i> অ্যাসাইনমেন্ট
                            </a>
                            <?php endif; ?>
                            
                            <a href="my_files.php" class="btn btn-outline-primary">
                                <i class="fas fa-file"></i> আমার ফাইল
                            </a>
                            
                            <?php if ($user_role == 'student'): ?>
                            <a href="assignments_student.php" class="btn btn-outline-warning">
                                <i class="fas fa-clipboard-list"></i> অ্যাসাইনমেন্ট
                            </a>
                            <?php endif; ?>
                        </div>
                        
                        <hr>
                        
                        <div class="text-center">
                            <h6>আপলোড সীমা</h6>
                            <p class="text-muted small">
                                সর্বোচ্চ ফাইল সাইজ: <strong>10MB</strong><br>
                                সমর্থিত ফরম্যাট: PDF, DOC, DOCX, JPG, PNG, MP4
                            </p>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Main Content -->
            <div class="col-md-9">
                <!-- Stats Cards -->
                <div class="row mb-4">
                    <div class="col-md-4">
                        <div class="card bg-primary text-white">
                            <div class="card-body">
                                <div class="d-flex justify-content-between">
                                    <div>
                                        <h4><?php echo count($my_files); ?></h4>
                                        <p class="mb-0">আমার ফাইল</p>
                                    </div>
                                    <div class="align-self-center">
                                        <i class="fas fa-file fa-2x"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-md-4">
                        <div class="card bg-success text-white">
                            <div class="card-body">
                                <div class="d-flex justify-content-between">
                                    <div>
                                        <?php
                                        $total_size = array_sum(array_column($my_files, 'file_size'));
                                        $size_mb = round($total_size / (1024 * 1024), 2);
                                        ?>
                                        <h4><?php echo $size_mb; ?>MB</h4>
                                        <p class="mb-0">ব্যবহৃত স্থান</p>
                                    </div>
                                    <div class="align-self-center">
                                        <i class="fas fa-hdd fa-2x"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-md-4">
                        <div class="card bg-info text-white">
                            <div class="card-body">
                                <div class="d-flex justify-content-between">
                                    <div>
                                        <h4><?php echo count($public_files); ?></h4>
                                        <p class="mb-0">পাবলিক ফাইল</p>
                                    </div>
                                    <div class="align-self-center">
                                        <i class="fas fa-globe fa-2x"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Recent Files -->
                <div class="card">
                    <div class="card-header">
                        <h5><i class="fas fa-clock"></i> সাম্প্রতিক ফাইলসমূহ</h5>
                    </div>
                    <div class="card-body">
                        <?php if (empty($my_files)): ?>
                            <div class="text-center py-5">
                                <i class="fas fa-folder-open fa-3x text-muted mb-3"></i>
                                <h5 class="text-muted">কোন ফাইল আপলোড করা হয়নি</h5>
                                <p class="text-muted">আপনার প্রথম ফাইল আপলোড করুন</p>
                                <a href="upload.php" class="btn btn-primary">
                                    <i class="fas fa-plus"></i> ফাইল আপলোড করুন
                                </a>
                            </div>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th>ফাইল নাম</th>
                                            <th>ধরন</th>
                                            <th>সাইজ</th>
                                            <th>আপলোড তারিখ</th>
                                            <th>কার্যক্রম</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach (array_slice($my_files, 0, 10) as $file): ?>
                                            <tr>
                                                <td>
                                                    <i class="fas fa-file text-primary me-2"></i>
                                                    <?php echo htmlspecialchars($file['original_name']); ?>
                                                    <?php if ($file['description']): ?>
                                                        <br><small class="text-muted"><?php echo htmlspecialchars($file['description']); ?></small>
                                                    <?php endif; ?>
                                                </td>
                                                <td>
                                                    <span class="badge bg-secondary"><?php echo strtoupper($file['category']); ?></span>
                                                </td>
                                                <td><?php echo round($file['file_size'] / 1024, 2); ?> KB</td>
                                                <td><?php echo date('d/m/Y H:i', strtotime($file['created_at'])); ?></td>
                                                <td>
                                                    <div class="btn-group btn-group-sm">
                                                        <a href="download.php?id=<?php echo $file['id']; ?>" class="btn btn-outline-primary">
                                                            <i class="fas fa-download"></i>
                                                        </a>
                                                        <a href="view.php?id=<?php echo $file['id']; ?>" class="btn btn-outline-info">
                                                            <i class="fas fa-eye"></i>
                                                        </a>
                                                        <a href="delete.php?id=<?php echo $file['id']; ?>" class="btn btn-outline-danger" 
                                                           onclick="return confirm('এই ফাইলটি মুছে ফেলতে চান?')">
                                                            <i class="fas fa-trash"></i>
                                                        </a>
                                                    </div>
                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                            
                            <?php if (count($my_files) > 10): ?>
                                <div class="text-center">
                                    <a href="my_files.php" class="btn btn-outline-primary">
                                        <i class="fas fa-list"></i> সব ফাইল দেখুন
                                    </a>
                                </div>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Public Files for Students -->
                <?php if ($user_role == 'student' && !empty($public_files)): ?>
                <div class="card mt-4">
                    <div class="card-header">
                        <h5><i class="fas fa-globe"></i> শিক্ষকদের পাবলিক ফাইল</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>ফাইল নাম</th>
                                        <th>শিক্ষক</th>
                                        <th>বিষয়</th>
                                        <th>আপলোড তারিখ</th>
                                        <th>কার্যক্রম</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($public_files as $file): ?>
                                        <tr>
                                            <td>
                                                <i class="fas fa-file text-success me-2"></i>
                                                <?php echo htmlspecialchars($file['original_name']); ?>
                                                <?php if ($file['description']): ?>
                                                    <br><small class="text-muted"><?php echo htmlspecialchars($file['description']); ?></small>
                                                <?php endif; ?>
                                            </td>
                                            <td><?php echo htmlspecialchars($file['uploader_name']); ?></td>
                                            <td>
                                                <?php if ($file['subject_name']): ?>
                                                    <span class="badge bg-info"><?php echo $file['subject_name']; ?></span>
                                                <?php else: ?>
                                                    <span class="text-muted">সাধারণ</span>
                                                <?php endif; ?>
                                            </td>
                                            <td><?php echo date('d/m/Y', strtotime($file['created_at'])); ?></td>
                                            <td>
                                                <div class="btn-group btn-group-sm">
                                                    <a href="download.php?id=<?php echo $file['id']; ?>" class="btn btn-outline-success">
                                                        <i class="fas fa-download"></i> ডাউনলোড
                                                    </a>
                                                    <a href="view.php?id=<?php echo $file['id']; ?>" class="btn btn-outline-info">
                                                        <i class="fas fa-eye"></i> দেখুন
                                                    </a>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

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