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

class ControllerDataNotaKoperasi extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        //validasi jika user belum login
        check_not_login();
        if ($this->session->userdata('level') != 1) {
            redirect();
        }
        $this->set_timezone();
        $this->load->library('encrypt');
        $this->load->library('form_validation');
        $this->load->library('Mycustom');
        $this->load->library('pdfgenerator');
        $this->load->library('Notifikasi');
        $this->load->model('bagian_owner/insert_model');
        $this->load->model('bagian_owner/select_model');
        $this->load->model('bagian_owner/update_model');
    }
    public function set_timezone()
    {
        date_default_timezone_set("Asia/Jakarta");
    }
    public function index()
    {
        $notif                 = $this->notifikasi->notif_owner();
        // Data Login
        $data_login            = $this->userlogin->user_login(); // Menampilkan siapa yang login
        $data_id_login         = $this->session->userdata('isloggedin'); // Menampilkan data session
        // Data Isian Konten
        // Query SQL Data atau isi data page
        // Menampilkan saldo pertama koperasi 
        $data_table          = $this->db->get_where('p_koperasi_nota', ['nota_id' => 0])->result();
        $jml_data            = count($data_table);
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'database/nota/koperasi',
            'page'                => 'index',
            'judul'               => 'Kelola Database Bagian Nota Koperasi || Bagian Pengurus',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Isi data
            'jml_data'            => $jml_data,
            'data_table'          => $data_table
        );
        $this->load->view('owner/index', $data);
    }
    // Crud
    public function crud()
    {
        // Cek Filter
        if (isset($_POST['cari_nota'])) :
            $this->_dataNomorNota();
        endif;
        if (isset($_POST['updateDatabaseTerbaru'])) :
            $data = array(
                'bayar' => htmlspecialchars($this->input->post('bayar')),
                'status_bayar' => htmlspecialchars($this->input->post('status_bayar'))
            );
            $this->db->where('nota_id', htmlspecialchars($this->input->post('nota_id')));
            $this->db->where('angsuran_id', htmlspecialchars($this->input->post('angsuran_id')));
            $this->db->update('p_koperasi_nota', $data);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data!</div>');
            redirect('owner/database/nota/detail_koperasi');
        endif;
        if (isset($_POST['deleteDatabaseTerbaru'])) :
            $this->_cekDetailKoperasi();
            $this->db->where('nota_id', htmlspecialchars($this->input->post('nota_id')));
            $this->db->where('angsuran_id', htmlspecialchars($this->input->post('angsuran_id')));
            $this->db->delete('p_koperasi_nota');
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Hapus Data!</div>');
            redirect('owner/database/nota/detail_koperasi');
        endif;
    }
    // Melihat data Nota berdasarkan Nomor Nota
    private function _dataNomorNota()
    {
        $nomor_nota            = htmlspecialchars($this->input->post('nomor_nota'));
        $notif                 = $this->notifikasi->notif_owner();
        // Data Login
        $data_login            = $this->userlogin->user_login(); // Menampilkan siapa yang login
        $data_id_login         = $this->session->userdata('isloggedin'); // Menampilkan data session
        // Data Isian Konten
        // Query SQL Data atau isi data page
        // Menampilkan saldo pertama koperasi 
        $data_table          = $this->db->get_where('p_koperasi_nota', ['nota_id' => $nomor_nota])->result();
        $jml_data            = count($data_table);
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'database/nota/koperasi',
            'page'                => 'index',
            'judul'               => 'Kelola Database Bagian Nota Koperasi || Bagian Pengurus',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Isi data
            'jml_data'            => $jml_data,
            'data_table'          => $data_table
        );
        $this->load->view('owner/index', $data);
    }
    // APakah bagian detail nota koperasi sudah dihapus
    private function _cekDetailKoperasi()
    {
        $angsuran_id = htmlspecialchars($this->input->post('angsuran_id'));
        $cekAngsuranID = $this->db->get_where('p_koperasi_angsuran', ['id' => $angsuran_id])->row_array();
        if ($cekAngsuranID['status_angsuran'] != 'BELUM') :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Detail Angsuran Koperasi Belum Di Ubah!</div>');
            redirect('owner/database/nota/detail_koperasi');
        endif;
    }
}

/* End of file ControllerDataNotaKoperasi.php and path \application\controllers\owner\database\nota\ControllerDataNotaKoperasi.php */