<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Pegawai extends CI_Controller
{


    function __construct()
    {
        parent::__construct();
        $this->load->model(array('Pegawai_model', 'Ion_auth_model'));
        $this->load->database();
        $this->load->library(array('ion_auth', 'form_validation', 'upload'));
        $this->load->helper(array('url', 'language'));

        $this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));

        $this->lang->load('auth');
    }

    public function index()
    {
        $q = urldecode($this->input->get('q', TRUE));
        $start = intval($this->input->get('start'));

        if ($q <> '') {
            $config['base_url'] = base_url() . 'pegawai/?q=' . urlencode($q);
            $config['first_url'] = base_url() . 'pegawai/?q=' . urlencode($q);
        } else {
            $config['base_url'] = base_url() . 'pegawai/';
            $config['first_url'] = base_url() . 'pegawai/';
        }

        $config['per_page'] = 5;
        $config['page_query_string'] = TRUE;
        $config['total_rows'] = $this->Pegawai_model->total_rows($q);
        $pegawai = $this->Pegawai_model->get_limit_data($config['per_page'], $start, $q);

        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $row = $this->Pegawai_model->get_style();

        $data = array(
            'pegawai_data' => $pegawai,
            'q' => $q,
            'pagination' => $this->pagination->create_links(),
            'total_rows' => $config['total_rows'],
            'start' => $start,
            'web_meta_color' => $row->meta_color,
            'web_navbar_color' => $row->navbar_color,
            'web_box_color' => $row->box_color,
            'web_box_bottom' => $row->box_bottom,
        );
        $this->template->load('template', 'pegawai/pegawai_list', $data);
    }

    public function read($id)
    {
        $id = decrypt_url($id);
        $row = $this->Pegawai_model->get_by_id($id);
        $rows = $this->Pegawai_model->get_style();
        if ($row) {
            $data = array(
                'id_pegawai' => $row->id_pegawai,
                'rfid_ktp' => $row->rfid_ktp,
                'nip' => $row->nip,
                'nik' => $row->nik,
                'nama_pegawai' => $row->nama_pegawai,
                'tempat_lahir' => $row->tempat_lahir,
                'tanggal_lahir' => $row->tanggal_lahir,
                'jenis_kelamin' => $row->jenis_kelamin,
                'agama' => $row->agama,
                'status_kawin' => $row->status_kawin,
                'telepon' => $row->telepon,
                'alamat' => $row->alamat,
                'jabatan' => $row->nama_jabatan,
                'foto_pegawai' => $row->foto_pegawai,
                'web_meta_color' => $rows->meta_color,
                'web_navbar_color' => $rows->navbar_color,
                'web_box_color' => $rows->box_color,
                'web_box_bottom' => $rows->box_bottom,
            );

            $this->template->load('template', 'pegawai/pegawai_read', $data);
        } else {
            $this->session->set_flashdata('message', '9797');
            redirect(site_url('pegawai'));
        }
    }

    public function create()
    {
        $row = $this->Pegawai_model->get_style();

        $data = array(
            'button' => 'Simpan',
            'action' => site_url('pegawai/create_action'),
            'id_pegawai' => set_value('id_pegawai'),
            'rfid_ktp' => set_value('rfid_ktp'),
            'nip' => set_value('nip'),
            'nik' => set_value('nik'),
            'nama_pegawai' => set_value('nama_pegawai'),
            'tempat_lahir' => set_value('tempat_lahir'),
            'tanggal_lahir' => set_value('tanggal_lahir'),
            'jenis_kelamin' => set_value('jenis_kelamin'),
            'agama' => set_value('agama'),
            'status_kawin' => set_value('status_kawin'),
            'telepon' => set_value('telepon'),
            'alamat' => set_value('alamat'),
            'jabatan' => set_value('jabatan'),
            'foto_pegawai' => set_value('foto_pegawai'),
            'web_meta_color' => $row->meta_color,
            'web_navbar_color' => $row->navbar_color,
            'web_box_color' => $row->box_color,
            'web_box_bottom' => $row->box_bottom,
        );

        $this->template->load('template', 'pegawai/pegawai_form', $data);
    }

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

        if ($this->form_validation->run() == FALSE) {
            $this->create();
        } else {
            $user = $this->ion_auth->user()->row();
            $id_user = $user->id;
            $nama_file = 'pegawai-' . date("YmdHis") . '-' . $id_user;

            $config['upload_path'] = './public/data/upload/';
            $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx|cdr|psd|png|bmp|jpg|jpeg';
            $config['file_name'] = $nama_file;

            $this->upload->initialize($config);
            if (!empty($_FILES['foto_pegawai']['name'])) {
                if ($this->upload->do_upload('foto_pegawai')) {
                    $file_upld = $this->upload->data();
                    $data = array(
                        'rfid_ktp' => $this->input->post('rfid_ktp', TRUE),
                        'nip' => $this->input->post('nip', TRUE),
                        'nik' => $this->input->post('nik', TRUE),
                        'nama_pegawai' => $this->input->post('nama_pegawai', TRUE),
                        'tempat_lahir' => $this->input->post('tempat_lahir', TRUE),
                        'tanggal_lahir' => $this->input->post('tanggal_lahir', TRUE),
                        'jenis_kelamin' => $this->input->post('jenis_kelamin', TRUE),
                        'agama' => $this->input->post('agama', TRUE),
                        'status_kawin' => $this->input->post('status_kawin', TRUE),
                        'telepon' => $this->input->post('telepon', TRUE),
                        'alamat' => $this->input->post('alamat', TRUE),
                        'jabatan' => $this->input->post('jabatan', TRUE),
                        'foto_pegawai' => $file_upld['file_name'],
                    );
                    $this->Pegawai_model->insert($data);
                    $this->session->set_flashdata('message', '1111');
                    redirect(site_url('pegawai'));
                } else {
                    $this->session->set_flashdata('message', '7373');
                    redirect(site_url('pegawai'));
                }
            } else {
                $data = array(
                    'rfid_ktp' => $this->input->post('rfid_ktp', TRUE),
                    'nip' => $this->input->post('nip', TRUE),
                    'nik' => $this->input->post('nik', TRUE),
                    'nama_pegawai' => $this->input->post('nama_pegawai', TRUE),
                    'tempat_lahir' => $this->input->post('tempat_lahir', TRUE),
                    'tanggal_lahir' => $this->input->post('tanggal_lahir', TRUE),
                    'jenis_kelamin' => $this->input->post('jenis_kelamin', TRUE),
                    'agama' => $this->input->post('agama', TRUE),
                    'status_kawin' => $this->input->post('status_kawin', TRUE),
                    'telepon' => $this->input->post('telepon', TRUE),
                    'alamat' => $this->input->post('alamat', TRUE),
                    'jabatan' => $this->input->post('jabatan', TRUE),
                );
                $this->Pegawai_model->insert($data);
                $this->session->set_flashdata('message', '1111');
                redirect(site_url('pegawai'));
            }
        }
    }

    public function update($id)
    {
        $id = decrypt_url($id);
        $row = $this->Pegawai_model->get_by_id($id);
        $rows = $this->Pegawai_model->get_style();

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('pegawai/update_action'),
                'id_pegawai' => set_value('id_pegawai', $row->id_pegawai),
                'rfid_ktp' => set_value('rfid_ktp', $row->rfid_ktp),
                'nip' => set_value('nip', $row->nip),
                'nik' => set_value('nik', $row->nik),
                'nama_pegawai' => set_value('nama_pegawai', $row->nama_pegawai),
                'tempat_lahir' => set_value('tempat_lahir', $row->tempat_lahir),
                'tanggal_lahir' => set_value('tanggal_lahir', $row->tanggal_lahir),
                'jenis_kelamin' => set_value('jenis_kelamin', $row->jenis_kelamin),
                'agama' => set_value('agama', $row->agama),
                'status_kawin' => set_value('status_kawin', $row->status_kawin),
                'telepon' => set_value('telepon', $row->telepon),
                'alamat' => set_value('alamat', $row->alamat),
                'jabatan' => set_value('jabatan', $row->jabatan),
                'foto_pegawai' => set_value('foto_pegawai', $row->foto_pegawai),
                'web_meta_color' => $rows->meta_color,
                'web_navbar_color' => $rows->navbar_color,
                'web_box_color' => $rows->box_color,
                'web_box_bottom' => $rows->box_bottom,
            );

            $this->template->load('template', 'pegawai/pegawai_form', $data);
        } else {
            $this->session->set_flashdata('message', '9797');
            redirect(site_url('pegawai'));
        }
    }

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

        if ($this->form_validation->run() == FALSE) {
            $this->update(encrypt_url($this->input->post('id_pegawai', TRUE)));
        } else {
            $data = array(
                'rfid_ktp' => $this->input->post('rfid_ktp', TRUE),
                'nip' => $this->input->post('nip', TRUE),
                'nik' => $this->input->post('nik', TRUE),
                'nama_pegawai' => $this->input->post('nama_pegawai', TRUE),
                'tempat_lahir' => $this->input->post('tempat_lahir', TRUE),
                'tanggal_lahir' => $this->input->post('tanggal_lahir', TRUE),
                'jenis_kelamin' => $this->input->post('jenis_kelamin', TRUE),
                'agama' => $this->input->post('agama', TRUE),
                'status_kawin' => $this->input->post('status_kawin', TRUE),
                'telepon' => $this->input->post('telepon', TRUE),
                'alamat' => $this->input->post('alamat', TRUE),
                'jabatan' => $this->input->post('jabatan', TRUE),
                'foto_pegawai' => $this->input->post('foto_pegawai', TRUE),
            );

            $this->Pegawai_model->update($this->input->post('id_pegawai', TRUE), $data);
            $this->session->set_flashdata('message', '2222');
            redirect(site_url('pegawai'));
        }
    }

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

        if ($this->form_validation->run() == FALSE) {
            $this->update(encrypt_url($this->input->post('id_pegawai', TRUE)));
        } else {
            $user = $this->ion_auth->user()->row();
            $id_user = $user->id;
            $nama_file = 'pegawai-' . date("YmdHis") . '-' . $id_user;

            $path = './public/data/upload/';

            $config['upload_path'] = './public/data/upload/';
            $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx|cdr|psd|png|bmp|jpg|jpeg';
            $config['file_name'] = $nama_file;

            $this->upload->initialize($config);

            if (!empty($_FILES['foto_pegawai']['name'])) {
                if ($this->upload->do_upload('foto_pegawai')) {
                    $file_upld = $this->upload->data();
                    $data = array(
                        'rfid_ktp' => $this->input->post('rfid_ktp', TRUE),
                        'nip' => $this->input->post('nip', TRUE),
                        'nik' => $this->input->post('nik', TRUE),
                        'nama_pegawai' => $this->input->post('nama_pegawai', TRUE),
                        'tempat_lahir' => $this->input->post('tempat_lahir', TRUE),
                        'tanggal_lahir' => $this->input->post('tanggal_lahir', TRUE),
                        'jenis_kelamin' => $this->input->post('jenis_kelamin', TRUE),
                        'agama' => $this->input->post('agama', TRUE),
                        'status_kawin' => $this->input->post('status_kawin', TRUE),
                        'telepon' => $this->input->post('telepon', TRUE),
                        'alamat' => $this->input->post('alamat', TRUE),
                        'jabatan' => $this->input->post('jabatan', TRUE),
                        'foto_pegawai' => $file_upld['file_name'],
                    );
                    @unlink($path . $this->input->post('filelama'));
                    $this->Pegawai_model->update($this->input->post('id_pegawai', TRUE), $data);
                    $this->session->set_flashdata('message', '2222');
                    redirect(site_url('pegawai'));
                } else {
                    $this->session->set_flashdata('message', '7373');
                    redirect(site_url('pegawai'));
                }
            } else {
                $data = array(
                    'rfid_ktp' => $this->input->post('rfid_ktp', TRUE),
                    'nip' => $this->input->post('nip', TRUE),
                    'nik' => $this->input->post('nik', TRUE),
                    'nama_pegawai' => $this->input->post('nama_pegawai', TRUE),
                    'tempat_lahir' => $this->input->post('tempat_lahir', TRUE),
                    'tanggal_lahir' => $this->input->post('tanggal_lahir', TRUE),
                    'jenis_kelamin' => $this->input->post('jenis_kelamin', TRUE),
                    'agama' => $this->input->post('agama', TRUE),
                    'status_kawin' => $this->input->post('status_kawin', TRUE),
                    'telepon' => $this->input->post('telepon', TRUE),
                    'alamat' => $this->input->post('alamat', TRUE),
                    'jabatan' => $this->input->post('jabatan', TRUE),
                );
                $this->Pegawai_model->update($this->input->post('id_pegawai', TRUE), $data);
                $this->session->set_flashdata('message', '2222');
                redirect(site_url('pegawai'));
            }
        }
    }

    public function delete($id)
    {
        $id = decrypt_url($id);
        $row = $this->Pegawai_model->get_by_id($id);

        if ($row) {
            $path = './public/data/upload/';
            @unlink($path . $row->foto_pegawai);
            $this->Pegawai_model->delete($id);
            $this->session->set_flashdata('message', '3333');
            redirect(site_url('pegawai'));
        } else {
            $this->session->set_flashdata('message', '9797');
            redirect(site_url('pegawai'));
        }
    }

    public function delete_list()
    {
        $path = './public/data/upload/';
        $id = $_POST['pilih'];
        $jml_dipilih = count($id);
        for ($x = 0; $x < $jml_dipilih; $x++) {
            $row[$x] = $this->Pegawai_model->get_by_id($id[$x]);
            @unlink($path . $row[$x]->foto_pegawai);
            $this->Pegawai_model->delete($id[$x]);
        }
        $this->session->set_flashdata('message', '3333');
        redirect(site_url('pegawai'));
    }

    public function import_data()
    {
        $user = $this->ion_auth->user()->row();
        $id_user = $user->id;
        $kode = "pegawai-" . date("Ymd") . "-" . $id_user;
        $this->load->library('upload');

        $config['upload_path'] = './public/data/import/';
        $config['allowed_types'] = 'xlsx';
        $config['max_size']    = '2048';
        $config['overwrite'] = true;
        $config['file_name'] = $kode;

        $this->upload->initialize($config);
        if ($this->upload->do_upload('file')) {
            include APPPATH . 'third_party/PHPExcel/PHPExcel.php';

            $excelreader = new PHPExcel_Reader_Excel2007();
            $loadexcel = $excelreader->load('public/data/import/' . $kode . '.xlsx');
            $sheet = $loadexcel->getActiveSheet()->toArray(null, true, true, true);

            $data = array();

            $numrow = 1;
            foreach ($sheet as $row) {
                if ($numrow > 1) {
                    array_push($data, array(
                        'rfid_ktp' => $row['A'],
                        'nip' => $row['B'],
                        'nik' => $row['C'],
                        'nama_pegawai' => $row['D'],
                        'tempat_lahir' => $row['E'],
                        'tanggal_lahir' => $row['F'],
                        'jenis_kelamin' => $row['G'],
                        'agama' => $row['H'],
                        'status_kawin' => $row['I'],
                        'telepon' => $row['J'],
                        'alamat' => $row['K'],
                        'jabatan' => $row['L'],
                        'foto_pegawai' => $row['M'],
                    ));
                }
                $numrow++;
            }

            $this->Pegawai_model->insert_multiple($data);
            $this->session->set_flashdata('message', '7575');
            redirect(site_url('pegawai'));
        } else {
            $this->session->set_flashdata('message', '7373');
            redirect(site_url('pegawai'));
        }
    }

    public function _rules()
    {
        $this->form_validation->set_rules('rfid_ktp', 'rfid ktp', 'trim');
        $this->form_validation->set_rules(
            'nip',
            'nip',
            'trim|required',
            array('required' => 'Masukan NIP !')
        );
        $this->form_validation->set_rules(
            'nik',
            'nik',
            'trim|required',
            array('required' => 'Masukan NIK !')
        );
        $this->form_validation->set_rules(
            'nama_pegawai',
            'nama pegawai',
            'trim|required',
            array('required' => 'Masukan Nama !')
        );
        $this->form_validation->set_rules(
            'tempat_lahir',
            'tempat lahir',
            'trim|required',
            array('required' => 'Masukan Tempah Lahir !')
        );
        $this->form_validation->set_rules(
            'tanggal_lahir',
            'tanggal lahir',
            'trim|required',
            array('required' => 'Masukan Tanggal Lahir !')
        );
        $this->form_validation->set_rules(
            'jenis_kelamin',
            'jenis kelamin',
            'trim|required',
            array('required' => 'Pilih Jenis Kelamin !')
        );
        $this->form_validation->set_rules(
            'agama',
            'agama',
            'trim|required',
            array('required' => 'Pilih Agama !')
        );
        $this->form_validation->set_rules(
            'status_kawin',
            'status kawin',
            'trim|required',
            array('required' => 'Pilih Statuw Kawin !')
        );
        $this->form_validation->set_rules('telepon', 'telepon', 'trim');
        $this->form_validation->set_rules('alamat', 'alamat', 'trim');
        $this->form_validation->set_rules('jabatan', 'jabatan', 'trim');
        $this->form_validation->set_rules('foto_pegawai', 'foto pegawai', 'trim');

        $this->form_validation->set_rules('id_pegawai', 'id_pegawai', 'trim');
        $this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
    }
}

/* End of file Pegawai.php */
/* Location: ./application/controllers/Pegawai.php */
/* Please DO NOT modify this information : */
/* Generated by IDETech Codeigniter CRUD Generator 2021-12-21 15:06:42 */
/* http://idetech.id */