<?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_InformasiSPP_model $informasispp_model
 */

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

    public function index()
    {
        $data['title'] = 'SPP';
        // $data['informasispp'] = $this->informasispp_model->get_data('tb_informasispp')->result();
        $data['informasispp'] = $this->informasispp_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');
        $this->load->view('templates/footer');
    }

    public function tambah()
    {
        $data['title'] = 'Tambah Waktu Pembayaran';
        $data['informasispp'] = $this->informasispp_model->get_data('tb_informasispp')->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', $data);
        $this->load->view('templates/footer');
    }

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

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

            // Cek apakah kombinasi bulan & tahun sudah ada
            $cek = $this->db->get_where('tb_informasispp', [
                'bulan' => $bulan,
                'tahun' => $tahun
            ])->num_rows();

            if ($cek > 0) {
                // Jika data sudah ada, tampilkan pesan error
                $this->session->set_flashdata('pesan', '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                Informasi SPP 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('informasispp/tambah');
            } else {
                // Jika belum ada, lanjutkan menyimpan data
                $data = array(
                    'bulan' => $bulan,
                    'tahun' => $tahun,
                    'jumlah_pembayaran' => $this->input->post('jumlah_pembayaran'),
                    'jatuh_tempo' => $this->input->post('jatuh_tempo')
                );

                $this->informasispp_model->insert_data($data, 'tb_informasispp');
                $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('informasispp');
            }
        }
    }


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

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

            // Cek apakah kombinasi bulan dan tahun sudah ada di data lain (bukan data yang sedang diedit)
            $this->db->where('bulan', $bulan);
            $this->db->where('tahun', $tahun);
            $this->db->where('id_spp !=', $id_spp); // pengecualian untuk data yang sedang diedit
            $cek = $this->db->get('tb_informasispp')->num_rows();

            if ($cek > 0) {
                // Jika kombinasi bulan & tahun sudah ada di data lain, tampilkan pesan
                $this->session->set_flashdata('pesan', '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                Informasi SPP 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('informasispp');
            } else {
                // Jika aman, lanjut update
                $data = array(
                    'id_spp' => $id_spp,
                    'bulan' => $bulan,
                    'tahun' => $tahun,
                    'jumlah_pembayaran' => $this->input->post('jumlah_pembayaran'),
                    'jatuh_tempo' => $this->input->post('jatuh_tempo')
                );

                $this->informasispp_model->update_data($data, 'tb_informasispp');
                $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('informasispp');
            }
        }
    }


    public function _rules()
    {
        $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!!'
        ));
    }

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

        $this->informasispp_model->delete($where, 'tb_informasispp');
        $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');
    }
}
