<?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;
}


//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;
}*/


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

$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_update_item_ajax extends database
{
	public function cenasUpdateItem($data, $id)
	{
        $where['id'] = $id;
		return $this->update('cenas_item', $data, $where);
	}
}

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


$obj = new cenas_update_item_ajax();
$res = $obj->cenasUpdateItem($data, $id);
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 Updated Successfully'));
    exit;
