<?php

namespace App\Controllers;

use App\Models\Ekstrakulikuler_model;


class Ekstrakulikuler extends BaseController
{
    public function __construct()
    {
        $this->Ekstrakulikuler_model = new Ekstrakulikuler_model();
    }
    private $Ekstrakulikuler_model;
    public function index()
    {
        // $ceklogin = cekLogin();
        // if ($ceklogin) return $ceklogin;
        // $user = getUserInfo();
        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Ekstrakulikuler'
        ];
        if (cekRoleadmin() == 'admin') {
            return view('Admin/Referensi/ekstrakulikuler', $data);
        } else {
            return view('Admin/Referensi/ekstrakulikuler_guru', $data);
        }
    }
    public function getData()
    {
        $exec = $this->Ekstrakulikuler_model->get_all_Ekstrakulikuler($this->appSetting()->tahunpelajaran, cekRoleid());
        $data['data'] = $exec;
        return $this->response->setJSON($data);
    }
    public function getSiswa()
    {
        $anggota = $this->Ekstrakulikuler_model->get_siswa($this->appSetting()->tahunpelajaran);

        return $this->response->setJSON($anggota);
    }
    public function getAnggota($ideskul)
    {
        $anggota = $this->Ekstrakulikuler_model->get_anggota($ideskul, $this->appSetting()->tahunpelajaran);

        return $this->response->setJSON($anggota);
    }
    public function add()
    {
        $validationRules = [

            'nama' => 'required',
            'pembina' => 'required',
        ];

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

                $insert = $this->Ekstrakulikuler_model->Ekstrakulikuler_add($data);
                if ($insert) {
                    return $this->response->setJSON(['success' => true, 'message' => 'Ekstrakulikuler Add successfully']);
                } else {
                    return $this->response->setStatusCode(404)->setJSON(['success' => false, 'message' => 'Ekstrakulikuler not found']);
                }
            } else {
                $response['errors'] = $this->validator->getErrors();
            }
            return $this->response->setJSON($response);
        }
    }
    public function addanggota()
    {

        if ($this->request->isAJAX()) {

            $tag = $this->request->getPost('tagify');
            $id_eskul = $this->request->getPost('id_eskul');

            foreach ($tag as $pengguna) {

                $data2 = [
                    'id_siswa' => $pengguna['value'],
                    'id_eskul' => $id_eskul, // Sesuaikan dengan kolom di tabel Anda
                    'tahunpelajaran' => $this->appSetting()->tahunpelajaran
                ];

                $cek2 = $this->Ekstrakulikuler_model->get_where_anggota($data2);
                if (!$cek2) {
                    $this->Ekstrakulikuler_model->ekstrakulikuler_addpenilaian($data2);
                }
            }


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

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

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

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

        if ($user) {
            $this->Ekstrakulikuler_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']);
        }
    }
    public function deleteAnggota($id)
    {
        $delete = $this->Ekstrakulikuler_model->delete_anggota($id);

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