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

class ControllerDataKontrak 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('anggota', ['nik' => 0])->result();
        $jml_data            = count($data_table);
        // Data tambahan
        $data_aturan         = $this->db->get_where('p_koperasi_aturan')->result();
        $data_produk         = $this->db->get_where('sistem_produk_koperasi')->result();
        $data_petugas        = $this->db->get_where('sistem_login')->result();
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'database/koperasi/kontrak',
            'page'                => 'index',
            'judul'               => 'Kelola Database Bagian Koperasi Kontrak || Bagian Pengurus',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Isi data
            'jml_data'            => $jml_data,
            'data_table'          => $data_table,
            // Data tambahan
            'data_aturan'         => $data_aturan,
            'data_produk'         => $data_produk,
            'data_petugas'        => $data_petugas
        );
        $this->load->view('owner/index', $data);
    }
    // Crud
    public function crud()
    {
        // Cek Filter
        if (isset($_POST['cari_nota'])) :
            $this->_dataNomorNIK();
        endif;
        if (isset($_POST['updateDatabaseTerbaru'])) :
            $data = array(
                'aturan_id'    => htmlspecialchars($this->input->post('aturan_id')),
                'pokok_pinjaman'    => htmlspecialchars($this->input->post('pokok_pinjaman')),
                'bsr_admin'        => htmlspecialchars($this->input->post('bsr_admin')),
                'bsr_rk' => htmlspecialchars($this->input->post('bsr_rk')),
                'bsr_pokok' => htmlspecialchars($this->input->post('bsr_pokok')),
                'bsr_simpanan' => htmlspecialchars($this->input->post('bsr_simpanan')),
                'bsr_jasa' => htmlspecialchars($this->input->post('bsr_jasa')),
                'bsr_angsuran' => htmlspecialchars($this->input->post('bsr_angsuran')),
                'tempo_pinjaman' => htmlspecialchars($this->input->post('tempo_pinjaman')),
                'ttl_bayar' => htmlspecialchars($this->input->post('ttl_bayar')),
                'jaminan' => htmlspecialchars($this->input->post('jaminan')),
                'status_kontrak' => htmlspecialchars($this->input->post('status_kontrak')),
                'login_id' => htmlspecialchars($this->input->post('login_id')),
                'update_at' => date('Y-m-d H:i:s')
            );
            $this->db->where('id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->update('p_koperasi_kontrak', $data);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data!</div>');
            redirect('owner/database/koperasi/kontrak');
        endif;
        if (isset($_POST['deleteDatabaseTerbaru'])) :
            $this->_cekDetailKoperasi();
            $this->db->where('id', htmlspecialchars($this->input->post('kontrak_id')));
            $this->db->delete('p_koperasi_kontrak');
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Hapus Data!</div>');
            redirect('owner/database/koperasi/kontrak');
        endif;
    }
    // Melihat data Nota berdasarkan Nomor Nota
    private function _dataNomorNIK()
    {
        $anggota_id            = htmlspecialchars($this->input->post('anggota_id'));
        $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 
        if (htmlspecialchars($this->input->post('status_kontrak')) == 1) :
            # code...
            $data_table          = $this->db->get_where('p_koperasi_kontrak', ['anggota_id' => $anggota_id])->result();
        else:
            $data_table          = $this->db->get_where('p_koperasi_kontrak', ['id' => $anggota_id])->result();
        endif;
        $jml_data            = count($data_table);
        // Data tambahan
        $data_aturan         = $this->db->get_where('p_koperasi_aturan')->result();
        $data_produk         = $this->db->get_where('sistem_produk_koperasi')->result();
        $data_petugas        = $this->db->get_where('sistem_login')->result();
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'database/koperasi/kontrak',
            'page'                => 'index',
            'judul'               => 'Kelola Database Bagian Koperasi Kontrak || Bagian Pengurus',
            'data_id_login'       => $data_id_login,
            'data_login'          => $data_login,
            'notif'               => $notif,
            // Isi data
            'jml_data'            => $jml_data,
            'data_table'          => $data_table,
            // Data tambahan
            'data_aturan'         => $data_aturan,
            'data_produk'         => $data_produk,
            'data_petugas'        => $data_petugas
        );
        $this->load->view('owner/index', $data);
    }
    // APakah bagian detail nota koperasi sudah dihapus
    private function _cekDetailKoperasi()
    {
        $kontrak_id = htmlspecialchars($this->input->post('kontrak_id'));
        // Cek Table Jaminan
        $cekJaminan = $this->db->get_where('p_koperasi_agungan', ['kontrak_id' => $kontrak_id])->row_array();
        if ($cekJaminan != NULL) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Hapus Data pada Table Jaminan!</div>');
            redirect('owner/database/koperasi/kontrak');
        endif;
        // Cek Table Progress
        $cekProgress = $this->db->get_where('p_koperasi_progres', ['kontrak_id' => $kontrak_id])->row_array();
        if ($cekProgress != NULL) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Hapus Data pada Table Progress!</div>');
            redirect('owner/database/koperasi/kontrak');
        endif;
        // Cek Table Progress
        $cekAngsuran = $this->db->get_where('p_koperasi_angsuran', ['kontrak_id' => $kontrak_id])->row_array();
        if ($cekAngsuran != NULL) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Hapus Data pada Table Angsuran!</div>');
            redirect('owner/database/koperasi/kontrak');
        endif;
    }
}

/* End of file ControllerDataKontrak.php and path \application\controllers\owner\database\koperasi\ControllerDataKontrak.php */