<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Chat extends Admin_Controller{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model("mod_chat");
	}		
	
	public function add()
	{
		$this->form_validation->set_rules('trn_user', 'User Id', 'required');
		$this->form_validation->set_rules('message', 'Message', 'required');
		//$this->form_validation->set_rules('html_redirect', 'data', 'required');
		
		if ($this->form_validation->run() == TRUE)
		{
			$trn_id = $this->input->post('trn_user',TRUE);
			$message = $this->input->post('message',TRUE);
			$html_redirect = $this->input->post('html_redirect',TRUE);		
			$this->mod_chat->serviceMsg($message,$trn_id);
			
			if($html_redirect == "true")
			{
				//redirect('/admin/chat/html', 'refresh');	
			} 
		}else{
			$this->session->set_flashdata('errors', validation_errors());		
		}
		
	}	
	
	public function html()
	{
		$data = array();
		$data['html'] = $this->html_backend();
		$tab['activeTab'] = "chat";
		
		$this->load->view('header',$tab);
		$this->load->view('chat/htmlView',$data);
		$this->load->view('footer');
	}
	
	public function html_backend()
	{
		//create 
		$data = array();
		$ret = false;
		
		//store
		$data['query'] = $this->mod_chat->getMsg();
		
		//send to view, store the results in variable
		$ret = $this->load->view('chat/htmlBackView',$data, true);
		
		return $ret;
	}
	
	
	public function show_Msg()
	{
		$data = array();
		$html = $this->html_backend();	
		echo $html; 
	}
	
	public function getMsg()
	{
		//$this->form_validation->set_rules('Id', 'User Id', 'required');
		
		//if ($this->form_validation->run() == TRUE)
		//{
			$date = date('Y-m-d');
			$trn_id = $this->input->post('Id',TRUE);	
			//$sql = $this->mod_chat->filterMsg($date,$trn_id);
			$sql = $this->db->query("SELECT `id`, `user`, `msg`, `time`, `trn_id` FROM `messages` WHERE date like '$date' AND trn_id = '$trn_id'");
			$result = $this->db->query("UPDATE `messages` SET `Status` = 0 WHERE `trn_id`= '$trn_id'");
			$i=0;
			if($sql->num_rows()>0)
			{
				foreach($sql->result() as $v)
				{
				  $i++;
				  echo "<b value='$v->trn_id'>$v->user ($v->trn_id)</b>:";
				  echo " $v->msg </br>";
				}
			}
			else
			{	
				echo"No Data Display";
			}	
		//}else{
			//$this->session->set_flashdata('errors', validation_errors());	
		//}
	} 
	
	
	public function show_user()
	{
		$date = date('Y-m-d');		
		$sql = $this->mod_chat->getUser($date);
		if($sql->num_rows()>0)
		{
			foreach($sql->result() as $v)
			{
				$total = $this->mod_chat->countStatus($v->sessionId);
				if($total > 0)
				{
					//$status = "New (".$total.')';
					$status = '<span class="badge badge-default badge-pill">'.$total.'</span>';
					
				}
				else
				{
					$status = "";	
				}	
				
				echo'<li id="'.$v->sessionId.'" value="'.$v->sessionId.'" onclick="newMsg(this.value)" class="list-group-item justify-content-between">
						<a href="#">User('.$v->sessionId.') <span id="item">'.$status.'</span> </a>
					 </li>';
			}
		}
		else
		{
			echo'<li id="myLi" value="" onclick="newMsg(this.value)" class="list-group-item"><a href="">No User Found!</a></li>';	
		}	
	}
	
}