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

/**
 * @property CI_Db $db
 * @property CI_Session $session
 * @property CI_Form_validation $form_validation
 * @property CI_Input $input
 * @property CI_InformasiSPPSantri_model $informasi_spp_santri_model
 */

class InformasiSPPSantri extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('informasi_spp_santri_model');
        is_logged_in();
    }

    public function index()
    {
        $data['title'] = 'SPP Santri';
        // $data['informasispp'] = $this->informasispp_model->get_data('tb_informasispp')->result();
        $data['informasisppsantri'] = $this->informasi_spp_santri_model->get_informasispp();
        $data['admin'] = $this->db->get_where('admin', ['email' => $this->session->userdata('email')])->row_array();

        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('admin/informasi_spp_santri');
        $this->load->view('templates/footer');
    }

    public function tambah()
    {
        $data['title'] = 'Tambah Data SPP Santri';
        $data['informasisppsantri'] = $this->informasi_spp_santri_model->get_data('tb_informasi_spp_santri')->result();
        $data['admin'] = $this->db->get_where('admin', ['email' => $this->session->userdata('email')])->row_array();

        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('admin/tambah_informasi_spp_santri', $data);
        $this->load->view('templates/footer');
    }

    public function tambah_aksi()
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->tambah();
        } else {
            $id_santri = $this->input->post('id_santri');
            $bulan     = $this->input->post('bulan');
            $tahun     = $this->input->post('tahun');

            // Cek apakah data sudah ada
            $cek = $this->db->get_where('tb_informasi_spp_santri', [
                'id_santri' => $id_santri,
                'bulan' => $bulan,
                'tahun' => $tahun
            ])->num_rows();

            if ($cek > 0) {
                // Data duplikat ditemukan
                $this->session->set_flashdata('pesan', '<div class="alert alert-danger alert-dismissible fade show" role="alert">
            Data untuk santri ini di bulan dan tahun tersebut sudah pernah ditambahkan!
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button></div>');
                redirect('informasisppsantri/tambah');
            } else {
                // Data aman untuk disimpan
                $tanggal_input = $this->input->post('jatuh_tempo');
                $tanggal_formatted = date('Y-m-d', strtotime(str_replace('/', '-', $tanggal_input)));
                $data = array(
                    'id_santri' => $id_santri,
                    'NIS' => $this->input->post('NIS'),
                    'nama_santri' => $this->input->post('nama_santri'),
                    'kelas_diniyah' => intval($this->input->post('kelas_diniyah')),
                    'bulan' => $bulan,
                    'tahun' => $tahun,
                    'jumlah_pembayaran' => $this->input->post('jumlah_pembayaran'),
                    'jatuh_tempo' => $tanggal_formatted,
                    'keterangan' => $this->input->post('keterangan')
                );

                $this->informasi_spp_santri_model->insert_data($data, 'tb_informasi_spp_santri');
                $this->session->set_flashdata('pesan', '<div class="alert alert-success alert-dismissible fade show" role="alert">
            Data berhasil ditambahkan!
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span>
            </button></div>');
                redirect('informasisppsantri');
            }
        }
    }


    public function get_nis_autocomplete()
    {
        $term = $this->input->get('term');
        $this->db->like('NIS', $term);
        $this->db->or_like('nama_santri', $term);
        $query = $this->db->get('tb_santri');

        $result = [];
        $kelas_diniyah = [
            1 => 'Al-Wadhih Banin',
            2 => 'Al-Wadhih Banat',
            3 => 'Al-Jurūmiyyah Banin',
            4 => 'Al-Jurūmiyyah Banat',
            5 => 'Al-Imriti Banin',
            6 => 'Al-Imriti Banat',
            7 => 'Alfiyah Ula Banin',
            8 => 'Alfiyah Ula Banat',
            9 => 'Alfiyah Tsani Banin',
            10 => 'Alfiyah Tsani Banat',
            11 => 'Alfiyah Tsalits Banin',
            12 => 'Alfiyah Tsalits Banat'
        ];
        foreach ($query->result() as $row) {
            $kelas_diniyah_text = isset($kelas_diniyah[$row->kelas_diniyah]) ? $kelas_diniyah[$row->kelas_diniyah] : 'Tidak Diketahui';
            $result[] = [
                'label' => $row->NIS . ' - ' . $row->nama_santri . ' (' . $kelas_diniyah_text . ')',
                'value' => $row->NIS,
                'nama_santri' => $row->nama_santri,
                'id_santri' => $row->id_santri,
                'kelas_diniyah' => $kelas_diniyah_text, // Kirim teksnya ke JS, bukan angkanya
                'kelas_diniyah_id' => $row->kelas_diniyah // untuk disimpan ke DB
            ];
        }

        echo json_encode($result);
    }

    public function view($id_santri)
    {
        $data['title'] = 'Informasi SPP Santri';
        $data['santri'] = $this->db->get_where('tb_santri', ['id_santri' => $id_santri])->row();
        $data['informasisppsantri'] = $this->informasi_spp_santri_model->get_informasi_spp_by_santri($id_santri);
        $data['admin'] = $this->db->get_where('admin', ['email' => $this->session->userdata('email')])->row_array();

        $this->load->view('templates/header');
        $this->load->view('templates/sidebar', $data);
        $this->load->view('admin/detail_informasi_spp_santri', $data);
        $this->load->view('templates/footer');
    }

    public function edit($id_spp_santri)
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->index();
        } else {
            $id_santri = $this->input->post('id_santri');
            $bulan = $this->input->post('bulan');
            $tahun = $this->input->post('tahun');

            // Cek apakah data dengan id_santri, bulan, tahun sudah ada, tapi dengan id_spp_santri yang berbeda
            $this->db->where('id_santri', $id_santri);
            $this->db->where('bulan', $bulan);
            $this->db->where('tahun', $tahun);
            $this->db->where('id_spp_santri !=', $id_spp_santri); // pengecualian untuk record ini
            $cek = $this->db->get('tb_informasi_spp_santri')->row();

            if ($cek) {
                // Jika sudah ada data lain dengan kombinasi itu
                $this->session->set_flashdata('pesan', '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                Data untuk santri tersebut pada bulan dan tahun ini sudah ada! <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span></button></div>');
                redirect('informasisppsantri');
            } else {
                // Aman, lanjut update
                $tanggal_input = $this->input->post('jatuh_tempo');
                $tanggal_formatted = date('Y-m-d', strtotime(str_replace('/', '-', $tanggal_input)));
                $data = array(
                    'id_spp_santri' => $id_spp_santri,
                    'NIS' => $this->input->post('NIS'),
                    'nama_santri' => $this->input->post('nama_santri'),
                    'kelas_diniyah' => $this->input->post('kelas_diniyah'),
                    'bulan' => $bulan,
                    'tahun' => $tahun,
                    'jumlah_pembayaran' => $this->input->post('jumlah_pembayaran'),
                    'jatuh_tempo' => $tanggal_formatted,
                    'keterangan' => $this->input->post('keterangan')
                );

                $this->db->where('id_spp_santri', $id_spp_santri);
                $this->db->update('tb_informasi_spp_santri', $data);

                $this->session->set_flashdata('pesan', '<div class="alert alert-success alert-dismissible fade show" role="alert">
                Data berhasil diubah! <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span></button></div>');
                redirect('informasisppsantri');
            }
        }
    }


    public function _rules()
    {
        $this->form_validation->set_rules('NIS', 'NIS', 'required', array(
            'required' => '%s harus diisi!!'
        ));
        $this->form_validation->set_rules('nama_santri', 'Nama Santri', 'required', array(
            'required' => '%s harus diisi!!'
        ));
        $this->form_validation->set_rules('kelas_diniyah', 'Kelas Diniyah', 'required|numeric|greater_than[0]', array(
            'required' => '%s harus diisi!!'
        ));
        $this->form_validation->set_rules('bulan', 'Bulan', 'required', array(
            'required' => '%s harus diisi!!'
        ));
        $this->form_validation->set_rules('tahun', 'Tahun', 'required', array(
            'required' => '%s harus diisi!!'
        ));
        $this->form_validation->set_rules('jumlah_pembayaran', 'Jumlah Pembayaran', 'required', array(
            'required' => '%s harus diisi!!'
        ));
        $this->form_validation->set_rules('jatuh_tempo', 'Jatuh Tempo', 'required', array(
            'required' => '%s harus diisi!!'
        ));
        $this->form_validation->set_rules('keterangan', 'Keterangan', 'required', array(
            'required' => '%s harus diisi!!'
        ));
    }

    public function delete($id)
    {
        $where = array('id_spp_santri' => $id);

        $this->informasi_spp_santri_model->delete($where, 'tb_informasi_spp_santri');
        $this->session->set_flashdata('pesan', '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                Data berhasil dihapus! <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span>
                </button></div>');
        redirect('informasispp');
    }
}
