<?php

namespace App\Controllers;

use App\Models\ModelLogin;

class Login extends BaseController
{

    public function index()
    {
        return view('login');
    }

    public function prosesLogin()
    {

        $model = new ModelLogin();

        $nip = $this->request->getPost('nip');
        $password = $this->request->getPost('password');

        $user = $model->login($nip);

        if ($user) {

            if ($password == $user['password']) {

                session()->set([
                    'id_guru' => $user['id_guru'],
                    'nip'     => $user['nip'],
                    'walikelas' => $user['walikelas'],
                    'nama'    => $user['nama'],
                    'role'    => $user['role'],
                    'login'   => true
                ]);

                return redirect()->to('/PilihKelas');
            }
        }

        return redirect()->back()->with('error', 'NIP atau Password salah.');
    }

    public function logout()
    {

        session()->destroy();

        return redirect()->to('/Login');
    }
}
