<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pegawai extends CI_Controller {
	function __construct(){
	error_reporting(0);
		parent::__construct();	
			$this->load->library('session');
			$this->load->library('form_validation');
			//$this->load->model('m_pegawai');
			$this->load->model('m_pegawai');
			$this->load->model('m_bidang');
			$this->load->model('m_jenis');
			if($this->session->userdata('token') !=md5(md5(md5(date('Ymd').'tamu')))){
				redirect(base_url("home"));
			}			
	}
	public function index()
	{
	$data['pegawai'] = $this->m_pegawai->tampil();
	$this->load->view('admin/pegawai', $data);			
	}
	public function add()
	{
	$data['cek']='';
	$data['pegawai'] = $this->m_pegawai->tampil();
	$data['bidang'] = $this->m_bidang->tampil();
	$data['jenis'] = $this->m_jenis->tampil();	
	$this->load->view('admin/pegawai-add', $data);				
	}	
	public function save()
	{
		if(isset($_POST['button'])){
		$this->form_validation->set_rules('namapegawai','namapegawai','required|max_length[100]');			
		$this->form_validation->set_rules('act','act','required');
		$act = $this->security->xss_clean($this->input->post('act'));	
		$a = array('\'', '/',';', '[', ']', '{', '}', '|', '^', '~',',');		
		$idpegawai = str_replace($a,"",$this->security->xss_clean($this->input->post('idpegawai')));					
		$idjenis = str_replace($a,"",$this->security->xss_clean($this->input->post('idjenis')));					
		$namapegawai = str_replace($a,"",$this->security->xss_clean($this->input->post('namapegawai')));	
		if($idpegawai ==''){	
		if($_FILES['input_gambar']['name'] !=''){				
		$upload = $this->m_pegawai->upload();
		if($upload['result'] == "success"){
		
		$namafilebaru = $upload['namafilebaru'];	
		$data_save = array(
								'namapegawai' => ucwords($namapegawai),
								'file_name' => $namafilebaru,
								'idjenis' => $idjenis,
						);	
		$this->m_pegawai->in($data_save,'pegawai');
		}
		}else{
		$data_save = array(
								'namapegawai' => ucwords($namapegawai),
								'idjenis' => $idjenis,
						);	
		$this->m_pegawai->in($data_save,'pegawai');			
			
		}
		
		$data['pegawai'] = $this->m_pegawai->tampil();		
		$data['alert'] = 'success';
		$data['pesan'] = 'Berhasil disimpan';		
		$this->load->view('admin/pegawai', $data);				
		}else if($idpegawai !=''){
		if($_FILES['input_gambar']['name'] !=''){		
			$upload = $this->m_pegawai->upload();
			if($upload['result'] == "success"){	
			$namafilebaru = $upload['namafilebaru'];				
				$data_save = array(
									'namapegawai' => ucwords($namapegawai),
									'file_name' => $namafilebaru,
									'idjenis' => $idjenis,
								);			
				$where = array(
								'idpegawai' => $idpegawai
						);
				$this->m_pegawai->update($where,$data_save,'pegawai');	
			}
		}else{
			$data_save = array(
									'namapegawai' => ucwords($namapegawai),
									'idjenis' => $idjenis,
							);			
			$where = array(
							'idpegawai' => $idpegawai
					);
			$this->m_pegawai->update($where,$data_save,'pegawai');					
		}		
		$data['pegawai'] = $this->m_pegawai->tampil();		
		$data['alert'] = 'success';
		$data['pesan'] = 'Berhasil diperbarui';		
		$this->load->view('admin/pegawai', $data);		
		}
		}
		if(isset($_POST['kembali'])){
		$data['alert'] = '';
		$data['pesan'] = '';				
		$data['pegawai'] = $this->m_pegawai->tampil();
		$this->load->view('admin/pegawai', $data);			
		}		
	}	
	
	public function edit()
	{
		$a = array('\'', '/',';', '[', ']', '{', '}', '|', '^', '~',',');	
		$id = str_replace($a,"",$this->uri->segment(4));
		$data['cek'] ='edit';
		$data['pegawai'] = $this->m_pegawai->tampil();		
		$data['edit'] = $this->m_pegawai->edit($id);
		$data['jenis'] = $this->m_jenis->tampil();	
		$this->load->view('admin/pegawai-add', $data);							
	}
	
	public function hapus()
	{
		$a = array('\'', '/',';', '[', ']', '{', '}', '|', '^', '~',',');	
		$id = str_replace($a,"",$this->uri->segment(4));
		//delete file lama 
		$cek= $this->m_pegawai->filelama($id);
		$namafilelama = $cek->file_name;
		if(file_exists('./images/pegawai/'.$namafilelama)){		
		unlink('./images/pegawai/'.$namafilelama); 		
		}
			
		$where = array(
						'idpegawai' => $id
				);
		$this->m_pegawai->hapus($where,'pegawai');
		$data['pegawai'] = $this->m_pegawai->tampil();		
		$data['alert'] = 'success';
		$data['pesan'] = 'Data berhasil dihapus';			
		$this->load->view('admin/pegawai', $data);				
		
	}		
}