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

class Mapping_kelas extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('mapping_kelas_model', 'kelas_model');
		$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 style=\"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('mapping_kelas_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->kelas_model->get_value_edit($id);
					
					$get_wali_kelas = $this->kelas_model->get_wali_kelas();
					
					$wali_kelas = '<label class="select">
									<select name="id_karyawan" id="id_karyawan" class="chosen-select">
										<option value="">--Select--</option>';
										
					/*
					$wali_kelas = '<label class="select">
									<select name="id_karyawan" id="id_karyawan" class="chosen-select">
										<option value="">--Select--</option>';
					*/
					
					foreach ($get_wali_kelas as $v_walikelas) {
						
						if ($edit->id_karyawan == $v_walikelas['id']) {
							$wali_kelas .='<option value="'.$v_walikelas['id'].'" selected>'.$v_walikelas['nama_karyawan'].'</option>';
						} else {
							$wali_kelas .='<option value="'.$v_walikelas['id'].'">'.$v_walikelas['nama_karyawan'].'</option>';
						}
					}
					
					$wali_kelas .= '</select>
								</label>';
								
					
					
					
					
					$get_kelas = $this->kelas_model->getKelas($edit->id_smk);
					$nama_kelas='<div id="id-MappingKls"><label class="select">
									<select name="kelas" onchange="getRuangKelas(this.value);" >
									<option value="">--Select--</option>';
					foreach ($get_kelas as $v_kelas) {	
						if($edit->kelas==$v_kelas['kelas']){	
							$nama_kelas.='<option value="'.$v_kelas['kelas'].'" selected="selected">'.$v_kelas['kelas'].'</option>';
						}else{
							$nama_kelas.='<option value="'.$v_kelas['kelas'].'">'.$v_kelas['kelas'].'</option>';
						}
						
					}
								$nama_kelas.='</select> <i></i></label></div>';
					/*
					if ($edit->kelas == 7) {
						$nama_kelas = '<label class="select">
									<select name="kelas" id="kelas" >
										<option value="">--Select--</option>
										<option value="7" selected>Kelas 7</option>
										<option value="8">Kelas 8</option>
										<option value="9">Kelas 9</option>
									</select> <i></i> 
								</label>';
					} else if ($edit->kelas == 8) {
						
						$nama_kelas = '<label class="select">
									<select name="kelas" id="kelas">
										<option value="">--Select--</option>
										<option value="7">Kelas 7</option>
										<option value="8" selected>Kelas 8</option>
										<option value="9">Kelas 9</option>
									</select> <i></i> 
								</label>';
						
					} else if ($edit->kelas == 9) {
						
						$nama_kelas = '<label class="select">
									<select name="kelas" id="kelas">
										<option value="">--Select--</option>
										<option value="7">Kelas 7</option>
										<option value="8">Kelas 8</option>
										<option value="9" selected>Kelas 9</option>
									</select> <i></i> 
								</label>';
						
					} else {
						$nama_kelas = '<label class="select">
									<select name="kelas" id="kelas">
										<option value="">--Select--</option>
										<option value="7">Kelas 7</option>
										<option value="8">Kelas 8</option>
										<option value="9">Kelas 9</option>
									</select> <i></i> 
								</label>';
					}
					*/
					
					$get_smk = $this->kelas_model->getDataSMK();
					
					$nama_smk='<label class="select">
									<select name="smk" onchange="getJurusan(this.value);cekKelas(this.value, 0), getRuangKelas(0);" >
									<option value="">--Select--</option>';
					
					foreach ($get_smk as $v_smk) {
						//for($i=0;$i<$temp_i;$i++){
							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>';
					
					$idMappingClass			= $edit->id_jur;
					
					$get_jur = $this->kelas_model->getJurEdit($edit->id_smk);
					if($edit->id_smk==1){
						$mapping = '<div id="id-Mapping">
						<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']){
										$mapping .='<option value="'.$v_jurusan['id'].'" selected>'.$v_jurusan['nama_jurusan'].'</option>';
									}else{
										$mapping .='<option value="'.$v_jurusan['id'].'">'.$v_jurusan['nama_jurusan'].'</option>';
									}
								}
																							
							$mapping .='</select> <i></i> 
						</label></div>';
					}else if($edit->id_smk==2){
						$mapping = '<div id="id-Mapping">
						<label class="select">
							<select name="id_mapping_jur" id="id_mapping_jur" onchange="cekKelas('.$edit->id_smk.',this.value);">
								<option value="4" selected> - </option>';
																						
							$mapping .='</select> <i></i> 
						</label></div>';
					}else if($edit->id_smk==3){
						$mapping = '<div id="id-Mapping">
						<label class="select">
							<select name="id_mapping_jur" id="id_mapping_jur" onchange="cekKelas('.$edit->id_smk.',this.value);">
								<option value="5" selected> - </option>';
																						
							$mapping .='</select> <i></i> 
						</label></div>';
					}
					
					
					
					$data['id']					= $id;
					$data['ruang_kelas']		= $edit->ruang_kelas;
					$data['header'] 			= 'Edit Mapping Kelas';
					$data['submit'] 			= 'update';	
				} else {
					echo $this->gparam['restrict']; die();
				}	
			} else {
				if ($this->gparam['privilage']->privilage_create == 'Y') {	
					
					$get_wali_kelas = $this->kelas_model->get_wali_kelas();
					
					$wali_kelas = '<label class="select">
									<select name="id_karyawan" id="id_karyawan" class="chosen-select">
										<option value="">--Select--</option>';
										
					/*
					$wali_kelas = '<label class="select">
									<select name="id_karyawan" id="id_karyawan" class="chosen-select">
										<option value="">--Select--</option>';
					*/
					
					foreach ($get_wali_kelas as $v_walikelas) {
						
						$wali_kelas .='<option value="'.$v_walikelas['id'].'">'.$v_walikelas['nama_karyawan'].'</option>';
						
					}
					
					$wali_kelas .= '</select> 
								</label>';
					
					$nama_kelas='<div id="id-MappingKls"><label class="select">
									<select name="kelas" id="kelas" onchange="getRuangKelas(this.value);" >
									<option value="">--Select--</option></select> <i></i> 
								</label></div>';
					
					$get_smk = $this->kelas_model->getDataSMK();
					$nama_smk='<label class="select">
									<select name="smk" onchange="getJurusan(this.value);cekKelas(this.value, 0), getRuangKelas(0);" >
									<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>';
						
					$mapping = '<div id="id-Mapping">
					<label class="select">
						<select name="id_mapping_jur" id="id_mapping_jur">
							<option value="">--Select--</option>																
						</select> <i></i> 
					</label></div>';
					
					$data['header'] = 'Add Mapping Kelas';
					$data['submit'] = 'create';					
							
				} else {
					echo $this->gparam['restrict']; die();
				}	
			}			
			$data['wali_kelas'] = $wali_kelas;
			$data['nama_kelas'] = $nama_kelas;
			$data['nama_smk'] = $nama_smk;
			$data['mapping'] = $mapping;
			$this->load->view('crud_mapping_kelas',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}

	
	function getJurusan() {
		$id_smk = $this->input->post("smk");
		$options = $this->kelas_model->getJurusan($id_smk);
	
		//untuk SMK
		if($id_smk==1){
			$html = '<label class="select">
							<select name="id_mapping_jur" id="id_mapping_jur" onchange="cekKelas('.$id_smk.', this.value), getWaliKelas(0,0);">
								<option value="">--Select--</option>																
							'.$options.'
							</select> <i></i> 
						</label>';
		}
		//untuk SMA
		else if($id_smk==2){
			$html = '<label class="select">
							<select name="id_mapping_jur" id="id_mapping_jur" onchange="cekKelas('.$id_smk.', this.value);">																
							<option value="4"> - </option>
							</select> <i></i> 
						</label>';
		}
		//untuk SMP
		else if($id_smk==3){
			$html = '<label class="select">
							<select name="id_mapping_jur" id="id_mapping_jur" onchange="cekKelas('.$id_smk.', this.value);">															
							<option value="5"> - </option>
							</select> <i></i> 
						</label>';
		}else{
			$html = '<label class="select">
							<select name="id_mapping_jur" id="id_mapping_jur" onchange="cekKelas('.$id_smk.', this.value);">
								<option value="">--Select--</option>
							</select> <i></i> 
						</label>';
		}
		echo $html;			
	}
	function cekKelas() {
		
		$id_jur = $this->input->post("id_jur");
		$id_smk = $this->input->post("id_smk");
		$options = $this->kelas_model->cekKelas($id_smk);
		$html='<label class="select">
									<select name="kelas" id="kelas" onchange="getRuangKelas(this.value, '.$id_jur.');" >
									<option value="">--Select--</option>'.$options.'</select> <i></i> 
								</label>';
								
		echo $html;			
	}
	function getRuangKelas() {
		echo "<input id='ruang_kelas' type='text' name='ruang_kelas'  value='' autocomplete='off' />";
					
	}
	public function crud() {
		
		$crud = $this->input->post('submit');
		if ($crud == 'create') {
			
			$insert = $this->kelas_model->insert_data();
			if ($insert) {
				$json = true;
			}
			
		} else if ($crud == 'update') {
			$update = $this->kelas_model->update_data();
			if ($update) {
				$json = true;			
			}
				
		}
		
		echo json_encode($json);
		
	}
	
	public function delete() {
		$id = $this->input->post("id");
		$delete = $this->kelas_model->delete_data($id);
		if ($delete) {
			$json = true;
		}
		echo json_encode($json);	
	}
	
	function cekRuangKelas() {
		$id = "";
		$ruang 	= $this->input->post('ruang_kelas');
		$id 	= $this->input->post('id');
		$jur 	= $this->input->post('id_mapping_jur');
		$kls 	= $this->input->post('kelas');
		
		if ($id == "") {
			$cek = $this->kelas_model->cekRuang($ruang, $jur, $kls);			
			if ($cek > 0) {
				$json = false;
			} else {
				$json = true;
			}
		} else {
			$cek = $this->kelas_model->cekRuangUpdate($ruang, $jur, $kls, $id);			
			if ($cek > 0) {
				$json = false;
			} else {
				$json = true;
			}
		}
		
		echo json_encode($json);
	}
	
	function cekWaliKelas() {
		$id = "";
		$id_kar = $this->input->post('id_karyawan');
		$id 	= $this->input->post('id');
		
		if ($id == "") {
			$cek = $this->kelas_model->cekWali($id_kar);			
			if ($cek > 0) {
				$json = false;
			} else {
				$json = true;
			}
		} else {
			$cek = $this->kelas_model->cekWaliUpdate($id_kar,$id);			
			if ($cek > 0) {
				$json = false;
			} else {
				$json = true;
			}
		}
		
		echo json_encode($json);
	
	}
	
	function load_data() {
		
		header('Content-Type: application/jsonp');
        $list = $this->kelas_model->get_list_data();		
        echo json_encode($list);
	}
	
	
}
