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

//require_once(str_replace("\\","/",APPPATH).'helpers/Secure_area_adm.php');
//class Ajax extends Secure_area_adm {
class Ajax extends CI_Controller { 
	function __construct()
    {
        parent::__construct();
        $this->load->model('M_general');
        $this->load->model('M_apotek');
    }


    public function get_datediff()
	{

		if($_POST['id'] != NULL) {

			$valu = $_POST['id'];
			
			$date1 = $valu;
			$date2 = date('Y-m-d');

			$diff = abs(strtotime($date2) - strtotime($date1));

			$years = floor($diff / (365*60*60*24));
			$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
			$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

			$opt[] = $years;
			$opt[] = $months;
			$opt[] = $days;

			

			echo json_encode($opt);
		}
	}


	public function select2_icd()
	{
		//$get_sp = $this->M_hakakses->get_role_keuangan();

		$array = array();
		$merge = array();
		
		
		
		//$untuk = $_POST['untuk'];
		$query=$_POST['term'];
		$limit=10;
		$page=$_POST['page'];
		$offset = ($page - 1) * $limit;


		
		$this->db->from('bbmc_re_icd s');
        $this->db->where('s.deleted', '0');
        $this->db->like('concat(s.icd," - ",s.diagnosa)', $query);
        
        $this->db->select('s.id_icd as id, concat(s.icd," - ",s.diagnosa) as value');
        $this->db->limit($limit,$offset);
        $results=$this->db->get();

		$results_daftar_online = $results->result_array();
		array_push($array, $results_daftar_online);
		$merge = array_merge($array[0]);
	
		
		

		$total_count = 10;
        $morePages = $total_count > ($offset + $limit);


		$response=array("results"=>$merge,"pagination" => array("more" => $morePages));

		echo json_encode($response);
	}

    
	public function select2_program_studi()
	{
		//$get_sms = $this->M_hakakses->get_role_akademik();
		$this->db->select('id_sms as id, concat(nm_lemb) as text');
		$this->db->where_in('id_sp',$_POST['id_sp']);
		$this->db->where("sms.deleted","0");
		$this->db->join('pddikti_re_jenjang_pendidikan jp', 'jp.id_jenj_didik=sms.id_jenj_didik and jp.deleted="0"','left');
		//$this->db->order_by('sms.id_jenj_didik','asc');
		$this->db->order_by('nm_lemb','asc');
		//$this->db->where_in('id_sms', $get_sms[2]);
		echo json_encode($this->db->get('pddikti_tr_sms sms')->result());
	}

	 public function select2_program_studis()
	{
		$this->db->select('id_sms as id, concat(nm_jenj_didik," ",nm_lemb) as text');
		$this->db->where_in('id_sp',$_POST['id_sp']);
		$this->db->where("sms.deleted","0");
		$this->db->join('pddikti_re_jenjang_pendidikan jp', 'jp.id_jenj_didik=sms.id_jenj_didik and jp.deleted="0"','left');
		$this->db->order_by('sms.id_jenj_didik','asc');
		$this->db->order_by('nm_lemb','asc');
		$this->db->where_in('id_sms', $this->session->userdata('id_sms'));
		$q = $this->db->get('pddikti_tr_sms sms');
		//echo $this->db->last_query();
		echo json_encode($q->result());
	}

	public function select2_kelas()
	{
		$this->db->select('id_kls as id, concat(mulai_smt," - ",nm_kls," - ",nama_cabang)  as text');
		$this->db->join('permohonan_daftar_cabang c','c.id_permohonan_daftar_cabang = kls.id_cabang');
		$this->db->where('id_sms',$_POST['id_sms']);
		$this->db->where('id_cabang',$_POST['id_cabang']);
		$this->db->where('mulai_smt',$_POST['mulai_smt']);
		$this->db->where("kls.deleted","0");
		//$this->db->join('pddikti_re_jenjang_pendidikan jp', 'jp.id_jenj_didik=sms.id_jenj_didik and jp.deleted="0"','left');
		//$this->db->order_by('kls.id_jenj_didik','asc');
		$this->db->order_by('nm_kls','asc');
		//$this->db->where_in('id_kls', $get_sms[2]);
		echo json_encode($this->db->get('pddikti_tr_kelas_kuliah kls')->result());
	}

	public function select2_mata_kuliah_pt()
	{
		//$get_sms = $this->M_hakakses->get_role_akademik();
		$this->db->select('id_mk as id, concat("Semester ",ganjil_genap,","," Tahun ",tahun,", ",kode_mk,"  ",nm_mk) as text');
		$this->db->where_in('id_sms',$_POST['id_sms']);
		$this->db->where("mk.deleted","0");
		//$this->db->join('pddikti_re_jenjang_pendidikan jp', 'jp.id_jenj_didik=sms.id_jenj_didik and jp.deleted="0"','left');
		//$this->db->order_by('mk.id_jenj_didik','asc');
		$this->db->order_by('nm_mk','asc');
	
		//$this->db->where_in('id_mk', $get_sms[2]);
		echo json_encode($this->db->get('pddikti_tr_mata_kuliah mk')->result());
	}

	public function select2_kelas_kuliah_pt()
	{
		//$get_sms = $this->M_hakakses->get_role_akademik();
		$this->db->select('id_kls as id, concat(mulai_smt, " - ",nm_kls," - ",nama_cabang)  as text');
		$this->db->join('permohonan_daftar_cabang c','c.id_permohonan_daftar_cabang = kls.id_cabang');
		$this->db->where_in('id_sms',$_POST['id_sms']);
		$this->db->where("kls.deleted","0");
		//$this->db->join('pddikti_re_jenjang_pendidikan jp', 'jp.id_jenj_didik=sms.id_jenj_didik and jp.deleted="0"','left');
		//$this->db->order_by('kls.id_jenj_didik','asc');
		$this->db->order_by('nm_kls','asc');
		//$this->db->where_in('id_kls', $get_sms[2]);
		echo json_encode($this->db->get('pddikti_tr_kelas_kuliah kls')->result());
	}

