<?php

class Asset_kategori_tindakan_model extends CI_Model {

	private $_table_name = "asset_kategori_tindakan";
	private $_table_sql = "asset_kategori_tindakan a_kt";

	public function tabel_data()
	{		
		$this->db->select('a_kt.*,');
		$this->db->from($this->_table_sql);		
		$this->db->order_by('a_kt.id', 'ASC');	

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

	public function lihat_data($context)
	{
		$id =  $context['id'];
		
		$this->db->select('a_kt.*');
		$this->db->from($this->_table_sql);
		$this->db->where('a_kt.id', $id);
		
		return $this->db->get()->row();
	}

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

		return $this;
	}

	public function add_data($context)
	{
		$this->value = $context['value'];
		$this->unit = $context['unit'];
		$this->id = $context['id'];

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

		$notif = $this->get_notif('Tambah');

		return $notif;
	}

	public function update_data($context)
	{		
		$this->value = $context['value'];
		$this->unit = $context['unit'];
		$this->id = $context['id'];

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

		$notif = $this->get_notif('Update');

		return $notif;
	}

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

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

		$notif = $this->get_notif('Hapus');

		return $notif;
	}

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

	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;		
	}
}	