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

class Pihak_list extends CI_Controller {
	function index(){

		#add authorization check hak akses di pihak
		#
		if($this->session->userdata('is_logged_in')===FALSE){
			redirect('login');
		}
		$segment = $this->uri->segment_array();
		$enc = $this->uri->segment(2);

		$pageNumber = $this->nativesession->get_flash_session('pageNumber_pihak');
		if(empty($pageNumber)){
			$pageNumber = 1;
			$begin = 0;
		}else{
			$begin = ($pageNumber-1)*30;
		}


		$data['isorangtuawali'] = 1;
		$data['idperkara'] = $this->encrypt->decode(base64_decode($enc));
		$data['enc_id'] = '';
		$data['enc_anak'] = '';
		$data['jenis_pihak'] = '';
		$data['popup'] = $this->nativesession->get_flash_session('popup');
		$this->nativesession->set_flash_session('popup', $data['popup']);
		if(count($segment)==5){
			if($segment[3]=='ortu'){
				$data['enc_id'] = $segment[2];
				$data['enc_anak'] = $segment[4];
				$data['jenis_pihak'] = $segment[5];
				$data['isorangtuawali'] = 2;
			}
		}

		if(count($segment)==5){
			if($segment[3]=='ortudiversi'){
				$data['enc_id'] = $segment[2];
				$data['enc_anak'] = $segment[4];
				$data['jenis_pihak'] = $segment[5];
				$data['isorangtuawali'] = 24;
			}
		}

		if(count($segment)==5){
			if($segment[3]=='ortudiversi_korban'){
				$data['enc_id'] = $segment[2];
				$data['enc_anak'] = $segment[4];
				$data['jenis_pihak'] = $segment[5];
				$data['isorangtuawali'] = 25;
			}
		}

		$keyword = $this->nativesession->get_flash_session('keyword_pihak');
		$this->load->model('pihak/list_pihak', 'pihak');
		$data['pihak'] = $this->pihak->getListPihak(9,'DESC',$begin,$keyword);
		$data['forPihakKe'] = $this->nativesession->get('forPihakKe');
		$data['total_rows'] = $this->pihak->fecth_data_num();
		$data['page_number'] = $pageNumber;
		$data['current_date'] = date('d/m/Y');
		$data['counter'] = $begin;
		$data['keyword'] = $keyword;
		$data['list_jenis_pihak'] = $this->pihak->getListJenisPihak();
		$data['country'] = $this->pihak->getListCountry();
		$data['agama'] = $this->pihak->getListAgama();
		$data['pendidikan'] = $this->pihak->getListPendidikan();
		$this->load->vars($data);
		$this->load->view('pihak/pihak_list');
	}

	function addOrtu(){
		if(empty($_POST)){
			show_404();
		}
		$idPihak = $this->encrypt->decode(base64_decode($this->input->post('val',TRUE)));
		if(is_numeric($idPihak)){
			if($idPihak>0){
				$data = $this->fetchPihak(1);
				if($data['isFound']==0){
					echo json_encode(array('st'=>0,'msg'=>'Error, Data Tidak Ditemukan.'));
					return;
				}elseif($data['isLengkap']==0){
					echo json_encode(array('st'=>0,'msg'=>'Data Tidak Lengkap, Data <b>'.$data['notFill'].'</b> Tidak Ada. Silahkan Lengkapi terlebih dahulu. Klik Edit.'));
					return;
				}else{
					$idAnak = $this->encrypt->decode(base64_decode($this->input->post('enc_anak',TRUE)));
					
					if(!is_numeric($idAnak) OR empty($idAnak)){
						echo json_encode(array('st'=>0,'msg'=>'Error, Data Anak Tidak Ditemukan.'));
						return;
					}
					$idperkara = $this->encrypt->decode(base64_decode($this->input->post('enc_id',TRUE)));
					if(!is_numeric($idperkara) OR empty($idperkara)){
						echo json_encode(array('st'=>0,'msg'=>'Error, Data Perkara Tidak Ditemukan.'));
						return;
					}

					$jenisPihak = $this->input->post('jenis_pihak',TRUE);
					if(!is_numeric($jenisPihak)){
						echo json_encode(array('st'=>0,'msg'=>'Error, Data Jenis Pihak Tidak Ditemukan.'));
						return;
					}
					if($jenisPihak<0 AND $jenisPihak>1){
						echo json_encode(array('st'=>0,'msg'=>'Error, Data Jenis Pihak Tidak Sesuai.'));
						return;
					}
					$this->load->model('pihak/list_pihak', 'pihak');
					$data_ortu = array(
						'id_anak' => $idAnak,
						'id_orang_tua' => $idPihak,
						'perkara_id' => $idperkara,
						'jenis_pihak_anak' => $jenisPihak
					);
					$result = $this->pihak->add_data_ortu($data_ortu);
					if($result===TRUE){
						echo json_encode(array('st'=>1,'msg'=>'Orang Tua Wali Berhasil Disimpan'));
						return;
					}else{
						echo json_encode(array('st'=>0,'msg'=>'ERROR, Orang Tua Wali TIDAK Berhasil Disimpan'));
						return;
					}
				}
			}
		}
	}

