<?php

namespace App\Controllers;

use App\Models\Matapelajaran_model;


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


    public function add()
    {
        $validationRules = [

            'nama' => 'required',
        ];

        if ($this->request->isAJAX()) {
            $response = ['success' => false, 'errors' => []];
            if ($this->validate($validationRules)) {
                $response['success'] = true;
                $data = array(

                    'nama_matapelajaran' => $this->request->getPost('nama'),
                    'status' => $this->request->getPost('status'),
                );

                $insert = $this->Matapelajaran_model->Matapelajaran_add($data);
                if ($insert) {
                    return $this->response->setJSON(['success' => true, 'message' => 'Mata Pelajaran Add successfully']);
                } else {
                    return $this->response->setStatusCode(404)->setJSON(['success' => false, 'message' => 'Mata Pelajaran not found']);
                }
            } else {
                $response['errors'] = $this->validator->getErrors();
            }
            return $this->response->setJSON($response);
        }
    }

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

    public function update()
    {
        $data = array(
            'nama_matapelajaran' => $this->request->getPost('nama'),
            'status' => $this->request->getPost('status'),
        );

        $exec = $this->Matapelajaran_model->Matapelajaran_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->Matapelajaran_model->find($id);

        if ($user) {
            $this->Matapelajaran_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']);
        }
    }
}
