<?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 profil_desa SET title=?,content=?,address=?,phone=?,email=?,lat=?,lng=? WHERE id=?"
                              : "INSERT INTO profil_desa(title,content,address,phone,email,lat,lng) VALUES (?,?,?,?,?,?,?)");
  if($id>0){ $stmt->bind_param('sssssssi', $_POST['title'],$_POST['content'],$_POST['address'],$_POST['phone'],$_POST['email'],$_POST['lat'],$_POST['lng'],$id); }
  else { $stmt->bind_param('sssssss', $_POST['title'],$_POST['content'],$_POST['address'],$_POST['phone'],$_POST['email'],$_POST['lat'],$_POST['lng']); }
  $stmt->execute(); header('Location: profil.php'); exit;
}
if($act==='del'){ $id=intval($_GET['id']??0); if($id>0){ $mysqli->query("DELETE FROM profil_desa WHERE id=$id"); } header('Location: profil.php'); exit; }
$row=null; if($act==='edit'){ $id=intval($_GET['id']??0); $row=$mysqli->query("SELECT * FROM profil_desa WHERE id=$id")->fetch_assoc(); } ?>
<div class="card" style="padding:18px">
  <div style="display:flex;justify-content:space-between;align-items:center"><h2>Profil Desa</h2><a class="btn primary" href="profil.php?a=form">+ Tambah</a></div>
  <?php if($act==='list'): $res=$mysqli->query("SELECT * FROM profil_desa ORDER BY id DESC"); ?>
    <table class="table"><thead><tr><th>ID</th><th>Judul</th><th>Alamat</th><th>Telepon</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['address']); ?></td><td><?php echo esc($r['phone']); ?></td>
      <td><a class="btn small" href="profil.php?a=edit&id=<?php echo $r['id']; ?>">Edit</a> <a class="btn small" href="profil.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="profil.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>Alamat</label><input class="input" name="address" value="<?php echo esc($row['address']??''); ?>"></div>
      <div><label>Telepon</label><input class="input" name="phone" value="<?php echo esc($row['phone']??''); ?>"></div>
      <div><label>Email</label><input class="input" name="email" value="<?php echo esc($row['email']??''); ?>"></div>
      <div><label>Latitude</label><input class="input" name="lat" value="<?php echo esc($row['lat']??''); ?>"></div>
      <div><label>Longitude</label><input class="input" name="lng" value="<?php echo esc($row['lng']??''); ?>"></div>
      <div style="grid-column:1/-1"><label>Konten</label><textarea class="input" name="content"><?php echo esc($row['content']??''); ?></textarea></div>
      <div style="grid-column:1/-1"><button class="btn primary">Simpan</button> <a class="btn" href="profil.php">Batal</a></div>
    </form>
  <?php endif; ?>
</div><?php include __DIR__.'/../includes/footer.php'; ?>