<?php

namespace App\Controllers;

use App\Models\Tahunpelajaran_model;


class TahunPelajaran extends BaseController
{
    public function __construct()
    {
        $this->Tahunpelajaran_model = new Tahunpelajaran_model();
    }
    private $Tahunpelajaran_model;
    public function index()
    {
        // $ceklogin = cekLogin();
        // if ($ceklogin) return $ceklogin;
        // $user = getUserInfo();
        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Tahun Pelajaran',
        ];
        return view('Admin/Referensi/tahunpelajaran', $data);
    }
    public function getData()
    {
        $exec = $this->Tahunpelajaran_model->get_all_tahunpelajaran();
        $data['data'] = $exec;
        return $this->response->setJSON($data);
    }


    public function add()
    {
        $validationRules = [
            'kode' => 'required',
            'nama' => 'required',
            'semester' => 'required',
            'tanggal_rapor' => 'required',
        ];

        if ($this->request->isAJAX()) {
            $response = ['success' => false, 'errors' => []];
            if ($this->validate($validationRules)) {
                $response['success'] = true;
                $data = array(
                    'kode_tahunpelajaran' => $this->request->getPost('kode'),
                    'nama_tahunpelajaran' => $this->request->getPost('nama'),
                    'semester' => $this->request->getPost('semester'),
                    'tanggal_rapor' => $this->request->getPost('tanggal_rapor'),
                    'status' => $this->request->getPost('status'),
                );
                if ($this->request->getPost('status') == 'aktif') {
                    $this->Tahunpelajaran_model->update_status(['id!=' => $this->request->getPost('kode')]);
                    $this->Tahunpelajaran_model->update_setting($this->request->getPost('kode'));
                }
                $insert = $this->Tahunpelajaran_model->tahunpelajaran_add($data);
                if ($insert) {
                    return $this->response->setJSON(['success' => true, 'message' => 'Tahun Pelajaran Add successfully']);
                } else {
                    return $this->response->setStatusCode(404)->setJSON(['success' => false, 'message' => 'Tahun Pelajaran not found']);
                }
            } else {
                $response['errors'] = $this->validator->getErrors();
            }
            return $this->response->setJSON($response);
        }
    }

    public function edit($id)
    {
        $data = $this->Tahunpelajaran_model->get_by_id($id);
        echo json_encode($data);
    }

    public function update()
    {
        $data = array(
            'kode_tahunpelajaran' => $this->request->getPost('kode'),
            'semester' => $this->request->getPost('semester'),
            'nama_tahunpelajaran' => $this->request->getPost('nama'),
            'tanggal_rapor' => $this->request->getPost('tanggal_rapor'),
            'status' => $this->request->getPost('status'),
        );
        if ($this->request->getPost('status') == 'aktif') {
            $this->Tahunpelajaran_model->update_status(['id!=' => $this->request->getPost('kode')]);
            $this->Tahunpelajaran_model->update_setting($this->request->getPost('kode'));
        }
        $exec = $this->Tahunpelajaran_model->tahunpelajaran_update(['id' => $this->request->getPost('id')], $data);
        // dd($error);
        if ($exec) {
            return $this->response->setJSON(['success' => true, 'message' => 'User update successfully']);
        } else {
            //return $this->response->setJSON($error);
            return $this->response->setJSON(['success' => false, 'message' => 'Tidak ada data yang mengalami perubahan']);
        }
    }

    public function delete($id)
    {
        $user = $this->Tahunpelajaran_model->find($id);

        if ($user) {
            $this->Tahunpelajaran_model->delete($id);
            return $this->response->setJSON(['success' => true, 'message' => 'User deleted successfully']);
        } else {
            return $this->response->setStatusCode(404)->setJSON(['success' => false, 'message' => 'User not found']);
        }
    }
}
