<?php 
class Model_kelurahan 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 A.*,B.nama_kec,C.nilai_radius from kelurahan A 
									LEFT JOIN kecamatan B ON A.id_kec=B.id_kec
									LEFT JOIN radius C ON A.id_radius=C.id_radius
									ORDER BY A.id_kec DESC, A.id_kel DESC LIMIT $awal, $akhir");		
    }
	function tambah_baru(){
	   $datadb = array('nama_kel'=>$this->db->escape_str($this->input->post('b')),
						'id_kec'=>$this->db->escape_str($this->input->post('a')),
						'id_radius'=>$this->db->escape_str($this->input->post('c')),
						'tanggal_input'=>date('Y-m-d H:i:s'),
						'username_input'=>$this->session->username);
		
        $this->db->insert('kelurahan',$datadb);
		$title = "Tambah Data Kelurahan <br />dengan ID [id=".$this->input->post('a')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("INSERT",$title,$descrip,'kelurahan');
			
    }
	function list_detail($id){
        return $this->db->query("SELECT * FROM kelurahan
								WHERE id_kel ='$id'");
    }
	function list_update(){
	 $datadb = array('nama_kel'=>$this->db->escape_str($this->input->post('b')),
						'id_kec'=>$this->db->escape_str($this->input->post('a')),
						'id_radius'=>$this->db->escape_str($this->input->post('c')),
						'tanggal_perubahan'=>date('Y-m-d H:i:s'),
					'username_perubahan'=>$this->session->username);
	$this->db->where('id_kel',$this->input->post('id'));
	$this->db->update('kelurahan',$datadb);
	$title = "Update Data kelurahan <br />dengan ID [id=".$this->input->post('id')."]";
		$descrip = $this->fetch_description($title,$datadb);
		$this->add_audittrail("UPDATE",$title,$descrip,'kelurahan');
			
    }
	function list_cari($cari){
        return $this->db->query("SELECT A.*,B.nama_kec,C.nilai_radius from kelurahan A 
									LEFT JOIN kecamatan B ON A.id_kec=B.id_kec
									LEFT JOIN radius C ON A.id_radius=C.id_radius 
								 WHERE B.nama_kec LIKE '%$cari%' OR A.nama_kel LIKE '%$cari%'
								 ORDER BY A.id_kel DESC");
    }
	function list_kecamatan(){
        return $this->db->query("SELECT * FROM kecamatan ORDER BY nama_kec ASC");
    }
	function list_radius(){
        return $this->db->query("SELECT * FROM radius ORDER BY kategori_radius ASC");
    }
	    
//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) {
		}
	}

}