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

$teacher_id = $_SESSION['teacher_id'];

// Delete Logic
if (isset($_GET['delete_id'])) {
    $del_id = $_GET['delete_id'];
    $pdo->prepare("DELETE FROM assignments WHERE id = ? AND teacher_id = ?")->execute([$del_id, $teacher_id]);
    header("Location: assignments.php?msg=deleted");
    exit;
}

// Fetch Assignments with Course Title
$stmt = $pdo->prepare("SELECT a.*, c.title as course_name FROM assignments a 
                       JOIN courses c ON a.course_id = c.id 
                       WHERE a.teacher_id = ? ORDER BY a.deadline ASC");
$stmt->execute([$teacher_id]);
$assignments = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<style>
    :root { --theme-green: #064e3b; --theme-dark: #022c22; --theme-gold: #d4af37; --bg-soft: #f8fafc; }
    .main-content { background-color: var(--bg-soft); min-height: 100vh; padding: 30px; transition: 0.4s; }
    .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; border: none; }
    .btn-theme:hover { background: var(--theme-dark); color: var(--theme-gold); transform: translateY(-2px); }
    
    .deadline-badge { font-size: 11px; padding: 5px 12px; border-radius: 50px; font-weight: 800; background: #fef2f2; color: #ef4444; border: 1px solid #fee2e2; }
    
    #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: 991px) {
        .main-content { padding: 15px !important; margin-left: 0; }
        .dashboard-header { flex-direction: column; align-items: flex-start !important; gap: 15px; padding: 15px 20px; }
        .dashboard-header .d-flex { width: 100%; align-items: center; }
        .btn-theme { width: 100%; justify-content: center; }
    }
</style>

<div class="main-content">
    <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-tasks me-2" style="color: var(--theme-green);"></i> Assignments</h4>
        </div>
        <a href="add_assignment.php" class="btn-theme shadow-sm"><i class="fas fa-plus-circle me-2"></i> Create New</a>
    </div>

    <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">Assignment Title</th>
                        <th>Course</th>
                        <th>Deadline</th>
                        <th class="text-end pe-4">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($assignments)): foreach($assignments as $row): ?>
                    <tr>
                        <td class="ps-4 py-3">
                            <div class="fw-bold text-dark"><?= htmlspecialchars($row['title']) ?></div>
                            <small class="text-muted d-block mt-1"><?= substr(htmlspecialchars($row['description']), 0, 50) ?>...</small>
                        </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><span class="deadline-badge"><i class="far fa-clock me-1"></i> <?= date('d M, h:i A', strtotime($row['deadline'])) ?></span></td>
                        <td class="text-end pe-4">
                            <a href="edit_assignment.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-light border me-1"><i class="fas fa-pen text-warning"></i></a>
                            <a href="?delete_id=<?= $row['id'] ?>" class="btn btn-sm btn-light border" onclick="return confirm('Delete this assignment?')"><i class="fas fa-trash-alt text-danger"></i></a>
                        </td>
                    </tr>
                    <?php endforeach; else: ?>
                    <tr><td colspan="4" class="text-center py-5 fw-bold text-muted">No assignments created yet.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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