<?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);

            if ($isdm[0]->nidn) {
                $noidentitas = $isdm[0]->nidn;
            } else {
                if ($isdm[0]->nuptk) {
                    $noidentitas = $isdm[0]->nuptk;
                } else {
                    $noidentitas = $isdm[0]->userid;
                }
            }

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

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

            $biodata->update($person);
        }

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

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

}
