<?php

namespace App\Controllers\PPDB;

use App\Controllers\BaseController;
use App\Models\PpdbSiswaModel;

class Profil extends BaseController
{
    protected $siswa;

    public function __construct()
    {
        $this->siswa = new PpdbSiswaModel();
    }

    public function index()
    {
        $id = session('ppdb_id');

        if (!$id) {
            return redirect()->to('/ppdb/login');
        }

        $data['siswa'] = $this->siswa->find($id);

        return view('ppdb/profil', $data);
    }

    public function save()
    {
        $id = session('ppdb_id');

        $this->siswa->update($id, [
            'nama_siswa'   => $this->request->getPost('nama_siswa'),
            'nik'          => $this->request->getPost('nik'),
            'tempat_lahir' => $this->request->getPost('tempat_lahir'),
            'tgl_lahir'    => $this->request->getPost('tgl_lahir'),
            'jenis_kelamin'=> $this->request->getPost('jenis_kelamin'),
            'alamat'       => $this->request->getPost('alamat'),
            'nama_ayah'    => $this->request->getPost('nama_ayah'),
            'nama_ibu'     => $this->request->getPost('nama_ibu'),
            'no_hp_ortu'   => $this->request->getPost('no_hp_ortu'),
            'current_step' => 'form_completed'
        ]);

        return redirect()->to('/ppdb/dashboard')
            ->with('success', 'Data profil berhasil disimpan');
    }
}
