<?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);
  $imgname=null; if(isset($_FILES['image']) && $_FILES['image']['error']===0){ $ext=pathinfo($_FILES['image']['name'],PATHINFO_EXTENSION); $imgname='cover_'.time().'_'.rand(1000,9999).'.'.$ext; move_uploaded_file($_FILES['image']['tmp_name'], __DIR__.'/../uploads/'.$imgname); }
  $slug=trim($_POST['slug']); if($slug===''){ $slug=slugify($_POST['title']); }
  if($id>0){
    if($imgname){ $stmt=$mysqli->prepare("UPDATE berita SET title=?, slug=?, content=?, image=?, published_at=?, status=? WHERE id=?"); $stmt->bind_param('ssssssi', $_POST['title'],$slug,$_POST['content'],$imgname,$_POST['published_at'],$_POST['status'],$id); }
    else { $stmt=$mysqli->prepare("UPDATE berita SET title=?, slug=?, content=?, published_at=?, status=? WHERE id=?"); $stmt->bind_param('sssssi', $_POST['title'],$slug,$_POST['content'],$_POST['published_at'],$_POST['status'],$id); }
  } else {
    $stmt=$mysqli->prepare("INSERT INTO berita(title,slug,content,image,published_at,status) VALUES (?,?,?,?,?,?)"); $stmt->bind_param('ssssss', $_POST['title'],$slug,$_POST['content'],$imgname,$_POST['published_at'],$_POST['status']);
  }
  $stmt->execute(); header('Location: berita.php'); exit;
}
if($act==='del'){ $id=intval($_GET['id']??0); if($id>0){ $o=$mysqli->query("SELECT image FROM berita WHERE id=$id")->fetch_assoc(); if($o && !empty($o['image'])) @unlink(__DIR__.'/../uploads/'.$o['image']); $mysqli->query("DELETE FROM berita WHERE id=$id"); } header('Location: berita.php'); exit; }
$row=null; if($act==='edit'){ $id=intval($_GET['id']??0); $row=$mysqli->query("SELECT * FROM berita WHERE id=$id")->fetch_assoc(); } ?>
<div class="card" style="padding:18px"><div style="display:flex;justify-content:space-between;align-items:center"><h2>Berita</h2><a class="btn primary" href="berita.php?a=form">+ Tambah</a></div>
<?php if($act==='list'): $res=$mysqli->query("SELECT * FROM berita ORDER BY id DESC"); ?>
<table class="table"><thead><tr><th>ID</th><th>Judul</th><th>Slug</th><th>Terbit</th><th>Status</th><th>Cover</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['slug']); ?></td><td><?php echo esc($r['published_at']); ?></td><td><?php echo esc($r['status']); ?></td>
<td><?php if(!empty($r['image'])) echo '<img src="../uploads/'.esc($r['image']).'" style="width:70px;border-radius:6px">'; ?></td>
<td><a class="btn small" href="berita.php?a=edit&id=<?php echo $r['id']; ?>">Edit</a> <a class="btn small" href="berita.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" enctype="multipart/form-data" action="berita.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><label>Slug</label><input class="input" name="slug" value="<?php echo esc($row['slug']??''); ?>"></div>
<div><label>Tanggal Terbit</label><input class="input" type="date" name="published_at" value="<?php echo esc($row['published_at']??''); ?>"></div>
<div><label>Status</label><input class="input" name="status" value="<?php echo esc($row['status']??'draft'); ?>"></div>
<div style="grid-column:1/-1"><label>Isi</label><textarea class="input" name="content"><?php echo esc($row['content']??''); ?></textarea></div>
<div><label>Gambar Sampul</label><input class="input" type="file" name="image"></div>
<div style="grid-column:1/-1"><button class="btn primary">Simpan</button> <a class="btn" href="berita.php">Batal</a></div></form>
<?php endif; ?></div><?php include __DIR__.'/../includes/footer.php'; ?>