<?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('bar', 'chef');
$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 complete extends database
{
    public function index($id, $returningUserID)
    {
        $data['cl_status']='Completed';
        $where['cl_id'] = $id;
        //$where['cl_user_id'] = $userID;
        
        
        $this->dbh = $this->connectPDO1();
        $this->dbh->beginTransaction();
        try
        {
            $res =  $this->update1('tb_order_item', $data, $where);
            if(!$res)
            {
                $this->dbh->rollback();
                return 0;
            }
            
            $notiInsert = [];
            $notiInsert['cl_type'] = 'Order out of Delivery';
            $notiInsert['cl_order_item_id'] = $id;
            $notiInsert['cl_user_id'] = $returningUserID;


            $notificationID =  $this->insert1($this->dbh, 'tb_notification', $notiInsert);
            if(!$notificationID)
            {

                $this->dbh->rollback();
                return 0;
            }
            
            $this->dbh->commit();
            return 1;
            
            
        } catch (Exception $ex) {
            $this->dbh->rollback();
            return 0;
        }
       
        
    }
	
    public function getUserByRole($role, $userID)
    {
        return $this->fetchRow('tb_users', ['cl_type' => $role, 'cl_user_id' => $userID]);
    }
    
    public function getOrderItem($id)
    {
        return $this->fetchRow('tb_order_item', ['cl_id' => $id]);
    }
    
    public function getOrder($id)
    {
        return $this->fetchRow('tb_order', ['cl_id' => $id]);
    }
    
    public function getUser($userID)
    {
        return $this->fetchRow('tb_users', ['cl_id' => $userID]);
    }
  
}

$obj = new complete();

$role = $user['cl_type'];

$adminUserID = $user['cl_user_id'];
$adminUser = $obj->getUser($adminUserID);
    
$orderItem = $obj->getOrderItem($id);
if(!$orderItem)
{
     http_response_code(500);
	echo json_encode(array('error_text' => 'Something Went Wrong'));
	exit;
}

$order = $obj->getOrder($orderItem['cl_order_id']);
if(!$order)
{
     http_response_code(500);
	echo json_encode(array('error_text' => 'Something Went Wrong'));
	exit;
}


if($order['cl_company_id'] != $adminUserID)
{
    http_response_code(401);
	echo json_encode(array('error_text' => 'Authnetication Error'));
	exit;
}


$barUser = $chefUser = $obj->getUserByRole('chef', $adminUserID);
if($adminUser['cl_food_delivery'] == 2)
{
    $returningUser = $runnerHeadUser = $obj->getUserByRole('runner_head', $adminUserID);
    $returningUserID = $returningUser['cl_id'];
}
else {
    
    $returningUserID = $managerID = $order['cl_user_id'];
}




$msg = "";
$res = $obj->index($id, $returningUserID);
if($res)
{
    http_response_code(200);
    echo json_encode('Order Completed Successfully');
    exit;
}
                
http_response_code(500);
echo json_encode('Something went wrong');
exit;
?>