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

require APPPATH . '/libraries/REST_Controller.php';
require APPPATH . '/libraries/Format.php';

use Restserver\Libraries\REST_Controller;

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");

class Api_inbox_activity_cek extends REST_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->helper(['jwt', 'authorization']);
	}


	public function index_post()
	{
		$mst_user_kode =  $this->mst_user_kode();

		$tanggal_saat_ini = date('Y-m-d');

		//PANGGIL DATA
		$inbox = $this->db->query("SELECT mst_inbox_kode, 
									mst_inbox_user_kode,
									mst_inbox_jenis,
									mst_inbox_gambar,
									mst_inbox_judul,
									mst_inbox_isi,
									mst_inbox_tanggal,
									mst_inbox_status
									FROM tbl_mst_inbox
									where mst_inbox_user_kode REGEXP '$mst_user_kode'
									and mst_inbox_tanggal >= '$tanggal_saat_ini' - INTERVAL mst_inbox_jangka_waktu DAY
									order by mst_inbox_tanggal desc");

		if ($inbox->num_rows() > 0) {
			$no = 1;
			foreach ($inbox->result() as $row) {
				$data['inbox'][] = array(
					'mst_inbox_kode' => encrypt($row->mst_inbox_kode, $this->config->item('key')),
					'mst_inbox_user_kode' => encrypt($row->mst_inbox_user_kode, $this->config->item('key')),
					'mst_inbox_jenis' => encrypt($row->mst_inbox_jenis, $this->config->item('key')),
					'mst_inbox_gambar' => encrypt($row->mst_inbox_gambar, $this->config->item('key')),
					'mst_inbox_judul' => encrypt($row->mst_inbox_judul, $this->config->item('key')),
					'mst_inbox_isi' => encrypt($row->mst_inbox_isi, $this->config->item('key')),
					'mst_inbox_tanggal' => encrypt($row->mst_inbox_tanggal, $this->config->item('key')),
					'mst_inbox_status' => encrypt($row->mst_inbox_status, $this->config->item('key')),
				);
				$no++;
			}

			$sess["status"] = "001";
			$sess["pesan"] = "Berhasil";
			$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
			$sess['data'] = array(
				'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
				'value' =>  encrypt(json_encode($data), $keytoken)
			);
			echo json_encode($sess);
		} else {
			$sess["status"] = "002";
			$sess["pesan"] = "Gagal";
			$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
			$sess['data'] = array(
				'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
				'value' =>  encrypt(json_encode(array()), $keytoken)
			);
			echo json_encode($sess);
		}
	}


	private function mst_user_kode()
	{
		$input = decrypt($this->input->post("mst_user_kode", true), $this->config->item('key'));
		$token = AUTHORIZATION::generateToken($input);
		try {
			$data = AUTHORIZATION::validateToken($token);
			if ($data === false) {
				$sess["status"] = "004";
				$sess["pesan"] = $this->config->item('error_parameter');
				$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
				$sess['data'] = array(
					'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
					'value' =>  encrypt(json_encode(array()), $keytoken)
				);
				echo json_encode($sess);
				exit();
			} else {
				return $data;
			}
		} catch (Exception $e) {
			$sess["status"] = "004";
			$sess["pesan"] = $this->config->item('error_parameter');
			$keytoken = strtoupper(substr(sha1(uniqid()), 0, 16));;
			$sess['data'] = array(
				'token' =>  encrypt($keytoken, $this->config->item('keytoken')),
				'value' =>  encrypt(json_encode(array()), $keytoken)
			);
			echo json_encode($sess);
			exit();
		}
	}
}
