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

class Trans_siswa_ppdb extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('trans_siswa_ppdb_model', 'trans_siswa');
		$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 = '';		
			$preview = '';		
			
			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>';
			}
			
			$cetakFaktur = "<a style=\"margin:4px;\" class=\"btn btn-success btn-xs\" onclick=\"cetakFaktur(\''+row.no_faktur+'\')\" href=\"javascript:void(0)\"\
									group=\"\" data-toggle=\"tooltip\" title=\"Cetak\"><i class=\"fa fa-print fa-lg\"></i>\
								  </a>";	
			
			/* if ($this->gparam['privilage']->privilage_update == 'Y') {
				$update_button = "<a class=\"l-btn l-btn-small l-btn-plain\" onclick=\"edit_view(\''+row.no_faktur+'\')\" href=\"javascript:void(0)\"\
									group=\"\" data-toggle=\"tooltip\" title=\"Update\"><i class=\"fa fa-pencil fa-lg\"></i>\
								  </a>";
			} */
			
			/*$preview = "<a class=\"l-btn l-btn-small l-btn-plain\" onclick=\"edit_view(\''+row.id+'\')\" href=\"javascript:void(0)\"\
									group=\"\" data-toggle=\"tooltip\" title=\"Update\"><i class=\"fa fa-camera-retro fa-lg\"></i>\
								  </a>";

			if ($this->gparam['privilage']->privilage_delete == 'Y') {	
				$delete_button = "<a class=\"l-btn l-btn-small l-btn-plain\" onclick=\"delete_data('+row.id+')\" href=\"javascript:void(0)\" group=\"\" data-toggle=\"tooltip\" title=\"Delete\">\
									<i class=\"fa fa-trash-o  fa-lg\"></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.no_faktur+'\')\" 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,
					'delete'				=> $delete_button,
					'cetakFaktur' 			=> $cetakFaktur
					/*'preview' 				=> $preview
					'delete'				=> $delete_button*/
			);
			
			$this->load->view('trans_siswa_ppdb_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";
			$no_trans = $this->uri->segment(3);	
			//echo $no_trans;
						
			if (!empty($no_trans)) {
				
				if ($this->gparam['privilage']->privilage_update == 'Y') {				
					//get value for edit
					echo $this->gparam['restrict']; die();	
				} else {
					echo $this->gparam['restrict']; die();
				}
				
			} else {
				if ($this->gparam['privilage']->privilage_create == 'Y') {	
					
					
					$getSiswa = $this->trans_siswa->getSiswa();
					$options = '<option value="">--Select--</option>';
					/*
					foreach ($getSiswa as $vSiswa) {
						$options .= '<option value="'.$vSiswa['id_siswa'].'">'.$vSiswa['nama'].'</option>';						
					}	*/
					
					$last_id = $this->trans_siswa->get_last_seq();
					
					$last_id = $last_id + 1;
					$length = strlen($last_id);
					
					if ($length == 1) {
						$num = '0000';
					} else if ($length == 2){
						$num = '000';
					} else if ($length == 3) {
						$num = '00';
					}else if ($length == 4) {
						$num = '0';
					
					} else {
						$num = '';
					}
					$data['num'] = 1;
					$data['last_id'] = $last_id;
					$data['faktur']  = '<input id="no_faktur" type="text" name="no_faktur"  value="FM'.date('Ymd').$num.$last_id.'" readonly />';
					
					$data['header']  = 'Add Pembayaran Siswa';
					$data['submit']  = 'create';					
					$data['siswa']   = $options;
						
					$data['trans'] ='							
										<section class="col col-6">
											<label class="label">Jenis Transaksi</label>
											<label class="select">	
												<select id="id-TransSiswa" name="id[]" class="chosen-select" multiple onchange="getDetail(this.value)"  >																											
												</select>												
											</label>
										</section>										
									';
					$get_smk = $this->trans_siswa->getDataSMK();
					$nama_smk='<label class="select">
									<select name="smk" onchange="getNama(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>';
								
					$data['nama_smk']=$nama_smk;
					
					/////baru
					
					$getMetodePembayaran = $this->trans_siswa->getMetodePembayaran();
					$metode_bayar='<label class="select">
									<select name="metode_bayar" >
									<option value="">--Select--</option>';
					
					foreach ($getMetodePembayaran as $v_metode) {
						//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{
								$metode_bayar .='<option value="'.$v_metode['id_jenis_bayar'].'">'.$v_metode['jenis'].'</option>';
							//}
						//}
						
					}
					$metode_bayar .='</select> <i></i> 
								</label>';
								
					$data['metode_bayar']=$metode_bayar;
					
					////
					
				} else {
					echo $this->gparam['restrict']; die();
				}	
			}				
			$this->load->view('crud_trans_siswa_ppdb',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}
	
	function getTrans() {
		$idSiswa = $this->input->post("id_siswa");		
		
		if ($idSiswa == "") {
			
			$html ='<section class="col col-6">
					<label class="label">Jenis Transaksi</label>
					<label class="select">	
						<select id="id-TransSiswa" name="id[]" class="chosen-select" multiple onchange="getDetail(this.value)" >								
						</select>												
					</label>
				</section>';
			
		} else { 
			$val = $this->trans_siswa->getTrans($idSiswa);		
			$html ='<section class="col col-6">
						<label class="label">Jenis Transaksi</label>
						<label class="select">	
							<select id="id-TransSiswa" name="id[]" class="chosen-select" multiple onchange="getDetail(this.value)" >	
							'.$val.'	
							</select>												
						</label>
					</section>';		
		}

		$html .='<script>
					$(function() {
						$(".chosen-select").chosen();
					})
				</script>';	
		echo $html;				
			
					
	}
	
	function getReset() {
		$html ='<table class="table table-bordered">
				<thead>
					<tr>
						<td>Detail List</td>
						<td>Jenis Pembayaran</td>
						<td>Tahun Ajaran</td>
						<td>Besarnya</td>
						<td>Dibayarkan</td>
					</tr>
				</thead>
				<tbody>';			 
		$html .='<tr><td colspan="4" align="right" >Total</td><td>Rp. 0</td></tr>';
		$html .='</tbody></table>';
		echo $html;
	}
	
	function getDetail() {
		
		$idCoa = $this->input->post("idCoa");
		$val = $this->trans_siswa->getDetail($idCoa);
		echo $val; 
		//print_r($id);
		
	}
	
	public function crud() {
		
		$crud = $this->input->post('submit');
		if ($crud == 'create') {
			$id_fak = $this->trans_siswa->next_faktur();
			$insert = $this->trans_siswa->insert_data($id_fak);
			if ($insert) {
				//$faktur = $this->input->post("no_faktur");
				$this->session->set_flashdata('faktur', $id_fak);	
				$json = true;
			}
			
		} else if ($crud == 'update') {
			echo $this->gparam['restrict']; die();				
		}
		
		echo json_encode($json);
		
	}
	
	public function delete() {
		
		$delete = $this->trans_siswa->delete_data();
		if ($delete) {
			$json = true;
		}
		echo json_encode($json);	
	}
	
	function load_data() {
		
		header('Content-Type: application/jsonp');
        $list = $this->trans_siswa->get_list_data();		
        echo json_encode($list);
	}
	
	function printFaktur() {
	
		define('FPDF_FONTPATH',$this->config->item('fonts_path'));
		$faktur = $_GET['faktur'];
		$diserahkan = $this->trans_siswa->getDiserahkan($faktur);
		$diterima  	= $this->trans_siswa->getDiterima($faktur);
		$total = $this->trans_siswa->getTotal($faktur);
		$get = $this->trans_siswa->getDetailFaktur($faktur);
		$getAsalSekolah = $this->trans_siswa->getAsalSekolah($faktur);
		
		//update faktur ke cetak
		//$upStatus = $this->trans_siswa->updateStatusFaktur($faktur);
		
		$data = array(
			'detail' 	 => $get, 
			'total'	 	 => $total,
			'diserahkan' => $diserahkan,
			'diterima' 	 => $diterima,
			'faktur'	 => $faktur,
			'getAsalSekolah'	 => $getAsalSekolah
		);
		
		$this->load->view("printTransSiswa",$data);
	}
	
	function successPage() {
		$this->load->view("success_page");
	}
	
	function getNama() {
		$id_smk = $this->input->post("smk");
		$options = $this->trans_siswa->getNama($id_smk);
	
		$html = '<div id="id-Mapping">
						<select id="id_siswa" name="id_siswa" onchange="getTrans(this.value),getReset(0);" class="chosen-select">
							<option value="">--Select--</option>																
						'.$options.'
						</select>
					</div>';
		$html .='<script>
					$(function() {
						$(".chosen-select").chosen();
					})
				</script>';	
		echo $html;			
	}
	function getTotalBiayaCicilan() {
		$total = $this->input->post("total");
	
		$html = '<div id="id-MappingTBC">Rp. '.number_format($total,2,",",".").'</div>';
		$html .='<script>
					$(function() {
						$(".chosen-select").chosen();
					})
				</script>';	
		echo $html;			
	}
	
	/*function getDataSiswa() {
		
		$siswa 		= $_GET['id_siswa'];
		$getSiswa 	= $this->trans_siswa->getSiswa($siswa);
		foreach ($getSiswa as $vSiswa) {
			$data[$vSiswa['id_siswa']] = $vSiswa['nama'];		
					
		}
		
		echo json_encode($data);
		
	}*/
	
}

