<?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='img_'.time().'_'.rand(1000,9999).'.'.$ext; move_uploaded_file($_FILES['image']['tmp_name'], __DIR__.'/../uploads/'.$imgname); }
  if($id>0){
    $stmt=$mysqli->prepare("UPDATE lembaga SET name=?,leader=?,description=?,contact=?"+($imgname?" , image=?":"")+" WHERE id=?");
  } else {
    $stmt=$mysqli->prepare("INSERT INTO lembaga(name,leader,description,contact,image) VALUES (?,?,?,?,?)");
  }
  if($id>0){
    if($imgname){ $stmt->bind_param('sssssi', $_POST['name'],$_POST['leader'],$_POST['description'],$_POST['contact'],$imgname,$id); }
    else { $stmt=$mysqli->prepare("UPDATE lembaga SET name=?,leader=?,description=?,contact=? WHERE id=?"); $stmt->bind_param('ssssi', $_POST['name'],$_POST['leader'],$_POST['description'],$_POST['contact'],$id); }
  } else {
    $stmt->bind_param('sssss', $_POST['name'],$_POST['leader'],$_POST['description'],$_POST['contact'],$imgname);
  }
  $stmt->execute(); header('Location: lembaga.php'); exit;
}
if($act==='del'){ $id=intval($_GET['id']??0); if($id>0){ $o=$mysqli->query("SELECT image FROM lembaga WHERE id=$id")->fetch_assoc(); if($o && !empty($o['image'])) @unlink(__DIR__.'/../uploads/'.$o['image']); $mysqli->query("DELETE FROM lembaga WHERE id=$id"); } header('Location: lembaga.php'); exit; }
$row=null; if($act==='edit'){ $id=intval($_GET['id']??0); $row=$mysqli->query("SELECT * FROM lembaga WHERE id=$id")->fetch_assoc(); } ?>
<div class="card" style="padding:18px">
  <div style="display:flex;justify-content:space-between;align-items:center"><h2>Lembaga Desa</h2><a class="btn primary" href="lembaga.php?a=form">+ Tambah</a></div>
  <?php if($act==='list'): $res=$mysqli->query("SELECT * FROM lembaga ORDER BY id DESC"); ?>
    <table class="table"><thead><tr><th>ID</th><th>Nama</th><th>Ketua</th><th>Kontak</th><th>Gambar</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['name']); ?></td><td><?php echo esc($r['leader']); ?></td><td><?php echo esc($r['contact']); ?></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="lembaga.php?a=edit&id=<?php echo $r['id']; ?>">Edit</a> <a class="btn small" href="lembaga.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="lembaga.php?a=save">
      <input type="hidden" name="id" value="<?php echo $row['id']??0; ?>">
      <div><label>Nama Lembaga</label><input class="input" name="name" value="<?php echo esc($row['name']??''); ?>" required></div>
      <div><label>Ketua</label><input class="input" name="leader" value="<?php echo esc($row['leader']??''); ?>"></div>
      <div><label>Kontak</label><input class="input" name="contact" value="<?php echo esc($row['contact']??''); ?>"></div>
      <div style="grid-column:1/-1"><label>Deskripsi</label><textarea class="input" name="description"><?php echo esc($row['description']??''); ?></textarea></div>
      <div><label>Gambar</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="lembaga.php">Batal</a></div>
    </form>
  <?php endif; ?>
</div><?php include __DIR__.'/../includes/footer.php'; ?>