	public function select2_wilayah()
	{
		$query=$_POST['term'];
		$limit=10;
		$page=$_POST['page'];
		$offset = ($page - 1) * $limit;

		$this->db->start_cache();
		$this->db->from('bbmc_re_wilayah kec');
		$this->db->join('bbmc_re_wilayah kab', 'kec.id_induk_wilayah = kab.id_wil and kab.id_level_wil="2"');
		$this->db->join('bbmc_re_wilayah prov', 'kab.id_induk_wilayah = prov.id_wil and prov.id_level_wil="1"');
		$this->db->where('kec.id_level_wil', '3');
		$this->db->like('concat(kec.nm_wil," - ",kab.nm_wil," - ",prov.nm_wil)', $query);
		$this->db->stop_cache();
		$total_count=$this->db->count_all_results();

		$this->db->select('kec.id_wil as id, concat(kec.nm_wil," - ",kab.nm_wil," - ",prov.nm_wil) as value');
		$this->db->limit($limit,$offset);
		$results=$this->db->get()->result_array();
		$this->db->flush_cache();

        $morePages = $total_count > ($offset + $limit);

		$response=array("results"=>$results,"pagination" => array("more" => $morePages));
		echo json_encode($response);
	}

	public function select2_kewarganegaraan()
	{
		$query=$_POST['term'];
		$limit=10;
		$page=$_POST['page'];
		$offset = ($page - 1) * $limit;

		$this->db->start_cache();
		$this->db->from('pddikti_re_negara');
		$this->db->like('nm_negara', $query);
		$this->db->start_cache();
		$total_count=$this->db->count_all_results();

		$this->db->select('id_negara as id, nm_negara as value');
		$this->db->limit($limit,$offset);
		$results=$this->db->get()->result_array();
		$this->db->flush_cache();

        $morePages = $total_count > ($offset + $limit);

		$response=array("results"=>$results,"pagination" => array("more" => $morePages));
		echo json_encode($response);
	}

	public function select2_mata_kuliah()
	{
		if($_POST['id_sp'])
		{
			$query=$_POST['term'];
			$limit=10;
			$page=$_POST['page'];
			$offset = ($page - 1) * $limit;

			$this->db->start_cache();
			$this->db->from('pddikti_tr_mata_kuliah');
			$this->db->group_start();
			$this->db->like('nm_mk', $query);
			$this->db->or_like('kode_mk', $query);
			$this->db->group_end();
			$this->db->where('deleted', '0');
			$this->db->where_in('id_sms', 'select id_sms from pddikti_tr_sms where id_sp = "'.$_POST['id_sp'].'"',false);
			$this->db->order_by('kode_mk','asc');
			$this->db->start_cache();

			$total_count=$this->db->count_all_results();

			$this->db->select('id_mk as id, concat(kode_mk," - ",nm_mk," (",sks_mk," sks)") value');
			$this->db->limit($limit,$offset);
			$results=$this->db->get()->result_array();
			$this->db->flush_cache();

	        $morePages = $total_count > ($offset + $limit);

			$response=array("results"=>$results,"pagination" => array("more" => $morePages));
		}
		else
		{
			$response=array("results"=>array(array('value'=>false)));
		}
		echo json_encode($response);
	}

	public function select2_mk_transfer()
	{
		$query=$_POST['term'];
		$limit=10;
		$page=$_POST['page'];
		$offset = ($page - 1) * $limit;

		$this->db->start_cache();
		$this->db->from('pddikti_tr_mata_kuliah mk');
		$this->db->join('pddikti_tr_mata_kuliah_kurikulum mkk', 'mk.id_mk=mkk.id_mk and mkk.deleted="0"');
		$this->db->join('pddikti_tr_kurikulum k', 'k.id_kurikulum_sp=mkk.id_kurikulum_sp and k.deleted="0"');
		$this->db->like('concat(kode_mk," ",nm_mk)', $query);
		$this->db->where('mk.deleted', '0');
		$this->db->where_in('k.id_sms', 'select id_sms from pddikti_tr_sms where id_sp = "'.$_POST['id_sp'].'"',false);
		$this->db->order_by('kode_mk','asc');
		$this->db->start_cache();

		$total_count=$this->db->count_all_results();

		$this->db->select('mk.id_mk as id, concat(kode_mk," - ",nm_mk," (",mk.sks_mk," sks) ",nm_kurikulum_sp) value');
		$this->db->limit($limit,$offset);
		$results=$this->db->get()->result_array();
		$this->db->flush_cache();

        $morePages = $total_count > ($offset + $limit);

		$response=array("results"=>$results,"pagination" => array("more" => $morePages));

		echo json_encode($response);
	}

	public function select2_dosen_pengajar()
	{
		$query=$_POST['term'];
		$limit=10;
		$page=$_POST['page'];
		$offset = ($page - 1) * $limit;

		$this->db->start_cache();
		$this->db->from('pddikti_tr_dosen d');
		$this->db->join('pddikti_tr_dosen_pt dp', 'd.id_sdm=dp.id_sdm and dp.deleted="0"');
		$this->db->group_start();
		$this->db->like('nidn', $query);
		$this->db->or_like('nm_sdm', $query);
		$this->db->group_end();
		$this->db->where('d.deleted', '0');
		$this->db->where('dp.id_sp',$_POST['id_sp']);
		$this->db->where('id_thn_ajaran',$_POST['id_thn_ajaran']);
		$this->db->order_by('id_thn_ajaran','desc');
		$this->db->start_cache();

		$total_count=$this->db->count_all_results();

		$this->db->select('id_reg_ptk as id, concat(nidn," - ",nm_sdm) value');
		$this->db->limit($limit,$offset);
		$results=$this->db->get()->result_array();
		$this->db->flush_cache();

        $morePages = $total_count > ($offset + $limit);

		$response=array("results"=>$results,"pagination" => array("more" => $morePages));
		echo json_encode($response);
	}

