<?php

namespace App\Controllers;

use App\Models\Rombel_model;
use App\Models\Matapelajaran_model;

class Rombel extends BaseController
{
    public function __construct()
    {
        $this->Rombel_model = new Rombel_model();
        $this->Matapelajaran_model = new Matapelajaran_model();
    }
    private $Rombel_model;
    private $Matapelajaran_model;
    public function index()
    {
        // $ceklogin = cekLogin();
        // if ($ceklogin) return $ceklogin;
        $tingkat = $this->Rombel_model->get_tingkat();
        $setting = $this->appSetting();
        $data = [
            // 'nama_rombel' => $rombel->nama,
            'setting' => $setting,
            'tingkat' => $tingkat,
            'matapelajaran' => $this->Matapelajaran_model->get_all_matapelajaran(),
            'guru' => $this->Rombel_model->get_wali(),
            'title' => 'Rombongan Belajar'
        ];
        return view('Admin/Referensi/rombel', $data);
    }
    public function getAnggota($idrombel)
    {
        $anggota = $this->Rombel_model->get_anggota($idrombel, $this->appSetting()->tahunpelajaran);

        return $this->response->setJSON($anggota);
    }
    public function addanggota()
    {
        if ($this->request->isAJAX()) {
            $tag = $this->request->getPost('tagify');
            $id_rombel = $this->request->getPost('id_rombel');

            foreach ($tag as $pengguna) {
                // Misalnya, simpan setiap pengguna ke dalam database
                $data = [
                    'id_siswa' => $pengguna['value'],
                    'id_rombel' => $id_rombel, // Sesuaikan dengan kolom di tabel Anda


                ];
                $cek = $this->Rombel_model->get_where_anggota($data);
                if (!$cek) {
                    $this->Rombel_model->rombel_addanggota($data);
                }
            }


            return $this->response->setJSON(['success' => true, 'message' => 'anggota Add successfully']);
        }
    }
    public function CopyPembelajaran()
    {
        if ($this->request->isAJAX()) {
            $rombel1 = $this->request->getPost('rombel1');
            $rombel2 = $this->request->getPost('rombel2');
            $this->Rombel_model->copy_pembelajaran($rombel1, $rombel2);
            return $this->response->setJSON(['success' => true, 'message' => 'Copy successfully']);
        }
    }
    public function saveMapping()
    {
        // Tangkap data dari form
        $matapelajaran = $this->request->getPost('mapel');
        $guru = $this->request->getPost('guru');
        $urut = $this->request->getPost('urut');
        $id_rombel = $this->request->getPost('idrombel');
        // Validasi data jika diperlukan
        // ...
        $result = [];
        for ($i = 0; $i < count($matapelajaran); $i++) {
            $data = [
                'id_rombel' => $id_rombel,
                'id_matapelajaran' => $matapelajaran[$i],
                'id_guru' => $guru[$i],
                'no_urut' => $urut[$i]
            ];
            $dataupdate = [
                'id_matapelajaran' => $matapelajaran[$i],
                'id_guru' => $guru[$i],
                'no_urut' => $urut[$i]
            ];
            $where = [
                'id_rombel' => $id_rombel,
                'id_matapelajaran' => $matapelajaran[$i],

            ];
            $cekmapping = $this->Rombel_model->cek_mapel_mapping($where);
            if (!$cekmapping) {
                $this->Rombel_model->pembelajaran_add($data);
                $result[] = "Data berhasil ditambahkan";
            } else {
                $this->Rombel_model->pembelajaran_update($where, $dataupdate);
                $result[] = "Data berhasil diperbarui";
            }
        }

        // Kirim respons
        return $this->response->setJSON(['success' => true, 'message' => 'Berhasil disimpan']);
    }
    public function deleteMapping($id)
    {
        $mapping = $this->Rombel_model->get_mapping_by_id($id);
        if ($mapping) {
            $this->Rombel_model->delete_mapping($id);
            return $this->response->setJSON(['success' => true, 'message' => 'Mapping deleted successfully']);
        } else {
            return $this->response->setStatusCode(404)->setJSON(['success' => false, 'message' => 'Mapping not found']);
        }
    }
    public function getData()
    {
        $exec = $this->Rombel_model->get_all_Rombel($this->appSetting()->tahunpelajaran);
        $data['data'] = $exec;
        return $this->response->setJSON($data);
    }
    public function getWalas()
    {
        $wali = $this->Rombel_model->get_wali();
        return $this->response->setJSON($wali);
    }
    public function getMapel()
    {
        $exec = $this->Matapelajaran_model->get_all_Matapelajaran();

        return $this->response->setJSON($exec);
    }
    public function getMapping($id)
    {
        $exec = $this->Rombel_model->get_mapel_mapping($id);

        return $this->response->setJSON($exec);
    }
    public function add()
    {
        $validationRules = [
            'kode' => 'required',
            'nama' => 'required',
            'tingkat' => 'required',
        ];

        if ($this->request->isAJAX()) {
            $response = ['success' => false, 'errors' => []];
            if ($this->validate($validationRules)) {
                $response['success'] = true;
                $data = array(
                    'kode_rombel' => $this->request->getPost('kode'),
                    'nama_rombel' => $this->request->getPost('nama'),
                    'tingkat' => $this->request->getPost('tingkat'),
                    'jenis' => $this->request->getPost('jenis'),
                    'walikelas' => $this->request->getPost('walikelas'),
                    'status' => $this->request->getPost('status'),
                    'tahunpelajaran' => $this->appSetting()->tahunpelajaran
                );

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

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

    public function update()
    {
        $data = array(
            'kode_rombel' => $this->request->getPost('kode'),
            'nama_rombel' => $this->request->getPost('nama'),
            'jenis' => $this->request->getPost('jenis'),
            'tingkat' => $this->request->getPost('tingkat'),
            'walikelas' => $this->request->getPost('walikelas'),
            'status' => $this->request->getPost('status'),
        );

        $exec = $this->Rombel_model->Rombel_update(['id' => $this->request->getPost('id')], $data);
        // dd($error);
        if ($exec) {
            return $this->response->setJSON(['success' => true, 'message' => 'rombel 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)
    {
        $rombel = $this->Rombel_model->find($id);
        if ($rombel) {
            $this->Rombel_model->delete($id);
            return $this->response->setJSON(['success' => true, 'message' => 'rombel deleted successfully']);
        } else {
            return $this->response->setStatusCode(404)->setJSON(['success' => false, 'message' => 'rombel not found']);
        }
    }
    public function deleteAnggota($id)
    {
        $delete = $this->Rombel_model->delete_anggota($id);

        return $this->response->setJSON(['success' => true, 'message' => 'Anggota deleted successfully']);
    }
}
