prepare("SELECT id, no_parsil, pemilik, status, luas_m2, geojson FROM data_parsil WHERE id = ?"); $stmt->bind_param('i', $id); $stmt->execute(); $result = $stmt->get_result()->fetch_assoc(); if ($result) { $result['geojson'] = json_decode($result['geojson']); echo json_encode($result); } else { http_response_code(404); echo json_encode(['error' => 'Data tidak ditemukan']); } } else { // Get all $result = $db->query("SELECT id, no_parsil, pemilik, status, luas_m2, geojson FROM data_parsil ORDER BY id DESC"); $rows = []; while ($row = $result->fetch_assoc()) { $row['geojson'] = json_decode($row['geojson']); $rows[] = $row; } echo json_encode($rows); } $db->close(); exit(); } // ---- POST: Tambah parsil tanah baru ---- if ($method === 'POST') { $input = json_decode(file_get_contents('php://input'), true); if (empty($input['no_parsil']) || empty($input['pemilik']) || empty($input['status']) || empty($input['geojson'])) { http_response_code(400); echo json_encode(['error' => 'Data tidak lengkap. Field wajib: no_parsil, pemilik, status, geojson']); exit(); } $no_parsil = trim($input['no_parsil']); $pemilik = trim($input['pemilik']); $status = $input['status']; $luas_m2 = isset($input['luas_m2']) ? (float)$input['luas_m2'] : 0; $geojson = json_encode($input['geojson']); // Validasi status $valid_status = ['SHM', 'HGB', 'HGU', 'HP']; if (!in_array($status, $valid_status)) { http_response_code(400); echo json_encode(['error' => 'Status tidak valid. Pilih: SHM, HGB, HGU, atau HP']); exit(); } $db = getDB(); $stmt = $db->prepare("INSERT INTO data_parsil (no_parsil, pemilik, status, luas_m2, geojson) VALUES (?, ?, ?, ?, ?)"); $stmt->bind_param('sssds', $no_parsil, $pemilik, $status, $luas_m2, $geojson); if ($stmt->execute()) { $new_id = $db->insert_id; http_response_code(201); echo json_encode([ 'message' => 'Data parsil berhasil disimpan', 'id' => $new_id, 'no_parsil' => $no_parsil, 'pemilik' => $pemilik, 'status' => $status, 'luas_m2' => $luas_m2 ]); } else { http_response_code(500); echo json_encode(['error' => 'Gagal menyimpan data: ' . $stmt->error]); } $db->close(); exit(); } // ---- PUT: Update data parsil ---- if ($method === 'PUT') { if (!$id) { http_response_code(400); echo json_encode(['error' => 'ID diperlukan untuk update']); exit(); } $input = json_decode(file_get_contents('php://input'), true); $db = getDB(); // Cek apakah hanya update geometri (edit bentuk di peta) if (isset($input['geojson']) && !isset($input['no_parsil'])) { // Update geometri & luas $geojson = json_encode($input['geojson']); $luas_m2 = isset($input['luas_m2']) ? (float)$input['luas_m2'] : 0; $stmt = $db->prepare("UPDATE data_parsil SET geojson = ?, luas_m2 = ? WHERE id = ?"); $stmt->bind_param('sdi', $geojson, $luas_m2, $id); } else { // Full update (edit semua atribut) $no_parsil = trim($input['no_parsil'] ?? ''); $pemilik = trim($input['pemilik'] ?? ''); $status = $input['status'] ?? ''; $luas_m2 = isset($input['luas_m2']) ? (float)$input['luas_m2'] : 0; $geojson = json_encode($input['geojson'] ?? []); $valid_status = ['SHM', 'HGB', 'HGU', 'HP']; if (!in_array($status, $valid_status)) { http_response_code(400); echo json_encode(['error' => 'Status tidak valid']); exit(); } $stmt = $db->prepare("UPDATE data_parsil SET no_parsil = ?, pemilik = ?, status = ?, luas_m2 = ?, geojson = ? WHERE id = ?"); $stmt->bind_param('sssdsi', $no_parsil, $pemilik, $status, $luas_m2, $geojson, $id); } if ($stmt->execute()) { echo json_encode(['message' => 'Data parsil berhasil diperbarui', 'id' => $id]); } else { http_response_code(500); echo json_encode(['error' => 'Gagal memperbarui data: ' . $stmt->error]); } $db->close(); exit(); } // ---- DELETE: Hapus parsil tanah ---- if ($method === 'DELETE') { if (!$id) { http_response_code(400); echo json_encode(['error' => 'ID diperlukan untuk hapus']); exit(); } $db = getDB(); $stmt = $db->prepare("DELETE FROM data_parsil WHERE id = ?"); $stmt->bind_param('i', $id); if ($stmt->execute()) { if ($stmt->affected_rows > 0) { echo json_encode(['message' => 'Data parsil berhasil dihapus', 'id' => $id]); } else { http_response_code(404); echo json_encode(['error' => 'Data tidak ditemukan']); } } else { http_response_code(500); echo json_encode(['error' => 'Gagal menghapus data: ' . $stmt->error]); } $db->close(); exit(); } // Method tidak dikenal http_response_code(405); echo json_encode(['error' => 'Method tidak diizinkan']); ?>