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

class Ppdb_siswa extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('ppdb_siswa_model', 'ppdb_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 = '';
			$approval_button = '';			
			$reject_button = '';			
			
			if ($this->gparam['privilage']->privilage_create == 'Y') {
				$url = "#".$this->gparam['controller']."/form";
				//create button
				$create_button = '[{
						text: \'Create\',
						iconCls: \'icon-add\',
						handler: function () {					
							window.location.href = "'.$url.'";
							
							
						}
					}]';
			}	
			
			if ($this->gparam['privilage']->privilage_update == 'Y') {
				$update_button = "<a class=\"l-btn l-btn-small l-btn-plain\" onclick=\"edit_view('+row.id_daftar+')\" href=\"javascript:void(0)\"\
									group=\"\" data-toggle=\"tooltip\" title=\"Update\"><i class=\"fa fa-pencil 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_daftar+')\" 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_approve == 'Y') {	

				$approval_button = "<a class=\"l-btn l-btn-small l-btn-plain\" onclick=\"approve_data('+row.id_daftar+')\" href=\"javascript:void(0)\" group=\"\" data-toggle=\"tooltip\" title=\"Approval\">\
									<i class=\"fa fa-check-circle  fa-lg\"></i>\
								</a>"; 	
								
				$reject_button = "<a class=\"l-btn l-btn-small l-btn-plain\" onclick=\"reject_data('+row.id_daftar+')\" href=\"javascript:void(0)\" group=\"\" data-toggle=\"tooltip\" title=\"Reject\">\
									<i class=\"fa fa-minus-circle  fa-lg\"></i>\
								</a>"; 						
			}	

			$image_button = "<a class=\"l-btn l-btn-small l-btn-plain\" onclick=\"preview_image('+row.id_daftar+')\" href=\"javascript:void(0)\"\
									group=\"\" data-toggle=\"tooltip\" title=\"Preview\"><i class=\"fa fa-camera-retro fa-lg\"></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,
					'approval_button'		=> $approval_button,
					'image_button'			=> $image_button,
					'reject_button'			=> $reject_button
			);
			
			$this->load->view('ppdb_siswa_view',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}
	
	function approve_data() {
		$approve = $this->ppdb_siswa->approve_siswa();
		if ($approve) {
			$json = true;
		}
		echo json_encode($json);		
	}
	
	function reject_data() {
		$approve = $this->ppdb_siswa->reject_data();
		if ($approve) {
			$json = true;
		}
		echo json_encode($json);	
	}
	
	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->ppdb_siswa->get_value_edit($id);
					
					$t_lahir = strtotime($edit->tanggal_lahir);
					
					$data['nama'] 			= $edit->nama;
					$data['tempat_lahir'] 	= $edit->tempat_lahir;
					$data['nisn'] 			= $edit->nisn;
					$data['nama_ayah']		= $edit->nama_ayah;
					$data['nama_ibu'] 		= $edit->nama_ibu;
					$data['telepon'] 		= $edit->telepon;
					
					$data['asal'] 			= $edit->asal_sekolah;
					$data['ker_ayah'] 		= $edit->pekerjaan_ayah;
					$data['ker_ibu'] 		= $edit->pekerjaan_ibu;
					
					
					$data['alamat'] 		= $edit->alamat;
					$data['tanggal_lahir'] 	= date('d-m-Y',$t_lahir);
					$data['id_daftar'] 		= $id;
					
					$j_kel = $edit->jenis_kelamin;
					$approval_status = $edit->approval_status;
					$arr_agama = array("Islam", "Kristen Protestan", "Katolik", "Hindu", "Buddha", "Kong Hu Cu");
					$agama='<label class="select">
									<select name="agama">';
					
					for($i=0; $i<6; $i++){
						if($edit->agama==$arr_agama[$i]){
							$agama.='<option value="'.$arr_agama[$i].'" selected>'.$arr_agama[$i].'</option>';
						}else{
							$agama.='<option value="'.$arr_agama[$i].'">'.$arr_agama[$i].'</option>';
						}
					}
					$agama.='</select> <i></i> 
								</label>';
									
					$get_smk = $this->ppdb_siswa->getDataSMK();
					
					/*
					$nama_smk='<label class="select">
									<select name="smk" onchange="getJurusan(this.value);cekKelas(this.value,0);getType(this.value);" >
									';
					*/
					
					$nama_smk='<label class="select">
									<select name="smk" onchange="cekKelas(0,this.value);getType(this.value);" >
									';
					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>';
								
					if ($j_kel == 'L') {
							$jekel = '<label class="select">
									<select name="jenis_kelamin">
										<option value="">--Select--</option>
										<option value="L" selected>Laki-Laki</option>
										<option value="P">Perempuan</option>									
									</select> <i></i> 
								</label>';
					} else {
							$jekel = '<label class="select">
									<select name="jenis_kelamin">
										<option value="">--Select--</option>
										<option value="L">Laki-Laki</option>
										<option value="P" selected>Perempuan</option>									
									</select> <i></i> 
								</label>';
					}
					
					$get_kelas = $this->ppdb_siswa->getKelas($edit->id_smk);
					$kelas='<div id="id-MappingKls"><label class="select">
									<select name="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>';
					$get_type = $this->ppdb_siswa->getType($edit->id_smk);
					$mappingType = '<div id="id-MappingType">
					<label class="select">
						<select name="id_mapping_type" id="id_mapping_type">
							<option value="">--Select--</option>';
							foreach ($get_type as $v_type) {
								if($edit->no_type==$v_type['no_type']){
									$mappingType .='<option value="'.$v_type['no_type'].'" selected>'.$v_type['no_type']." - ".$v_type['jenis_pembayaran'].'</option>';
								}else{
									$mappingType .='<option value="'.$v_type['no_type'].'">'.$v_type['no_type']." - ".$v_type['jenis_pembayaran'].'</option>';
								}
							}
																						
						$mappingType .='</select> <i></i> 
					</label></div>';
					
					
									
					$get_jur = $this->ppdb_siswa->getJurEdit($edit->id_smk);
					$mappingJur = '<div id="id-MappingJur">
					<label class="select">
						<select name="id_mapping_jur" id="id_mapping_jur" onchange="cekKelas('.$edit->id_smk.',this.value);">
							<option value="">--Select--</option>';
							foreach ($get_jur as $v_jurusan) {
								if($edit->id_jur==$v_jurusan['id']){
									$mappingJur .='<option value="'.$v_jurusan['id'].'" selected>'.$v_jurusan['nama_jurusan'].'</option>';
								}else{
									$mappingJur .='<option value="'.$v_jurusan['id'].'">'.$v_jurusan['nama_jurusan'].'</option>';
								}
							}
																						
						$mappingJur .='</select> <i></i> 
					</label></div>';
					$status = '<label class="select">
							<select name="status" id="status">';
							
							if($approval_status==0){
								$status.='<option value="0" selected>Aktif</option>';
								$status.='<option value="2">Lunas</option>';
								$status.='<option value="3">Batal</option>';
							}else if($approval_status==2){
								$status.='<option value="0">Aktif</option>';
								$status.='<option value="2" selected>Lunas</option>';
								$status.='<option value="3">Batal</option>';
							}else if($approval_status==3){
								$status.='<option value="0">Aktif</option>';
								$status.='<option value="2"selected>Lunas</option>';
								$status.='<option value="3" selected>Batal</option>';
							}													
						$status.='</select> <i></i> 
					</label>';
					
					$data['header'] 		= 'Edit Pendaftaran Siswa';
					$data['submit'] 		= 'update';	
				} else {
					echo $this->gparam['restrict']; die();
				}	
			} else {
				if ($this->gparam['privilage']->privilage_create == 'Y') {	
					
					
					
					$get_smk = $this->ppdb_siswa->getDataSMK();
					/*
					$nama_smk='<label class="select">
									<select name="smk" onchange="getJurusan(this.value);cekKelas(this.value,0);getType(this.value);">
									<option value="">--Select--</option>';
					*/
					
					$nama_smk='<label class="select">
									<select name="smk" onchange="cekKelas(0,this.value);getType(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>';
					
					$jekel = '<label class="select">
									<select name="jenis_kelamin">
										<option value="">--Select--</option>
										<option value="L">Laki-Laki</option>
										<option value="P">Perempuan</option>									
									</select> <i></i> 
								</label>';
					
					//$get_kelas = $this->ppdb_siswa->getKelas();
					$kelas='<div id="id-MappingKls"><label class="select">
									<select name="kelas" onchange="getType(this.value);" >
									<option value="">--Select--</option></select> <i></i> 
								</label></div>';
				
					$mappingJur = '<div id="id-MappingJur">
					<label class="select">
						<select name="id_mapping_jur" id="id_mapping_jur" onchange="cekKelas();">
							<option value="">--Select--</option>																
						</select> <i></i> 
					</label></div>';
					
					$agama='<label class="select">
									<select name="agama">
									<option value="" selected>--Select--</option>
									<option value="Islam">Islam</option>
									<option value="Kristen Protestan">Kristen Protestan</option>
									<option value="Katolik">Katolik</option>
									<option value="Hindu">Hindu</option>
									<option value="Buddha">Buddha</option>
									<option value="Kong Hu Cu">Kong Hu Cu</option>
									</select> <i></i> 
								</label>';
									
					$mappingType = '<div id="id-MappingType">
					<label class="select">
						<select name="id_mapping_type" id="id_mapping_type" >
							<option value="">--Select--</option>																
						</select> <i></i> 
					</label></div>';
					
					$status = '<label class="select">
						<select name="status" id="status">
							<option value="0" selected>Aktif</option>													
						</select> <i></i> 
					</label>';
					
					$data['header'] = 'Add Pendaftaran Siswa';
					$data['submit'] = 'create';
				} else {
					echo $this->gparam['restrict']; die();
				}	
			}
			$data['mappingType'] = $mappingType;
			$data['mappingJur'] = $mappingJur;
			$data['nama_smk'] = $nama_smk;
			$data['kelas'] = $kelas;
			$data['jkel'] = $jekel;
			$data['agama'] = $agama;
			$data['status'] = $status;
			$this->load->view('crud_ppdb_siswa',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}	
	
	
	public function crud() {
		
		$crud = $this->input->post('submit');
		if ($crud == 'create') {
			
			$insert = $this->ppdb_siswa->insert_data();
			if ($insert) {
				$json = true;
			}
			
		} else if ($crud == 'update') {
			$update = $this->ppdb_siswa->update_data();
			if ($update) {
				$json = true;			
			}
				
		}
		
		echo json_encode($json);
		
	}
	
	function get_fancy_image() {
		header('Content-Type: application/json'); // parsing json
        $list = $this->ppdb_siswa->get_fancy();
        echo json_encode($list);
	}
	
	public function delete() {
		
		$delete = $this->ppdb_siswa->delete_data();
		if ($delete) {
			$json = true;
		}
		echo json_encode($json);	
	}
	
	function load_data() {
		header('Content-Type: application/jsonp');
        $list = $this->ppdb_siswa->get_list_data();
		
        echo json_encode($list);
	}
	
	function getJurusan() {
		$id_smk = $this->input->post("smk");
		$options = $this->ppdb_siswa->getJurusan($id_smk);
	
		$html = '<label class="select">
						<select name="id_mapping_jur" id="id_mapping_jur" onchange="cekKelas(this.value,'.$id_smk.');">
							<option value="">--Select--</option>																
						'.$options.'
						</select> <i></i> 
					</label>';
		echo $html;			
	}
	function cekKelas() {
		
		$id_jur = $this->input->post("id_jur");
		$id_smk = $this->input->post("id_smk");
		$options = $this->ppdb_siswa->cekKelas($id_smk);
		/*
		$html='<label class="select">
									<select name="kelas" >
									<option value="">--Select--</option>'.$options.'</select> <i></i> 
								</label>';
					*/			
		$html='<label class="select">
									<select name="kelas" >'.$options.'</select> <i></i> 
								</label>';
								
		echo $html;			
	}
	function getType() {
		
		$id_smk = $this->input->post("id_smk");
		
		$options = $this->ppdb_siswa->cekType($id_smk);
		$html='<label class="select">
									<select name="id_mapping_type" >
									<option value="">--Select--</option>'.$options.'</select> <i></i> 
								</label>';
								
		echo $html;			
	}
}
