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

require_once(str_replace("\\","/",APPPATH).'helpers/Secure_area_adm.php');
class Registrasi extends Secure_area_adm {

	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url')); 

		$data['jenis_page'] 			 = "front_office/";
		$data['controller_dir']		 = 'reg_dir/';
		$data['controller'] 			 = 'registrasi';
		$data['parent_controller'] = 'registrasi';
		$data['active'] 					 = 'registrasi';
		//$data['smt_aktif'] 				 = $this->db->get_where('pddikti_re_semester',array('a_smt_aktif'=>'1'))->row();
		ini_set('max_execution_time', 0);
		ini_set('memory_limit', '-1');
		$this->load->vars($data);
		$this->load->helper('currency_format_helper');
	}

	public function index()
	{
		//echo $this->session->userdata('id_cabang');
		//echo $this->session->userdata('userid');
		$data['judul'] ='Pasien';
		$data['page']  ='registrasi/master';
		$data['desc_judul'] ='Menampilkan dan Mengelola Data Pasien';

		$data['breadcumb'][] = array('icon'=>'user',
																 'link'=>'',
																 'judul'=>'Pasien');

		//echo $this->session->userdata('userid');
		$this->load->view('template',$data);
	}
	
	public function data_table()
	{
		$draw   = $this->input->post('draw', true);
		$start  = $this->input->post('start', true);
		$length = $this->input->post('length', true);
		
		$this->db->start_cache();
		$this->db->from('bbmc_tr_pasien pasien');
		$this->db->join('bbmc_re_agama agama', 'agama.id_agama = pasien.id_agama', 'left');
		$this->db->join('bbmc_re_pekerjaan pkrjn', 'pkrjn.id_pekerjaan = pasien.id_pekerjaan', 'left');
		$this->db->join('bbmc_re_jenjang_pendidikan pnddkn', 'pnddkn.id_jenj_didik = pasien.id_pendidikan', 'left');
		$this->db->join('bbmc_re_wilayah wlyh', 'wlyh.id_wil = pasien.id_kecamatan', 'left');
		$this->db->where('pasien.deleted','0');
		//$this->db->where('agama.deleted','0');
		//$this->db->where('pkrjn.deleted','0');
		$this->db->where('pnddkn.deleted','0');	
		$this->db->stop_cache();
		$this->db->select('pasien.id_re_pasien');
		$recordsTotal = $this->db->count_all_results();
		$recordsFiltered = $recordsTotal;

		if($this->input->get('filter') == 'true')
		{
			parse_str($this->input->post('form_filter'), $form_filter);
			$this->db->select('pasien.id_re_pasien');
			$this->db->start_cache();
			foreach ($form_filter as $kolom => $value) {
				if($value) 
				{
					if($kolom == 'nama_pasien' || $kolom == 'tgl_registrasi' || $kolom == 'nrm') {
						$this->db->like($kolom, $value);
					} 
					else {
						if($kolom == 'id_type_pasien') {
							$this->db->where_in("pasien.id_type_pasien", $value);
						} elseif ($kolom == 'jenis_kelamin') {
							$this->db->where_in("pasien.jenis_kelamin", $value);
						}
					}	
				}
			}
				$this->db->stop_cache();
				$recordsFiltered = $this->db->count_all_results();
		}
		$order = $this->input->post('order', TRUE);
		if (isset($order)) 
		{
			foreach ($this->input->post('order', TRUE) as $key => $value) {
				$orderBy = $_POST['columns'][$value['column']]['data'];
				$orderType = $value['dir'];
				$this->db->order_by($orderBy, $orderType);
			}
		}
		$this->db->select('pasien.id_re_pasien as id_re_pasien, pasien.nrm as nrm, pasien.tgl_registrasi, pasien.nama_pasien, pasien.alamat, pasien.tmpt_lahir, pasien.tgl_lahir, pasien.umur_tahun, pasien.umur_bulan, 
											 pasien.jenis_kelamin, pasien.id_status, agama.nm_agama, pasien.telpon, pasien.email, pasien.gol_darah, pasien.kebangsaan, pnddkn.nm_jenj_didik, pkrjn.nm_pekerjaan, pasien.banjar, pasien.desa,
											 wlyh.nm_wil, pasien.id_type_pasien, pasien.id_perusahaan, pasien.id_nmr_asuransi');
		$this->db->limit($length, $start);
		$data = $this->db->get()->result_array();
		//echo $this->db->last_query();
		$this->db->flush_cache();
		$i=1; foreach ($data as $key => $row ) {
			
			$data[$key]['i'] = $i;

			$data[$key]['nrm'] = 'BBMC-'.$row['nrm'];

			$data[$key]['tgl_registrasi'] = $row['tgl_registrasi'];

			$data[$key]['nama_pasien'] = $row['nama_pasien'];

			$data[$key]['alamat_pasien'] = $row['alamat'];

			$data[$key]['id_type_pasien'] = $this->Content_m->get_value('bbmc_re_tipe_pasien','id_tipe_pasien',$row['id_type_pasien'],'tipe_pasien');

			if($row['id_perusahaan'] == 'gudang_garam') {
					$data[$key]['perusahaan_penanggung'] = "Gudang Garam";
			} elseif($row['id_perusahaan'] == 'dineta_jaya') {                                    
					$data[$key]['perusahaan_penanggung'] = "Dineta Jaya";
			} elseif ($row['id_perusahaan'] == 'bali_gatra') {
					$data[$key]['perusahaan_penanggung'] = "Bali Gatra";
			} else {
					$data[$key]['perusahaan_penanggung'] = "";
			}		

			$data[$key]['nomor_asuransi_jiwa'] = $row['id_nmr_asuransi'];

			$data[$key]['email'] = $row['email'];

			if($row['kebangsaan'] == 'wni') {
					$data[$key]['kebangsaan'] = "Warga Negara Indonesia (WNI)";
			} elseif($row['kebangsaan'] == 'wna') {                                    
					$data[$key]['kebangsaan'] = "Warga Negara Asing (WNA)";
			}else{
				$data[$key]['kebangsaan'] = "";
			}			

			$data[$key]['tempat_lahir'] = $row['tmpt_lahir'];

			$data[$key]['tanggal_lahir'] = $row['tgl_lahir'];

			$data[$key]['umur'] = $row['umur_tahun'].' Tahun '.$row['umur_bulan'].' Bulan' ;

			if($row['jenis_kelamin'] == 'L') {
					$data[$key]['jenis_kelamin'] = 'Laki - Laki';
			} elseif ($row['jenis_kelamin'] == 'P') {
					$data[$key]['jenis_kelamin'] = 'Perempuan';
			}else{
				$data[$key]['jenis_kelamin'] = '';
			}

			if($row['id_status'] == 'lajang') {
					$data[$key]['status'] = 'Lajang';
			}elseif ($row['id_status'] == 'menikah') {
					$data[$key]['status'] = 'Menikah';
			}elseif ($row['id_status'] == 'cerai') {
					$data[$key]['status'] = 'Cerai'; 
			}else{
				$data[$key]['status'] = ''; 
			}			

			$data[$key]['agama'] = $row['nm_agama'];

			$data[$key]['telepon_pasien'] = $row['telpon'];

			if($row['gol_darah'] == 'A') {
					$data[$key]['golongan_darah'] = 'Gol Darah A';
			} elseif ($row['gol_darah'] == 'B') {
					$data[$key]['golongan_darah'] = 'Gol Darah B';
			} elseif ($row['gol_darah'] == 'O') {
					$data[$key]['golongan_darah'] = 'Gol Darah O'; 
			}	elseif ($row['gol_darah'] == 'AB') {
					$data[$key]['golongan_darah'] = 'Gol Darah AB'; 
			}else{
				$data[$key]['golongan_darah'] = ''; 
			}

			$data[$key]['pendidikan_akhir'] = $row['nm_jenj_didik'];

			$data[$key]['pekerjaan'] = $row['nm_pekerjaan'];

			if(!empty($row['banjar'])) {			
				$data[$key]['alamat_banjar'] = $row['banjar'];
			} else {
				$data[$key]['alamat_banjar'] = '';
			}

			if(!empty($row['desa'])) {
				$data[$key]['alamat_desa'] = $row['desa'];
			} else {
				$data[$key]['alamat_desa'] = '';				
			}

			if(!empty($row['nm_wil'])) {
				$data[$key]['alamat_kecamatan'] = $row['nm_wil'];
			} else {
				$data[$key]['alamat_kecamatan'] = '';				
			}

			if(!empty($row['nm_wil'])) {
				$data[$key]['alamat_kabupaten'] = $row['nm_wil'];
			} else {
				$data[$key]['alamat_kabupaten'] = '';				
			}

			if(!empty($row['nm_wil'])) {
				$data[$key]['alamat_provinsi'] = $row['nm_wil'];
			} else {
				$data[$key]['alamat_provinsi'] = '';				
			}			


			//$data['nrm'] = $a;
			$tambah_kunjungan = '<a data-toggle="tooltip" class="btn-xs btn-info" style="background:green" data-placement="top" title="Tambah Kunjungan Pasien" href="'.site_url('front_office/reg_dir/kunjungan/tambah_kunjungan/'.$row['id_re_pasien']).'"><i class="fa fa-sign-in"></i></a>';              
			
			$edit = '<a data-toggle="tooltip" class="btn-xs btn-info" data-placement="top" title="Ubah Data Pasien" href="'.site_url('front_office/reg_dir/registrasi/edit_data/'.$row['id_re_pasien']).'"><i class="fa fa-edit"></i></a>';                     
			
			$delete = '<a data-toggle="tooltip" class="btn-xs btn-danger" data-placement="top" title="Hapus Pasien" onclick="viewHapusData(\''.$row['id_re_pasien'].'\',\''.$row['nama_pasien'].' '.$row['nrm'].' '.$row['id_type_pasien'].'\')"> <i class="fa fa-times"></i></a>';

			$data[$key]['aksi'] = $tambah_kunjungan.' '.$edit.' '.$delete;			

			$i++;
		}
		$response = array(
			"draw" => intval($draw),
			"recordsTotal" => $recordsTotal,
			"recordsFiltered" => $recordsFiltered,
			"data" => $data
		);
		echo json_encode($response);
	}

	public function tambah_data() 
	{
		$data['judul'] = 'Registrasi Pasien Baru';
		$data['desc_judul'] = 'Menambahkan Data Pasien Baru';
		$data['page'] = 'registrasi/add';

		$data['breadcumb'][]=array('icon'=>'user',
												 			 'link'=>site_url('front_office/reg_dir/registrasi'),
															 'judul'=>'Pasien');

		$data['breadcumb'][]=array('icon'=>'plus',
															 'link'=>'',
															 'judul'=>$data['judul']);

		$query = $this->Content_m->get_last_value('bbmc_tr_pasien','date_added','date_added');
		if($query != '') { 
			$val_nrm 		 = $query->nrm;
			$num     		 = $val_nrm + 1;
			$data['nrm'] = $num;
		} else {
			$data['nrm'] = 1;
		}															 

		$this->db->select('id_agama as id, nm_agama text');
		$this->db->where('id_agama!=', '98');
		$this->db->where('deleted','0');
		$data['agama']=json_encode($this->db->get('bbmc_re_agama')->result());		

		$this->db->select('id_pekerjaan as id, nm_pekerjaan text');
		$this->db->where('deleted','0');
		$data['pekerjaan']=json_encode($this->db->get('bbmc_re_pekerjaan')->result());

		$this->db->select('id_jenj_didik as id, nm_jenj_didik text');
		$this->db->where('deleted','0');
		$data['pendidikan']=json_encode($this->db->get('bbmc_re_jenjang_pendidikan')->result());		

		$this->load->view('template',$data);
	}

	public function tambah_data_proses_standard()
	{
		extract($_POST);
		if($this->validasi())
		{
			$id_re_pasien = $this->db->query("select UUID() as id")->row()->id;
			date_default_timezone_set('Asia/Makassar');
			$data_pasien = array(
				'id_re_pasien' => $id_re_pasien,
				'nrm' => $nrm,
				'tgl_registrasi' => $tgl_registrasi,
				'no_ktp'=>$no_ktp,
				'nama_pasien' => $nm_pasien,
				'alamat' => $alamat_pasien,
				'tmpt_lahir' => $tmpt_lahir,
				'tgl_lahir' => $tgl_lahir,
				'umur_tahun' => $umur_tahun,
				'umur_bulan' => $umur_bulan,
				'jenis_kelamin' => $jns_kel,
				'id_status' => $status,
				'id_agama' => $id_agama,
				'telpon' => $no_tel,
				'email' => $email,
				'gol_darah' => $gol_darah,
				'kebangsaan' => $kebangsaan,
				'id_pendidikan' => $id_jenjang_pendidikan,
				'id_pekerjaan' => $id_pekerjaan,
				'banjar' => $banjar,
				'desa' => $desa,
				'id_kecamatan' => $id_wil_kec,
				'id_kabupaten' => $id_wil_kab,
				'id_provinsi' => $id_wil_prov,
				'id_type_pasien' => $type_pasien,
				'id_perusahaan' => $id_perusahaan,
				'id_nmr_asuransi' => $no_kartu,
				'date_added' => date("Y-m-d H:i:s"),
				'added_by' => $this->session->userdata('userid'),
				'deleted' => 0
			);

			$this->db->insert('bbmc_tr_pasien', $data_pasien);
			echo json_encode(array("status"=>"1", "title"=>"Sukses !", "message"=>"Data Berhasil Ditambahkan"));
		} 
	}

	public function hapus_data_proses()
	{

		date_default_timezone_set('Asia/Makassar');
		$id_re_pasien = $this->input->post('id');
		if($this->db->update('bbmc_tr_pasien', array("deleted"=>"1","date_modified"=>date("Y-m-d H:i:s")), array("id_re_pasien"=>$id_re_pasien)))
		{
			
			echo json_encode(array("status"=>"1","title"=>"Sukses!","message"=>"Data Berhasil Dihapus"));
			//echo json_encode(array("status"=>"1","title"=>"Sukses!","message"=>$this->db->last_query()));
		}
		else
		{
			echo json_encode(array("status"=>"3","title"=>"Terjadi Kesalahan!","message"=>"Data Gagal Dihapus"));
		}
		
	}
	

	public function edit_data($id_re_pasien)
	{
		$data['re_pasien'] = $this->db->get_where('bbmc_tr_pasien', array('id_re_pasien'=>$id_re_pasien))->row();
		if($data['re_pasien'])
		{		
			$data['judul'] = 'Edit Data Pasien';
			$data['desc_judul'] = 'Mengubah Data Pasien';
			$data['page'] = 'registrasi/edit';
			$data['disabled'] = '';

			$data['breadcumb'][]=array('icon'=>'user',
																'link'=>site_url('front_office/reg_dir/registrasi'),
																'judul'=>'Pasien');

			$data['breadcumb'][]=array('icon'=>'edit',
																'link'=>'',
																'judul'=>$data['judul']);													 

			$data['get'] = $this->Content_m->get_row('bbmc_tr_pasien','id_re_pasien',$id_re_pasien);
			$data['id_re_pasien'] = $id_re_pasien;

			$this->db->select('id_agama as id, nm_agama text');
			$this->db->where('id_agama!=', '98');
			$this->db->where('deleted','0');
			$data['agama']=json_encode($this->db->get('bbmc_re_agama')->result());		

			$this->db->select('id_pekerjaan as id, nm_pekerjaan text');
			$this->db->where('deleted','0');
			$data['pekerjaan']=json_encode($this->db->get('bbmc_re_pekerjaan')->result());

			$this->db->select('id_jenj_didik as id, nm_jenj_didik text');
			$this->db->where('deleted','0');
			$data['pendidikan']=json_encode($this->db->get('bbmc_re_jenjang_pendidikan')->result());		

			$this->db->select('kec.id_wil as id, kec.nm_wil as text');
			$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(array('kec.id_level_wil'=>'3','kec.id_wil'=>$data['re_pasien']->id_kecamatan));
			$data['wilayah_kec']=$this->db->get('bbmc_re_wilayah kec')->row();

			$this->db->select('kab.id_wil as id, kab.nm_wil as text');
			$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(array('kec.id_level_wil'=>'3','kec.id_wil'=>$data['re_pasien']->id_kecamatan));
			$data['wilayah_kab']=$this->db->get('bbmc_re_wilayah kec')->row();

			$this->db->select('prov.id_wil as id, prov.nm_wil as text');
			$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(array('kec.id_level_wil'=>'3','kec.id_wil'=>$data['re_pasien']->id_kecamatan));
			$data['wilayah_prov']=$this->db->get('bbmc_re_wilayah kec')->row();			

			$this->load->view('template',$data);	
		} else
				show_404();				
	}

	public function edit_data_proses()
	{
		extract($_POST);
		if($this->validasi())
		{
			// $id_re_kunjungan = $this->db->query("select UUID() as id")->row()->id;
			date_default_timezone_set('Asia/Makassar');			
			$data_pasien = array(
				'tgl_registrasi' => $tgl_registrasi,
				'nama_pasien' => $nm_pasien,
				'nrm'=>$nrm,
				'no_ktp'=>$no_ktp,
				'alamat' => $alamat_pasien,
				'tmpt_lahir' => $tmpt_lahir,
				'tgl_lahir' => $tgl_lahir,
				'umur_tahun' => $umur_tahun,
				'umur_bulan' => $umur_bulan,
				'jenis_kelamin' => $jns_kel,
				'id_status' => $status,
				'id_agama' => $id_agama,
				'telpon' => $no_tel,
				'email' => $email,
				'gol_darah' => $gol_darah,
				'kebangsaan' => $kebangsaan,
				'id_pendidikan' => $id_jenjang_pendidikan,
				'id_pekerjaan' => $id_pekerjaan,
				'banjar' => $banjar,
				'desa' => $desa,
				'id_kecamatan' => $id_wil_kec,
				'id_kabupaten' => $id_wil_kab,
				'id_provinsi' => $id_wil_prov,
				'id_type_pasien' => $type_pasien,
				'id_perusahaan' => $id_perusahaan,
				'id_nmr_asuransi' => $no_kartu,
				'date_modified' => date("Y-m-d H:i:s"),
				'modified_by' => $this->session->userdata('userid'),
				'deleted' => '0'
			);

			$this->db->update('bbmc_tr_pasien', $data_pasien, array("id_re_pasien" => $id_re_pasien));
			echo json_encode(array("status"=>"1", "title"=>"Sukses !", "message"=>"Data Berhasil Diubah"));
		}
	}

	public function validasi()
	{
		// Form Tambah Kegiatan
		$this->form_validation->set_rules('tgl_registrasi', 'Tanggal Registrasi', 'trim|required');
		$this->form_validation->set_rules('nm_pasien', 'Nama Pasien', 'trim|required|min_length[3]');
		$this->form_validation->set_rules('alamat_pasien', 'Alamat Pasien', 'trim|required|min_length[3]');
		$this->form_validation->set_rules('tmpt_lahir', 'Tempat Lahir Pasien', 'trim|required');
		$this->form_validation->set_rules('tgl_lahir', 'Tanggal Lahir Pasien', 'trim|required');
		$this->form_validation->set_rules('umur_tahun', 'Umur Tahun', 'trim|required');
		//$this->form_validation->set_rules('umur_bulan', 'Umur Bulan', 'trim|required');
		//$this->form_validation->set_rules('jns_kel', 'Jenis Kelamin Pasien', 'trim|required');
		//$this->form_validation->set_rules('status', 'Status Pasien', 'trim|required');
		//$this->form_validation->set_rules('id_agama', 'Agama Pasien', 'trim|required');
		//$this->form_validation->set_rules('no_tel', 'No. Telepon Pasien', 'trim|required');
		//$this->form_validation->set_rules('email', 'Email Pasien', 'trim|required');
		//$this->form_validation->set_rules('gol_darah', 'Golongan Darah Pasien', 'trim|required');
		//$this->form_validation->set_rules('kebangsaan', 'Kebangsaan Pasien', 'trim|required');
		//$this->form_validation->set_rules('id_jenjang_pendidikan', 'Pendidikan Pasien', 'trim|required');
		//$this->form_validation->set_rules('id_pekerjaan', 'Pekerjaan Pasien', 'trim|required');
		//$this->form_validation->set_rules('banjar', 'Alamat Banjar Pasien', 'trim|required');
		//$this->form_validation->set_rules('desa', 'Alamat Desa Pasien', 'trim|required');
		//$this->form_validation->set_rules('id_wil_kec', 'Alamat Kecamatan Pasien', 'trim|required');
		$this->form_validation->set_rules('type_pasien', 'Tipe Pasien', 'trim|required');

		$this->form_validation->set_message('required', '{field} harus diisi!');
		$this->form_validation->set_message('numeric', '{field} harus berupa angka!');
		$this->form_validation->set_message('max_length', '{field} tidak boleh lebih dari {param} karakter!');
		$this->form_validation->set_message('min_length', '{field} tidak boleh kurang dari {param} karakter!');
		$this->form_validation->set_message('valid_email', '{field} tidak valid!');

		$valid = $this->form_validation->run();
		if($valid)
		{
			return $valid;
		}
		else
		{
			echo json_encode(array("status"=>"2", "title"=>"Error!", "message"=>"Terjadi kesalahan, silahkan periksa kembali data yang diinputkan", "detail_error"=>$this->form_validation->field_data()));
		}
	}	
  
}