	public function select2_peserta_kelas()
	{
		$query=$_POST['term'];
		$limit=10;
		$page=$_POST['page'];
		$offset = ($page - 1) * $limit;

		$this->db->start_cache();
		$this->db->from('siakad_tr_history_krs krs');
		$this->db->join('pddikti_tr_mahasiswa_pt mpt','krs.id_reg_pd=mpt.id_reg_pd and mpt.deleted="0"');
		$this->db->join('pddikti_tr_mahasiswa m', 'm.id_pd=mpt.id_pd and m.deleted="0"');
		$this->db->group_start();
		$this->db->like('nipd', $query);
		$this->db->or_like('nm_pd', $query);
		$this->db->group_end();
		$this->db->where('krs.deleted', '0');
		$this->db->where('id_status_krs', '2');
		$this->db->where('id_mk',$_POST['id_mk']);
		$this->db->where('krs.id_smt',$_POST['id_smt']);
		$this->db->order_by('nipd','asc');
		$this->db->start_cache();

		$total_count=$this->db->count_all_results();

		$this->db->select('id_krs as id, concat(nipd," - ",nm_pd) value');
		$this->db->limit($limit,$offset);
		$results=$this->db->get()->result_array();
		$this->db->flush_cache();

        $morePages = $total_count > ($offset + $limit);

		$response=array("results"=>$results,"pagination" => array("more" => $morePages));
		echo json_encode($response);
	}

	public function select2_krs_mhs()
	{
		$query=$_POST['term'];
		$limit=10;
		$page=$_POST['page'];
		$offset = ($page - 1) * $limit;

		$this->db->start_cache();
		$this->db->from('siakad_tr_history_krs krs');
		$this->db->join('pddikti_tr_kelas_kuliah k','krs.id_mk=k.id_mk and k.deleted="0" and k.id_smt="'.$_POST['id_smt'].'"');
		$this->db->join('pddikti_tr_mata_kuliah m', 'm.id_mk=k.id_mk and m.deleted="0"');
		$this->db->join('pddikti_tr_sms sms', 'sms.id_sms=k.id_sms and sms.deleted="0"');
		$this->db->like("concat(kode_mk,' ',nm_mk,' - ',nm_kls,' - ',coalesce(m.sks_mk,'0'),' sks (',nm_lemb,')')", $query);
		$this->db->where('krs.deleted', '0');
		$this->db->where('krs.id_smt',$_POST['id_smt']);
		$this->db->where('id_reg_pd',$_POST['id_reg_pd']);
		$this->db->order_by('k.id_sms','asc');
		$this->db->order_by('m.kode_mk','asc');
		$this->db->start_cache();

		$total_count=$this->db->count_all_results();

		$this->db->select("id_krs,id_kls as id, concat(kode_mk,' ',nm_mk,' - ',nm_kls,' - ',coalesce(m.sks_mk,'0'),' sks (',nm_lemb,')') value");
		$this->db->limit($limit,$offset);
		$results=$this->db->get()->result_array();
		$this->db->flush_cache();

        $morePages = $total_count > ($offset + $limit);

		$response=array("results"=>$results,"pagination" => array("more" => $morePages));
		echo json_encode($response);
	}

	public function select2_mhs_keluar()
	{
		$query=$_POST['term'];
		$limit=10;
		$page=$_POST['page'];
		$offset = ($page - 1) * $limit;

		$this->db->start_cache();
		$this->db->from('pddikti_tr_mahasiswa_pt mpt');
		$this->db->join('pddikti_tr_mahasiswa m','id_pd');
		$this->db->like("concat(nipd,' - ',nm_pd)", $query);
		$this->db->where('mpt.deleted', '0');
		$this->db->where('id_jns_keluar IS NULL');
		if (!in_array(1, $this->session->userdata('id_global_hakakses'))){ 
			$this->db->where('mpt.id_cabang',$this->session->userdata('id_cabang'));
		}
		if($this->session->userdata('id_sp') != ''){
			$this->db->where('mpt.id_sp',$this->session->userdata('id_sp'));
		}
		if($this->session->userdata('angkatan') != ''){
			$this->db->like('mpt.mulai_smt',$this->session->userdata('angkatan'));
		}
		
		$this->db->order_by('nipd','asc');
		$this->db->start_cache();

		$total_count=$this->db->count_all_results();

		$this->db->select("id_reg_pd as id, concat(nipd,' - ',nm_pd) value");
		$this->db->limit($limit,$offset);
		$results=$this->db->get()->result_array();
		$this->db->flush_cache();

        $morePages = $total_count > ($offset + $limit);

		$response=array("results"=>$results,"pagination" => array("more" => $morePages));
		echo json_encode($response);
	}

	public function select2_dosen_pembimbing()
	{
		$query=$_POST['term'];
		$limit=10;
		$page=$_POST['page'];
		$offset = ($page - 1) * $limit;

		$this->db->start_cache();
		$this->db->distinct();
		$this->db->from('pddikti_tr_dosen d');
		$this->db->join('pddikti_tr_dosen_pt dp','d.id_sdm=dp.id_sdm and dp.deleted="0" and id_sp="'.$_POST['id_sp'].'"');
		$this->db->like("concat(nidn,' - ',nm_sdm)", $query);
		$this->db->where('d.deleted', '0');
		$this->db->order_by('nidn','asc');
		$this->db->group_by('id_sdm');
		$this->db->start_cache();
		$this->db->select('d.id_sdm');
		$total_count=$this->db->count_all_results();

		$this->db->select("d.id_sdm as id, concat(nidn,' - ',nm_sdm) value");
		$this->db->limit($limit,$offset);
		$results=$this->db->get()->result_array();
		$this->db->flush_cache();

        $morePages = $total_count > ($offset + $limit);

		$response=array("results"=>$results,"pagination" => array("more" => $morePages));
		echo json_encode($response);
	}

	public function select2_nama_pasien()
	{
		$query = $_POST['term'];
		$limit = 10;
		$page = $_POST['page'];
		$offset = ($page - 1) * $limit;

		$this->db->start_cache();
		$this->db->from('bbmc_tr_pasien re_pasien');
		$this->db->like("concat(nrm,' - ',nama_pasien)", $query);
		$this->db->where('re_pasien.deleted', '0');

		$this->db->order_by('nrm','asc');
		$this->db->start_cache();

		$total_count = @$this->db->count_all_results();

		$this->db->select("id_re_pasien as id, concat('BBMC-',nrm,' - ',nama_pasien) value");
		$this->db->limit($limit, $offset);
		$results = $this->db->get()->result_array();
		$this->db->flush_cache();

        $morePages = $total_count > ($offset + $limit);

		$response = array("results"=>$results, "pagination"=>array("more" => $morePages));
		echo json_encode($response);
	}
	

