<?php

class Data_asset_model extends CI_Model {

	public function tabel_data($context)
	{		
		$table_name = $context['table_name'];
		$keyword = $context['keyword'];
		$limit = $context['limit'];
		$start = $context['start'];

		$this->db->select('*');
		$this->db->from($table_name);

		if($keyword != "")	$this->db->where("value LIKE '%$keyword%'");

		$this->db->order_by('status, value', 'ASC');		
		$this->db->limit($limit, $start);

		return $this->db->get()->result();
	}

	public function jml_data($context)
	{
		$table_name = $context['table_name'];
		$keyword = $context['keyword'];

		$this->db->select('*');
		$this->db->from($table_name);

		if($keyword != "")	$this->db->where("value LIKE '%$keyword%'");

		return $this->db->get()->num_rows();
	}

	public function lihat_data($context)
	{
		$table_name = $context['table_name'];
		$id =  $context['id'];

		$this->db->select('*');
		$this->db->from($table_name);
		$this->db->where('id', $id);
		
		return $this->db->get()->row();
	}

	public function tambah_data()
	{
		$this->id = '';
		$this->value = '';
		$this->status = '';

		return $this;
	}

	public function add_data()
	{		
		$post = $this->input->post();

		$table_name = $post['table_name'];

		if($post['id'] != '') $this->id = $post['id'];

		$this->value = $post['value'];
		$this->status = $post['status'];	
		$this->created_date = date('Y-m-d H:i:s');

		$query = $this->db->insert($table_name, $this);

		return $this->get_notif('Tambah');
	}
	
	public function update_data()
	{
		$post = $this->input->post();

		$table_name = $post['table_name'];

		if($post['id'] != $post['id_old']) $this->id = $post['id'];	

		$this->value = $post['value'];	
		$this->status = $post['status'];	

		$query = $this->db->where('id', $post['id_old'])->update($table_name, $this);	

		return $this->get_notif('Update');
	}

	public function delete_data($context)
	{
		$table_name = $context['table_name'];
		$id =  $context['id'];

		$query = $this->db->where('id', $id)->delete($table_name);		

		return $this->get_notif('Hapus');
	}

	//--------------------------------------------------------------------------------------------

	public function get_notif($name_function)
	{
		$eror = $this->db->error();

		if($eror['code'] == 0){
			$notif = array('eror'=>'success', 'pesan'=>$name_function.' data berhasil');
		} else if($eror['code'] == 1062){
			$notif = array('eror'=>'warning', 'pesan'=>'Username sudah terdaftar');
		} else $notif = array('eror'=>'warning', 'pesan'=>"Terjadi kesalahan. Mohon hubungi (septiaputra@gmail.com)<br>$eror[code]: $eror[message]");	

		return $notif;		
	}

}	