<?php
include_once "../../includes/config.php";
include_once "../../includes/database.php";
include_once "../../includes/validation.php";
include_once "../../includes/Helper.php";
include_once "../../includes/ajax/check_session_ajax.php";
require_once '../../includes/ajax/check_edit_page_ajax.php';

include_once "../../includes/auth.php";
$auth = array('manager');
$authRes = checkAuth($auth, $user);
if(!$authRes)
{
    http_response_code(401);
	echo json_encode(array('error_text' => 'Authntication Error'));
	exit;
}

include_once "../../includes/ajax/check_paid_ajax.php";

class deleteOrderItem extends database
{
    public function index($id)
    {
        return $this->update('tb_order_item', ['deleted_at' => date('Y-m-d H:i:s')], ['cl_id' => $id]);
    }
    
    public function getOrder($id, $userID, $companyID)
    {
        $sql = ' SELECT * FROM tb_order o INNER JOIN tb_order_item oi ON o.cl_id=oi.cl_order_id WHERE o.deleted_at IS NULL AND oi.deleted_at IS NULL AND oi.cl_id=? AND o.cl_user_id=? AND o.cl_company_id=? ';
        return $this->callQueryRow($sql, [$id, $userID,$companyID]);
    }
}

$obj = new deleteOrderItem();


$order = $obj->getOrder($id, $user['cl_id'], $user['cl_user_id']);
if(!$order)
{
    http_response_code(500);
    echo json_encode(['error_text' => 'Something Went Wrong']);
    exit;
}

$res = $obj->index($id);
if(!$res)
{
    http_response_code(500);
    echo json_encode(['error_text' => 'Something Went Wrong']);
    exit;
}

http_response_code(200);
    echo json_encode(['message' => 'Item Deleted Successful']);
    exit;