<?php

namespace App\Controllers;

use Core\Controller;
use App\Models\Maba;

class MabaController extends Controller
{
    protected $maba;

    public function __construct()
    {
        $this->maba = new Maba();
    }

    /**
     * FORM PROFIL MAHASISWA
     */
    public function profile()
    {
        $nisn = $_SESSION['user']['username']; // login pakai nisn
        $mahasiswa = $this->maba->findMahasiswaByNim($nisn);

        require_once __DIR__ . '/../Views/maba/profile.php';
    }

    public function saveProfile()
    {
        $nisn = $_SESSION['user']['username'];
        $data = [
            'alamat' => 
                        ($_POST['nama_dusun'] ?? '') . ' ' .
                        ($_POST['rt'] ?? '') . '/' . ($_POST['rw'] ?? '') . ' ' .
                        ($_POST['kelurahan'] ?? '') . ' ' .
                        ($_POST['kecamatan'] ?? ''),
            'telpon'            => $_POST['telpon'] ?? null,
            'tmpt_lahir'        => $_POST['tmpt_lahir'] ?? null,
            'tgl_lahir'         => $_POST['tgl_lahir'] ?? null,
            'agama'             => $_POST['agama'] ?? null,
            'jenis_kelamin'     => $_POST['jenis_kelamin'] ?? null,
            'nik'               => $_POST['nik'] ?? null,
            'jalur_pendaftaran' => 12,
            'npwp'              => $_POST['npwp'] ?? null,
            'kewarganegaraan'   => 'ID',
            'jenis_pendaftaran' => 1,
            'tgl_masuk_kuliah'  => '2025-09-15',
            'mulai_semester'    => '20251',
            'jalan'             => $_POST['jalan'] ?? null,
            'rt'                => $_POST['rt'] ?? null,
            'rw'                => $_POST['rw'] ?? null,
            'nama_dusun'        => $_POST['nama_dusun'] ?? null,
            'kelurahan'         => $_POST['kelurahan'] ?? null,
            'kecamatan'         => $_POST['kecamatan'] ?? null,
            'kode_pos'          => $_POST['kode_pos'] ?? null,
            'jenis_tinggal'     => $_POST['jenis_tinggal'] ?? null,
            'alat_transportasi' => $_POST['alat_transportasi'] ?? null,
            'telp_rumah'        => $_POST['telp_rumah'] ?? null,
            'no_hp'             => $_POST['no_hp'] ?? null,
            'email'             => $_POST['email'] ?? null,
            'terima_kps'        => $_POST['terima_kps'] ?? null,
            'no_kps'            => $_POST['no_kps'] ?? null,
        ];

        $this->maba->updateProfile($nisn, $data);
        header("Location: /dashboard/maba-profile");
        exit;
    }

    /**
     * FORM ORANG TUA / WALI
     */
    public function orangTua()
    {
        $nisn = $_SESSION['user']['username'];
        $mahasiswa = $this->maba->findMahasiswaByNim($nisn);

        require_once __DIR__ . '/../Views/maba/orangtua.php';
    }

    public function saveOrangTua()
    {
        $nisn = $_SESSION['user']['username'];
        $data = [
            'nik_ayah'         => $_POST['nik_ayah'] ?? null,
            'nama_ayah'        => $_POST['nama_ayah'] ?? null,
            'tgl_lahir_ayah'   => $_POST['tgl_lahir_ayah'] ?? null,
            'pendidikan_ayah'  => $_POST['pendidikan_ayah'] ?? null,
            'pekerjaan_ayah'   => $_POST['pekerjaan_ayah'] ?? null,
            'penghasilan_ayah' => $_POST['penghasilan_ayah'] ?? null,
            'nik_ibu'          => $_POST['nik_ibu'] ?? null,
            'nama_ibu'         => $_POST['nama_ibu'] ?? null,
            'tgl_lahir_ibu'    => $_POST['tgl_lahir_ibu'] ?? null,
            'pendidikan_ibu'   => $_POST['pendidikan_ibu'] ?? null,
            'pekerjaan_ibu'    => $_POST['pekerjaan_ibu'] ?? null,
            'penghasilan_ibu'  => $_POST['penghasilan_ibu'] ?? null,
            'nama_wali'        => $_POST['nama_wali'] ?? null,
            'tgl_lahir_wali'   => $_POST['tgl_lahir_wali'] ?? null,
            'pendidikan_wali'  => $_POST['pendidikan_wali'] ?? null,
            'pekerjaan_wali'   => $_POST['pekerjaan_wali'] ?? null,
            'penghasilan_wali' => $_POST['penghasilan_wali'] ?? null,
        ];

        $this->maba->updateOrangTua($nisn, $data);
        header("Location: /dashboard/maba-orangtua");
        exit;
    }

    /**
     * FORM PILIH KELAS
     */
    public function pilihKelas()
    {
        $nisn = $_SESSION['user']['username'];
        $mahasiswa = $this->maba->findMahasiswaByNim($nisn);
        
        // tambahkan ini
        $hasToken = $this->maba->hasTokenMaba($nisn);
    
        require_once __DIR__ . '/../Views/maba/pilihkelas.php';
    }

    public function savePilihKelas()
    {
        $nisn = $_SESSION['user']['username'];
        $data = [
            'jenis_pembiayaan'   => $_POST['jenis_pembiayaan'] ?? null,
            'jumlah_biaya_masuk' => $_POST['jumlah_biaya_masuk'] ?? null,
            'kelas'              => $_POST['kelas'] ?? null,
            'pusat_pokjar'      => $_POST['pusat_pokjar'] ?? null,
        ];

        $this->maba->updateKelas($nisn, $data);
        header("Location: /dashboard/maba-pilihkelas");
        exit;
    }

    public function tokenisasi()
    {
        $nisn = $_SESSION['user']['username'];
        $errors = [];
    
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $token = trim($_POST['token'] ?? '');
    
            if (empty($token)) {
                $errors['token'] = "Token wajib diisi";
            } else {
                // cek token valid
                $isValid = $this->maba->validateToken($token);
    
                if (!$isValid) {
                    $errors['token'] = "Token tidak valid";
                } else {
                    // simpan nisn ke tb_token_maba
                    $this->maba->insertTokenMaba($nisn, $token);
                    header("Location: /dashboard/maba-pilihkelas");
                    exit;
                }
            }
        }
    
        // reload pilih kelas dengan error
        $mahasiswa = $this->maba->findMahasiswaByNim($nisn);
        $hasToken = $this->maba->hasTokenMaba($nisn);
        require_once __DIR__ . '/../Views/maba/pilihkelas.php';
    }
    
    public function searchKecamatan()
    {
        $keyword = $_GET['q'] ?? '';
        $stmt = $this->maba->searchWilayah($keyword, 3);
        header('Content-Type: application/json');
        echo json_encode($stmt);
        exit;
    }

}
