<?php
require_once __DIR__ . '/../library/auth.php'; 
$school_id = $_SESSION['school_id'];
allow_roles(['admin', 'teacher', 'accountant']);
// Handle Delete
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $stmt = $pdo->prepare("DELETE FROM notifications WHERE id = ?");
    $stmt->execute([$id]);
    header("Location: list.php?msg=Notice deleted successfully");
    exit;
}

// Handle Status Toggle
if (isset($_GET['toggle'])) {
    $id = (int)$_GET['toggle'];
    $stmt = $pdo->prepare("UPDATE notifications SET is_active = NOT is_active WHERE id = ?");
    $stmt->execute([$id]);
    header("Location: list.php");
    exit;
}

$page_title = 'All Notices';
include __DIR__ . '/../include/header.php';
include __DIR__ . '/../include/sidebar.php';
?>

<div class="content-wrapper">
    <section class="content-header">
        <h1>All Notifications</h1>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-12">
                <?php if (isset($_GET['msg'])): ?>
                <div class="alert alert-success alert-dismissible">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <?php echo htmlspecialchars($_GET['msg']); ?>
                </div>
                <?php endif; ?>

                <div class="card">
                    <div class="card-header bg-gradient-primary">
    <div class="row">
        <div class="col-sm-6">
            <h3 class="card-title text-white mb-0">Manage Notices (<?php 
                echo $pdo->query("SELECT COUNT(*) FROM notifications")->fetchColumn(); 
            ?>)</h3>
        </div>
        <div class="col-sm-6 text-right">
            <a href="add_notice.php" class="btn btn-light btn-sm">
                <i class="fas fa-plus"></i> Add New Notice
            </a>
        </div>
    </div>
</div>
                    
                    <div class="card-body">
                        <div class="table-responsive">
                            <table id="notices-table" class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>S.No.</th>
                                        <th>Title</th>
                                        <th>Target</th>
                                        <th>Class</th>
                                        <th>Status</th>
                                        <th>Date</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $stmt = $pdo->query("
                                        SELECT n.*, c.class_name 
                                        FROM notifications n 
                                        LEFT JOIN classes c ON n.class_id = c.id 
                                        ORDER BY n.created_at DESC
                                    ");
                                    while ($row = $stmt->fetch()): 
                                    ?>
                                    <tr>
                                        <td><strong>#<?php echo $row['id']; ?></strong></td>
                                        <td>
                                            <strong><?php echo htmlspecialchars($row['title']); ?></strong>
                                            <br><small><?php echo substr($row['message'], 0, 50); ?>...</small>
                                        </td>
                                        <td>
                                            <span class="badge badge-<?php 
                                                echo $row['for_role'] == 'all' ? 'primary' : 
                                                    ($row['for_role'] == 'student' ? 'info' : 
                                                    ($row['for_role'] == 'teacher' ? 'success' : 'warning'));
                                            ?>">
                                                <?php echo ucwords(str_replace('_', ' ', $row['for_role'])); ?>
                                            </span>
                                        </td>
                                        <td><?php echo htmlspecialchars($row['class_name'] ?? 'All Classes'); ?></td>
                                        <td>
                                            <span class="badge badge-<?php echo $row['is_active'] ? 'success' : 'secondary'; ?>">
                                                <?php echo $row['is_active'] ? 'Active' : 'Inactive'; ?>
                                            </span>
                                        </td>
                                        <td><?php echo date('d/m/Y H:i', strtotime($row['created_at'])); ?></td>
                                        <td>
                                            <div class="btn-group btn-group-sm" role="group">
                                                <a href="edit_notice.php?id=<?php echo $row['id']; ?>" 
                                                   class="btn btn-warning" title="Edit">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <a href="?toggle=<?php echo $row['id']; ?>" 
                                                   class="btn btn-<?php echo $row['is_active'] ? 'secondary' : 'success'; ?>" 
                                                   title="<?php echo $row['is_active'] ? 'Deactivate' : 'Activate'; ?>">
                                                    <i class="fas fa-<?php echo $row['is_active'] ? 'toggle-off' : 'toggle-on'; ?>"></i>
                                                </a>
                                                <button onclick="confirmDelete(<?php echo $row['id']; ?>)" 
                                                        class="btn btn-danger" title="Delete">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

<script>
function confirmDelete(id) {
    if (confirm('Are you sure? This cannot be undone!')) {
        window.location = 'list.php?delete=' + id;
    }
}
</script>

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