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

class Ref_biaya extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('ref_biaya_model', 'biaya');
		$this->gparam['privilage'] = getPrivilage($this->gparam['controller']);
		$this->gparam['restrict'] = 'You Cannot Access This Menu';
    }
	
	public function index()
	{
		
		if ($this->gparam['privilage']->privilage_view == 'Y') {
		
			$gridopt = $this->input->get(array('psize', 'pnumber'));
			header('Content-Type: text/html');
			$create_button = '""';			
			$update_button = '';
			$delete_button = '';		
			
			if ($this->gparam['privilage']->privilage_create == 'Y') {
				//create button
				$create_button = '<a href="javascript:void(0)"  class="btn btn-primary btn-xs" onclick="create_view();" >
									<i class="fa fa-lg fa-fw fa-plus"></i>Create
								  </a>';
			}	
			
			if ($this->gparam['privilage']->privilage_update == 'Y') {
				$update_button = "<a tyle=\"margin:4px;\" class=\"btn btn-success btn-xs\" onclick=\"edit_view('+row.id+')\" href=\"javascript:void(0)\"\
									group=\"\" data-toggle=\"tooltip\" title=\"Update\"><i class=\"fa fa-lg fa-fw fa-edit\"></i>\
								  </a>";
			}

			if ($this->gparam['privilage']->privilage_delete == 'Y') {	
				$delete_button = "<a style=\"margin:4px;\" class=\"btn btn-danger btn-xs\" onclick=\"delete_data('+row.id+')\" href=\"javascript:void(0)\" group=\"\" data-toggle=\"tooltip\" title=\"Delete\">\
									<i class=\"fa fa-lg fa-fw fa-trash-o\"></i>\
								</a>"; 
			}				
			
			// pnumber, psize
			$data = array(
					'controller'  	=> $this->gparam['controller'],
					'psize'			=> (empty($gridopt['psize'])?10:$gridopt['psize']),
					'pnumber'  		=> (empty($gridopt['pnumber'])?1:$gridopt['pnumber']),
					'create' 		=> $create_button,
					'update' 		=> $update_button,
					'delete'		=> $delete_button
			);
			
			$this->load->view('ref_biaya_view',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}
	
	function form() {
		
		if ($this->gparam['privilage']->privilage_view == 'Y') {
			$data['controller'] = $this->gparam['controller'];				
			$data['action'] = base_url()."index.php/".$this->gparam['controller']."/crud";
			$id = $this->uri->segment(3);	
			
			if (!empty($id)) {
				if ($this->gparam['privilage']->privilage_update == 'Y') {				
					//get value for edit
					$edit = $this->biaya->get_value_edit($id);
					
					$data['jenispembayaran'] = $edit->jenispembayaran;
					$jumlah_bayar ='<input id="besarnya" type="text" name="besarnya"  value="'.$edit->besarnya.'" autocomplete="off" />';
					$data['id'] 	= $id;
					$data['header'] = 'Edit List Biaya';
					$data['submit'] = 'update';
					
					if (($edit->jenis_transaksi == 0)) { 
						$jenis  = 
						'<label class="select">
						<select name="jenis_transaksi" onchange="get_coa(this.value)" >
							<option value="">--Select--</option>
							<option value="0" selected> Uang Keluar </option>
							<option value="1"> Uang Masuk </option>
						</select><i></i>
						</label>';
						$table = 'ref_list_uang_keluar';
					} else {
						$jenis  = 
						'<label class="select">
						<select name="jenis_transaksi" onchange="get_coa(this.value)" >
							<option value="">--Select--</option>
							<option value="0"> Uang Keluar </option>
							<option value="1" selected> Uang Masuk </option>
						</select><i></i>
						</label>';
						$table = 'ref_list_uang_masuk';
					}
					$get_smk = $this->biaya->getDataSMK();
					$nama_smk='<label class="select">
									<select name="smk" onchange="getKelas(this.value);" >
									<option value="">--Select--</option>';
					
					foreach ($get_smk as $v_smk) {
						if($edit->id_smk==$v_smk['id']){
							$nama_smk .='<option value="'.$v_smk['id'].'" selected>'.$v_smk['name'].'</option>';
						}else{
							$nama_smk .='<option value="'.$v_smk['id'].'">'.$v_smk['name'].'</option>';
						}
					}
					$nama_smk .='</select> <i></i> 
								</label>';
						
					$arr_cicil = array("Tidak Dapat Dicicil", "Dapat Dicicil");		
					$dicicil='<label class="select">
									<select name="dicicil" >
									<option value="">--Select--</option>';
					
					for($i=0;$i<=1;$i++){
						if($edit->dicicil==$i){
							$dicicil .='<option value="'.$i.'" selected>'.$arr_cicil[$i].'</option>';
						}else{
							$dicicil .='<option value="'.$i.'">'.$arr_cicil[$i].'</option>';
						}
					}
					$dicicil .='</select> <i></i> 
								</label>';
					
					$arr_opsi = array("Tidak Opsional", "Opsional");		
					$opsional='<label class="select">
									<select name="opsional" >';
					if($edit->dana_opsional==0){
						$opsional .='<option value="0" selected>'.$arr_opsi[0].'</option><option value="1">'.$arr_opsi[1].'</option>';
					}else{
						$opsional .='<option value="0">'.$arr_opsi[0].'</option><option value="1" selected>'.$arr_opsi[1].'</option>';
					}
					
					$opsional .='</select> <i></i> 
								</label>';
								
					$get_kelas = $this->biaya->getKelas($edit->id_smk);
					$kelas='<div id="id-MappingKls"><label class="select">
									<select name="kelas" id="kelas">
									<option value="">--Select--</option>';
					foreach ($get_kelas as $v_kelas) {	
						if($edit->kelas==$v_kelas['kelas']){	
							$kelas.='<option value="'.$v_kelas['kelas'].'" selected="selected">'.$v_kelas['kelas'].'</option>';
						}else{
							$kelas.='<option value="'.$v_kelas['kelas'].'">'.$v_kelas['kelas'].'</option>';
						}
						
					}
								$kelas.='</select> <i></i></label></div>';
								
					
					$bln = array("July","August","September","October","November","December","January","February","March","April","May","June");		
					$periode='<label class="select">
												<select name="periode" >
												<option value="">--Select--</option>';
					for($i=0;$i<12;$i++){
						if($edit->periode==($i+1)){	
							$periode.='<option value="'.($i+1).'" selected="selected">'.$bln[$i].'</option>';
						}else{
							$periode.='<option value="'.($i+1).'">'.$bln[$i].'</option>';
						}
					}
					$periode.='</select> <i></i> 
											</label>';	
											
											
											
						
					$data_coa = $this->biaya->get_list_coa($table);
					$coa = '<label class="select">
						<select name="id_coa" >
						<option value="">--Select--</option>';
					foreach ($data_coa as $list_coa) {
						if ($edit->id_coa == $list_coa['id']) {							
							$coa .= '<option value="'.$list_coa['id'].'" selected> '.$list_coa['kodedesk'].' - '.$list_coa['kodesub'].' - '.$list_coa['daftarlist'].' </option>';
						} else {							
							$coa .= '<option value="'.$list_coa['id'].'"> '.$list_coa['kodedesk'].' - '.$list_coa['kodesub'].' - '.$list_coa['daftarlist'].' </option>';
						}
							
					}
					
					$coa .= '</select><i></i>
					</label>';
					
					
				} else {
					echo $this->gparam['restrict']; die();
				}	
			} else {
				if ($this->gparam['privilage']->privilage_create == 'Y') {	
					$jumlah_bayar ='<input id="besarnya" type="text" name="besarnya"  value="" autocomplete="off" />';
					$data['header'] = 'Add List Biaya';
					$data['submit'] = 'create';
					$jenis  = 
					'<label class="select">
					<select name="jenis_transaksi" onchange="get_coa(this.value)" >
						<option value="">--Select--</option>
						<option value="0"> Uang Keluar </option>
						<option value="1"> Uang Masuk </option>
					</select><i></i>
					</label>';
					
					$coa = '<label class="select">
						<select name="id_coa" >
						<option value="">--Select--</option>						
					</select><i></i>
					</label>';
					
					$get_smk = $this->biaya->getDataSMK();
					$nama_smk='<label class="select">
									<select name="smk"  onchange="getKelas(this.value);" >
									<option value="">--Select--</option>';
					
					foreach ($get_smk as $v_smk) {
						//for($i=0;$i<$temp_i;$i++){
							//if($edit->kelas==$v_smk['name']){
							//	$nama_smk .='<option value="'.$v_smk['name'].'" selected>'.$v_smk['name'].'</option>';
							//}else{
								$nama_smk .='<option value="'.$v_smk['id'].'">'.$v_smk['name'].'</option>';
							//}
						//}
						
					}
					$nama_smk .='</select> <i></i> 
								</label>';
								
					$options = $this->biaya->cekKelas();
					$id_jur = $this->input->post("id_jur");
						
											
					$arr_cicil = array("Tidak Dapat Dicicil", "Dapat Dicicil");		
					$dicicil='<label class="select">
									<select name="dicicil" >
									<option value="" selected>--Select--</option>';
					
					for($i=0;$i<=1;$i++){
						//if($edit->dicicil==$i){
						//	$dicicil .='<option value="'.$i.'" selected>'.$arr_cicil[$i].'</option>';
						//}else{
						$dicicil .='<option value="'.$i.'">'.$arr_cicil[$i].'</option>';
						//}
					}
					$dicicil .='</select> <i></i> 
								</label>';
								
					$arr_opsi = array("Tidak Opsional", "Opsional");		
					$opsional='<label class="select">
									<select name="opsional" >';
					
					$opsional .='<option value="0" selected>'.$arr_opsi[0].'</option><option value="1">'.$arr_opsi[1].'</option>';
					$opsional .='</select> <i></i> 
								</label>';
					
					$kelas='<div id="id-MappingKls"><label class="select">
									<select name="kelas" id="kelas">
										<option value="">--Select--</option>
												</select> <i></i> 
											</label></div>';	
					
					$bln = array("July","August","September","October","November","December","January","February","March","April","May","June");		
					$periode='<label class="select">
												<select name="periode" >
												<option value="">--Select--</option>';
					for($i=0;$i<12;$i++){
						$periode.='<option value="'.($i+1).'">'.$bln[$i].'</option>';
					}
					$periode.='</select> <i></i> 
											</label>';	
				} else {
					echo $this->gparam['restrict']; die();
				}	
			}
			
			$data['dicicil'] 		= $dicicil;
			$data['jenis_transaksi'] 	= $jenis;
			$data['coa'] 				= $coa;
			$data['besarnya']	 		= $jumlah_bayar;
			$data['nama_smk']	 		= $nama_smk;
			$data['kelas']	 		= $kelas;
			$data['periode']	 		= $periode;
			$data['opsional']	 		= $opsional;
			$this->load->view('crud_ref_biaya_view',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}	
	function getKelas() {
		$id_smk = $this->input->post("smk");
		$options = $this->biaya->getKelasSMK($id_smk);
	
		$html = '<div id="id-MappingKls"><label class="select">
						<select name="kelas" id="kelas">
							<option value="">--Select--</option>																
						'.$options.'
						</select> <i></i> 
					</label></div>';
		echo $html;			
	}
	function get_coa() {
		$type = $this->input->post('type');
		if ($type == 0) {
			//coa uang keluar
			$tabel = 'ref_list_uang_keluar';
		} else {
			//coa uang masuk
			$tabel = 'ref_list_uang_masuk';
		}
		$result = $this->biaya->get_list_coa($tabel);
		$options = '<label class="select">
						<select name="id_coa" >
						<option value="">--Select--</option>';
		foreach($result as $coa) {
				
			$options .= '<option value="'.$coa['id'].'"> '.$coa['kodedesk'].' - '.$coa['kodesub'].' - '.$coa['daftarlist'].' </option>';
			
		}
		$options .= '</select><i></i>
					</label>';
		
		echo $options;
	}
	
	
	public function crud() {
		
		$crud = $this->input->post('submit');
		if ($crud == 'create') {
			
			$insert = $this->biaya->insert_data();
			if ($insert) {
				$json = true;
			}
			
		} else if ($crud == 'update') {
			$update = $this->biaya->update_data();
			if ($update) {
				$json = true;			
			}
				
		}
		
		echo json_encode($json);
		
	}
	
	public function delete() {
		
		$delete = $this->biaya->delete_data();
		if ($delete) {
			$json = true;
		}
		echo json_encode($json);	
	}
	
	function load_data() {
		header('Content-Type: application/jsonp');
        $list = $this->biaya->get_list_data();
		
        echo json_encode($list);
	}
	
	
}
