<?php
include "../config/connect.php";

// Menggunakan PDO untuk koneksi database
try {
    // Mengambil parameter 'dropdown', 'option', dan 'option1' dari query string
    $dropdown = $_GET['dropdown'] ?? '';
    $option = $_GET['option'] ?? '';
    $option1 = $_GET['option1'] ?? '';

    $options = [];

    switch ($dropdown) {
        case 'dropdown2':
            // Query untuk data paket
            $stmt = $pdo->prepare("SELECT id, nama FROM profile_paket WHERE id_data_server = :id_data_server AND jenis_profile = :jenis_profile AND status IS NULL");
            $stmt->execute([':id_data_server' => $option, ':jenis_profile' => $option1]);
            $options = $stmt->fetchAll(PDO::FETCH_ASSOC);
            break;

        case 'dropdown3':
            $stmt = $pdo->prepare("SELECT id, nama FROM user_akses WHERE FIND_IN_SET(:id_data_server, id_data_server)");
            $stmt->execute([':id_data_server' => $option]);
            $options = $stmt->fetchAll(PDO::FETCH_ASSOC);
            break;

        case 'dropdown4':
            // Query untuk data desa
            $stmt = $pdo->prepare("SELECT id, nama FROM master_desa WHERE id_data_server = :id_data_server");
            $stmt->execute([':id_data_server' => $option]);
            $options = $stmt->fetchAll(PDO::FETCH_ASSOC);
            break;

        case 'dropdown5':
            // Query untuk setting mikrotik
            $stmt = $pdo->prepare("SELECT id, nama FROM setting_mikrotik WHERE id_data_server = :id_data_server");
            $stmt->execute([':id_data_server' => $option]);
            $options = $stmt->fetchAll(PDO::FETCH_ASSOC);
            break;

        case 'dropdown6':
            // Query untuk profile tagihan
            $stmt = $pdo->prepare("SELECT id, nama FROM profile_tagihan WHERE id_data_server = :id_data_server");
            $stmt->execute([':id_data_server' => $option]);
            $options = $stmt->fetchAll(PDO::FETCH_ASSOC);
            break;

        default:
            $options = [];
            break;
    }

    // Mengembalikan data dalam format JSON
    header('Content-Type: application/json');
    echo json_encode($options);

} catch (PDOException $e) {
    // Mengembalikan error dalam format JSON
    header('Content-Type: application/json');
    echo json_encode(['error' => $e->getMessage()]);
}
?>
