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

class Cicilan_keuangan_ppdb extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('cicilan_keuangan_model_ppdb', 'ppdb_keuangan');
		$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') {
				$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.no_faktur+'\')\" href=\"javascript:void(0)\"\
									group=\"\" data-toggle=\"tooltip\" title=\"Update\"><i class=\"fa fa-pencil fa-lg\"></i>\
								  </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>";	
			/*
			$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>"; 
			}*/		

			
			
			// 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,
					'cetakFaktur' 			=> $cetakFaktur
					/*'preview' 				=> $preview
					'delete'				=> $delete_button*/
			);
			
			$this->load->view('cicilan_keuangan_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
					$edit = $this->ppdb_keuangan->get_value_edit($no_trans);					
					$data['header'] 			= 'Edit Pembayaran Pendaftaran';
					$data['submit'] 			= 'update';	
					
				} else {
					echo $this->gparam['restrict']; die();
				}
				
			} else {
				if ($this->gparam['privilage']->privilage_create == 'Y') {	
					
					$last_id = $this->ppdb_keuangan->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 = '';
					}
					
						
						$g_siswa = $this->ppdb_keuangan->get_siswa();
						//$siswa  = '<select name="id_daftar" class="selectpicker" data-live-search="true">
						$siswa  = '<option value="">--Select--</option>';
						//$siswa .='<label class="select">';						
						foreach($g_siswa as $v_siswa) {
								//$siswa .= '<option data-tokens="'.$v_siswa['nama'].'" value = "'.$v_siswa['id_daftar'].'" >'.$v_siswa['nama'].'</option>';							
								$siswa .= '<option value = "'.$v_siswa['id_siswa'].'" >'.$v_siswa['nama'].' ( '.$v_siswa['nisn'].' )</option>';							
						}
						
						/* $siswa .='<i></i>';							
						$siswa .= '</select>';
						$siswa .='</label>';*/
						
						$cicilan = '<div id="id-MappingCicilan">
					<label class="select">
						<select name="id_mapping_cicilan" id="id_mapping_cicilan" onchange="getDetailCicilan(this.value);">
							<option value="">--Select--</option>																
						</select> <i></i> 
					</label></div>';
					
					$data['cicilan']	= $cicilan;
					$data['last_id']   = $last_id;
					$data['siswa']   = $siswa;
					$data['header']  = 'Penarikan Tabungan Siswa (TABSIS)';
					$data['submit']  = 'create';
					$data['faktur']  = '<input id="no_faktur" type="text" name="no_faktur"  value="FK'.date('Ymd').$num.$last_id.'" readonly />'; 	
										
				} else {
					echo $this->gparam['restrict']; die();
				}	
			}				
			$this->load->view('crud_cicilan_keuangan_ppdb',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}
	
	function getDetailCicilan() {
		
		$idSiswa = $this->input->post("idSiswa");
		$get = $this->ppdb_keuangan->getDetailCicilan($idSiswa);
		
		$html = '
			<section class="col col-2">
				<label class="label">Total Tabsis</label>							
				<label class="input"> 
					<input id="sisa" type="text" name="sisa"  value="'.$get.'" readonly="readonly" />									
				</label>								
			</section>
			
		';
		echo $html;
		
	}
	
	function getCicilan() {
		
		$idSiswa = $this->input->post("idSiswa");
		$gets = $this->ppdb_keuangan->getCicilan();
		
		if(empty($idSiswa)){
						$html = '<div id="id-MappingCicilan">
						<label class="select">
							<select name="id_mapping_cicilan" id="id_mapping_cicilan" onchange="getDetailCicilan('.$idSiswa.',this.value);"><option value="">--Select--</option>	';

		}else{
			$html = '<div id="id-MappingCicilan">
						<label class="select">
							<select name="id_mapping_cicilan" id="id_mapping_cicilan" onchange="getDetailCicilan('.$idSiswa.',this.value);"><option value="">--Select--</option>	';
			foreach ($gets as $data) {
				$html .= '<option value="'.$data['Id'].'">'.$data['daftarlist'].'</option>';
			}
			$html .= '</select> <i></i> 
							</label></div>';
		}
		echo $html;
		
	}
	
	/*function get_kd_detail() {
	
		$kd_desk = $this->input->post('kd_desk');
		$get = $this->uang_masuk->get_kd_detail($kd_desk);
		
		$select  = '<label class="label">Kode Detail</label>';
		$select .= '<label class="select">
						<select name="kodesub[]">
							<option value="">--Select--</option>';
		foreach ($get as $value) {
			$select .= '<option value="'.$value['kodesub'].'">'.$value['daftarlist'].'</option>';
		}		
		$select .='</select>';
		$select .='<i></i>';
		$select .='</label>';
		
		echo $select;
		
	}*/
	
	public function crud() {
		
		$crud = $this->input->post('submit');
		if ($crud == 'create') {
			$id_fak = $this->ppdb_keuangan->next_faktur();
			$insert = $this->ppdb_keuangan->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();
			$update = $this->ppdb_keuangan->update_data();
			if ($update) {
				$json = true;			
			}
				
		}
		
		echo json_encode($json);
		
	}
	
	/*public function delete() {
		
		$delete = $this->uang_masuk->delete_data();
		if ($delete) {
			$json = true;
		}
		echo json_encode($json);	
	}*/
	
	function load_data() {
		
		header('Content-Type: application/jsonp');

        $list = $this->ppdb_keuangan->get_list_data();		
        echo json_encode($list);
	}
	
	
	function successPage() {
		$this->load->view("success_page");
	}
	
	function printFaktur() {
	
		define('FPDF_FONTPATH',$this->config->item('fonts_path'));
		$faktur = $_GET['faktur'];
		
		$getDetail  = $this->ppdb_keuangan->getDetail($faktur);
		$diserahkan = $this->ppdb_keuangan->getDiserahkan($faktur);
		$diterima  	= $this->ppdb_keuangan->getDiterima($faktur);
		$data = array(
			'total'	 	 => $getDetail->besarnya,
			'diserahkan' => $diserahkan,
			'diterima' => $diterima,
			'keterangan' => $getDetail->jenis_pembayaran,	 
			'faktur'	 => $faktur
		);
		
		$this->load->view("printPPDB",$data);
	}
	
}
