<?php

namespace App\Http\Controllers;

use App\Models\Auth\Biodata;
use App\Models\Pengumuman;
use App\Models\Sms;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class DashboardController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $title   = 'Dashboard PKM';
        $pengumuman = $pengumuman = Pengumuman::query()->first();

        if (Auth::user()->hasrole(['mahasiswa'])) {
            $user = Auth::user()->rBiodata->noidentitas;

            $dataMhs = GetDataApiController::getAccount($user);
            $faks = Sms::where('id_sms', $dataMhs['data_mahasiswa']['id_sms'])->first();
            $person['fakultas'] = $faks->parent->nm_lemb;

            $biodata = Biodata::where('noidentitas', $user)->first();

            $biodata->update($person);
        }

        if(Auth::user()->hasrole(['dosen'])){
            $user = Auth::user()->rBiodata->email;

            $isdm   = GetDataApiController::sso($user);
            $dosen   = GetDataApiController::getDosenPerson($user);

            $noidentitas = $dosen['data'][0]['nidn'];

            $biodata = Biodata::where('email', $user)->first();

            if (is_null($biodata->noidentitas) || empty($biodata->noidentitas)) {
                $person['noidentitas'] = $noidentitas;
            }

            $person['fakultas'] = $isdm[0]->namaparentsatker;
            $person['prodi'] = $isdm[0]->namasatker;

            $biodata->update($person);

        }

        $data = [
            'title'   => $title,
            'pengumuman'  => $pengumuman,
        ];

        return view('backend.index', $data);
    }

}
