<?php

namespace App\Controllers;


use App\Models\StudentModel;

class Student extends BaseController
{
    public function index()
    {
        $model = new StudentModel();

        $data['students'] = $model->whereIn('status', [1, 2])->orderBy('created_datetime', 'DESC')->findAll();

        return view('student', $data);
    }

    public function save()
    { 
        $model = new StudentModel();

        $result = $model->save([
            'first_name'     => $this->request->getPost('first_name'),
            'last_name'      => $this->request->getPost('last_name'),
            'address'        => $this->request->getPost('address'),
            'city'           => $this->request->getPost('city'),
            'contact_no_1'   => $this->request->getPost('contact_no_1'),
            'contact_no_2'   => $this->request->getPost('contact_no_2'),
            'guardian_name'  => $this->request->getPost('guardian_name'),
            'email'          => strtolower($this->request->getPost('email')),
            'status'         => $this->request->getPost('status')
        ]);

        if ($result) {
            session()->setFlashdata(
                'success',
                'Student saved successfully.'
            );
        } else {
            session()->setFlashdata(
                'error',
                'Unable to save Student.'
            );
        }

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

    public function get($id)
    {
        $model = new StudentModel();

        $data = $model->find($id);

        return $this->response->setJSON($data);
    }

    public function update()
    {
        $model = new StudentModel();

        $id = $this->request->getPost('updid');

        $result = $model->update($id, [
            'first_name'    => $this->request->getPost('updfirst_name'),
            'last_name'     => $this->request->getPost('updlast_name'),
            'address'       => $this->request->getPost('updaddress'),
            'city'          => $this->request->getPost('updcity'),
            'contact_no_1'  => $this->request->getPost('updcontact_no_1'),
            'contact_no_2'  => $this->request->getPost('updcontact_no_2'),
            'guardian_name' => $this->request->getPost('updguardian_name'),
            'email'         => strtolower($this->request->getPost('updemail')),
            'status'        => $this->request->getPost('updstatus'),
        ]);

        if ($result) {
            session()->setFlashdata(
                'success',
                'Student updated successfully.'
            );
        } else {
            session()->setFlashdata(
                'error',
                'Unable to update Student.'
            );
        }

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

    public function delete()
    {
        $model = new StudentModel();

        $id = $this->request->getPost('delid');

        $result = $model->update($id, [
            'status' => 3
        ]);

        if ($result) {
            session()->setFlashdata(
                'success',
                'Student deleted successfully.'
            );
        } else {
            session()->setFlashdata(
                'error',
                'Student to delete Student.'
            );
        }

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