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

class Message extends CI_Controller
{
	public function get_messages()
	{

		$this->load->model('Message_archive');
		$this->load->library('Token');
		$productArray = [];
		$headers = $this->input->request_headers();

		// if ($this->token->token_post($headers)) {

			if (is_null($this->Message_archive->get_all_msgs()))
				$productArray = ['code' => '99', 'message' => 'No Messages available!', 'data' => ''];
			else {
				$productArray = ['code' => '00', 'message' => 'All Messages Retrieved', 'data' => $this->Message_archive->get_all_msgs()];
			}
		// } else
		// 	$productArray = ['code' => '99', 'msg' => 'Unauthorized Access! [Invalid/Expired Token]'];

		header('Content-Type: application/json');
		echo json_encode($productArray);
	}


	public function get_messages_user()
	{

		$this->load->model('Message_archive');
// 		$this->load->library('Token');
		$productArray = [];
// 		$headers = $this->input->request_headers();

// 		if ($this->token->token_post($headers)) {

			if (is_null($this->Message_archive->get_user_msgs()))
				$productArray = ['code' => '99', 'message' => 'No Messages available!', 'data' => ''];
			else {
				$productArray = ['code' => '00', 'message' => 'All Messages Retrieved', 'data' => $this->Message_archive->get_user_msgs()];
			}
// 		} else
// 			$productArray = ['code' => '99', 'msg' => 'Unauthorized Access! [Invalid/Expired Token]'];

		header('Content-Type: application/json');
		echo json_encode($productArray);
	}


	public function get_messages_recent()
	{

		$this->load->model('Message_archive');
		$this->load->library('Token');
		$productArray = [];
		$headers = $this->input->request_headers();

// 		if ($this->token->token_post($headers)) {

			if (is_null($this->Message_archive->get_msgs_recent()))
				$productArray = ['code' => '99', 'message' => 'No Messages available!', 'data' => ''];
			else {
				$productArray = ['code' => '00', 'message' => 'All Messages Retrieved', 'data' => $this->Message_archive->get_msgs_recent()];
			}
// 		} else
// 			$productArray = ['code' => '99', 'msg' => 'Unauthorized Access! [Invalid/Expired Token]'];

		header('Content-Type: application/json');
		echo json_encode($productArray);
	}

	public function get_messages_by_service()
	{
		$id = $this->input->get('id');
		$this->load->model('Message_archive');
		$this->load->library('Token');
		$productArray = [];
// 		$headers = $this->input->request_headers();

// 		if ($this->token->token_post($headers)) {

			if (is_null($this->Message_archive->get_messages_by_service($id)))
				$productArray = ['code' => '99', 'message' => 'No Messages available!', 'data' => ''];
			else {
				$productArray = ['code' => '00', 'message' => 'All Messages Retrieved', 'data' => $this->Message_archive->get_messages_by_service($id)];
			}
// 		} else
// 			$productArray = ['code' => '99', 'msg' => 'Unauthorized Access! [Invalid/Expired Token]'];

		header('Content-Type: application/json');
		echo json_encode($productArray);
	}

	public function save_new_msg(){
        header('Content-Type: application/json');
        $this->load->model('Message_archive');
        echo json_encode($this->Message_archive->save_new_msg());
    } 

	
}
