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

	class Chat extends Site_Controller{
		
		function __construct()
		{
			parent::__construct();
			date_default_timezone_set('Asia/Phnom_Penh');
			$this->load->library(array('form_validation'));	
			$this->load->helper(array('string'));
			$this->load->model(array("Mod_chat"));
		}
		
		public function update()
		{
			$this->form_validation->set_rules('message', 'Massage', 'trim|required');
			if ($this->form_validation->run() == TRUE)
			{
			   $message = $this->input->post('message',TRUE);
			   $message = $this->security->xss_clean($message);
			   $trn_id = $this->session->userdata('sessionId');		
			   $this->Mod_chat->insertMsg($message,$trn_id);			
			}
		}
	
		public function view()
		{
			$query = $this->Mod_chat->getMsg();
			
			if($query->num_rows()==0)
			{
				$status_code = 2;
			}else{
				$status_code = 1;
			}
			
			if($query->num_rows() == 1)
			{
			   $message = 'Welcome to Channel Limousine Car Rental Service. We Will get back to you as soon as possible.';
			   $trn_id = $this->session->userdata('sessionId');		
			   $this->Mod_chat->welcomeMsg($message,$trn_id);
			}
			
			if($query->num_rows()>0)
			{
				foreach($query->result() as $row)
				{
					$escmsg = htmlspecialchars(stripslashes($row->msg));
					if($row->user == "Customer Service")
					{
						$img = "111111.png";
						$user = "Customer Service";	
					}else{
						$img = "22222.png";
						$user = "User";	
					}

					echo'<div class="chat-message clearfix">';
							echo'<img src="'.base_url('assets/img/myicon/'.$img).'" alt="" width="22" height="22">';
							echo'<div class="chat-message-content clearfix">';
							  echo'<span class="chat-time">'.$row->time.'</span>';
							  echo'<h5>'.$user.'</h5>';
							  echo'<p>'.$escmsg.'</p>';
							echo'</div>';//end chat-message-content 
					echo'</div>';//end chat-message
					echo'<hr>';
				}
			}
		}
	
	}
	
