prepare("SELECT * FROM parsil_tanah WHERE id = ?"); $stmt->bind_param('i', $id); $stmt->execute(); $row = $stmt->get_result()->fetch_assoc(); if ($row) { $row['id'] = (int) $row['id']; $row['luas_meter2'] = (float) $row['luas_meter2']; $row['geojson'] = json_decode($row['geojson']); echo json_encode(['status' => 'success', 'data' => $row]); } else { http_response_code(404); echo json_encode(['status' => 'error', 'message' => 'Data tidak ditemukan']); } $stmt->close(); } else { $res = $conn->query("SELECT * FROM parsil_tanah ORDER BY created_at DESC"); $data = []; while ($row = $res->fetch_assoc()) { $row['id'] = (int) $row['id']; $row['luas_meter2'] = (float) $row['luas_meter2']; $row['geojson'] = json_decode($row['geojson']); $data[] = $row; } echo json_encode(['status' => 'success', 'data' => $data]); } $conn->close(); exit; } // ── POST (Tambah) ───────────────────────────────────────────── if ($method === 'POST') { $body = json_decode(file_get_contents('php://input'), true); $nama = trim($body['nama_pemilik'] ?? ''); $no_sert = trim($body['no_sertifikat'] ?? ''); $status = $body['status_sertifikat'] ?? ''; $luas = (float) ($body['luas_meter2'] ?? 0); $geojson = isset($body['geojson']) ? json_encode($body['geojson']) : null; $valid_status = ['SHM', 'HGB', 'HGU', 'HP']; if (!$nama || !$no_sert || !in_array($status, $valid_status) || !$geojson || $luas <= 0) { http_response_code(400); echo json_encode(['status' => 'error', 'message' => 'Data tidak lengkap atau tidak valid']); exit; } $conn = getConnection(); $stmt = $conn->prepare( "INSERT INTO parsil_tanah (nama_pemilik, no_sertifikat, status_sertifikat, luas_meter2, geojson) VALUES (?, ?, ?, ?, ?)" ); $stmt->bind_param('sssds', $nama, $no_sert, $status, $luas, $geojson); if ($stmt->execute()) { echo json_encode([ 'status' => 'success', 'message' => 'Data parsil berhasil ditambahkan', 'id' => $stmt->insert_id ]); } else { http_response_code(500); echo json_encode(['status' => 'error', 'message' => $stmt->error]); } $stmt->close(); $conn->close(); exit; } // ── PUT (Update) ────────────────────────────────────────────── if ($method === 'PUT' && $id) { $body = json_decode(file_get_contents('php://input'), true); $nama = trim($body['nama_pemilik'] ?? ''); $no_sert = trim($body['no_sertifikat'] ?? ''); $status = $body['status_sertifikat'] ?? ''; $luas = (float) ($body['luas_meter2'] ?? 0); $geojson = isset($body['geojson']) ? json_encode($body['geojson']) : null; $valid_status = ['SHM', 'HGB', 'HGU', 'HP']; if (!$nama || !$no_sert || !in_array($status, $valid_status) || !$geojson || $luas <= 0) { http_response_code(400); echo json_encode(['status' => 'error', 'message' => 'Data tidak lengkap atau tidak valid']); exit; } $conn = getConnection(); $stmt = $conn->prepare( "UPDATE parsil_tanah SET nama_pemilik=?, no_sertifikat=?, status_sertifikat=?, luas_meter2=?, geojson=? WHERE id=?" ); $stmt->bind_param('sssdsi', $nama, $no_sert, $status, $luas, $geojson, $id); if ($stmt->execute()) { echo json_encode(['status' => 'success', 'message' => 'Data parsil berhasil diperbarui']); } else { http_response_code(500); echo json_encode(['status' => 'error', 'message' => $stmt->error]); } $stmt->close(); $conn->close(); exit; } // ── DELETE ──────────────────────────────────────────────────── if ($method === 'DELETE' && $id) { $conn = getConnection(); $stmt = $conn->prepare("DELETE FROM parsil_tanah WHERE id=?"); $stmt->bind_param('i', $id); if ($stmt->execute()) { echo json_encode(['status' => 'success', 'message' => 'Data parsil berhasil dihapus']); } else { http_response_code(500); echo json_encode(['status' => 'error', 'message' => $stmt->error]); } $stmt->close(); $conn->close(); exit; } http_response_code(405); echo json_encode(['status' => 'error', 'message' => 'Method tidak diizinkan']);