<?php 
class Model_kewenangan extends CI_model{
///private $db2;

 public function __construct()
 {
  parent::__construct();
        // $this->db2 = $this->load->database('dbperkara', TRUE);
 }
//xxxxxxxxxxxxxxxxxxxxxxxxx
    function list_($awal, $akhir){
        return $this->db->query("SELECT * from kewenangan_satker ORDER BY id_kewenangan DESC LIMIT $awal, $akhir");
    }
	function tambah_baru(){
	   $datadb = array('kode'=>$this->db->escape_str($this->input->post('a')),
						'nama_kewenangan'=>$this->input->post('b'),
						'panggilan_p'=>$this->db->escape_str($this->input->post('c')),
					'panggilan_t'=>$this->db->escape_str($this->input->post('d')),
					'sebutan_p'=>seo_title($this->input->post('e')),
					'sebutan_t'=>$this->db->escape_str($this->input->post('f')),
					'tanggal_input'=>date('Y-m-d H:i:s'),
					'username_input'=>$this->session->username);
		
        $this->db->insert('kewenangan_satker',$datadb);
		$title = "Tambah Data Kewenangan <br />dengan ID [id=".$this->input->post('id')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("INSERT",$title,$descrip,'tb_kewenangan');
			
    }
	function list_detail($id){
        return $this->db->query("SELECT * FROM kewenangan_satker
								WHERE id_kewenangan ='$id'");
    }
	function list_update(){
	 $datadb = array('kode'=>$this->db->escape_str($this->input->post('a')),
						'nama_kewenangan'=>$this->input->post('b'),
						'panggilan_p'=>$this->db->escape_str($this->input->post('c')),
					'panggilan_t'=>$this->db->escape_str($this->input->post('d')),
					'sebutan_p'=>seo_title($this->input->post('e')),
					'sebutan_t'=>$this->db->escape_str($this->input->post('f')),
					'tanggal_perubahan'=>date('Y-m-d H:i:s'),
					'username_perubahan'=>$this->session->username);
	$this->db->where('id_kewenangan',$this->input->post('id'));
	$this->db->update('kewenangan_satker',$datadb);
	$title = "Update Data Jenis Perkara <br />dengan ID [id=".$this->input->post('id')."]";
		$descrip = $this->fetch_description($title,$datadb);
		$this->add_audittrail("UPDATE",$title,$descrip,'kewenangan_satker');
			
    }
	function list_cari($cari){
        return $this->db->query("SELECT * FROM kewenangan_satker 
								 WHERE nama_kewenangan LIKE '%$cari%'
								 ORDER BY id_kewenangan DESC");
    }
//xxxxxxxxxxxxxxxxxxxxxxxxx

	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' => 'by komar',
				'action' => $action,
				'title' => $title,
				'description' => $descrip
			);
			$this->db->insert('sys_audittrail', $data);
		} catch (Exception $e) {
		}
	}

}