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

class Data_intervensi extends CI_Model{

	function getPihakIntervensi($idperkara){
		try {
			return $this->db->query('SELECT * FROM perkara_pihak3 WHERE perkara_id = '.$idperkara);
		} catch (Exception $e) {
			
		}
	}
	function maxUrutanPenggugat($idperkara){
		try {
			return $this->db->query('SELECT MAX(urutan) AS urutan  FROM perkara_pihak1 WHERE perkara_id= '.$idperkara);
		} catch (Exception $e) {
			
		}
	}

	function maxUrutanTergugat($idperkara){
		try {
			return $this->db->query('SELECT MAX(urutan) AS urutan  FROM perkara_pihak2 WHERE perkara_id= '.$idperkara);
		} catch (Exception $e) {
			
		}
	}
	
	function getUrutanPihak($pihakKe,$IDPerkara){
		if(is_numeric($pihakKe) AND $pihakKe>0 AND $pihakKe<6){
			if(!is_numeric($pihakKe)){
				return FALSE;
			}
			if($pihakKe==5){
				$table = "perkara_pihak_korban";
			}else{
				$table = "perkara_pihak".$pihakKe;
			}
		}else{
			return -1;
		}

		if(!is_numeric($IDPerkara) AND $IDPerkara<0){
			return -2;
		}
		try {
			$query = $this->db->query("SELECT MAX(urutan) AS urutan FROM ".$table." WHERE perkara_id =".$IDPerkara);
			if($query->num_rows>0){
				foreach ($query->result() as $row) {
					return $row->urutan+1;
				}
	    	}else{
	    		return 1;
	    	}
		} catch (Exception $e) {
			return -3;
		}
	}

	function isPihakberpekara($idpihak,$idperkara){
		try {
			return $this->db->query('SELECT pihak_id, nama, status_perkara 
				FROM v_pihak_perkara 
				WHERE pihak_id = '.$idpihak.' AND perkara_id = '.$idperkara.';');
		} catch (Exception $e) {
			return FALSE;
		}
	}

	function getPihakInfo($idpihak){
		try {
			return $this->db->query('SELECT id,nama,alamat,jenis_pihak_id FROM pihak WHERE id = '.$idpihak);
		} catch (Exception $e) {
			return FALSE;
		}
	}

	function getDetilInfoPihak($idPihak){
		try {
			return $this->db->query('SELECT p.id,jp.nama AS jenis_pihak,ji.nama AS jenis_indentitas,
					p.keterangan,nomor_indentitas,p.nama,tempat_lahir,tanggal_lahir,jenis_kelamin,
					golongan_darah,alamat,pekerjaan,st.nama AS status_kawin,n.nama AS warga_negara,t.nama AS pendidikan, a.nama AS agama
					FROM pihak AS p
					LEFT JOIN 
						negara AS n
					ON p.warga_negara_id = n.id
					LEFT JOIN 
						agama AS a
					ON p.agama_id = a.id
					LEFT JOIN
						tingkat_pendidikan AS t
					ON p.pendidikan_id = t.id
					LEFT JOIN 
						status_perkawinan AS st
					ON p.status_kawin = st.id
					LEFT JOIN 
						jenis_pihak AS jp
					ON p.jenis_pihak_id = jp.id
					LEFT JOIN 
						jenis_identitas AS ji
					ON p.jenis_indentitas = ji.id
					WHERE p.id='.$idPihak.';');
		} catch (Exception $e) {
			log_message('error', $e);
			return '';
		}
	}
}