<?php

namespace App\Http\Controllers;

use Exception;
use Illuminate\Http\Request;
use GuzzleHttp\Client;

class GetDataApiController extends Controller
{
    const URL   = 'https://siakadu.unesa.ac.id/api/apiunggun';
    const URLS  = 'https://siakadu.unesa.ac.id/api/dashboard';

    //
    public static function getDosen()
    {
        $client = new Client();

        $apiRequest = $client->request('POST', GetDataApiController::URLS, [
            'form_params' =>
            [
                'kondisi' => 'dosen_aktif',
            ]
        ]);

        return json_decode($apiRequest->getBody()->getContents(), true);
    }

    public static function getAccount($nim)
    {
        $client = new Client();

        $apiRequest = $client->request('POST', GetDataApiController::URL, [
            'form_params' =>
            [
                'username'  => $nim,
                'kondisi'   => 'cekhakakses'
            ]
        ]);

        $gcon = utf8_encode($apiRequest->getBody()->getContents());
        $data = unserialize(utf8_decode($gcon));

        return $data;
    }

    public static function sso($email)
    {
        $client = new Client();
        $apiRequest = $client->request('GET', 'https://sso.unesa.ac.id/userid/' . $email);
        $isdm = json_decode($apiRequest->getBody()->getContents());

        return $isdm;
    }

    public static function getDosenPerson($email)
    {
        $client = new Client();

        $apiRequest = $client->request('POST', GetDataApiController::URLS, [
            'form_params' =>
            [
                'kondisi' => 'dosen_aktif',
                'email' => $email
            ]
        ]);

        return json_decode($apiRequest->getBody()->getContents(), true);
    }
}
