<?php

namespace App\Controllers;

use App\Models\Projek_model;
use App\Models\Rapor_model;


class Projek extends BaseController
{
    public function __construct()
    {
        $this->Projek_model = new Projek_model();
    }
    private $Projek_model;
    public function index()
    {
        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Perencanaan Projek',
            'tema' => $this->Projek_model->get_tema(),
            'rombel' => $this->Projek_model->get_rombel(cekRoleid(), $this->appSetting()->tahunpelajaran),
            'dimensi' => $this->Projek_model->get_dimensi()
        ];
        return view('Admin/Projek/perencanaan', $data);
    }
    public function getPerencanaan()
    {
        $data['data'] = $this->Projek_model->get_all_projek(cekRoleid(), $this->appSetting()->tahunpelajaran);
        return $this->response->setJSON($data);
    }
    public function getDimensi()
    {
        $data = $this->Projek_model->get_dimensi();
        return $this->response->setJSON($data);
    }
    public function getProjekId($id)
    {
        $data = $this->Projek_model->get_projek_id($id);
        return $this->response->setJSON($data);
    }
    public function addProjek()
    {
        $validationRules = [
            'tema' => 'required',
            'nama' => 'required',
            'deskripsi' => 'required',
        ];

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

            if ($this->validate($validationRules)) {
                $response['success'] = true;
                $data = array(
                    'nama_projek' => $this->request->getPost('nama'),
                    'tema' => $this->request->getPost('tema'),
                    'id_rombel' => $this->request->getPost('rombel'),
                    'id_dimensi' => implode(',', $this->request->getPost('dimensi')),
                    'deskripsi' => $this->request->getPost('deskripsi'),
                    'tahunpelajaran' => $this->appSetting()->tahunpelajaran
                );
                $insert = $this->Projek_model->projek_add($data);
                if ($insert) {
                    return $this->response->setJSON(['success' => true, 'message' => 'Projek Add successfully']);
                } else {
                    return $this->response->setStatusCode(404)->setJSON(['success' => false, 'message' => 'User not found']);
                }
            } else {
                $response['errors'] = $this->validator->getErrors();
            }
            return $this->response->setJSON($response);
        }
    }
    public function updateProjek()
    {

        $data = [
            'nama_projek' => $this->request->getPost('nama'),
            'tema' => $this->request->getPost('tema'),
            'id_rombel' => $this->request->getPost('rombel'),
            'id_dimensi' => implode(',', $this->request->getPost('dimensi')),
            'deskripsi' => $this->request->getPost('deskripsi'),
        ];

        $exec = $this->Projek_model->projek_update(['id' => $this->request->getPost('id')], $data);

        if ($exec) {
            return $this->response->setJSON(['success' => true, 'message' => 'User update successfully']);
        } else {
            return $this->response->setJSON(['success' => false, 'message' => 'Tidak ada data yang mengalami perubahan']);
        }
    }
    public function deleteProjek($id)
    {
        $this->Projek_model->delete_by_id($id);
        $this->Projek_model->delete_where('projek_penilaian', ['id_projek' => $id]);
        return $this->response->setJSON(['success' => true, 'message' => 'Projek deleted successfully']);
    }

    public function indexKoordinator()
    {
        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Perencanaan Projek',
        ];
        return view('Admin/Projek/koordinator', $data);
    }
    public function getRombel()
    {
        $data = $this->Projek_model->get_all_rombel(cekRoleid(), $this->appSetting()->tahunpelajaran);
        return $this->response->setJSON($data);
    }
    public function saveKoordinator()
    {
        $koor = $this->request->getPost('koor');

        // Simpan data nilai dan switch ke database menggunakan model
        foreach ($koor as $koor) {
            $where = ['id' => $koor['id_rombel']];
            $this->Projek_model->koor_update($where, ['koordinator' => $koor['koordinator']]);
        }

        // Kirim respons ke client
        return $this->response->setJSON(['status' => 'success', 'message' => 'Data berhasil disimpan']);
    }
}
