<?php
$c = file_get_contents('../app/Controllers/Mahasiswa.php');
$method = "    public function searchPT()
    {
        \$keyword = \$this->request->getGet('q');
        if (empty(\$keyword)) return \$this->response->setJSON([]);

        \$pddiktiUrl = \$this->pengaturanModel->where('pengaturan_key', 'pddikti_url')->first()['pengaturan_value'];
        \$pddiktiUsername = \$this->pengaturanModel->where('pengaturan_key', 'pddikti_username')->first()['pengaturan_value'];
        \$pddiktiPassword = \$this->pengaturanModel->where('pengaturan_key', 'pddikti_password')->first()['pengaturan_value'];

        if (empty(\$pddiktiUrl)) return \$this->response->setJSON([]);

        try {
            \$client = \Config\Services::curlrequest();
            \$response = \$client->post(\$pddiktiUrl, [
                'json' => ['act' => 'GetToken', 'username' => \$pddiktiUsername, 'password' => \$pddiktiPassword],
                'verify' => false
            ]);
            \$token = json_decode(\$response->getBody(), true)['data']['token'];

            \$responsePT = \$client->post(\$pddiktiUrl, [
                'json' => [
                    'act' => 'GetPerguruanTinggi',
                    'token' => \$token,
                    'filter' => \"nama_perguruan_tinggi ILIKE '%\" . \$keyword . \"%'\",
                    'limit' => 20
                ],
                'verify' => false
            ]);
            \$dataPT = json_decode(\$responsePT->getBody(), true)['data'] ?? [];

            \$results = [];
            foreach (\$dataPT as \$pt) {
                \$results[] = [
                    'id' => \$pt['id_perguruan_tinggi'],
                    'text' => \$pt['kode_perguruan_tinggi'] . ' - ' . \$pt['nama_perguruan_tinggi']
                ];
            }
            return \$this->response->setJSON(\$results);
        } catch (\Exception \$e) {
            return \$this->response->setJSON([]);
        }
    }

    public function searchProdi()
    {
        \$id_pt = \$this->request->getGet('id_pt');
        if (empty(\$id_pt)) return \$this->response->setJSON([]);

        \$pddiktiUrl = \$this->pengaturanModel->where('pengaturan_key', 'pddikti_url')->first()['pengaturan_value'];
        \$pddiktiUsername = \$this->pengaturanModel->where('pengaturan_key', 'pddikti_username')->first()['pengaturan_value'];
        \$pddiktiPassword = \$this->pengaturanModel->where('pengaturan_key', 'pddikti_password')->first()['pengaturan_value'];

        if (empty(\$pddiktiUrl)) return \$this->response->setJSON([]);

        try {
            \$client = \Config\Services::curlrequest();
            \$response = \$client->post(\$pddiktiUrl, [
                'json' => ['act' => 'GetToken', 'username' => \$pddiktiUsername, 'password' => \$pddiktiPassword],
                'verify' => false
            ]);
            \$token = json_decode(\$response->getBody(), true)['data']['token'];

            \$responseProdi = \$client->post(\$pddiktiUrl, [
                'json' => [
                    'act' => 'GetProdi',
                    'token' => \$token,
                    'filter' => \"id_perguruan_tinggi='\" . \$id_pt . \"'\",
                    'limit' => 100
                ],
                'verify' => false
            ]);
            \$dataProdi = json_decode(\$responseProdi->getBody(), true)['data'] ?? [];

            \$results = [];
            foreach (\$dataProdi as \$p) {
                \$results[] = [
                    'id' => \$p['id_prodi'],
                    'text' => \$p['nama_jenjang_pendidikan'] . ' ' . \$p['nama_program_studi']
                ];
            }
            return \$this->response->setJSON(\$results);
        } catch (\Exception \$e) {
            return \$this->response->setJSON([]);
        }
    }
";

if (strpos($c, "public function searchPT()") === false) {
    $c = str_replace("public function searchWilayah()", $method . "\n    public function searchWilayah()", $c);
    file_put_contents('../app/Controllers/Mahasiswa.php', $c);
    echo "Methods added.";
} else {
    echo "Methods already exist.";
}
