<?php
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../app/Config/Paths.php';
$paths = new Config\Paths();
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
$db = \Config\Database::connect();
$mhs = $db->table('mahasiswa')->where('nama_lengkap', 'HIDAYATUL AULIA')->get()->getRowArray();
if(!$mhs) die('Mahasiswa tidak ditemukan di lokal');
$pengaturan = $db->table('pengaturan')->get()->getResultArray();
$p = [];
foreach($pengaturan as $pg) $p[$pg['pengaturan_key']] = $pg['pengaturan_value'];
$client = \Config\Services::curlrequest();
$res = $client->post($p['pddikti_url'], ['json'=>['act'=>'GetToken','username'=>$p['pddikti_username'],'password'=>$p['pddikti_password']], 'verify'=>false]);
$token = json_decode($res->getBody(), true)['data']['token'];
$pt = $client->post($p['pddikti_url'], ['json'=>['act'=>'GetProfilPT','token'=>$token], 'verify'=>false]);
$id_pt = json_decode($pt->getBody(), true)['data'][0]['id_perguruan_tinggi'];
$prodi = $db->table('program_studi')->where('id', $mhs['prodi_id'])->get()->getRowArray();
$riwayat = [
    'id_mahasiswa'=>$mhs['id_mahasiswa_pddikti'],
    'nim'=>$mhs['nim'],
    'id_jenis_daftar'=>1,
    'id_jalur_daftar'=>8,
    'id_periode_masuk'=>$mhs['angkatan'].'1',
    'tanggal_daftar'=>date('Y-m-d'),
    'id_perguruan_tinggi'=>$id_pt,
    'id_prodi'=>$prodi['id_prodi_pddikti'],
    'id_pembiayaan'=>1,
    'biaya_masuk'=>0
];
$insert = $client->post($p['pddikti_url'], ['json'=>['act'=>'InsertRiwayatPendidikanMahasiswa','token'=>$token,'record'=>$riwayat], 'verify'=>false]);
$resIns = json_decode($insert->getBody(), true);
print_r($resIns);
if($resIns['error_code'] == 0){
    $db->table('mahasiswa')->where('id', $mhs['id'])->update(['id_registrasi_mahasiswa'=>$resIns['data']['id_registrasi_mahasiswa']]);
    echo "\nBerhasil!";
}
