<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Santri_model extends CI_Model
{
    public function get_data($table)
    {
        return $this->db->get($table);
    }

    public function insert_data($data, $table)
    {
        $this->db->insert($table, $data);
    }

    public function check_nis_exists($nis)
    {
        $this->db->where('NIS', $nis);
        $query = $this->db->get('tb_santri');

        return $query->num_rows() > 0;
    }

    public function update_data($data, $table)
    {
        $this->db->where('id_santri', $data['id_santri']);
        $this->db->update($table, $data);
    }
    public function get_data_by_id($id_santri)
    {
        return $this->db->get_where('tb_santri', array('id_santri' => $id_santri))->row();
    }

    public function get_santri_by_id($id_santri)
    {
        $this->db->where('id_santri', $id_santri);
        $query = $this->db->get('tb_santri');
        return $query->row(); // Mengembalikan satu baris data
    }

    public function get_data_by_id_santri($id_santri)
    {
        $this->db->where('id_santri', $id_santri);
        $santri = $this->db->get('tb_santri')->row_array();

        if ($santri) {
            // tambahkan pelanggaran juga kalau dibutuhkan
            $this->db->where('id_santri', $id_santri);
            $pelanggaran = $this->db->get('tb_pelanggaran_santri')->result_array();

            return [
                "santri" => $santri,
                "pelanggaran_santri" => $pelanggaran
            ];
        }

        return null;
    }


    public function get_data_by_nis($NIS)
    {
        // Ambil data santri dan wali
        $this->db->select('
        tb_santri.*,
        tb_wali_santri.id_wali_santri,
        tb_wali_santri.nama_wali_santri,
        tb_wali_santri.alamat AS alamat_wali,
        tb_wali_santri.no_telepon,
        tb_wali_santri.pekerjaan,
        tb_wali_santri.email AS email_wali,
        tb_wali_santri.password AS password_wali
    ');
        $this->db->from('tb_santri');
        $this->db->join('tb_wali_santri', 'tb_wali_santri.id_santri = tb_santri.id_santri', 'left');
        $this->db->where('tb_santri.NIS', $NIS);
        $result = $this->db->get()->row();

        if (!$result) {
            return null;
        }

        $santri_data = [
            'santri' => [
                'id_santri' => $result->id_santri,
                'NIS' => $result->NIS,
                'nama_santri' => $result->nama_santri,
                'kelas_diniyah' => $result->kelas_diniyah,
                'status_santri' => $result->status_santri,
                'kondisi_kesehatan' => $result->kondisi_kesehatan,
                'alamat_santri' => $result->alamat_santri,
                'tempat_lahir' => $result->tempat_lahir,
                'tanggal_lahir' => $result->tanggal_lahir,
                'tahun_masuk' => $result->tahun_masuk,
                'tahun_keluar' => $result->tahun_keluar,
                'keterangan_keluar' => $result->keterangan_keluar,
            ],
            'wali_santri' => [
                'id_wali_santri' => $result->id_wali_santri,
                'nama_wali_santri' => $result->nama_wali_santri,
                'alamat' => $result->alamat_wali,
                'no_telepon' => $result->no_telepon,
                'pekerjaan' => $result->pekerjaan,
                'email' => $result->email_wali,
                'password' => $result->password_wali
            ],
            'prestasi_santri' => [],
            'pelanggaran_santri' => [],
            'informasi_spp_santri' => []
        ];

        // Ambil prestasi
        $this->db->select('*');
        $this->db->from('tb_prestasi_santri');
        $this->db->where('id_santri', $result->id_santri);
        $prestasi = $this->db->get()->result();

        foreach ($prestasi as $row) {
            $santri_data['prestasi_santri'][] = [
                'nama_santri' => $row->nama_santri,
                'prestasi' => $row->prestasi,
                'agenda' => $row->agenda,
                'perlombaan' => $row->perlombaan,
                'tanggal' => $row->tanggal,
                'bukti_prestasi' => $row->bukti_prestasi,
            ];
        }

        // Ambil pelanggaran
        $this->db->select('nama_santri, pelanggaran, tanggal, sanksi, keterangan AS keterangan_pelanggaran');
        $this->db->from('tb_pelanggaran_santri');
        $this->db->where('id_santri', $result->id_santri);
        $pelanggaran = $this->db->get()->result();

        foreach ($pelanggaran as $row) {
            $santri_data['pelanggaran_santri'][] = [
                'nama_santri' => $row->nama_santri,
                'pelanggaran' => $row->pelanggaran,
                'tanggal' => $row->tanggal,
                'sanksi' => $row->sanksi,
                'keterangan' => $row->keterangan_pelanggaran
            ];
        }

        // Ambil informasi spp
        $this->db->select('nama_santri, bulan, tahun, jumlah_pembayaran, jatuh_tempo, keterangan AS keterangan_spp');
        $this->db->from('tb_informasi_spp_santri');
        $this->db->where('id_santri', $result->id_santri);
        $spp_result = $this->db->get()->result();

        foreach ($spp_result as $row) {
            $santri_data['informasi_spp_santri'][] = [
                'nama_santri' => $row->nama_santri,
                'bulan' => $row->bulan,
                'tahun' => $row->tahun,
                'jumlah_pembayaran' => $row->jumlah_pembayaran,
                'jatuh_tempo' => $row->jatuh_tempo,
                'keterangan' => $row->keterangan_spp
            ];
        }

        return $santri_data;
    }


    public function get_data_by_email_wali($email)
    {
        // Ambil data santri dan wali
        $this->db->select('
        tb_santri.*,
        tb_wali_santri.id_wali_santri,
        tb_wali_santri.nama_wali_santri,
        tb_wali_santri.alamat AS alamat_wali,
        tb_wali_santri.no_telepon,
        tb_wali_santri.pekerjaan,
        tb_wali_santri.email AS email_wali,
        tb_wali_santri.password AS password_wali
    ');
        $this->db->from('tb_santri');
        $this->db->join('tb_wali_santri', 'tb_wali_santri.id_santri = tb_santri.id_santri', 'left');
        $this->db->where('tb_wali_santri.email', $email);
        $result = $this->db->get()->row();
        // echo $this->db->last_query();

        if (!$result) {
            return null;
        }

        $santri_data = [
            'santri' => [
                'id_santri' => $result->id_santri,
                'NIS' => $result->NIS,
                'nama_santri' => $result->nama_santri,
                'kelas_diniyah' => $result->kelas_diniyah,
                'status_santri' => $result->status_santri,
                'kondisi_kesehatan' => $result->kondisi_kesehatan,
                'alamat_santri' => $result->alamat_santri,
                'tempat_lahir' => $result->tempat_lahir,
                'tanggal_lahir' => $result->tanggal_lahir,
                'tahun_masuk' => $result->tahun_masuk,
                'tahun_keluar' => $result->tahun_keluar,
                'keterangan_keluar' => $result->keterangan_keluar,
            ],
            'wali_santri' => [
                'id_wali_santri' => $result->id_wali_santri,
                'nama_wali_santri' => $result->nama_wali_santri,
                'alamat' => $result->alamat_wali,
                'no_telepon' => $result->no_telepon,
                'pekerjaan' => $result->pekerjaan,
                'email' => $result->email_wali,
                'password' => $result->password_wali
            ],
            'prestasi_santri' => [],
            'pelanggaran_santri' => [],
            'informasi_spp_santri' => []
        ];

        // Ambil prestasi
        $this->db->select('*');
        $this->db->from('tb_prestasi_santri');
        $this->db->where('id_santri', $result->id_santri);
        $prestasi = $this->db->get()->result();

        foreach ($prestasi as $row) {
            $santri_data['prestasi_santri'][] = [
                'nama_santri' => $row->nama_santri,
                'prestasi' => $row->prestasi,
                'agenda' => $row->agenda,
                'perlombaan' => $row->perlombaan,
                'tanggal' => $row->tanggal,
                'bukti_prestasi' => $row->bukti_prestasi,
            ];
        }

        // Ambil pelanggaran 
        $this->db->select('nama_santri, pelanggaran, tanggal, sanksi, keterangan AS keterangan_pelanggaran');
        $this->db->from('tb_pelanggaran_santri');
        $this->db->where('id_santri', $result->id_santri);
        $pelanggaran = $this->db->get()->result();

        foreach ($pelanggaran as $row) {
            $santri_data['pelanggaran_santri'][] = [
                'nama_santri' => $row->nama_santri,
                'pelanggaran' => $row->pelanggaran,
                'tanggal' => $row->tanggal,
                'sanksi' => $row->sanksi,
                'keterangan' => $row->keterangan_pelanggaran
            ];
        }

        // Ambil informasi spp
        $this->db->select('nama_santri, bulan, tahun, jumlah_pembayaran, jatuh_tempo, keterangan AS keterangan_spp');
        $this->db->from('tb_informasi_spp_santri');
        $this->db->where('id_santri', $result->id_santri);
        $spp_result = $this->db->get()->result();

        foreach ($spp_result as $row) {
            $santri_data['informasi_spp_santri'][] = [
                'nama_santri' => $row->nama_santri,
                'bulan' => $row->bulan,
                'tahun' => $row->tahun,
                'jumlah_pembayaran' => $row->jumlah_pembayaran,
                'jatuh_tempo' => $row->jatuh_tempo,
                'keterangan' => $row->keterangan_spp
            ];
        }

        return $santri_data;
    }

    public function delete($where, $table)
    {
        $this->db->where($where);
        $this->db->delete($table);
    }
}