	public function select2_penugasan_dosen()
	{
		$query=$_POST['term'];
		$limit=10;
		$page=$_POST['page'];
		$offset = ($page - 1) * $limit;

		$this->db->start_cache();
		$this->db->distinct();
		$this->db->from('pddikti_tr_dosen');
		$this->db->like("concat(nm_sdm)", $query);
		$this->db->where('deleted', '0');
		//$this->db->order_by('nidn','asc');
		$this->db->start_cache();
		$this->db->select('id_sdm');
		$total_count=$this->db->count_all_results();

		$this->db->select("id_sdm as id, nm_sdm value");
		$this->db->limit($limit,$offset);
		$results=$this->db->get()->result_array();
		$this->db->flush_cache();

        $morePages = $total_count > ($offset + $limit);

		$response=array("results"=>$results,"pagination" => array("more" => $morePages));
		echo json_encode($response);
	}

	public function get_mhs_ipk()
	{
		$this->db->select('sum(m.sks_mk*coalesce(nilai_indeks,0))/sum(m.sks_mk) as ipk');
		$this->db->from('pddikti_tr_nilai n');
		$this->db->join('pddikti_tr_kelas_kuliah k','id_kls');
		$this->db->join('pddikti_tr_mata_kuliah m', 'id_mk');
		$this->db->where(array('n.deleted'=>'0','n.id_reg_pd'=>$_POST['id_reg_pd']));
		$this->db->group_start();
		$this->db->where('nilai_huruf IS NOT NULL OR nilai_angka IS NOT NULL');
		$this->db->group_end();
		echo json_encode(number_format($this->db->get()->row()->ipk,2,',',''));
	}

	public function get_kuliah_mhs_data()
	{
		$this->db->select('sum(m.sks_mk*coalesce(nilai_indeks,0))/sum(m.sks_mk) as ipk');
		$this->db->from('pddikti_tr_nilai n');
		$this->db->join('pddikti_tr_kelas_kuliah k','id_kls');
		$this->db->join('pddikti_tr_mata_kuliah m', 'id_mk');
		$this->db->where(array('n.deleted'=>'0','n.id_reg_pd'=>$_POST['id_reg_pd']));
		$this->db->group_start();
		$this->db->where('nilai_huruf IS NOT NULL OR nilai_angka IS NOT NULL');
		$this->db->group_end();
		$ipk=number_format($this->db->get()->row()->ipk,2,',','');

		$this->db->select('sum(m.sks_mk*coalesce(nilai_indeks,0))/sum(m.sks_mk) as ips');
		$this->db->from('pddikti_tr_nilai n');
		$this->db->join('pddikti_tr_kelas_kuliah k','id_kls');
		$this->db->join('pddikti_tr_mata_kuliah m', 'id_mk');
		$this->db->where(array('n.deleted'=>'0','n.id_reg_pd'=>$_POST['id_reg_pd'],'k.id_smt'=>$_POST['id_smt']));
		$this->db->group_start();
		$this->db->where('nilai_huruf IS NOT NULL OR nilai_angka IS NOT NULL');
		$this->db->group_end();
		$ips=number_format($this->db->get()->row()->ips,2,',','');

		$this->db->select('coalesce(sum(m.sks_mk),0) as sks_mk');
		$this->db->from('pddikti_tr_nilai n');
		$this->db->join('pddikti_tr_kelas_kuliah k','id_kls');
		$this->db->join('pddikti_tr_mata_kuliah m', 'id_mk');
		$this->db->where(array('n.deleted'=>'0','n.id_reg_pd'=>$_POST['id_reg_pd']));
		$sks_total=$this->db->get()->row()->sks_mk;

		$this->db->select('coalesce(sum(m.sks_mk),0) as sks_mk');
		$this->db->from('pddikti_tr_nilai n');
		$this->db->join('pddikti_tr_kelas_kuliah k','id_kls');
		$this->db->join('pddikti_tr_mata_kuliah m', 'id_mk');
		$this->db->where(array('n.deleted'=>'0','n.id_reg_pd'=>$_POST['id_reg_pd'],'k.id_smt'=>$_POST['id_smt']));
		$sks_smt=$this->db->get()->row()->sks_mk;

		$response['ipk']=$ipk;
		$response['ips']=$ips;
		$response['sks_total']=$sks_total;
		$response['sks_smt']=$sks_smt;
		echo json_encode($response);
	}

	public function val_pelanggaran()
	{
		extract($_GET);

		if($_GET['id'] != NULL) {
			$val = $this->Content_m->get_total_pelanggaran($id, $tgl_awal=null, $tgl_akhir=null);

			$text = '-';
			if($val >= 50){
				$text = 'DROP OUT';
			}
			else if($val >= 40){
				$text = 'SP 3';
			}
			else if($val >= 25){
				$text = 'SP 2';
			}
			else if($val >= 10){
				$text = 'SP 1';
			}


			echo json_encode(array('val'=>$val,'val_text'=> $text, 'val_i'=>$i));
		}else{
			echo json_encode(array('val'=>'Error!','val_text'=> '0'));
		}	
	}


