<?php require_once __DIR__.'/../includes/db.php'; require_once __DIR__.'/../includes/auth.php'; include __DIR__.'/../includes/header.php';
$act=$_GET['a']??'list';
if($act==='save' && $_SERVER['REQUEST_METHOD']==='POST'){ $id=intval($_POST['id']??0);
  $stmt=$mysqli->prepare($id>0? "UPDATE pengumuman SET title=?,content=?,start_date=?,end_date=?,status=? WHERE id=?"
                              : "INSERT INTO pengumuman(title,content,start_date,end_date,status) VALUES (?,?,?,?,?)");
  if($id>0){ $stmt->bind_param('sssssi', $_POST['title'],$_POST['content'],$_POST['start_date'],$_POST['end_date'],$_POST['status'],$id); }
  else { $stmt->bind_param('sssss', $_POST['title'],$_POST['content'],$_POST['start_date'],$_POST['end_date'],$_POST['status']); }
  $stmt->execute(); header('Location: pengumuman.php'); exit;
}
if($act==='del'){ $id=intval($_GET['id']??0); if($id>0) $mysqli->query("DELETE FROM pengumuman WHERE id=$id"); header('Location: pengumuman.php'); exit; }
$row=null; if($act==='edit'){ $id=intval($_GET['id']??0); $row=$mysqli->query("SELECT * FROM pengumuman WHERE id=$id")->fetch_assoc(); } ?>
<div class="card" style="padding:18px"><div style="display:flex;justify-content:space-between;align-items:center"><h2>Pengumuman</h2><a class="btn primary" href="pengumuman.php?a=form">+ Tambah</a></div>
<?php if($act==='list'): $res=$mysqli->query("SELECT * FROM pengumuman ORDER BY id DESC"); ?>
<table class="table"><thead><tr><th>ID</th><th>Judul</th><th>Mulai</th><th>Selesai</th><th>Status</th><th>Aksi</th></tr></thead><tbody>
<?php while($r=$res->fetch_assoc()): ?><tr><td><?php echo $r['id']; ?></td><td><?php echo esc($r['title']); ?></td><td><?php echo esc($r['start_date']); ?></td><td><?php echo esc($r['end_date']); ?></td><td><?php echo esc($r['status']); ?></td>
<td><a class="btn small" href="pengumuman.php?a=edit&id=<?php echo $r['id']; ?>">Edit</a> <a class="btn small" href="pengumuman.php?a=del&id=<?php echo $r['id']; ?>" onclick="return confirmDelete()">Hapus</a></td></tr><?php endwhile; ?>
</tbody></table>
<?php else: ?>
<form class="grid" method="post" action="pengumuman.php?a=save"><input type="hidden" name="id" value="<?php echo $row['id']??0; ?>">
<div><label>Judul</label><input class="input" name="title" value="<?php echo esc($row['title']??''); ?>" required></div>
<div style="grid-column:1/-1"><label>Isi</label><textarea class="input" name="content"><?php echo esc($row['content']??''); ?></textarea></div>
<div><label>Tanggal Mulai</label><input class="input" type="date" name="start_date" value="<?php echo esc($row['start_date']??''); ?>"></div>
<div><label>Tanggal Selesai</label><input class="input" type="date" name="end_date" value="<?php echo esc($row['end_date']??''); ?>"></div>
<div><label>Status</label><input class="input" name="status" value="<?php echo esc($row['status']??'aktif'); ?>"></div>
<div style="grid-column:1/-1"><button class="btn primary">Simpan</button> <a class="btn" href="pengumuman.php">Batal</a></div></form>
<?php endif; ?></div><?php include __DIR__.'/../includes/footer.php'; ?>