<?php
error_reporting(1);
ini_set('display_errors', 1);
session_start();
if(!isset($_SESSION['id']) || $_SESSION['id']=='')
{
	http_response_code(401);
    echo json_encode(array('error_text' => 'User Not AUthenticated'));
    exit;
}

if(!isset($_POST['id']) || !is_numeric($_POST['id']))
{
	http_response_code(401);
    echo json_encode(array('error_text' => 'Cenas Table ID not found'));
    exit;
}


//include_once "../ichat/admin/route.php";
require_once 'database.php';
/*if(isset($_SESSION[$elhash]) && $_SESSION[$elhash] != "") { 
    $random_hash = $_SESSION[$elhash];
} else {
    $random_hash = uniqid();
    $_SESSION[$elhash] = $random_hash;
}*/

$id = $_POST['id'];

if(!isset($_POST['data']))
{
	http_response_code(401);
    echo json_encode(array('error_text' => 'No input Found'));
    exit;
}

$data = $_POST['data'];
class cenas_add_item_ajax extends database
{
	public function cenasAddItem($data)
	{
		return $this->insert('cenas_item', $data);
	}
}

if(trim($data['nr']) == '' || !is_numeric($data['nr']))
{
	http_response_code(400);
    echo json_encode(array('error_text' => 'NR should be a number'));
    exit;
}

$data['cenas_id'] = $id;
$obj = new cenas_add_item_ajax();
$res = $obj->cenasAddItem($data);
if(!$res)
{
	http_response_code(500);
    echo json_encode(array('error_text' => 'Something Went Wrong'));
    exit;
}

http_response_code(200);
    echo json_encode(array('message' => 'Item Added Successfully'));
    exit;
