<?php
require_once __DIR__ . '/../library/auth.php'; 
$school_id = $_SESSION['school_id'];
allow_roles(['admin', 'teacher', 'accountant']);

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header("Location: list.php");
    exit;
}

$notice_id = (int)$_GET['id'];
$classes = [];

// Fetch Notice
$stmt = $pdo->prepare("SELECT * FROM notifications WHERE id = ?");
$stmt->execute([$notice_id]);
$notice = $stmt->fetch();

if (!$notice) {
    header("Location: list.php?msg=Notice not found");
    exit;
}

if ($_POST) {
    $title = trim($_POST['title']);
    $message_text = trim($_POST['message']);
    $for_role = $_POST['for_role'];
    $class_id = $_POST['class_id'] ?? null;
    $is_active = isset($_POST['is_active']) ? 1 : 0;
    
    if (empty($title) || empty($message_text)) {
        $error = '<div class="alert alert-danger">Title and Message required!</div>';
    } else {
        $stmt = $pdo->prepare("UPDATE notifications SET title = ?, message = ?, for_role = ?, class_id = ?, is_active = ? WHERE id = ?");
        $stmt->execute([$title, $message_text, $for_role, $class_id, $is_active, $notice_id]);
        
        header("Location: list.php?msg=Notice updated successfully");
        exit;
    }
}

// Load Classes
$stmt = $pdo->query("SELECT id, class_name FROM classes ORDER BY id");
$classes = $stmt->fetchAll();

$page_title = 'Edit Notice';
include __DIR__ . '/../include/header.php';
include __DIR__ . '/../include/sidebar.php';
?>

<div class="content-wrapper">
    <section class="content-header">
        <h1>Edit Notice #<?php echo $notice['id']; ?></h1>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-8">
                <div class="card card-warning">
                    <div class="card-header">
                        <h3 class="card-title">Update Notice: <?php echo htmlspecialchars($notice['title']); ?></h3>
                    </div>
                    
                    <form method="POST">
                        <?php if (isset($error)) echo $error; ?>
                        
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-8">
                                    <div class="form-group">
                                        <label>Title <span class="text-danger">*</span></label>
                                        <input type="text" name="title" class="form-control" required maxlength="255"
                                               value="<?php echo htmlspecialchars($notice['title']); ?>">
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label>Target Audience <span class="text-danger">*</span></label>
                                        <select name="for_role" class="form-control" required>
                                            <option value="">Select Audience</option>
                                            <option value="all" <?php echo $notice['for_role'] == 'all' ? 'selected' : ''; ?>>All</option>
                                            <option value="student" <?php echo $notice['for_role'] == 'student' ? 'selected' : ''; ?>>Students</option>
                                            <option value="teacher" <?php echo $notice['for_role'] == 'teacher' ? 'selected' : ''; ?>>Teachers</option>
                                            <option value="parent" <?php echo $notice['for_role'] == 'parent' ? 'selected' : ''; ?>>Parents</option>
                                            <option value="admin" <?php echo $notice['for_role'] == 'admin' ? 'selected' : ''; ?>>Admin</option>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label>Message <span class="text-danger">*</span></label>
                                <textarea name="message" class="form-control" rows="6" required><?php echo htmlspecialchars($notice['message']); ?></textarea>
                            </div>

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>Class</label>
                                        <select name="class_id" class="form-control">
                                            <option value="">All Classes</option>
                                            <?php foreach ($classes as $class): ?>
                                            <option value="<?php echo $class['id']; ?>" 
                                                    <?php echo $notice['class_id'] == $class['id'] ? 'selected' : ''; ?>>
                                                <?php echo htmlspecialchars($class['class_name']); ?>
                                            </option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="form-check-label mb-2">
                                            <input type="checkbox" name="is_active" value="1" 
                                                   <?php echo $notice['is_active'] ? 'checked' : ''; ?> class="mr-2">
                                            Active / Visible on Dashboard
                                        </label>
                                        <small class="text-muted d-block">Uncheck to hide from dashboard</small>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="card-footer">
                            <button type="submit" class="btn btn-warning btn-lg">
                                <i class="fas fa-save"></i> Update Notice
                            </button>
                            <a href="list.php" class="btn btn-secondary btn-lg">
                                <i class="fas fa-arrow-left"></i> Back to List
                            </a>
                            <a href="add_notice.php" class="btn btn-primary btn-lg float-right">
                                <i class="fas fa-plus"></i> Add New
                            </a>
                        </div>
                    </form>
                </div>
            </div>

            <div class="col-md-4">
                <div class="card card-info">
                    <div class="card-header">
                        <h3 class="card-title">Notice Preview</h3>
                    </div>
                    <div class="card-body p-3 bg-light">
                        <div class="alert alert-info">
                            <h6 class="alert-heading"><?php echo htmlspecialchars($notice['title']); ?></h6>
                            <p class="mb-1"><?php echo nl2br(htmlspecialchars(substr($notice['message'], 0, 150))); ?>...</p>
                            <hr>
                            <small>
                                Target: <span class="badge badge-info"><?php echo ucwords(str_replace('_', ' ', $notice['for_role'])); ?></span><br>
                                <?php if ($notice['class_id']): ?>
                                Class: <span class="badge badge-primary"><?php echo htmlspecialchars($notice['class_name'] ?? 'Class ' . $notice['class_id']); ?></span>
                                <?php endif; ?>
                            </small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

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