setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo json_encode(['success' => false, 'message' => 'Database connection failed']); exit(); } $type = isset($_GET['type']) ? $_GET['type'] : ''; $action = isset($_GET['action']) ? $_GET['action'] : ''; $id = isset($_GET['id']) ? intval($_GET['id']) : null; $method = $_SERVER['REQUEST_METHOD']; $validTypes = ['masjid', 'gereja', 'klenteng', 'vihara', 'pura']; if (!in_array($type, $validTypes)) { echo json_encode(['success' => false, 'message' => 'Invalid type']); exit(); } $table = 'tempat_ibadah'; switch ($action) { case 'getAll': $stmt = $pdo->prepare("SELECT * FROM $table WHERE jenis = ? ORDER BY nama ASC"); $stmt->execute([$type]); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode(['success' => true, 'data' => $data]); break; case 'getOne': $stmt = $pdo->prepare("SELECT * FROM $table WHERE id = ? AND jenis = ?"); $stmt->execute([$id, $type]); $data = $stmt->fetch(PDO::FETCH_ASSOC); if ($data) { echo json_encode(['success' => true, 'data' => $data]); } else { echo json_encode(['success' => false, 'message' => 'Data tidak ditemukan']); } break; case 'create': if ($method === 'POST') { $input = json_decode(file_get_contents('php://input'), true); $stmt = $pdo->prepare("INSERT INTO $table (jenis, nama, alamat, no_hp, latitude, longitude, radius, keterangan) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([ $type, $input['nama'], $input['alamat'] ?? '', $input['no_hp'] ?? '', $input['latitude'], $input['longitude'], $input['radius'] ?? 500, $input['keterangan'] ?? '' ]); echo json_encode(['success' => true, 'message' => 'Data berhasil disimpan', 'id' => $pdo->lastInsertId()]); } break; case 'update': if ($method === 'PUT') { $input = json_decode(file_get_contents('php://input'), true); $stmt = $pdo->prepare("UPDATE $table SET nama=?, alamat=?, no_hp=?, latitude=?, longitude=?, radius=?, keterangan=? WHERE id=? AND jenis=?"); $stmt->execute([ $input['nama'], $input['alamat'] ?? '', $input['no_hp'] ?? '', $input['latitude'], $input['longitude'], $input['radius'] ?? 500, $input['keterangan'] ?? '', $id, $type ]); echo json_encode(['success' => true, 'message' => 'Data berhasil diupdate']); } break; case 'delete': if ($method === 'DELETE') { $stmt = $pdo->prepare("DELETE FROM $table WHERE id = ?"); $stmt->execute([$id]); echo json_encode(['success' => true, 'message' => 'Data berhasil dihapus']); } break; default: echo json_encode(['success' => false, 'message' => 'Invalid action']); } ?>