<?php

namespace App\Controllers;

use App\Models\GradeModel;
use App\Models\LessonModel;

class Lesson extends BaseController
{
    public function index()
    {
        $model = new LessonModel();
        $gradeModel = new GradeModel();

        $data['lessons'] = $model
            ->select('lesson.*, grade.code AS grade_code, grade.name AS grade_name, grade.id AS grade_id')
            ->join('grade', 'grade.id = lesson.grade_id')
            ->whereIn('lesson.status', [1, 2])
            ->orderBy('lesson.created_datetime', 'DESC')
            ->findAll();
        $data['grades'] = $gradeModel->whereIn('status', [1])->orderBy("CAST(REPLACE(name, 'Grade ', '') AS UNSIGNED)", '', false)->findAll();

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

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

        $result = $model->save([
            'grade_id' => $this->request->getPost('grade'),
            'name' => $this->request->getPost('name'),
            'code' => strtoupper($this->request->getPost('code')),
            'description' => $this->request->getPost('description'),
            'status' => $this->request->getPost('status')
        ]);

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

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

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

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

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

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

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

        $result = $model->update($id, [
            'grade_id' => strtoupper($this->request->getPost('updgrade')),
            'code' => strtoupper($this->request->getPost('updcode')),
            'name' => $this->request->getPost('updname'),
            'description' => $this->request->getPost('upddescription'),
            'status' => $this->request->getPost('updstatus'),
        ]);

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

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

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

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

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

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

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