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

class Rekonvensi_m extends CI_Model {

	function get_info_rekonvensi($idperkara){
		try {
			return $this->db->query("SELECT * FROM perkara_rekonvensi WHERE perkara_id = ".$idperkara);
		} catch (Exception $e) {
			
		}
	}


	function get_para_pihak($idperkara,$pihakke){
		try {
			return $this->db->query("SELECT pihak_id, nama,alamat FROM perkara_pihak".$pihakke." WHERE perkara_id = ".$idperkara);
		} catch (Exception $e) {
			
		}
	}

	function add_rekonvensi($data){
		try {
			$this->db->replace('perkara_rekonvensi', $data);
			$title = "Tambah table <b>perkara_rekonvensi</b> dari halaman <b>Tambah Data Rekonvensi</b> dengan Primary Key [perkara_id=".$data['perkara_id']."]";
			$descrip = $this->fetch_description($title,$data);
			$this->add_audittrail("INSERT",$title,$descrip,'perkara_rekonvensi');
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}	
	}

	function add_pihak_rekonvensi($idperkara,$data_pihak){
		try {
			$this->db->where('perkara_id',$idperkara);
			$this->db->delete('perkara_rekonvensi_detil');
			foreach ($data_pihak as $data) {
				$this->db->replace('perkara_rekonvensi_detil', $data);
			}
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}	
	}

	function fetch_description($title,$data){
		$descrip = '<br><table style="vertical-align:top" cellspacing="0" cellpadding="1" border="1">';
		$descrip .= '<tr><th>Nama Kolom</th><th>Nilai</th></tr>';
		foreach ($data as $key => $value) {
			$descrip .= '<tr>';
			$descrip .= '<td>'.$key.'</td>';
			$descrip .= '<td>'.$value.'</td>';
			$descrip .= '</tr>';
		}
		$descrip .= '</table>';
		return $descrip;
	}
	
	function add_audittrail($action,$title,$descrip,$tablename){
		try {
			$data = array(
				'datetime' => date("Y-m-d H:i:s"),
				'ipaddress' => $this->input->ip_address(),
				'username' => $this->session->userdata('username'),
				'tablename' => $tablename,
				'formname' => 'add/edit Keberatan',
				'action' => $action,
				'title' => $title,
				'description' => $descrip
			);
			$this->db->insert('sys_audittrail', $data);
		} catch (Exception $e) {
			
		}
	}

	function deleteData($idperkara){
		try {
			$this->db->where('perkara_id',$idperkara);
			$this->db->delete('perkara_rekonvensi');
			$title = "DELETE Data Gugatan Rekonvensi dari halaman <b>EDIT DATA GUGATAN REKONVENSI</b> dengan Primary Key [perkara_id=".$idperkara."]";
			$this->add_audittrail("DELETE",$title,'DELETE DATA KEBERTAN','perkara_keberatan');
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}
	}


	function getListParaPihak($idperkara){
		try {
			return $this->db->query('SELECT pihak_id,urutan,status_pihak_id FROM perkara_rekonvensi_detil WHERE perkara_id='.$idperkara);
		} catch (Exception $e) {
			return FALSE;
		}
	}

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

