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

class Presensi extends MX_Controller {
	
	
	public function __construct(){
		parent::__construct();
		
		$this->page->use_directory();
		$this->load->model('model_presensi');	
	}

	public function index() {		
		$this->page->view('presensi_index', array(
			'add'				=> $this->page->base_url('/add'),
			'formImportExcel'	=> $this->page->base_url('/formImportExcel'),
			'grid'				=> $this->model_presensi->get_presensi(),
		));
	}
	
	private function form($action = 'insert', $id = ''){
		$this->page->view('presensi_form', array (
			'back'			=> $this->agent->referrer(),
			'action'		=> $this->page->base_url("/{$action}/{$id}"),
			'presensi'		=> $this->model_presensi->by_id_presensi($id),
		));
	}


	public function formImportExcel() {
		$this->page->view('presensi_form_excel', array (
			'back'			=> $this->agent->referrer(),
			'action'		=> $this->page->base_url("/importExcel"),
		));
	}

	public function importExcel() {
		$this->load->library('Excel_reader');

		$target = basename($_FILES['import_excel']['name']) ;
		move_uploaded_file($_FILES['import_excel']['tmp_name'], $target);
    
		$data = new Spreadsheet_Excel_Reader($_FILES['import_excel']['name'],false);
    
//    menghitung jumlah baris file xls
		$baris = $data->rowcount($sheet_index=0);
    
//    import data excel mulai baris ke-2 (karena tabel xls ada header pada baris 1)
		for ($i=2; $i<=$baris; $i++)
		{
//       membaca data (kolom ke-1 sd terakhir)
			$nama	= $data->val($i, 1);
			$tanggal		= $data->val($i, 2);
			$jam_masuk		= $data->val($i, 3);
			$jam_keluar		= $data->val($i, 4);
			$jam_kerja		= $data->val($i, 5);
			 $id_karyawan	= $this->db->get_where('karyawan', array('nama' => $nama))->row();
			// if (empty($id_karyawan)) {
			// 	$insertkaryawan = $this->db->insert('presensi',['nama'=>$id_karyawan]);
			// 	$id_karyawan = $insertkaryawan;
			// }

//      setelah data dibaca, masukkan ke tabel pegawai sql
			$hasil=array();
			if (!empty(@$id_karyawan)) {
				$dtsoal = [
					'id_karyawan'	=> $id_karyawan->id,
					'tanggal'		=> date('Y-m-d',strtotime($tanggal)),
					'jam_masuk'		=> $jam_masuk,
					'jam_keluar'	=> $jam_keluar,
					'jam_kerja'		=> $jam_kerja,
				];
				$hasil = $this->db->insert('presensi',$dtsoal);
			}
		}
    
		if(!$hasil){
//          jika import gagal
			$this->session->set_flashdata('info','Data berhasil diimpor.');
		}else{
//          jika impor berhasil
			$this->session->set_flashdata('info','Data berhasil diimpor.');
		}
    
//    hapus file xls yang udah dibaca
		unlink($_FILES['import_excel']['name']);

		redirect($this->page->base_url());
	}
	
	public function add(){
		$this->form();
	}
	
	public function edit($id){
		$this->form('update', $id);
	}
	
	public function insert(){
		$data = array(
			'id_karyawan' 	=> $this->input->post('id_karyawan'),
			'tanggal' 		=> $this->input->post('tanggal'),
			'jam_masuk' 	=> $this->input->post('jam_masuk'),
			'jam_keluar' 	=> $this->input->post('jam_keluar'),
			'jam_kerja' 	=> $this->input->post('jam_kerja'),
		);
		$this->db->insert('presensi', $data);
		
		redirect($this->page->base_url());
	}
	
	public function update($id){
		$data = array(
			'id_karyawan' 	=> $this->input->post('id_karyawan'),
			'tanggal' 		=> $this->input->post('tanggal'),
			'jam_masuk' 	=> $this->input->post('jam_masuk'),
			'jam_keluar' 	=> $this->input->post('jam_keluar'),
			'jam_kerja' 	=> $this->input->post('jam_kerja'),		
		);
		$this->db->where('id', $id);
		$this->db->update('presensi', $data);
		
		redirect($this->page->base_url());
	}
	
	public function delete($id){
		$this->db->delete('presensi', array('id' => $id));
		redirect($this->agent->referrer());
	}
	
	// public function options_jabatan($id){
	// 	$jabatan = $this->db->order_by('nama')->get('jabatan');
	// 	return options($jabatan, 'id', $id, 'nama');
	// }
	
}

/* End of file Jabatan.php */
/* Location: ./application/modules/master_data/controllers/Jabatan.php */