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

class Ref_spp_type extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('ref_spp_type_model', 'spp_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('ref_spp_type_view',$data);
		} else {
			echo $this->gparam['restrict'];
		}
		
	}
	
	public function delete() {
		
		$delete = $this->spp_model->delete_data();
		if ($delete) {
			$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->spp_model->get_value_edit($id);
					
					$data['ref_name'] 		= $edit->ref_name;
					//$data['besarnya'] 		= $edit->besarnya;
					$data['id'] 			= $id;
					
					$get_smk = $this->spp_model->getDataSMK();
					$nama_smk='<label class="select">
									<select name="smk" >
									<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>';
								
					$type_bayar = array("Bayar Penuh", "Bayar Tidak Penuh", "Bebas Bayar");
					$type_disc='<label class="select">
									<select name="type_dis" onchange="getBesarnya(this.value, '.$edit->besarnya.', '.$edit->type_dis.');" >
									<option value="">--Select--</option>';
					for($i=1; $i<=3; $i++){
						if($edit->type_dis==$i){
							$type_disc .='<option value="'.$i.'" selected>'.$type_bayar[$i-1].'</option>';
						}else{
							$type_disc .='<option value="'.$i.'">'.$type_bayar[$i-1].'</option>';
						}
					}
					$type_disc .='</select> <i></i> 
								</label>';
					if(($edit->type_dis==1) || ($edit->type_dis==3)){
						$besarnya = '<div id="id-Besar">
							
								<label class="input"> 
									<input id="id-besarnya" type="hidden" name="besarnya"  value="'.$edit->besarnya.'" autocomplete="off" />									
								</label></div>';
					}else if($edit->type_dis==2){
						$besarnya = '<div id="id-Besar"><label class="label">Besarnya</label>
							
								<label class="input"> 
									<input id="id-besarnya" type="text" name="besarnya"  value="'.$edit->besarnya.'" autocomplete="off" />									
								</label></div>';
					}else{
						$besarnya = '<div id="id-Besar">
							
								<label class="input"> 
									<input id="id-besarnya" type="hidden" name="besarnya"  value="'.$edit->besarnya.'" autocomplete="off" />									
								</label></div>';
					}
										
					$data['header'] 		= 'Edit Reference SPP';
					$data['submit'] 		= 'update';	
				} else {
					echo $this->gparam['restrict']; die();
				}	
			} else {
				if ($this->gparam['privilage']->privilage_create == 'Y') {	
					
					$get_smk = $this->spp_model->getDataSMK();
					$nama_smk='<label class="select">
									<select name="smk" >
									<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>';
								
					$type_bayar = array("Bayar Penuh", "Bayar Tidak Penuh", "Bebas Bayar");
					$type_disc='<label class="select">
									<select name="type_dis" onchange="getBesarnya(this.value, 0, 0);" >
									<option value="">--Select--</option>';
					for($i=1; $i<=3; $i++){
						//if($edit->type_dis==$i){
						//	$type_disc .='<option value="'.$i.'" selected>'.$type_bayar[$i-1].'</option>';
						//}else{
							$type_disc .='<option value="'.$i.'">'.$type_bayar[$i-1].'</option>';
						//}
					}
					$type_disc .='</select> <i></i> 
								</label>';
					
						$besarnya = '<div id="id-Besar">
							
								<label class="input"> 
									<input id="id-besarnya" type="hidden" name="besarnya"  value="0" autocomplete="off" />									
								</label></div>';
					
					$data['header'] = 'Add Reference SPP';
					$data['submit'] = 'create';
				} else {
					echo $this->gparam['restrict']; die();
				}	
			}
			
			$tahun_ajaran = $this->spp_model->get_tahun_ajaran();
			
			$data['tahun_ajaran'] = '<input name="tahun_ajaran" value="'.$tahun_ajaran.'" type="hidden" id="id-tahun_ajaran">';
			$data['besarnya'] 		= $besarnya;
			$data['nama_smk'] 		= $nama_smk;
			$data['type_spp'] 		= $type_disc;
			$this->load->view('crud_ref_spp_type',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}
	
	function getBesarnya(){
		$type = $this->input->post("type");
		$besar = $this->input->post("besar");
		$type_edit = $this->input->post("type_edit");
		if(($type==1) || ($type==3)){
			$html = '
								<label class="input"> 
									<input id="id-besarnya" type="hidden" name="besarnya"  value="0" autocomplete="off" />									
								</label>';
		}else if($type==2){
			if($type == $type_edit){
				$html = '<label class="label">Besarnya</label>
								<label class="input"> 
									<input id="id-besarnya" type="text" name="besarnya"  value="'.$besar.'" autocomplete="off" />									
								</label>';	
			}else{
				$html = '<label class="label">Besarnya</label>
								<label class="input"> 
									<input id="id-besarnya" type="text" name="besarnya"  value="0" autocomplete="off" />									
								</label>';	
			}
		}else{
			$html = '
								<label class="input"> 
									<input id="id-besarnya" type="hidden" name="besarnya"  value="0" autocomplete="off" />									
								</label>';
		}
			
			
				
		echo $html;
	}
	public function crud() {
		
		$crud = $this->input->post('submit');
		if ($crud == 'create') {
			
			$insert = $this->spp_model->insert_data();
			if ($insert) {
				$json = true;
			}
			
		} else if ($crud == 'update') {
			$update = $this->spp_model->update_data();
			if ($update) {
				$json = true;			
			}
				
		}
		
		echo json_encode($json);
		
	}
	
	function load_data() {
		header('Content-Type: application/jsonp');
        $list = $this->spp_model->get_list_data();
		
        echo json_encode($list);
	}
	
	
}
