<?php

namespace App\Controllers;

use App\Models\Pengumuman_model;
use App\Models\Siswa_model;

class Web extends BaseController
{
    public function __construct()
    {

        $this->Siswa_model = new Siswa_model();
    }

    private $Siswa_model;
    public function index(): string
    {
        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Web Kelulusan'
        ];
        return view('Web/index', $data);
    }
    public function info()
    {
        $idsiswa = session()->get('siswa_id');
        if (!$idsiswa) {
            return redirect('/');
        }
        $siswa = $this->Siswa_model->get_by_id(session()->get('siswa_id'));
        $pengumuman = new Pengumuman_model();
        $pengumuman = $pengumuman->get_all_pengumumans();
        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Info Kelulusan',
            'siswa' => $siswa,
            'pengumuman' => $pengumuman
        ];
        return view('Web/info', $data);
    }
    public function siswalogin()
    {
        // Validasi input
        $validationRules = [
            'username' => 'required',
            'password' => 'required'
        ];

        if (!$this->validate($validationRules)) {
            // Jika validasi gagal, kirim pesan error
            return $this->response->setJSON(['message' => 'Username dan password wajib diisi']);
        } else {
            // Simpan input dari form
            $username = $this->request->getPost('username');
            $password = $this->request->getPost('password') ?? "";

            // Ambil data siswa dari model
            $siswa = $this->Siswa_model->get_where(['username' => $username]);

            if (!$siswa) {
                // Jika siswa tidak ditemukan, kirim pesan error
                return $this->response->setJSON(['success' => false, 'message' => 'Username atau password salah']);
            } else {
                if (password_verify($password, $siswa->password)) {
                    // Jika password benar, kirim pesan berhasil
                    $session = session();
                    $sessionData = [
                        'siswa_id' => $siswa->id,
                        // Tambahkan data lain yang perlu disimpan di sesi
                    ];
                    $session->set($sessionData);
                    return $this->response->setJSON(['success' => true, 'message' => 'Login berhasil']);
                } else {
                    // Jika password salah, kirim pesan error
                    return $this->response->setJSON(['success' => false, 'message' => 'Username atau password salah']);
                }
            }
        }
    }

    public function siswalogout()
    {
        // Ambil objek session
        $session = session();
        // Untuk logout dari session siswa
        if ($session->has('siswa_id')) {
            $session->remove('siswa_id');
        }
        return redirect()->to('/');
    }
}