	public function val_drp()
	{

		extract($_GET);

		if($_GET['id'] != NULL) {
			
			$this->db->select('sum(point_pelanggaran) as point_pelanggaran');
            $this->db->from('siakad_tr_pelanggaran');
            $this->db->where('deleted','0');
            $this->db->where('tgl >=',$tgl_awal);
            $this->db->where('tgl <=',$tgl_akhir);
            $this->db->where('id_reg_pd',$id);
            $q = $this->db->get()->row()->point_pelanggaran;
            $p_pelanggaran = $q>0?$q:"0";


            $array_date = [];
			$this->db->select('tgl_absen');
			$this->db->from('siakad_tr_absen_kuliah');
			$this->db->where('id_reg_pd',$id);
			$this->db->where('tgl_absen >=',$tgl_awal);
			$this->db->where('tgl_absen <=',$tgl_akhir);
			$this->db->where('deleted','0');
			$this->db->group_by('tgl_absen');
			$get_jml_date_absen = $this->db->get();

			$i=0;foreach($get_jml_date_absen->result() as $keyb){
				$array_date[$i] = $keyb->tgl_absen;
				$i++;
			}

			$for = count($array_date);
			$drp=0;
			for ($ii=0; $ii < $for ; $ii++) { 
				$this->db->select('*');
				$this->db->from('siakad_tr_absen_kuliah');
				$this->db->where('id_reg_pd',$id);
				$this->db->where('tgl_absen',$array_date[$ii]);
				$where = "(absen = 'A' OR absen = 'I' OR absen = 'S')";
				$this->db->where($where);
				$this->db->where('deleted','0');
				$q = $this->db->get();

				if($q->num_rows() > 0){
				  $absen = $q->row()->absen;
				  //echo $this->db->last_query()."<br>";
				  $drp = $drp+1;
				}
			}


			$time_second = $this->Content_m->get_duration_of_study($tgl_awal,$tgl_akhir,$id);
			//echo $this->db->last_query();
			if($time_second != ''){
				$point_qooco = $this->Content_m->get_point_qooco_new($time_second);
				$str_qocoo = "Point: ".$point_qooco.", Duration: ".$this->Content_m->set_timesecond_to_time($time_second);
			}else{
				$point_qooco = 0;
				$str_qocoo = 'Tidak ditemukan file qooco';
			}
			
			//get point bisa di ringkas
			$point=$this->Content_m->get_total_pelanggaran($id);
            if($point >= 6){
                $uang_saku= "Rp. 0";
            }else{
                $p_uang_saku = $p_pelanggaran + $drp + $point_qooco;
                if($p_uang_saku == 0){ 
                  $uang_saku=  "Rp. 200.000";
                }else if($p_uang_saku == 1 || $p_uang_saku == 2){ 
                  $uang_saku=  "Rp. 100.000";
                }else{ 
                  $uang_saku=  "Rp. 0";
                }
            }

			echo json_encode(
				array(
					'val_pelanggaran'=>$p_pelanggaran,
					'val_ketidakhadiran'=>$drp,
					'val_resume_qooco'=>$str_qocoo,
					'val_uang_saku'=>$uang_saku,
					'val_i'=>$iiii
				)
			);
		}else{
			echo json_encode(array('val'=>'Error!','val_text'=> '0'));
		}	
	}

	public function val_absensi()
	{
		extract($_GET);


		if($_GET['id'] != NULL) {


			/*
			$h=$this->Content_m->cek_absen($id,$tgl_sekarang,$id_kls,'H',$id_mk,$id_absen_jam);
			//if($h){
				//$h=true;$d=false;$tl=false;$i=false;$s=false;$a=false;
			//}
			$d=$this->Content_m->cek_absen($id,$tgl_sekarang,$id_kls,'D',$id_mk,$id_absen_jam);
			$tl=$this->Content_m->cek_absen($id,$tgl_sekarang,$id_kls,'TL',$id_mk,$id_absen_jam);
			$i=$this->Content_m->cek_absen($id,$tgl_sekarang,$id_kls,'I',$id_mk,$id_absen_jam);
			$s=$this->Content_m->cek_absen($id,$tgl_sekarang,$id_kls,'S',$id_mk,$id_absen_jam);
			$a=$this->Content_m->cek_absen($id,$tgl_sekarang,$id_kls,'A',$id_mk,$id_absen_jam);
			*/

			$absen = $this->Content_m->cek_absen_new($id,$tgl_sekarang,$id_kls,$id_mk,$id_absen_jam);
			if($absen == 'H'){
				$h=true;$d=false;$tl=false;$i=false;$s=false;$a=false;
			}
			else if($absen == 'D'){
				$h=false;$d=true;$tl=false;$i=false;$s=false;$a=false;
			}
			else if($absen == 'TL'){
				$h=false;$d=false;$tl=true;$i=false;$s=false;$a=false;
			}
			else if($absen == 'I'){
				$h=false;$d=false;$tl=false;$i=true;$s=false;$a=false;
			}
			else if($absen == 'S'){
				$h=false;$d=false;$tl=false;$i=false;$s=true;$a=false;
			}
			else if($absen == 'A'){
				$h=false;$d=false;$tl=false;$i=false;$s=false;$a=true;
			}
			else{
				$h=false;$d=false;$tl=false;$i=false;$s=false;$a=false;
			}
			//$h=true;$d=false;$tl=false;$i=false;$s=false;$a=false;

			echo json_encode(
				array(
					'val_h'=>$h,
					'val_d'=>$d,
					'val_tl'=>$tl,
					'val_i'=>$i,
					'val_s'=>$s,
					'val_a'=>$a,
					'i'=>$iiii
				)
			);
		}else{
			echo json_encode(array('val'=>'Error!','val_text'=> '0'));
		}
	}

	public function val_detail_absen()
	{
		extract($_GET);
		if($_GET['id'] != NULL) {

			$this->db->select('*');
	        $this->db->from('siakad_tr_absen_kuliah');
	        $this->db->where('deleted','0');
	        $this->db->where('id_kls',$id_kls);
	        $this->db->where('tgl_absen',$tgl_absen);
	        $this->db->where('id_reg_pd',$id);
	        $qq = $this->db->get();
	        $text = '';
	        foreach($qq->result() as $keyb){ 
	           // $text = $text.$this->Content_m->get_value('siakad_tr_absen_jam','id_absen_jam',$keyb->id_absen_jam,'jam')." jam - ".$keyb->absen=='A'?'TK':$keyb->absen.", ";
	        	$jam = $this->Content_m->get_value('siakad_tr_absen_jam','id_absen_jam',$keyb->id_absen_jam,'jam');
	        	$absen = $keyb->absen=='A'?'TK':$keyb->absen;
	        	$sesi = $keyb->sesi;
	        	$kode_mk = $this->Content_m->get_value('pddikti_tr_mata_kuliah','id_mk',$keyb->id_mk,'kode_mk');
	        	$text = $text.$jam." jam - ".$kode_mk." : ".$absen." - sesi : ".$sesi."<br>";
	        
	        }

	       

			echo json_encode(
				array(
					'val_detail'=>$text	
				)
			);

		}
	}

