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

class Pengacara_list extends CI_Controller {
	function index(){
		#add authorization check hak akses di pihak
		#
		if($this->session->userdata('is_logged_in')===FALSE){
			redirect('login');
		}
		$pageNumber = $this->nativesession->get_flash_session('pageNumber_pihak');
		if(empty($pageNumber)){
			$pageNumber = 1;
			$begin = 0;
		}else{
			$begin = ($pageNumber-1)*30;
		}

		$keyword = $this->nativesession->get_flash_session('keyword_pihak');
		$this->load->model('pengacara/list_pengacara', 'pengacara');
		$data['pengacara'] = $this->pengacara->getListPengacara(9,'DESC',$begin,$keyword);
		$data['total_rows'] = $this->pengacara->fecth_data_num();
		$data['page_number'] = $pageNumber;
		$data['current_date'] = date('d/m/Y');
		$data['counter'] = $begin;
		$data['keyword'] = $keyword;
		$data['country'] = $this->pengacara->getListCountry();
		$data['agama'] = $this->pengacara->getListAgama();
		$data['pendidikan'] = $this->pengacara->getListPendidikan();
		$this->load->vars($data);
		$this->load->view('pengacara/pengacara_list');
	}

	function fetchPengacara(){
		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('pengacara/list_pengacara', 'pengacara');
				$result = $this->pengacara->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 = '';
							}
							$wargaNegara = $row->warga_negara;
							$IDWargaNegara = $row->warga_negara_id;
							$kawin = $row->status_kawin;
							$pendidikan = $row->pendidikan;
							$IDPendidikan = $row->pendidikan_id;
							$keterangan = $row->keterangan;
							if(!empty($tglLahir)){
								$umur = $this->tanggalhelper->getSelisihTahun($tglLahir,date('Y-m-d'));
							}else{
								$umur = "";
							}
							$no_induk = $row->nomor_induk;
							$no_sk_menkeh = $row->no_sk_menkeh;
							$alamat_kantor = $row->alamat_kantor;
							$tgl_sk_menteri = $row->tgl_sk_menteri;
							$tgl_sumpah_pn = $row->tgl_sumpah_pn;
							$tgl_sumpah_pt = $row->tgl_sumpah_pt;
							$pangkat = $row->pangkat;
							$jabatan = $row->jabatan;
							$kesatuan = $row->kesatuan;
						}
						$isLengkap = 1;
						$notFill ='-';
						$jenis_pengadilan = $this->session->userdata('jenis_pengadilan');

						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($sex)){
							$isLengkap = 0;
							$notFill ='Jenis Kelamin';
						}
						if(empty($wargaNegara)){
							$isLengkap = 0;
							$notFill ='Warga Negara';
						}
						if(empty($alamat)){
							$isLengkap = 0;
							$notFill ='Alamat';
						}
						if(empty($pekerjaan)){
							$isLengkap = 0;
							$notFill ='Pekerjaan';
						}

						if($jenis_pengadilan!=3){
							if(empty($tempatLahir)){
								$isLengkap = 0;
								$notFill ='Tempat Lahir';
							}
							if(empty($tglLahir)){
								$isLengkap = 0;
								$notFill ='Tanggal Lahir';
							}
							
							if(empty($pendidikan)){
								$isLengkap = 0;
								$notFill ='Pendidikan';
							}
							if(empty($kawin)){
								$isLengkap = 0;
								$notFill ='Status Perkawinan';
							}
						}

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


						echo json_encode(array(
							'st'=>1,
							'isLengkap'=>$isLengkap,
							'notFill'=>$notFill,
							'enc_pengacara'=>$id,
							'nama'=>$nama,
							'tempatLahir'=>$tempatLahir,
							'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,
							'golDarah'=>$golDarah,
							'enc_act' =>base64_encode($this->encrypt->encode('add')),
							'no_induk'=>$no_induk,
							'no_sk_menkeh'=>$no_sk_menkeh,
							'alamat_kantor'=>$alamat_kantor,
							'tgl_sk_menteri'=>$this->tanggalhelper->convertToInputDate($tgl_sk_menteri),
							'tgl_sumpah_pn'=>$this->tanggalhelper->convertToInputDate($tgl_sumpah_pn),
							'tgl_sumpah_pt'=>$this->tanggalhelper->convertToInputDate($tgl_sumpah_pt),
							'pangkat'=>$pangkat,
							'jabatan'=>$jabatan,
							'kesatuan'=>$kesatuan,
							));
						return;
					}
				}else{
					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('pengacara_fullname',TRUE));
		$_POST['pengacara_fullname'] = str_replace(' ', '', $_POST['pengacara_fullname']);

		$tempatLahir = trim($this->input->post('pengacara_tempat_lahir',TRUE));
		$_POST['pengacara_tempat_lahir'] = str_replace(' ', '', $_POST['pengacara_tempat_lahir']);

		$pangkat = trim($this->input->post('pangkat',TRUE));
		//$_POST['pangkat'] = str_replace(' ', '', $_POST['pangkat']);

		$jabatan = trim($this->input->post('jabatan',TRUE));
		//$_POST['jabatan'] = str_replace(' ', '', $_POST['jabatan']);

		$kesatuan = trim($this->input->post('kesatuan',TRUE));
		//$_POST['kesatuan'] = str_replace(' ', '', $_POST['kesatuan']);

		$this->form_validation->set_rules('pengacara_fullname', 'Nama Lengkap', 'trim|required|xss_clean|valid_naming');
		$this->form_validation->set_rules('pengacara_umur', 'Umur Pihak', 'trim|numeric|xss_clean');
		$this->form_validation->set_rules('pengacara_jenis_identitas', 'Jenis Identitas', 'trim|numeric|xss_clean');
		$this->form_validation->set_rules('pengacara_no_identitas', 'No Identitas', 'trim|xss_clean');
		$this->form_validation->set_rules('pengacara_jenis_kelamin', 'Jenis Kelamin', 'trim|required|numeric|xss_clean');
		$this->form_validation->set_rules('pengacara_warga_negara', 'Warga Negara', 'trim|required|numeric|xss_clean');
		$this->form_validation->set_rules('pengacara_alamat', 'Alamat', 'trim|required|min_length[15]|xss_clean');
		$this->form_validation->set_rules('pengacara_keterangan', 'pengacara_keterangan', 'trim|xss_clean');
		$this->form_validation->set_rules('pengacara_email', 'Alamat Email', 'trim|valid_email|xss_clean');
		$this->form_validation->set_rules('pengacara_no_tlp', 'No Telepon', 'trim|numeric|min_length[5]|xss_clean');
		$this->form_validation->set_rules('pengacara_alamat_kantor', 'Alamat Kantor', 'trim|required|min_length[15]|xss_clean');

		// if ($this->session->userdata('jenis_pengadilan')!=3){
			if ($this->session->userdata('jenis_pengadilan')!=3){
			$this->form_validation->set_rules('pengacara_tempat_lahir', 'Tempat Lahir', 'trim|required|alpha|min_length[5]|xss_clean');
			$this->form_validation->set_rules('pengacara_tgl_lahir', 'Tanggal Lahir', 'trim|required|exact_length[10]|xss_clean');
			$this->form_validation->set_rules('pengacara_agama', 'Agama', 'trim|required|numeric|xss_clean');
			#$this->form_validation->set_rules('pengacara_pekerjaan', 'Pekerjaan', 'trim|required|min_length[5]|xss_clean');
			$this->form_validation->set_rules('pengacara_status_kawin', 'Status Kawin', 'trim|required|numeric|xss_clean');
			$this->form_validation->set_rules('pengacara_pendidikan', 'Tingkat Pendidikan', 'trim|required|numeric|xss_clean');
		}

		// if ($this->session->userdata('jenis_pengadilan')==3){
		if ($this->session->userdata('jenis_pengadilan')==3){
			$this->form_validation->set_rules('pengacara_tempat_lahir', 'Tempat Lahir', 'trim|alpha|min_length[5]|xss_clean');
			$this->form_validation->set_rules('pengacara_tgl_lahir', 'Tanggal Lahir', 'trim|exact_length[10]|xss_clean');
			$this->form_validation->set_rules('pengacara_agama', 'Agama', 'trim|numeric|xss_clean');
			#$this->form_validation->set_rules('pengacara_pekerjaan', 'Pekerjaan', 'trim|required|min_length[5]|xss_clean');
			$this->form_validation->set_rules('pengacara_status_kawin', 'Status Kawin', 'trim|numeric|xss_clean');
			$this->form_validation->set_rules('pengacara_pendidikan', 'Tingkat Pendidikan', 'trim|numeric|xss_clean');
		}


		if($this->session->userdata('jenis_pengadilan')==2){ 
		$this->form_validation->set_rules('pangkat', 'Pangkat', 'trim|required|min_length[5]|xss_clean');
		$this->form_validation->set_rules('jabatan', 'Jabatan', 'trim|required|min_length[5]|xss_clean');
		$this->form_validation->set_rules('kesatuan', 'Kesatuan', 'trim|required|min_length[5]|xss_clean');
		}

		$act = $this->input->post('act',TRUE);
		$no_induk = $this->input->post('pengacara_no_induk',TRUE);
		if(!empty($no_induk) AND $act=='add'){
			$this->form_validation->set_rules('pengacara_no_induk', 'Nomor Induk', 'trim|required|xss_clean|is_unique[pihak_pengacara.nomor_induk]');
		}
		if ($this->form_validation->run() === FALSE){
			echo json_encode(array('st'=>0,'msg'=>'<strong>Peringatan:</strong><br /> Tidak Berhasil'.validation_errors()));
			return;
		}else{
			$jenisPihak = $this->input->post('pengacara_jenis_pihak',TRUE);
			if($jenisPihak<1 AND $jenisPihak>3){
				echo json_encode(array('st'=>0,'msg'=>'<strong>Peringatan:</strong><br /> Jenis Pihak Tidak Diketahui.'));
				return;
			}

			$sex = $this->input->post('pengacara_jenis_kelamin',TRUE);
			if($sex<1 AND $sex>3){
				echo json_encode(array('st'=>0,'msg'=>'<strong>Peringatan:</strong><br /> Jenis Kelamin Tidak Diketahui.'));
				return;
			}

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

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

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

			$pendidikan = $this->input->post('pengacara_pendidikan',TRUE);
			if($pendidikan<1 AND $pendidikan>11){
				echo json_encode(array('st'=>0,'msg'=>'<strong>Peringatan:</strong><br /> Tingkat Pendidikan Tidak Diketahui.'));
				return;
			}

			
			$tglLahir = $this->tanggalhelper->convertToMysqlDate($this->input->post('pengacara_tgl_lahir',TRUE));
			$umur=$this->input->post('pengacara_umur',TRUE);
			if($umur>100 OR $umur<5){
				echo json_encode(array('st'=>0,'msg'=>'<strong>Peringatan:</strong><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'=>'<strong>Peringatan:</strong><br /> Periksa Kembali Tanggal Lahir.'));
				return;
			}

			$tgl_sk_menteri = $this->tanggalhelper->convertToMysqlDate($this->input->post('tgl_sk_menteri',TRUE));
			$selisih = $this->tanggalhelper->getSelisihHari($tgl_sk_menteri,$curr_date);
			if($selisih<0){
				echo json_encode(array('st'=>0,'msg'=>'<strong>Peringatan:</strong><br /> Periksa Kembali Tanggal SK Menteri.'));
				return;
			}

			$tgl_sumpah_pn = $this->tanggalhelper->convertToMysqlDate($this->input->post('tgl_sumpah_pn',TRUE));
			$selisih = $this->tanggalhelper->getSelisihHari($tgl_sumpah_pn,$curr_date);
			if($selisih<0){
				echo json_encode(array('st'=>0,'msg'=>'<strong>Peringatan:</strong><br /> Periksa Kembali Tanggal Sumpah PN.'));
				return;
			}

			$tgl_sumpah_pt = $this->tanggalhelper->convertToMysqlDate($this->input->post('tgl_sumpah_pt',TRUE));
			$selisih = $this->tanggalhelper->getSelisihHari($tgl_sumpah_pt,$curr_date);
			if($selisih<0){
				echo json_encode(array('st'=>0,'msg'=>'<strong>Peringatan:</strong><br /> Periksa Kembali Tanggal Sumpah PT.'));
				return;
			}
			

			$data_pengacara = array(
					'nama' => $nama,
					'jenis_pihak_id' => 1,
					'jenis_indentitas' => $this->input->post('pengacara_jenis_identitas',TRUE),
					'nomor_indentitas' => $this->input->post('pengacara_no_identitas',TRUE),
					'tempat_lahir' => $tempatLahir,
					'tanggal_lahir' => $tglLahir,
					'jenis_kelamin' => $sex,
					'golongan_darah' => $this->input->post('pengacara_gol_darah',TRUE),
					'alamat' => trim($this->input->post('pengacara_alamat',TRUE)),
					'agama_id' => $agama,
					'status_kawin' => $statusKawin,
					'pekerjaan' => 'Pengacara',
					'pendidikan_id' => $pendidikan,
					'telepon' => trim($this->input->post('pengacara_no_tlp',TRUE)),
					'email' => trim($this->input->post('pengacara_email',TRUE)),
					'keterangan' => trim($this->input->post('pengacara_keterangan',TRUE)),
					'warga_negara_id' => $this->input->post('pengacara_warga_negara',TRUE)
				);
			$data = array(
				'nomor_induk' => $this->input->post('pengacara_no_induk',TRUE),
				'alamat_kantor' => $this->input->post('pengacara_alamat_kantor',TRUE),
				'no_sk_menkeh' => $this->input->post('no_sk_menteri',TRUE),
				'tanggal_sk_menkeh' => $tgl_sk_menteri,
				'tanggal_penyumpahan_pn' => $tgl_sumpah_pn,
				'tanggal_penyumpahan_pt' => $tgl_sumpah_pt,
				'pangkat' => $pangkat,
				'jabatan' => $jabatan,
				'kesatuan' => $kesatuan
			);

			if($act==="add"){
				$data_pengacara['diinput_oleh'] = $this->session->userdata('username');
				$data_pengacara['diinput_tanggal'] = date("Y-m-d h:i:s",time());
				$this->load->model('pengacara/list_pengacara', 'pengacara');
				$query = $this->pengacara->add_data($data_pengacara,$data);
				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('pengacara_id',TRUE)));
				if(!is_numeric($idPihak) OR empty($idPihak)){
					echo json_encode(array('st'=>0,'msg'=>'<strong>Peringatan:</strong><br /> System Error, Please refresh your browser.Error Code 199'));
				}else{
					$data_pengacara['diperbaharui_oleh'] = $this->session->userdata('username');
					$data_pengacara['diperbaharui_tanggal'] = date("Y-m-d h:i:s",time());
					$this->load->model('pengacara/list_pengacara', 'pengacara');
					$query = $this->pengacara->update_data($data_pengacara,$idPihak,$data);
					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'=>'<strong>Peringatan:</strong><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_pengacara',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));
	}
}