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

class ControllerDataAlamat 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('sistem_alamat')->result();
        $jml_data   = count($data_table);
        $data = array(
            // Untuk memanggil halaman sesuai menu
            'folder'              => 'database/sistem/alamat',
            'page'                => 'index',
            'judul'               => 'Kelola Database Bagian Sistem Alamat || 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()
    {
        if (isset($_POST['updateDatabaseTerbaru'])) :
            $data = array(
                'nm_desa'       => htmlspecialchars($this->input->post('nm_desa')),
                'nm_kecamatan'  => htmlspecialchars($this->input->post('nm_kecamatan')),
                'kot_kab'       => htmlspecialchars($this->input->post('kot_kab')),
                'prov'          => htmlspecialchars($this->input->post('prov'))
            );
            $this->db->where('id', htmlspecialchars($this->input->post('alamat_id')));
            $this->db->update('sistem_alamat', $data);
            $this->session->set_flashdata('pesan_berhasil', '<div class="alert alert-success" id="pesan_berhasil" role="alert">Berhasil Merubah Data!</div>');
            redirect('owner/database/sistem/alamat');
        endif;
        if (isset($_POST['deleteDatabaseTerbaru'])) :
            // Cek data table Anggota
            $anggota = $this->db->get_where('anggota', ['id_alamat' => $this->input->post('alamat_id')])->row_array();
            if ($anggota != null) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Hapus Data Pada table ( anggota ).</div>');
                redirect('owner/database/sistem/alamat');
            endif;
            // Cek data table Anggota
            $sistem_identitas = $this->db->get_where('sistem_identitas', ['alamat_id' => $this->input->post('alamat_id')])->row_array();
            if ($sistem_identitas != null) :
                $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Hapus Data Pada table ( sistem_identitas ).</div>');
                redirect('owner/database/sistem/alamat');
            endif;
            $this->db->where('id', htmlspecialchars($this->input->post('alamat_id')));
            $this->db->delete('sistem_alamat');
        endif;
    }
}

/* End of file ControllerDataAlamat.php and path \application\controllers\owner\database\sistem\ControllerDataAlamat.php */