	function addOrtuDiversi(){
		if(empty($_POST)){
			show_404();
		}

		$idPihak = $this->encrypt->decode(base64_decode($this->input->post('val',TRUE)));
		
		if(is_numeric($idPihak)){
			if($idPihak>0){
				$data = $this->fetchPihak(1);
				if($data['isFound']==0){
					echo json_encode(array('st'=>0,'msg'=>'Error, Data Tidak Ditemukan.'));
					return;
				}elseif($data['isLengkap']==0){
					echo json_encode(array('st'=>0,'msg'=>'Data Tidak Lengkap, Data <b>'.$data['notFill'].'</b> Tidak Ada. Silahkan Lengkapi terlebih dahulu. Klik Edit.'));
					return;
				}else{
					$idAnak = $this->encrypt->decode(base64_decode($this->input->post('enc_anak',TRUE)));
					
					if(!is_numeric($idAnak) OR empty($idAnak)){
						echo json_encode(array('st'=>0,'msg'=>'Error, Data Anak Tidak Ditemukan.'));
						return;
					}
					$idperkara = $this->encrypt->decode(base64_decode($this->input->post('enc_id',TRUE)));
					if(!is_numeric($idperkara) OR empty($idperkara)){
						echo json_encode(array('st'=>0,'msg'=>'Error, Data Perkara Tidak Ditemukan.'));
						return;
					}

					$jenisPihak = $this->input->post('jenis_pihak',TRUE);
					if(!is_numeric($jenisPihak)){
						echo json_encode(array('st'=>0,'msg'=>'Error, Data Jenis Pihak Tidak Ditemukan.'));
						return;
					}
					if($jenisPihak<0 AND $jenisPihak>1){
						echo json_encode(array('st'=>0,'msg'=>'Error, Data Jenis Pihak Tidak Sesuai.'));
						return;
					}
					$this->load->model('diversi/data_pihak_diversi', 'pihak_diversi');
					$status_anak = $this->input->post('status_anak',TRUE);
					if($status_anak==24){
						$result = $this->pihak_diversi->add_data_ortu_diversi($idPihak,$idperkara,$idAnak);	
					}else if($status_anak==25){
						$result = $this->pihak_diversi->add_data_ortu_diversi_korban($idPihak,$idperkara,$idAnak);	
					}
					
					if($result===TRUE){
						echo json_encode(array('st'=>1,'msg'=>'Orang Tua Wali Berhasil Disimpan'));
						return;
					}else{
						echo json_encode(array('st'=>0,'msg'=>'ERROR, Orang Tua Wali TIDAK Berhasil Disimpan'));
						return;
					}
				}
			}
		}
	}

