<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model_approval extends CI_Model {

	function __construct()
	{
		parent::__construct();
	}

	public function getApproval($where=array())
	{
		foreach ($where as $key => $value) 
		{
			$this->db->where($key, $value); 
		}

		$query = $this->db->select('a.*,b.name as approvedByName')
						->join('user b','b.id = a.approved_by','left')
						// ->where('a.is_delete',0)
						->order_by('a.ID')
						->group_by('a.status')
						->get('sv__approval a')
						->result_array();

		return $query;
	}

	public function update_sv_status($table_name, $id_table, $sets = array(), $status_id = null)
	{
		if(!$table_name) return NULL;

		if($sets == null)
		{
			$set = array
			(
				'is_delete' 	=> 1,
				'updated_by'	=> $_SESSION['user']['id'],
				'updatedTS'		=> date('Y-m-d H:i:s')
			);
	
			$where = array
			(
				'table_name' 	=> $table_name,
				'id_table'		=> $id_table,
			);
		}else{
			$set = $sets;

			$where = array
			(
				'ID'			=> $status_id,
				'table_name' 	=> $table_name,
				'id_table'		=> $id_table,
			);
		}

		$this->db->update('sv__approval',$set,$where); return true;
	}

	function get_sv_status($table_name, $id_table)
	{
		$query = $this->db->where('table_name',$table_name)
					->where('id_table',$id_table)
					// ->where('is_delete',0)
					->get('sv__approval');

		return ($query->num_rows() > 0) ? $query->result_array() : null;
	}

	public function updateOrCreateNew($table_name, $id_table, $status, $approved_by, $reason = '')
	{
		if(empty($status)) return false;
		
		// $this->db->set('a.is_delete',1)
		// 		->where('table_name',$table_name)
		// 		->where('id_table',$id_table)
		// 		->where_not_in('status',$status)
		// 		->update('sv__approval a');

		$get_status_approval = $this->getApproval(array('a.table_name' => $table_name, 'a.id_table' => $id_table, 'a.is_delete' => 0, 'a.status' => $status));

		if(!$get_status_approval) $delete_status_approval = $this->update_sv_status($table_name,$id_table, array());

		$stat_stored = $this->get_sv_status($table_name, $id_table);

		if(!empty($stat_stored))
		{
			$batch_insert = array();
			$bool = false;
			
			foreach ($stat_stored as $key => $stats) 
			{
				// die(debug($stat));
				if($stats['status'] == $status)
				{
					$bool = true;
				}
			}

			if(!$bool)
			{
				$temp = array
				(
					'table_name'	=> $table_name,
					'id_table'		=> $id_table,
					'status'		=> $status,
					'reason'		=> $reason,
					'approved_by'	=> $approved_by,
				);
				array_push($batch_insert,$temp);
				$insert = $this->db->insert_batch('sv__approval',$batch_insert);return true;
			}else{
				$this->session->set_flashdata('error',"Status already exist. Please set new status. Thank You.");
			}

		} else {
			$batch_insert = array();

			$temp = array
			(
				'table_name'	=> $table_name,
				'id_table'		=> $id_table,
				'status'		=> $status,
				'reason'		=> $reason,
				'approved_by'	=> $approved_by,
			);
			array_push($batch_insert,$temp);

			$insert = $this->db->insert_batch('sv__approval',$batch_insert);return true;
		}
	}

	function get_form_detail($table_name, $table_id)
	{
		$query = $this->db->where('id',$table_id)
							->get($table_name);

		return ($query->num_rows() > 0) ? $query->result_array()[0] : null;
	}
}
?>