<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Template_keterangan_saksi_m extends CI_Model {

	function parseOrderby($col){
		if($col==1){
			return "nama"; 
		}else if($col==2){
			return "aktif";
		}
	}
	

	function get_template_list($col=2,$asc_desc='ASC',$begin=0,$key=''){
		$orderby = $this->parseOrderby($col);
		$where = '';
		if(!empty($key)){
			$where .= ' WHERE (nama LIKE "%'.$key.'%" OR aktif LIKE "%'.$key.'%") ';
		}
		try {
			return $this->db->query("SELECT SQL_CALC_FOUND_ROWS id,
				nama,IF(aktif='T','Tidak','Aktif') AS aktif_text, aktif
				FROM template_keterangan_saksi_m ".$where." ORDER BY ".$orderby." ".$asc_desc." LIMIT ".$begin.",50;");
		} catch (Exception $e) {
			
		}
	}

	function fecth_data_num(){
		$query = $this->db->query("SELECT FOUND_ROWS() as cnt;");
		if($query->num_rows>0){
			foreach ($query->result() as $row) {
				return $row->cnt;
			}
    		return $query->num_rows;
    	}else{
    		return 0;
    	}
	}


	function get_template_info($id){
		if(empty($id)){
			return "";
		}
		try {
			return $this->db->query("select m.id as idH, m.nama, IF(m.aktif='T','Tidak','Aktif') AS aktif_text, m.aktif, d.*
										from template_keterangan_saksi_m m
									left join template_keterangan_saksi_d d
										on m.id=d.m_id where m.id=".$id);
		} catch (Exception $e) {
			
		}
	}

	

	function get_last_id(){
		try {
			$this->db->select_max('id');
			$query = $this->db->get('template_keterangan_saksi_m');
			if ($query->num_rows() > 0){
				return $query->row();
			}else{
				return 0;
			}		
		} catch (Exception $e) {
			
		}
	}

	function add_data($data, $data_detail=array()){
		try {
			if(!empty($data_detail)){
				$this->db->insert('template_keterangan_saksi_m', $data);
				$m_id=$this->db->insert_id();
				foreach ($data_detail as $key => $value) {
					$value['m_id']=$m_id;
					$this->db->insert('template_keterangan_saksi_d', $value);
				}
				return TRUE;
			}
		} catch (Exception $e) {
			return FALSE;
		}
	}

	function update_data($data,$id,$data_detail=array()){
		try {
			if(!empty($data_detail)){
				$this->db->where('id', $id);
				$this->db->update('template_keterangan_saksi_m', $data); 
				$this->db->where('m_id', $id);
				$this->db->delete('template_keterangan_saksi_d');
				foreach ($data_detail as $key => $value) {
					$value['m_id']=$id;
					$this->db->insert('template_keterangan_saksi_d', $value);
				}
				return TRUE;
			}
		} catch (Exception $e) {
			return FALSE;
		}
	}
	function cekjenisperkara($satker){
		
		try {
				$sql=('SELECT a.jenis_pengadilan,b.jenis_perkara_id,c.nama FROM alur_perkara a 
						LEFT JOIN jenis_alur_perkara b ON b.alur_perkara_id=a.id
						LEFT JOIN jenis_perkara c ON b.jenis_perkara_id=c.id
						WHERE a.jenis_pengadilan="'.$satker.'";');
				$result = $this->db->query($sql);
				return $result;
		} catch (Exception $e) {
			return '';
		}
	}
}
?>