	function fetchPihak($status=0){
		if(empty($_POST)){
			show_404();
		}
		if($this->session->userdata('is_logged_in')==FALSE){
			echo json_encode(array('st'=>0,'msg'=>'Please Refresh Your Page'));
			return;
		}
		$idPihak = $this->encrypt->decode(base64_decode($this->input->post('val',TRUE)));
		if(is_numeric($idPihak)){
			if($idPihak>0){
				$this->load->model('pihak/list_pihak', 'pihak');
				$result = $this->pihak->getInfoPihak($idPihak);
				if($result!=''){
					if($result->num_rows()>0){
						foreach ($result->result() as $row) {
							$id = base64_encode($this->encrypt->encode($row->id));
							$nama = $row->nama;
							$alamat = $row->alamat;
							$pekerjaan = $row->pekerjaan;
							$tglLahir = $row->tanggal_lahir;
							$tempatLahir = $row->tempat_lahir;
							$jenisPihak = $row->jenis_pihak;
							$IDJenisPihak = $row->jenis_pihak_id;
							$jenisIdentitas = $row->jenis_indentitas;
							$noIdentitas = $row->nomor_indentitas;
							$golDarah = $row->golongan_darah;
							$agama = $row->agama;
							$IDAgama = $row->agama_id;
							$IDStatusKawin = $row->status_kawin_id;
							if($row->jenis_kelamin=='L'){
								$sex = 'Laki Laki';
								$IDSex = 1;
							}elseif($row->jenis_kelamin=='P'){
								$sex = 'Perempuan';
								$IDSex = 2;
							}else{
								$sex='';
								$IDSex = 0;
							}
							$wargaNegara = $row->warga_negara;
							$IDWargaNegara = $row->warga_negara_id;
							$kawin = $row->status_kawin;
							$pendidikan = $row->pendidikan;
							$IDPendidikan = $row->pendidikan_id;
							$keterangan = $row->keterangan;
							$difabel = $row->difabel;
							$difabel = ($difabel=='Y')? 'Ya':'Tidak';
							if(!empty($tglLahir)){
								$umur = $this->tanggalhelper->getSelisihTahun($tglLahir,date('Y-m-d'));
							}else{
								$umur = "";
							}

							$nip_nrp = $row->nip_nrp;
							$pangkat = $row->pangkat;
							$jabatan = $row->jabatan;
							$kesatuan = $row->kesatuan;
						}
						
						$isLengkap = 1;
						$notFill ='error';
						$forPihakKe = $this->nativesession->get('forPihakKe');						
						$jenis_pengadilan = $this->session->userdata('jenis_pengadilan');

						if($jenis_pengadilan==3){
							if(empty($tempatLahir)){
								if($forPihakKe==1 OR $forPihakKe==3)
								$isLengkap = 1;
								$notFill =' ';
							}
							if(empty($tglLahir)){
								if($forPihakKe==1 OR $forPihakKe==3)
								$isLengkap = 1;
								$notFill =' ';
							}
							if(empty($pendidikan)){
								if($forPihakKe==1 OR $forPihakKe==3)
								$isLengkap = 1;
								$notFill =' ';
							}
							if(empty($kawin)){
								$isLengkap = 1; 
								$notFill =' ';
							}
							if(empty($sex)){
								$isLengkap = 1;
								$notFill =' ';
							}
						}

						if ($jenis_pengadilan!=3){
							if(empty($tempatLahir)){
								if($forPihakKe==1 OR $forPihakKe==3)
								$isLengkap = 0;
								$notFill ='Tempat Lahir';
							}
						
							if(empty($tglLahir)){
								if($forPihakKe==1 OR $forPihakKe==3)
								$isLengkap = 0;
								$notFill ='Tanggal Lahir';
							}

							if(empty($pendidikan)){
								if($forPihakKe==1 OR $forPihakKe==3)
								$isLengkap = 0;
								$notFill ='Pendidikan';
							}
						
							if(empty($kawin)){
								if($forPihakKe==1 OR $forPihakKe==3)
								$isLengkap = 0; 
								$notFill ='Status Perkawinan';
							}

							if(empty($sex)){
								if($forPihakKe==1 OR $forPihakKe==3)
								$isLengkap = 0;
								$notFill ='Jenis Kelamin';
							}
						}

						if(empty($id)){
							$isLengkap = 0;
							$notFill ='enc_id';
						}
						if(empty($jenisPihak)){
							$isLengkap = 0;
							$notFill ='Jenis Pihak';
						}
						if(empty($nama)){
							$isLengkap = 0;
							$notFill ='Nama';
						}
												
						if(empty($wargaNegara)){
							$isLengkap = 0;
							$notFill ='Warga Negara';
						}
						if(empty($alamat)){
							$isLengkap = 0;
							$notFill ='Alamat';
						}
						if(empty($pekerjaan)){
							$isLengkap = 0;
							$notFill ='Pekerjaan';
						}
						

						if($status==1){
							$data['isFound'] = 1;
							$data['isLengkap'] = $isLengkap;
							$data['notFill'] = $notFill;
							$data['nama'] = $nama;
							return $data;
						}
						if($IDJenisPihak!=1){
							$isLengkap = 1;
						}
						echo json_encode(array(
							'st'=>1,
							'isLengkap'=>$isLengkap,
							'notFill'=>$notFill,
							'enc_pihak'=>$id,
							'nama'=>$nama,
							'jenisPihak'=>$jenisPihak,
							'IDJenisPihak'=>$IDJenisPihak,
							'tempatLahir'=>$tempatLahir,
							'tglLahir'=>(!empty($tglLahir))? $this->tanggalhelper->convertToInputDate($tglLahir):'',
							'umur'=>$umur,
							'sex'=>$sex,
							'IDSex'=>$IDSex,
							'pekerjaan'=>$pekerjaan,
							'kawin'=>$kawin,
							'statusKawin'=>$kawin,
							'IDStatusKawin'=>$IDStatusKawin,
							'pendidikan'=>$pendidikan,
							'IDPendidikan'=>$IDPendidikan,
							'keterangan'=>$keterangan,
							'jenisIdentitas'=>$jenisIdentitas,
							'noIdentitas'=>$noIdentitas,
							'agama'=>$agama,
							'IDAgama'=>$IDAgama,
							'wargaNegara'=>$wargaNegara,
							'IDWargaNegara'=>$IDWargaNegara,
							'alamat'=>$alamat,
							'difabel'=>$difabel,
							'golDarah'=>$golDarah,
							'nip_nrp'=>$nip_nrp,
							'pangkat'=>$pangkat,
							'jabatan'=>$jabatan,
							'kesatuan'=>$kesatuan,
							'enc_act' =>base64_encode($this->encrypt->encode('add'))
							));
						return;
					}
				}else{
					if($status==1){
						$data['isFound'] = 0;
						$data['isLengkap'] = 1;
						$data['notFill'] = '';
						$data['nama'] = 'Error';
						return $data;
					}
					echo json_encode(array('st'=>0,'msg'=>'Error, Data Tidak Ditemukan.'));
					return;
				}
			}
		}else{
			echo json_encode(array('st'=>0,'msg'=>'Error, Please Refresh Your Page'));
			return;
		}
	}