	public function val_detail_absen_ekstra()
	{
		extract($_GET);
		if($_GET['id'] != NULL) {

			$this->db->select('*');
	        $this->db->from('siakad_tr_absen_kuliah');
	        $this->db->where('deleted','0');
	        $this->db->where('id_kls',$id_kls);
	        $this->db->where('sesi',$id_ekstra);
	        $this->db->where('tgl_absen',$tgl_absen);
	        $this->db->where('id_reg_pd',$id);
	        $qq = $this->db->get();
	        $text = '';
	        foreach($qq->result() as $keyb){ 
	           // $text = $text.$this->Content_m->get_value('siakad_tr_absen_jam','id_absen_jam',$keyb->id_absen_jam,'jam')." jam - ".$keyb->absen=='A'?'TK':$keyb->absen.", ";
	        	$absen = $keyb->absen=='A'?'TK':$keyb->absen;
	        	$text = $text.$this->Content_m->get_value('siakad_tr_absen_jam','id_absen_jam',$keyb->id_absen_jam,'jam')." jam - ".$absen."<br>";
	        
	        }

			echo json_encode(
				array(
					'val_detail'=>$text	
				)
			);

		}
	}


	public function val_retraining_mks()
	{
		extract($_GET);
		if($_GET['id'] != NULL) {
			echo json_encode(
				array(
					'val_id'=>$id,
					'val_detail'=>1,
					'val_i'=>$iiii,
					'val_biaya'=>1
				)
			);
		}
	}
	public function val_retraining_mk($ids=null, $id_mks=null, $id_klss=null)
	{

		if($ids!=null)
			$id = $ids;
		if($id_mks!=null)
			$id_mk = $id_mks;
		if($id_klss!=null)
			$id_kls = $id_klss;


		extract($_GET);
		if($_GET['id'] != NULL) {
			
			$this->db->select('SUM(CASE WHEN (ak.absen = "H" OR ak.absen = "D") THEN aj.jam ELSE 0 END) AS jam');
            $this->db->from('siakad_tr_absen_kuliah ak');
            $this->db->join('siakad_tr_absen_jam aj','ak.id_absen_jam = aj.id_absen_jam');
            $this->db->where('ak.id_reg_pd',$id);
            $this->db->where('ak.deleted','0');
            $this->db->where('aj.deleted','0');
            $this->db->where('ak.id_mk',$id_mk);
            $this->db->where('ak.id_kls',$id_kls);
            $a=$this->db->get()->row()->jam;
            
            $html = "";
            $ttl = $a>0?$a:"0";
            $ttl_biaya_retraining = 0;
            $biaya_retraining = 250000;
            if($kode_mk == 'KTO'){
            	$biaya_retraining = 500000;
            }

            $cek = $this->Content_m->get_value3('siakad_tr_retraining_complete','id_reg_pd',$id,'id_mk',$id_mk,'id_kls',$id_kls,'id_siakad_tr_retraining_complete');
            if($ttl==0){ 
            	if($cek == ''){
            		$html= '<span style="color:darkorange"><b>Retraining</b></span> <br><span style="font-size:9px">('.$this->Content_m->currency($biaya_retraining).')</span>';
            		$ttl_biaya_retraining = $biaya_retraining;
            	}
                else{
                	$html= '<span style="color:silver; text-decoration:line-through;"><b>Retraining</b></span> <br><span style="font-size:9px">('.$this->Content_m->currency(0).')</span>';
                }
               
               
            }else{
                                            
                                           
                if($cc != 0){ 

                    $persen_hadir = ($ttl/$cc)*100;
                    $persen_tdk_hadir = 100-$persen_hadir;
                    $cek_bekerja = $this->Content_m->get_value('pddikti_tr_mahasiswa_pt','id_reg_pd',$id,'status_bekerja');

                    if($cek_bekerja == 1){
                        $pembagi = 50;
                    }else{
                        $pembagi = 20;
                    } 
                    
                 	if($ttl >= $cc) {
                        $html= '<span style="color:darkred"><b>Excelent</b></span> <br><span style="font-size:9px">('.$this->Content_m->currency(0).')</span>';
            		}else if($persen_tdk_hadir <= $pembagi) {
                        $html= '<span style="color:darkgreen"><b>Complete</b></span> <br><span style="font-size:9px">('.$this->Content_m->currency(0).')</span>';
                 	}else { 
                 		if($cek == ''){
		            		$html= '<span style="color:darkorange"><b>Retraining</b></span> <br><span style="font-size:9px">('.$this->Content_m->currency($biaya_retraining).')</span>';
                 			$ttl_biaya_retraining = $biaya_retraining;
                 		}
		                else{
		                	$html= '<span style="color:silver; text-decoration:line-through;"><b>Retraining</b></span> <br><span style="font-size:9px">('.$this->Content_m->currency(0).')</span>';
		                }
               
                        
                  	}

                }
            }
            $show = "$ttl Jam. $html ";
			echo json_encode(
				array(
					'val_id'=>$id,
					'val_detail'=>$show,
					'val_i'=>$iiii,
					'val_biaya'=>$ttl_biaya_retraining
				)
			);

		}
	}

	public function val_nilai_adm_finaltest()
	{
		extract($_GET);
		$final_test = $this->Content_m->get_value2('siakad_tr_nilai_final_test','id_reg_pd',$id_reg_pd,'id_mk_sub',$id_mata_kuliah_sub,'nilai_final_test');

		if($_GET['id'] != NULL) {
			echo json_encode(
				array(
					'val_id'=>$id,
					'score_final'=>$final_test,
				
					'val_i'=>$i
				)
			);
		}

	}

