<?php
if (isset($_POST['add_notification'])) {
    $message = trim($_POST['message']);
    $target = $_POST['target'];

    $stmt = $connection->prepare("INSERT INTO notifications (message, target) VALUES (?, ?)");
    $stmt->bind_param("ss", $message, $target);
    $stmt->execute();

    $_SESSION['msg'] = "Notification added successfully.";
    $_SESSION['type'] = "success";
    header("Location: admin_manage_notifications");
    exit();
}

if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $connection->query("DELETE FROM notifications WHERE id = $id");
    $_SESSION['msg'] = "Notification deleted.";
    $_SESSION['type'] = "success";
    header("Location: admin_manage_notifications");
    exit();
}

if (isset($_GET['toggle'])) {
    $id = (int)$_GET['toggle'];
    $getStatus = $connection->query("SELECT is_active FROM notifications WHERE id = $id");
    if ($getStatus && $getStatus->num_rows > 0) {
        $current = $getStatus->fetch_assoc()['is_active'];
        $newStatus = ($current == 1) ? 0 : 1;
        $connection->query("UPDATE notifications SET is_active = $newStatus WHERE id = $id");
        $_SESSION['msg'] = "Notification status updated.";
        $_SESSION['type'] = "success";
        header("Location: admin_manage_notifications");
    exit();
    }
        
}


$notifs = $connection->query("SELECT * FROM notifications ORDER BY id DESC");
?>
<style>
  td.truncate-tooltip {
    max-width: 300px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
  }
</style>

<div class="page-title">
  <div>
    <h1><i class="fa fa-bullhorn"></i> Notification Management</h1>
    <p>Create and manage portal notifications</p>
  </div>
</div>

<div class="card">
  <div class="table-scrol">
    <div class="panel panel-primary ch">
      <div class="panel-heading">
        <h4 class="text-center">Add New Notification</h4>
      </div>
      <div class="panel-body">
        <form method="POST" class="mb-4">
          <div class="form-group">
            <label for="message">Notification Message</label>
            <textarea name="message" id="message" class="form-control" rows="3" placeholder="Enter notification..." required></textarea>
          </div>

          <div class="form-group">
            <label for="target">Target Audience</label>
            <select name="target" id="target" class="form-control">
              <option value="all">All</option>
              <option value="staff">Staff Only</option>
              <option value="student">Student Only</option>
            </select>
          </div>

          <button type="submit" name="add_notification" class="btn btn-primary">Add Notification</button>
        </form>

        <hr>

        <h4 class="text-center">All Notifications</h4>
        <div class="table-responsive">
          <table class="table table-bordered table-striped" id="myTable">
            <thead>
              <tr>
              	<th>S/N</th>
                <th>Message</th>
                <th>Target</th>
                <th>Status</th>
                <th>Actions</th>
              </tr>
            </thead>
            <tbody>
		  <?php 
			$i = 1; 
			while ($row = $notifs->fetch_assoc()): 
		  ?>
			<tr>
			  <td><?= $i++ ?></td>
			  <td class="truncate-tooltip" title="<?= htmlspecialchars($row['message']) ?>">
  <?= htmlspecialchars($row['message']) ?>
</td>

			  <td><?= ucfirst($row['target']) ?></td>
			  <td>
				<?php if ($row['is_active']): ?>
				  <span class="badge badge-success">Active</span>
				<?php else: ?>
				  <span class="badge badge-secondary">Inactive</span>
				<?php endif; ?>
			  </td>
			  <td>
				<a href="?toggle=<?= $row['id'] ?>" class="btn btn-warning btn-sm">
				  <?= $row['is_active'] ? 'Deactivate' : 'Activate' ?>
				</a>
				<a href="?delete=<?= $row['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Delete this notification?')">
				  Delete
				</a>
			  </td>
			</tr>
		  <?php endwhile; ?>
		</tbody>

          </table>
        </div>

      </div>
    </div>
  </div>
</div>