	function validateInput(){
		if(empty($_POST)){
			show_404();
		}

		if($this->session->userdata('is_logged_in')===FALSE){
			echo json_encode(array('st'=>0,'msg'=>'Anda Belum Login, Silahkan Login Terlebih Dahulu.'));
			return;
		}
		$nama = trim($this->input->post('pihak_fullname',TRUE));
		$_POST['pihak_fullname'] = str_replace(' ', '', $_POST['pihak_fullname']);
		
		$tempatLahir = trim($this->input->post('pihak_tempat_lahir',TRUE));
		$_POST['pihak_tempat_lahir'] = str_replace(' ', '', $_POST['pihak_tempat_lahir']);

		$tglLahir = $this->input->post('pihak_tgl_lahir',TRUE);
		if(!empty($tglLahir))
			$tglLahir = $this->tanggalhelper->convertToMysqlDate($this->input->post('pihak_tgl_lahir',TRUE));
		else
			$tglLahir = NULL;
		$sex = $this->input->post('pihak_jenis_kelamin',TRUE);
		//$sex = $this->input->post('pihak_jenis_kelamin',TRUE);
		if($sex<1 AND $sex>3){
			echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Jenis Kelamin Tidak Diketahui.'));
			return;
		}

		if($sex==1){
			$sex='L';
		}elseif ($sex==2) {
			$sex='P';
		}
		$agama = $this->input->post('pihak_agama',TRUE);
		$statusKawin = $this->input->post('pihak_status_kawin',TRUE);
		
		$apakahtni=$this->input->post('apakahtni',TRUE);
		switch($apakahtni) {
			case "1":
				$pekerjaan="Tentara Nasional Indonesia";
				break;
			case "3":
				$pekerjaan="Pegawai Negeri Sipil";
				break;
			case "4":
				$pekerjaan="Pegawai BUMN/BUMD";
				break;
			case "5":
				$pekerjaan="POLRI";
				break;
			case "2":
				$pekerjaan = $this->input->post('pihak_pekerjaan',TRUE);
				break;
			default:
				$pekerjaan = $this->input->post('pihak_pekerjaan',TRUE);
				break;
		}
		
		$pendidikan = $this->input->post('pihak_pendidikan',TRUE);

        $jenis_pengadilan = $this->session->userdata('jenis_pengadilan');
		$this->form_validation->set_rules('pihak_jenis_pihak', 'Jenis Pihak', 'trim|required|numeric|xss_clean');
		if($jenis_pengadilan==3){ 
		$this->form_validation->set_rules('pihak_fullname', 'Nama Lengkap', 'trim|required|xss_clean');
		}else{
		$this->form_validation->set_rules('pihak_fullname', 'Nama Lengkap', 'trim|required|xss_clean|valid_naming');	
		}
		$jenisPihak = $this->input->post('pihak_jenis_pihak',TRUE);
		$forPihakKe = $this->nativesession->get('forPihakKe');

		if(intval($jenisPihak)==1 OR intval($jenisPihak)==0){
			if($forPihakKe==1){
				if($jenis_pengadilan!=3){ 
					$this->form_validation->set_rules('pihak_tempat_lahir', 'Tempat Lahir', 'trim|required|alpha|min_length[2]|xss_clean');
					$this->form_validation->set_rules('pihak_tgl_lahir', 'Tanggal Lahir', 'trim|required|exact_length[10]|xss_clean');
					$this->form_validation->set_rules('pihak_umur', 'Umur Pihak', 'trim|numeric|xss_clean');
					$this->form_validation->set_rules('pihak_agama', 'Agama', 'trim|required|numeric|xss_clean');
					$this->form_validation->set_rules('pihak_pendidikan', 'Tingkat Pendidikan', 'trim|required|numeric|xss_clean');
					$this->form_validation->set_rules('pihak_status_kawin', 'Status Kawin', 'trim|required|numeric|xss_clean');
					$this->form_validation->set_rules('pihak_jenis_kelamin', 'Jenis Kelamin', 'trim|required|numeric|xss_clean');
				}else{
					$this->form_validation->set_rules('pihak_tempat_lahir', 'Tempat Lahir', 'trim|alpha|min_length[2]|xss_clean');
					$this->form_validation->set_rules('pihak_tgl_lahir', 'Tanggal Lahir', 'trim|exact_length[10]|xss_clean');
					$this->form_validation->set_rules('pihak_umur', 'Umur Pihak', 'trim|numeric|xss_clean');
					$this->form_validation->set_rules('pihak_agama', 'Agama', 'trim|numeric|xss_clean');
					$this->form_validation->set_rules('pihak_pendidikan', 'Tingkat Pendidikan', 'trim|numeric|xss_clean');
					$this->form_validation->set_rules('pihak_status_kawin', 'Status Kawin', 'trim|numeric|xss_clean');
				}
			}
			$this->form_validation->set_rules('pihak_jenis_identitas', 'Jenis Identitas', 'trim|numeric|xss_clean');
			$this->form_validation->set_rules('pihak_no_identitas', 'No Identitas', 'trim|xss_clean');
			
			$this->form_validation->set_rules('pihak_warga_negara', 'Warga Negara', 'trim|required|numeric|xss_clean');
			if ($apakahtni==2 OR empty($apakahtni)){
				$this->form_validation->set_rules('pihak_pekerjaan', 'Pekerjaan', 'trim|required|min_length[5]|xss_clean');
			}
			$this->form_validation->set_rules('pihak_keterangan', 'pihak_keterangan', 'trim|xss_clean');
		}

		$this->form_validation->set_rules('pihak_email', 'Alamat Email', 'trim|valid_email|xss_clean');
		$this->form_validation->set_rules('pihak_no_tlp', 'No Telepon', 'trim|numeric|min_length[5]|max_length[50]|xss_clean');
		$this->form_validation->set_rules('pihak_alamat', 'Alamat', 'trim|required|min_length[15]|max_length[500]|xss_clean');
		
		if ($this->form_validation->run() === FALSE){
			echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Tidak Berhasil'.validation_errors()));
			return;
		}else{
			$act = $this->input->post('act',TRUE);
			$jenisPihak = $this->input->post('pihak_jenis_pihak',TRUE);
			if($jenisPihak<1 AND $jenisPihak>3){
				echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Jenis Pihak Tidak Diketahui.'));
				return;
			}

			if($jenisPihak==1){
				$data_pihak = array(
					'nama' => $nama,
					'jenis_pihak_id' => $jenisPihak,
					'jenis_indentitas' => $this->input->post('pihak_jenis_identitas',TRUE),
					'nomor_indentitas' => $this->input->post('pihak_no_identitas',TRUE),
					'tempat_lahir' => (empty($tempatLahir))? NULL:$tempatLahir,
					'tanggal_lahir' => (empty($tglLahir))? NULL:$tglLahir,
					'jenis_kelamin' => $sex,
					'golongan_darah' => $this->input->post('pihak_gol_darah',TRUE),
					'alamat' => trim($this->input->post('pihak_alamat',TRUE)),
					'agama_id' => (empty($agama))? NULL:$agama,
					'status_kawin' => $statusKawin,
					'difabel' => trim($this->input->post('pihak_difabel',TRUE)),
					'pekerjaan' => trim($pekerjaan),
					'pendidikan_id' => (empty($pendidikan))? NULL:$pendidikan,
					'telepon' => trim($this->input->post('pihak_no_tlp',TRUE)),
					'email' => trim($this->input->post('pihak_email',TRUE)),
					'keterangan' => trim($this->input->post('pihak_keterangan',TRUE)),
					'warga_negara_id' => $this->input->post('pihak_warga_negara',TRUE)
				);
				$sex = $this->input->post('pihak_jenis_kelamin',TRUE);
				if($sex<1 AND $sex>3){
					echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Jenis Kelamin Tidak Diketahui.'));
					return;
				}

				if($sex==1){
					$sex='L';
				}elseif ($sex==2) {
					$sex='P';
				}

				$agama = $this->input->post('pihak_agama',TRUE);
				if($agama<1 AND $agama>7){
					echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Agama Tidak Diketahui.'));
					return;
				}

				$pekerjaan = $this->input->post('pihak_pekerjaan',TRUE);
				/*
				if($pekerjaan<1 AND $pekerjaan>88){
					echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Jenis Pekerjaan Tidak Diketahui.'));
					return;
				}*/

				$statusKawin = $this->input->post('pihak_status_kawin',TRUE);
				if($statusKawin<1 AND $statusKawin>4){
					echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Status Kawin Tidak Diketahui.'));
					return;
				}

				$pendidikan = $this->input->post('pihak_pendidikan',TRUE);
				if($pendidikan<1 AND $pendidikan>11){
					echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Tingkat Pendidikan Tidak Diketahui.'));
					return;
				}
				
				$umur=$this->input->post('pihak_umur',TRUE);
				if(!empty($umur)){
					if($umur>100 OR $umur<5){
						echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Periksa Kembali Umur Pihak.'.$umur));
						return;
					}
				}
				
				$curr_date = date('Y-m-d');
				$selisih = $this->tanggalhelper->getSelisihHari($tglLahir,$curr_date);
				if($selisih<0){
					echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Periksa Kembali Tanggal Lahir.'));
					return;
				}
			}else{
				$data_pihak = array(
					'nama' => $nama,
					'jenis_pihak_id' => $jenisPihak,
					'alamat' => trim($this->input->post('pihak_alamat',TRUE)),
					'telepon' => trim($this->input->post('pihak_no_tlp',TRUE)),
					'email' => trim($this->input->post('pihak_email',TRUE)),
					'keterangan' => trim($this->input->post('pihak_keterangan',TRUE))
				);
			}
			
			
			if($act==="add"){
				$data_pihak['diinput_oleh'] = $this->session->userdata('username');
				$data_pihak['diinput_tanggal'] = date("Y-m-d h:i:s",time());
				$this->load->model('pihak/list_pihak', 'pihak');
				$query = $this->pihak->add_data($data_pihak);
				if($query==TRUE){
					echo json_encode(array('st'=>1,'msg'=>'Berhasil<br /> Data Berhasil Disimpan.'));
					return;
				}else{
					echo json_encode(array('st'=>0,'msg'=>'Error<br /> Coba Ulangi Kembali. Error Code 198'));
					return;
				}
			}elseif ($act==="updt") {
				$idPihak = $this->encrypt->decode(base64_decode($this->input->post('pihak_id',TRUE)));
				if(!is_numeric($idPihak) OR empty($idPihak)){
					echo json_encode(array('st'=>0,'msg'=>'Error:<br /> System Error, Please refresh your browser.Error Code 199'.$act));
				}else{
					$data_pihak['diperbaharui_oleh'] = $this->session->userdata('username');
					$data_pihak['diperbaharui_tanggal'] = date("Y-m-d h:i:s",time());
					$this->load->model('pihak/list_pihak', 'pihak');
					$query = $this->pihak->update_data($data_pihak,$idPihak);
					if($query==TRUE){
						echo json_encode(array('st'=>1,'msg'=>'Berhasil<br /> Data Berhasil Disimpan.'));
						return;
					}else{
						echo json_encode(array('st'=>0,'msg'=>'Error<br /> Coba Ulangi Kembali. Error Code 198'));
						return;
					}
				}
			}else{
				echo json_encode(array('st'=>0,'msg'=>'Error:<br /> System Error, Please refresh your browser.Error Code 199#'.$act));
			}
		}
	}

	function setPage(){
		if(empty($_POST)){
			show_404();
		}
		$pageNumber = $this->input->post('pageNumber',TRUE);
		$keyword = $this->input->post('keyword',TRUE);
		if(is_numeric($pageNumber) AND $pageNumber>0){
			$this->nativesession->set_flash_session('security', TRUE);
			$this->nativesession->set_flash_session('pageNumber_pihak', $pageNumber);
			$this->nativesession->set_flash_session('keyword_pihak', $keyword);
			echo json_encode(array('st'=>1));
		}else{
			show_404();
		}
	}

	function searchPage(){
		if(empty($_POST)){
			show_404();
		}
		$keyword = $this->input->post('keyword_pihak',TRUE);
		$column = $this->input->post('search_field',TRUE);
		$this->nativesession->set_flash_session('security', TRUE);
		$this->nativesession->set_flash_session('keyword_pihak', $keyword);
		$this->nativesession->set_flash_session('column', $column);
		echo json_encode(array('st'=>1));
	}
}