	public function val_nilai_adm()
	{
		extract($_GET);

		$this->db->select('nilai_end_of_unit_test');
		$this->db->from('siakad_tr_nilai_end_of_unit_test');
		$this->db->where('id_reg_pd',$id_reg_pd);
		$this->db->where('id_mk_sub',$id_mata_kuliah_sub);
		$q = $this->db->get();
		//echo $this->db->last_query();

		//$end_of_unit_test  = $this->Content_m->get_value2('siakad_tr_nilai_end_of_unit_test','id_reg_pd',$id_reg_pd,'id_mk_sub',$id_mata_kuliah_sub,'nilai_end_of_unit_test');
		if($q->num_rows() > 0){
			$end_of_unit_test = $q->row()->nilai_end_of_unit_test;
		}else{
			$end_of_unit_test = 0;
		}
		$nilai_eout = $end_of_unit_test==0?'':$end_of_unit_test;


		$this->db->select('nilai_final_test');
		$this->db->from('siakad_tr_nilai_final_test');
		$this->db->where('id_reg_pd',$id_reg_pd);
		$this->db->where('id_mk_sub',$id_mata_kuliah_sub);
		$qq = $this->db->get();
		//$final_test = $this->Content_m->get_value2('siakad_tr_nilai_final_test','id_reg_pd',$id_reg_pd,'id_mk_sub',$id_mata_kuliah_sub,'nilai_final_test');

		if($qq->num_rows() > 0){
			$final_test = $qq->row()->nilai_final_test;
		}else{
			$final_test = 0;
		}
		$nilai_ft = $final_test==0?'':$final_test;

		
		if($end_of_unit_test > 0){ 
            $nilai1= ($end_of_unit_test/10)*7;
        }else{
            $nilai1= "0";
        } 

        if($final_test > 0){ 
            $nilai2= ($final_test/10)*3;
        }else{
            $nilai2= "0";
        } 

        $score = $nilai1+$nilai2; 
        $score = round($score);
        if($score == 0)
            $score = "-"; 

		$nilai = '-';
        if($score == 0 ){
            $nilai = "-";
        }
        if($score >= 1 && $score <= 55){
            $nilai = "D";
        }
        if($score >= 55 && $score <= 69){
            $nilai = "C";
        }
        if($score >= 69 && $score <= 85){
            $nilai = "B";
        }
        if($score >= 85 && $score <= 100){
            $nilai = "A";
        } 
        

		if($_GET['id'] != NULL) {
			echo json_encode(
				array(
					'val_id'=>$id,
					'score_final'=>$score,
					'nilai_angka'=>$nilai_eout,
					'val_i'=>$i,
					't_nilai'=>$nilai,
					'nilai_final'=>$nilai_ft
				)
			);
		}
	}

