<?php
//session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once "../../../config.php";
require_once "../../../validation.php";
require_once "../../../Helper.php";
require_once '../check_edit_page_ajax.php';


require_once '../../database1.php';
require_once "../../check_admin.php";

class addAjax extends database
{
   	public function index($data)
    {
        return $this->insert('tb_warranty_comments',$data);
    }

}


$obj = new addAjax();   

if(!isset($_POST['value']))
{
	http_response_code(400);
	echo json_encode(array('error_text' => 'No input found'));
	exit;
}

$data['comment'] = $_POST['value'];
$helper = new Helper();
$validation = new validation();


$res = array();
$requiredFields = array('comment');
$return = $validation->checkRequired($requiredFields, $data);
if($return)
{
	$res['error_description'][] = $return;
}

if(!empty($res['error_description']))
{
	$result['error_description'] = $validation->setErrors($res['error_description']);
	$result['error_text'] = 'Comment is required';

	http_response_code(400);
	echo json_encode($result);
	exit;
}

$data['warranty_id'] = $id;
$data['user_id'] = $session->admin_id;
$data = $validation->setColumnPrefix($data);

$obj = NEW AddAjax();
$res = $obj->index($data);
if(!$res)
{
	http_response_code(401);
	echo json_encode(array('error_text' => 'Something Went Wrong'));
	exit;
}

session_start();
$_SESSION['error_message'] = 'Comment Added Successfully';
$_SESSION['error_type'] = 'success';

http_response_code(200);
echo json_encode(array('message' => 'Comment Added Successfully', 'id' => $res));
exit;