<?php 
class Model_radius 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 radius ORDER BY id_radius DESC LIMIT $awal, $akhir");
    }
	function list_komponen_($awal, $akhir){
        return $this->db->query("SELECT * from komponen_biaya_satker ORDER BY id ASC LIMIT $awal, $akhir");
    }
	function tambah_baru(){
	   $datadb = array('kategori_radius'=>$this->db->escape_str($this->input->post('a')),
						'nilai_radius'=>$this->input->post('b'),
						'tanggal_input'=>date('Y-m-d H:i:s'),
					'username_input'=>$this->session->username);
		
        $this->db->insert('radius',$datadb);
		$title = "Tambah Data radius <br />dengan ID [id=".$this->input->post('id')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("INSERT",$title,$descrip,'radius');
			
    }
	function tambah_komponen_baru(){
	   $datadb = array('nama_komponen'=>$this->db->escape_str($this->input->post('a')),
						'jumlah'=>$this->input->post('b'),
						'tanggal_input'=>date('Y-m-d H:i:s'),
					'username_input'=>$this->session->username);
		
        $this->db->insert('komponen_biaya_satker',$datadb);
		$title = "Tambah Data komponen_biaya <br />dengan ID [id=".$this->input->post('a')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("INSERT",$title,$descrip,'komponen_biaya_satker');
			
    }
	function list_detail($id){
        return $this->db->query("SELECT * FROM radius
								WHERE id_radius ='$id'");
    }
	function list_detail_komponen($id){
        return $this->db->query("SELECT * FROM komponen_biaya_satker
								WHERE id ='$id'");
    }
	function list_update(){
	 $datadb = array('kategori_radius'=>$this->db->escape_str($this->input->post('a')),
						'nilai_radius'=>$this->input->post('b'),
					'tanggal_perubahan'=>date('Y-m-d H:i:s'),
					'username_perubahan'=>$this->session->username);
	$this->db->where('id_radius',$this->input->post('id'));
	$this->db->update('radius',$datadb);
	$title = "Update Data radius <br />dengan ID [id=".$this->input->post('id')."]";
		$descrip = $this->fetch_description($title,$datadb);
		$this->add_audittrail("UPDATE",$title,$descrip,'radius');
			
    }
	function list_update_komponen(){
	 $datadb = array('nama_komponen'=>$this->db->escape_str($this->input->post('a')),
						'jumlah'=>$this->input->post('b'),
					'tanggal_perubahan'=>date('Y-m-d H:i:s'),
					'username_perubahan'=>$this->session->username);
	$this->db->where('id',$this->input->post('id'));
	$this->db->update('komponen_biaya_satker',$datadb);
	$title = "Update Data komponen_biaya <br />dengan ID [id=".$this->input->post('id')."]";
		$descrip = $this->fetch_description($title,$datadb);
		$this->add_audittrail("UPDATE",$title,$descrip,'komponen_biaya_satker');
			
    }
	function list_cari($cari){
        return $this->db->query("SELECT * FROM radius 
								 WHERE kategori_radius LIKE '%$cari%'
								 ORDER BY id_radius 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) {
		}
	}

}