<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
  
// database connection will be here
// include database and object files
include_once '../config/database.php';
include_once '../config/bukku.php';
include_once '../admin/admin.php';
include_once '../admin/bukkuInvoice.php';
  
// instantiate database and product object
$database = new Database();
$db = $database->getConnection();
$bukku = new Bukku();
$bBT = $bukku->getToken();
$bUrl = $bukku->getBaseURL();
  
// initialize object
$admin = new Admin($db);
$bukkuInvoice = new BukkuInvoice($bBT, $bUrl);

$request_method=$_SERVER["REQUEST_METHOD"];

// get posted data
$data = json_decode(file_get_contents("php://input"));

switch($request_method)
{
    case 'POST':
        
        // make sure data is not empty
        if(!empty($data)){
            $result = $bukkuInvoice->getInvoices($data->invID);
            
            if ($result == false){
                http_response_code(404);
                echo json_encode(array("message" => "Error Occurred"));
            }else if (!empty($result)){
                http_response_code(200);
                echo json_encode($result->transaction);
            }
        }else{
            // set response code - 400 bad request
            http_response_code(400);
            // tell the user
            echo json_encode(array("message" => "Input data is missing"));
        }
        break;
        
    case 'PUT':
        
        // make sure data is not empty
        if(!empty($data)){
            // set product property values
            $bukkuInvoice->input = $data;
            
            $result = $bukkuInvoice->createInvoice();
            
            if ($result == false){
                http_response_code(404);
                echo json_encode(array("message" => "Error Occurred"));
            }else if (!empty($result)){
                $tempResult = json_decode($result);
                $admin->invID = $tempResult->transaction->id;
                $admin->invNo = $tempResult->transaction->number;
                $admin->requestedCourseList = $data->requestedCourseList;
                
                $result2 = $admin->updateInNo();
                if ($result2 == false){
                    http_response_code(404);
                    echo json_encode(array("message" => "Error Occurred"));
                }else{
                    http_response_code(200);
                    echo json_encode(array("message" => $tempResult->transaction->number));
                }
            }
        }else{
            // set response code - 400 bad request
            http_response_code(400);
            // tell the user
            echo json_encode(array("message" => "Input data is missing"));
        }
        break;
        
        default:
            // set response code - 405 bad request
            http_response_code(405);
            // tell the user
            echo json_encode(array("message" => "Invalid Method"));
            break;
}