	public function val_ret()
	{
		extract($_GET);
		echo "string";
		header("Content-type: application/x-msdownload");
		header("Content-Disposition: attachment; filename="."asd.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
	}

	public function val_akd_nilai($id_reg_pd=null)
	{
		$get_id_reg_pd = $id_reg_pd;
		extract($_GET);

		$rata_rata = $this->Content_m->akd_nilai_rata_rata($id_reg_pd);
        $akd_nilai_perkuliahan = $this->Content_m->get_value('siakad_tr_drp_motor_akd_nilai','nilai_rata_rata',$rata_rata,'point_drp');

        if($akd_nilai_perkuliahan == ''){
        	$akd_nilai_perkuliahan = '0.0';
        }
        //echo $this->db->last_query();
        //echo;	

        if($get_id_reg_pd == null){
        	echo json_encode(
				array(
					'val_text'=>$akd_nilai_perkuliahan,
				)
			);
        }else{
        	return $akd_nilai_perkuliahan;
        }
		
	}

	public function val_akd_ketidakhadiran($id_reg_pd=null,$id_sp=null)
	{
		$get_id_reg_pd = $id_reg_pd;
		extract($_GET);

		$ketidakhadiran = $this->Content_m->akd_ketidakhadiran($id_reg_pd);

        $jmlh_smt = $this->Content_m->get_value('pddikti_tr_satuan_pendidikan','id_sp',$id_sp,'jmlh_smt');
        $this->db->select('*');
        $this->db->from('siakad_tr_drp_motor_akd_kehadiran');
        $this->db->where('ketidakhadiran',$ketidakhadiran);
        $this->db->where('jmlh_smt',$jmlh_smt);
        $q = $this->db->get();

        if($q->num_rows() == 0){
        	$ttl_tdkhdr = '0.0';
        }else{
        	$ttl_tdkhdr = $q->row()->point_drp;
        }

       
        $this->db->where('id_reg_pd',$id_reg_pd);
        $qq = $this->db->get('siakad_tr_absen_kuliah')->num_rows();
        if($qq == 0){
        	$ttl_tdkhdr = '0.0';
        }
        //echo $this->db->last_query();
        //echo;	
        
        if($get_id_reg_pd == null){
			echo json_encode(
				array(
					'val_text'=>$ttl_tdkhdr,
				)
			);
		}else{
        	return $ttl_tdkhdr;
        }

		
	}

	public function val_kks_kehadiran($id_reg_pd=null)
	{
		$get_id_reg_pd = $id_reg_pd;
		extract($_GET);
		$kehadiran = $this->Content_m->kks_kehadiran($id_reg_pd);
		$kks_kehadiran = $this->Content_m->get_value('siakad_tr_drp_motor_kks_kehadiran','kehadiran',$kehadiran,'point_drp');

		if($kks_kehadiran == '')
			$kks_kehadiran = '0.0';
		
		if($get_id_reg_pd == null){
			echo json_encode(
				array(		
					'val_text'=>$kks_kehadiran,
				)
			);
		}else{
        	return $kks_kehadiran;
        }
	}

	public function val_mar_medi_cc($id_reg_pd=null,$id_sp=null)
	{
		$get_id_reg_pd = $id_reg_pd;
		extract($_GET);
		$medi_cc = $this->Content_m->peringkat_medi_cc($id_reg_pd,$id_sp);
        
		$peringkat = $this->Content_m->get_value('siakad_tr_drp_motor_mar_medi_cc','peringkat',$medi_cc['peringkat'],'point_drp');
		if($peringkat == '')
			$peringkat = '0.0';

		
		if($get_id_reg_pd == null){
			echo json_encode(
				array(
					'val_text'=>$peringkat,
				)
			);
		}else{
        	return $peringkat;
        }
	

	}

	public function val_kks_prestasi($id_reg_pd=null)
	{
		$get_id_reg_pd = $id_reg_pd;
		extract($_GET);
		$point = $this->Content_m->point_restasi($id_reg_pd);

		if($get_id_reg_pd == null){
			echo json_encode(
			array(
					'val_text'=>$point,
				)
			);
		}else{
        	return $point;
        }
	
		
		
	}

	public function val_ttl_point()
	{
		extract($_GET);
		$a = $this->val_akd_nilai($id_reg_pd);
		$b = $this->val_akd_ketidakhadiran($id_reg_pd,$id_sp);
		$c = $this->val_kks_kehadiran($id_reg_pd);
		$d = $this->val_kks_prestasi($id_reg_pd,$id_sp);
		$e = $this->val_mar_medi_cc($id_reg_pd);

		$ttl = $a + $b + $c + $d + $e ;

		$data['drp_ttl_point'] = $ttl;
		$data['drp_tgl_generate'] = date('Y-m-d h:i:s'); 
		$data['drp_id_sdm_generate'] = $this->session->userdata('userid');
		$this->db->where('id_reg_pd',$id_reg_pd);
		$this->db->update('pddikti_tr_mahasiswa_pt',$data);
		
		echo json_encode(
			array(
				'val_text'=>$ttl,
			)
		);
	}

	public function val_peringkat()
	{
		extract($_GET);

		$peringkat = $this->Content_m->get_peringkat($id_reg_pd, $id_sp, $id_cabang, $mulai_smt );

		echo json_encode(
			array(
				'val_text'=>$peringkat
			)
		);
	}

	public function val_stok_obat()
	{
		extract($_GET);
		
		$stok = array();
		$tertinggi = array();

		foreach ($this->M_general->select_db('bbmc_re_apotik_obat')->result() as $key ) {
			//ini bisa di get semua skaligur biar gak lambat
			$data_stok[$key->id_re_apotik_obat] = $this->M_apotek->get_sisa_stok($key->id_re_apotik_obat) + $key->stok_awal;
			$data_tertinggi[$key->id_re_apotik_obat] = $this->M_apotek->get_pembelian_tertinggi($key->id_re_apotik_obat);

			
		}
		

		array_push($stok, $data_stok);
		array_push($tertinggi, $data_tertinggi);

		echo json_encode(
			array(
				'val_stok'=>$stok,

				'val_harga_beli_tertinggi'=>$tertinggi,
			)
		);

	}

	public function upload_file_foto($id_reg_pd,$id_kunjungan, $id_tindakan)
	{
		user_error( print_r( $_FILES, true ) );

		$file_name = $_FILES[ 'file' ][ 'name' ];
		$uploads_dir = 'media/hasil_lab/';
		$target_path = $uploads_dir . basename( $_FILES[ 'file' ][ 'name' ] );

		if ( move_uploaded_file( $_FILES[ 'file' ][ 'tmp_name' ], $target_path ) )
		{
		    //echo 'File uploaded: ' . $target_path;
		    $this->db->trans_start();

		    //$data['file'] = $file_name;
		    //$this->db->where('id_re_laboratorium',$id_lab);
		    //$this->db->update('bbmc_tr_laboratorium',$data);

		    //id_reg_pd
		    //id_tindakan
		    //id_kunjungan
		    $this->db->where('id_reg_pd',$id_reg_pd);
		    $this->db->where('tindakan_lab',$id_tindakan);
		    $this->db->where('id_re_kunjungan',$id_kunjungan);
		    $this->db->where('file',$file_name);
		    $this->db->delete('bbmc_tr_laboratorium_dokumen');

		    $data = array(
		    	'id_reg_pd'=>$id_reg_pd,
		    	'tindakan_lab'=>$id_tindakan,
		    	'id_re_kunjungan'=>$id_kunjungan,
		    	'file'=>$file_name
		    );
		    $this->db->insert('bbmc_tr_laboratorium_dokumen',$data);

		    $this->db->trans_complete();
		}
		else
		{
		    echo 'Error in uploading file ' . $target_path;
		}
	}

	
	public function upload_file($id_reg_pd,$id_kunjungan, $id_tindakan)
	{
		user_error( print_r( $_FILES, true ) );

		$file_name = $_FILES[ 'file' ][ 'name' ];
		$uploads_dir = 'media/hasil_lab/';
		$target_path = $uploads_dir . basename( $_FILES[ 'file' ][ 'name' ] );

		if ( move_uploaded_file( $_FILES[ 'file' ][ 'tmp_name' ], $target_path ) )
		{
		    //echo 'File uploaded: ' . $target_path;
		    $this->db->trans_start();

		    //$data['file'] = $file_name;
		    //$this->db->where('id_re_laboratorium',$id_lab);
		    //$this->db->update('bbmc_tr_laboratorium',$data);

		    //id_reg_pd
		    //id_tindakan
		    //id_kunjungan
		    $this->db->where('id_reg_pd',$id_reg_pd);
		    $this->db->where('tindakan_lab',$id_tindakan);
		    $this->db->where('id_re_kunjungan',$id_kunjungan);
		    $this->db->where('file',$file_name);
		    $this->db->delete('bbmc_tr_laboratorium_dokumen');

		    $data = array(
		    	'id_reg_pd'=>$id_reg_pd,
		    	'tindakan_lab'=>$id_tindakan,
		    	'id_re_kunjungan'=>$id_kunjungan,
		    	'file'=>$file_name
		    );
		    $this->db->insert('bbmc_tr_laboratorium_dokumen',$data);

		    $this->db->trans_complete();
		}
		else
		{
		    echo 'Error in uploading file ' . $target_path;
		}
	}

	public function notif()
	{


		if($this->session->userdata('id_jabatan') == '8'){
			//jika pegawe lab
			$where = "(`status_perawatan` = '4' OR `pemeriksaan_khusus` LIKE '%cek_laboratorium%')";
			$this->db->where($where);
			$this->db->where('batal_periksa','0');
			$this->db->where('bell','1');
			$this->db->like('tgl_kunjungan',date('Y-m-d'));
			$this->db->order_by('tgl_kunjungan');
			$q= $this->Content_m->get_kunjungan_pasien();
			//echo $this->db->last_query();

			$qa = $q->num_rows();

			//echo $this->db->last_query();
			if($qa >= 1){
				echo "notif_on";
			}else{
				echo "notif_off";
			}

		}
		
		else if($this->session->userdata('id_jabatan') == '10'){
			//jika pegawe lab
			$this->db->where('batal_periksa','0');
			$this->db->where('status_perawatan','2');
			$this->db->where('bell','1');
			$this->db->like('tgl_kunjungan',date('Y-m-d'));
			$this->db->order_by('tgl_kunjungan');
			$q= $this->Content_m->get_kunjungan_pasien();
			//echo $this->db->last_query();

			$qa = $q->num_rows();

			//echo $this->db->last_query();
			if($qa >= 1){
				echo "notif_on";
			}else{
				echo "notif_off";
			}

		}else{
			echo "notif_off";
		}


		
		
	}



}
