<?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 complete extends database
{
    public function index($id, $userID, $companyID, $chefUser, $barUser)
    {
        $data['cl_status'] = 'Pending';
        $where['cl_id'] = $id;
        $where['cl_user_id'] = $userID;
        $where['cl_company_id'] = $companyID;
        
        $this->dbh = $this->connectPDO1();
        $this->dbh->beginTransaction();
        try
        {
        
            $orderItems = $this->fetchAll('tb_order_item', ['cl_order_id' => $id]);
            if(!empty($orderItems))
            {
                foreach($orderItems AS $orderItem)
                {
                    
                    $menuItem = $this->fetchRow('tb_menu', ['cl_id' => $orderItem['cl_menu_id']]);
                    if(!$menuItem)
                    {
                       
                        $this->dbh->rollback();
                        return 0;
                    }
                   
                    
                    if($menuItem['cl_type'] == 'Food')
                    {
                        $notiUserID = $chefUser['cl_id'];
                    }
                    else if($menuItem['cl_type'] == 'Drinks')
                    {
                        $notiUserID = $barUser['cl_id'];
                    }
                    
                   
                    $notiInsert = [];
                    $notiInsert['cl_type'] = 'New Order';
                    $notiInsert['cl_order_item_id'] = $orderItem['cl_id'];
                    $notiInsert['cl_user_id'] = $notiUserID;
                    
                    
                    $notificationID =  $this->insert1($this->dbh, 'tb_notification', $notiInsert);
                    if(!$notificationID)
                    {
                        
                        $this->dbh->rollback();
                        return 0;
                    }

                    $res =  $this->update('tb_order', $data, $where);
                    if(!$res)
                    {
                        
                        $this->dbh->rollback();
                        return 0;
                    }
                }
            }
            else
            {
                $this->dbh->rollback();
                        return 0;
            }
            
            $this->dbh->commit();
            return 1;
        }
        catch (PDOException $e) {
            // roll back transaction
            $this->dbh->rollback();
            return 0;
        }
    }
    
    public function getUser($userID)
    {
        return $this->fetchRow('tb_users', ['cl_id' => $userID]);
    }
    
    public function getUserByRole($role, $userID)
    {
        return $this->fetchRow('tb_users', ['cl_type' => $role, 'cl_user_id' => $userID]);
    }
}

$obj = new complete();

$role = $user['cl_type'];
if($role == 'customer_admin')
{
    $adminUserID = $user['cl_id'];
    $adminUser = $obj->getUser($adminUserID);
    
    
}
else if($role == 'manager')
{
    $adminUserID = $user['cl_user_id'];
    $adminUser = $obj->getUser($adminUserID);
    

}

$barUser = $chefUser = $obj->getUserByRole('chef', $adminUserID);
if($adminUser['cl_chef_order'] == 2)
{
    $barUser = $obj->getUserByRole('bar', $adminUserID);
}




$msg = "";
$res = $obj->index($id, $user['cl_id'], $user['cl_user_id'], $chefUser, $barUser);
if($res)
{
    http_response_code(200);
    echo json_encode(['message' => 'Successfully Confirmed Order']);
    exit;
}
                
http_response_code(500);
echo json_encode(['error_text' => 'Something went wrong']);
exit;
