<?php 
include 'includes/header.php'; 
include 'includes/sidebar.php'; 

$teacher_id = $_SESSION['teacher_id'];

// ডিলিট লজিক
if (isset($_GET['delete_id'])) {
    $del_id = $_GET['delete_id'];
    $stmt = $pdo->prepare("SELECT file_path FROM course_materials WHERE id = ? AND teacher_id = ?");
    $stmt->execute([$del_id, $teacher_id]);
    $file = $stmt->fetch();
    
    if ($file) {
        if (file_exists('../' . $file['file_path'])) { unlink('../' . $file['file_path']); }
        $pdo->prepare("DELETE FROM course_materials WHERE id = ?")->execute([$del_id]);
        echo "<script>window.location.href='manage_materials.php?msg=deleted';</script>";
    }
}

// ফাইল লিস্ট সংগ্রহ
$stmt = $pdo->prepare("SELECT m.*, c.title as course_name FROM course_materials m 
                       JOIN courses c ON m.course_id = c.id 
                       WHERE m.teacher_id = ? ORDER BY m.created_at DESC");
$stmt->execute([$teacher_id]);
$materials = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<style>
    :root { --theme-green: #064e3b; --theme-dark: #022c22; --theme-gold: #d4af37; --bg-soft: #f8fafc; }
    .dashboard-header { background: #fff; padding: 20px 30px; border-radius: 15px; box-shadow: 0 5px 20px rgba(0,0,0,0.03); margin-bottom: 25px; border-left: 6px solid var(--theme-green); }
    .custom-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 18px; box-shadow: 0 10px 40px rgba(0,0,0,0.03); overflow: hidden; border-top: 5px solid var(--theme-green); }
    .btn-theme { background: var(--theme-green); color: #fff; border-radius: 50px; padding: 10px 25px; font-weight: 700; text-decoration: none; display: inline-flex; align-items: center; transition: 0.3s; }
    .btn-theme:hover { background: var(--theme-dark); color: var(--theme-gold); transform: translateY(-2px); }
    
    .file-icon { width: 40px; height: 40px; border-radius: 10px; display: flex; align-items: center; justify-content: center; background: rgba(6, 78, 59, 0.05); color: var(--theme-green); font-size: 18px; }
    
    #sidebarToggle { width: 42px !important; height: 42px !important; padding: 0; display: inline-flex !important; align-items: center; justify-content: center; flex-shrink: 0; border-radius: 8px; border: 1px solid #e2e8f0; background: #fff; }

    @media (max-width: 768px) {
        .main-content { padding: 15px !important; }
        .dashboard-header { flex-direction: column; align-items: flex-start !important; gap: 15px; padding: 15px 20px; }
        .dashboard-header .btn-theme { width: 100%; justify-content: center; }
    }
</style>

<div class="main-content p-4">
    <div class="dashboard-header d-flex justify-content-between align-items-center fade-up">
        <div class="d-flex align-items-center">
            <button class="btn btn-light border d-lg-none me-3 shadow-sm" id="sidebarToggle"><i class="fas fa-bars text-dark"></i></button>
            <h4 class="mb-0 fw-bold text-dark"><i class="fas fa-file-pdf me-2" style="color: var(--theme-green);"></i> Course Materials</h4>
        </div>
        <a href="add_material.php" class="btn-theme shadow-sm"><i class="fas fa-plus-circle me-2"></i> Upload New File</a>
    </div>

    <?php if(isset($_GET['msg']) && $_GET['msg'] == 'success'): ?>
        <div class="alert alert-success fw-bold border-0 shadow-sm mb-4 bg-white text-success fade-up">
            <i class="fas fa-check-circle me-2"></i> Material uploaded successfully!
        </div>
    <?php endif; ?>

    <div class="custom-card fade-up" style="animation-delay: 0.1s;">
        <div class="table-responsive">
            <table class="table align-middle border-0 mb-0">
                <thead>
                    <tr style="background: var(--bg-soft);">
                        <th class="ps-4">File Name</th>
                        <th>Course</th>
                        <th>Date</th>
                        <th class="text-end pe-4">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($materials)): foreach($materials as $row): ?>
                    <tr>
                        <td class="ps-4 py-3">
                            <div class="d-flex align-items-center">
                                <div class="file-icon me-3"><i class="fas fa-file-alt"></i></div>
                                <div>
                                    <div class="fw-bold text-dark"><?= htmlspecialchars($row['title']) ?></div>
                                    <small class="text-muted"><?= strtoupper(pathinfo($row['file_path'], PATHINFO_EXTENSION)) ?> File</small>
                                </div>
                            </div>
                        </td>
                        <td>
                            <span class="badge bg-light text-muted border px-3 py-2 rounded-pill fw-bold" style="font-size: 11px;">
                                <i class="fas fa-book me-1" style="color: var(--theme-gold);"></i> <?= htmlspecialchars($row['course_name']) ?>
                            </span>
                        </td>
                        <td><div class="small text-muted fw-bold"><?= date('d M, Y', strtotime($row['created_at'])) ?></div></td>
                        <td class="text-end pe-4">
                            <a href="../<?= $row['file_path'] ?>" target="_blank" class="btn btn-sm btn-light border text-primary me-2"><i class="fas fa-eye"></i></a>
                            <a href="?delete_id=<?= $row['id'] ?>" class="btn btn-sm btn-light border text-danger" onclick="return confirm('Delete this file?')"><i class="fas fa-trash-alt"></i></a>
                        </td>
                    </tr>
                    <?php endforeach; else: ?>
                    <tr><td colspan="4" class="text-center py-5 fw-bold text-muted">No materials uploaded yet.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?>