<?php
require_once 'functions.php';
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if (!$id) { echo 'Invalid id'; exit; }

$sql = "SELECT r.*, a.wadmkk FROM rtrw_object r JOIN adm_unit a ON r.adm_unit_id=a.id WHERE r.id = " . intval($id) . " LIMIT 1";
$res = $mysqli->query($sql);
if (!$res || $res->num_rows == 0) { echo 'Not found'; exit; }
$row = $res->fetch_assoc();
$attrs = json_decode($row['attributes'], true);
?>
<!doctype html>
<html>
<head><meta charset="utf-8"><title>Detail: <?php echo htmlspecialchars($row['namobj']) ?></title><link rel="stylesheet" href="style.css"></head>
<body>
  <div class="container">
    <h1>Detail Objek</h1>
    <p><a href="index.php">&larr; Kembali</a></p>
    <table class="detail">
      <tr><th>ID</th><td><?php echo $row['id'] ?></td></tr>
      <tr><th>ObjectID</th><td><?php echo $row['objectid'] ?></td></tr>
      <tr><th>Nama Objek</th><td><?php echo htmlspecialchars($row['namobj']) ?></td></tr>
      <tr><th>Wilayah</th><td><?php echo htmlspecialchars($row['wadmkk']) ?></td></tr>
      <tr><th>Luas (ha)</th><td><?php echo is_null($row['luasha']) ? '-' : number_format($row['luasha'],3) ?></td></tr>
      <tr><th>Shape Area</th><td><?php echo is_null($row['shape_area']) ? '-' : $row['shape_area'] ?></td></tr>
    </table>

    <h2>Atribut (kategori)</h2>
    <table class="attrs">
      <thead><tr><th>Key</th><th>Value</th></tr></thead>
      <tbody>
        <?php if (is_array($attrs)): foreach($attrs as $k=>$v): ?>
          <tr><td><?php echo htmlspecialchars($k) ?></td><td><?php echo htmlspecialchars((string)$v) ?></td></tr>
        <?php endforeach; else: ?>
          <tr><td colspan="2">No attributes</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</body>
</html>
