<?php

namespace App\Controllers;

use App\Models\Dosen;

class DosenController extends \Core\Controller
{
    public function profile()
    {
        if (!isset($_SESSION['user'])) {
            header('Location: /');
            exit;
        }

        // Ambil niy dari array session user
        $niy = $_SESSION['user']['niy'] ?? null;

        if (!$niy) {
			
            die( $niy . 'NIY tidak ditemukan di session.');
			echo $niy;
        }

        $dosenModel = new Dosen();
        $dosen = $dosenModel->findByNiy($niy);

        if (!$dosen) {
            die('Data dosen tidak ditemukan.');
        }

        $this->view('dosen/profile', [
            'dosen' => $dosen
        ]);
    }

    public function updateProfile()
    {
        if (!isset($_SESSION['user'])) {
            header('Location: /');
            exit;
        }

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $niy = $_SESSION['user']['niy'] ?? null;

            if (!$niy) {
                die('Niy tidak ditemukan di session.');
            }

            $data = [
                'nama' => $_POST['nama'] ?? '',
                'alamat' => $_POST['alamat'] ?? '',
                'jenis_kelamin' => $_POST['jenis_kelamin'] ?? '',
                'gelar' => $_POST['gelar'] ?? '',
            ];

            $dosenModel = new Dosen();
            $dosenModel->updateByNiy($niy, $data);

            header('Location: /dashboard/dosen-profile');
            exit;
        }
    }
}
