<?php

namespace App\Controllers;

use App\Models\Tujuan_model;


class Tujuan extends BaseController
{
    public function __construct()
    {
        $this->Tujuan_model = new Tujuan_model();
    }
    private $Tujuan_model;
    public function index()
    {


        // $ceklogin = cekLogin();
        // if ($ceklogin) return $ceklogin;
        // $user = getUserInfo();
        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Tujuan Pembelajaran'
        ];
        return view('Admin/Referensi/tujuan', $data);
    }
    public function CopyTujuan()
    {
        if ($this->request->isAJAX()) {
            $pembelajaran1 = $this->request->getPost('pembelajaran1');
            $pembelajaran2 = $this->request->getPost('pembelajaran2');
            $idrombel = $this->request->getPost('idrombel');
            $this->Tujuan_model->copy_tujuan($pembelajaran1, $pembelajaran2, $idrombel);
            return $this->response->setJSON(['success' => true, 'message' => 'Copy successfully']);
        }
    }
    public function getTujuan($idpembelajaran)
    {
        $exec = $this->Tujuan_model->get_where_tujuan(['id_pembelajaran' => $idpembelajaran]);

        return $this->response->setJSON($exec);
    }
    public function addTujuan()
    {
        // Tangkap data dari form
        $tujuan = $this->request->getPost('tujuan');
        $guru = session()->get('user_id');
        $idmapel = $this->request->getPost('idmapel');
        $idrombel = $this->request->getPost('idrombel');
        $status = $this->request->getPost('status');
        $idpembelajaran = $this->request->getPost('idpembelajaran');
        // Validasi data jika diperlukan
        $id = $this->request->getPost('id');
        $result = [];
        for ($i = 0; $i < count($tujuan); $i++) {
            $data = [
                'id_pembelajaran' => $idpembelajaran,
                'tujuan' => $tujuan[$i],
                'id_guru' => $guru,
                'id_rombel' => $idrombel,
                'id_matapelajaran' => $idmapel,
                'status' => $status[$i]
            ];
            $dataupdate = [
                'tujuan' => $tujuan[$i],
                'status' => $status[$i]
            ];
            $where = ['id' => $id[$i]];
            if ($id[$i]) {
                $cektujuan = $this->Tujuan_model->cek_where($where);
                if (!$cektujuan) {
                    $this->Tujuan_model->tujuan_add($data);
                    $result[] = "Data berhasil ditambahkan";
                } else {
                    $this->Tujuan_model->tujuan_update($where, $dataupdate);
                    $result[] = "Data berhasil diperbarui";
                }
            } else {
                $this->Tujuan_model->tujuan_add($data);
                $result[] = "Data berhasil ditambahkan";
            }
        }

        // Kirim respons
        return $this->response->setJSON(['success' => true, 'message' => 'Berhasil disimpan']);
    }
    public function getPembelajaran()
    {

        $exec = $this->Tujuan_model->get_all_pembelajaran($this->appSetting()->tahunpelajaran, cekRoleid());
        $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_Tujuan' => $this->request->getPost('nama'),
                    'status' => $this->request->getPost('status'),
                );

                $insert = $this->Tujuan_model->Tujuan_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->Tujuan_model->get_by_id($id);
        echo json_encode($data);
    }

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

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

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