<?php

namespace App\Http\Controllers\Webprofile\Backend;

use App\Http\Controllers\Controller;
use App\Models\Webprofile\Setting;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Alert;

class PersonController extends Controller
{
    public function index()
    {
        $unit = $this->listUnit();

        $pick = Setting::where('name_setting', 'team')->first();

        $listTeam = [];
        if ($pick) {
            $listTeam = $this->listTeam($pick->value_setting);
        }

        $data = [
            'unit' => $unit,
            'pick' => $pick,
            'listTeam' => $listTeam,
        ];

        return view('webprofile.backend.person.index', $data)->withTitle('Person');
    }

    public function store(Request $request)
    {
        $data = $request->except('_token');

        $data['show_name_setting'] = 'TEAM';
        $data['name_setting'] = 'team';
        $data['userid_created'] = auth()->user()->id;
        $data['userid_updated'] = auth()->user()->id;

        Setting::updateOrCreate(['name_setting' => $data['name_setting']], $data);

        Alert::success('Data berhasil disimpan')->persistent('Ok');

        return redirect()->route('person.index');
    }

    private function listUnit()
    {
        try {
            $client = new Client();
            $URI = 'https://i-sdm.unesa.ac.id/api/satker/asdfLKJH12309==';
            $response = $client->get($URI);
            $statuscode = $response->getStatusCode();

            if (200 === $statuscode) {
                // Do something
                $responseJson = json_decode($response->getBody(), true);
            } elseif (304 === $statuscode) {
                // Nothing to do
            } elseif (404 === $statuscode) {
                // Clean up DB or something like this
                $responseJson = [];
            } else {
            }

            foreach ($responseJson as $value) {
                $unit[$value['idsatker']] = $value['namasatker'];
            }
        } catch (\Throwable $th) {
            //throw $th;
            $unit[] = 'error';
        }

        return $unit;
    }

    private function listTeam($unit)
    {
        try {
            $client = new Client();
            $URI = 'http://i-sdm.unesa.ac.id/api/pegawai-per-prodi/asdfLKJH12309==/'.$unit;
            $response = $client->get($URI);
            $statuscode = $response->getStatusCode();

            if (200 === $statuscode) {
                // Do something
                $responseJson = json_decode($response->getBody(), true);
            } elseif (304 === $statuscode) {
                // Nothing to do
            } elseif (404 === $statuscode) {
                // Clean up DB or something like this
                $responseJson = [];
            } else {
            }
        } catch (\Throwable $th) {
            //throw $th;
        }

        return $responseJson;
    }

    public function showDosen()
    {
        $data = $request->except('_token');

        $data['show_name_setting'] = 'Dosen';
        $data['name_setting'] = 'dosen';
        $data['userid_created'] = auth()->user()->id;
        $data['userid_updated'] = auth()->user()->id;

        Setting::updateOrCreate(['name_setting' => $data['name_setting']], $data);

        Alert::success('Data berhasil disimpan')->persistent('Ok');

        return redirect()->route('person.index');
    }

    public function showTendik()
    {
        $data = $request->except('_token');

        $data['show_name_setting'] = 'TENDIK';
        $data['name_setting'] = 'tendik';
        $data['userid_created'] = auth()->user()->id;
        $data['userid_updated'] = auth()->user()->id;

        Setting::updateOrCreate(['name_setting' => $data['name_setting']], $data);

        Alert::success('Data berhasil disimpan')->persistent('Ok');

        return redirect()->route('person.index');
    }
}
