<?php

use api\classes\API_office_travel_reimburse;

session_start();

require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_office_travel_reimburse.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_companies.php";

$API_office_travel_reimburse = new API_office_travel_reimburse();

if (!$GLOBALS['modules_enabled']['office']) {
    echo '405 Not Allowed';
    exit;
}

if ($API_office_travel_reimburse->request_method === 'GET') {
    $API_office_travel_reimburse->checkPermissions('office-travel-reimburse', 'RO');

    if (!isset($_GET['builder']) || !is_array($_GET['builder'])) {
        $_GET['builder'] = [];
    }

    # Append user_uuid where builder to always search for the user_uuid that is requesting the travel entries
    $_GET['builder'][] = [
        'where' => [
            0 => 'user_uuid',
            1 => $_SESSION['user']['user_uuid']
        ]
    ];

    $travel_reimbursements = $API_office_travel_reimburse->getTravelReimburse();
    $API_office_travel_reimburse->apiOutput($code = 200, $travel_reimbursements);

} elseif ($API_office_travel_reimburse->request_method === 'POST') {

    $API_office_travel_reimburse->checkPermissions('office-travel-reimburse', 'RW');
    $API_office_travel_reimburse->return_url = false;
    $API_office_travel_reimburse->setTravelReimburseCompany();
    $API_office_travel_reimburse->postedData['user_uuid'] = $_SESSION['user']['user_uuid'];

    $requiredFields = [
        'user_uuid' => ['type' => 'uuid'],
        'departure_postcode' => ['type' => 'string'],
        'destination_postcode' => ['type' => 'string'],
        'travel_date' => ['type' => 'date'],
        'travel_distance' => ['type' => 'int'],
        'office_travelreimburse_company_uuid' => ['type' => 'uuid'],
        'office_travelreimburse_company_name' => ['type' => 'string'],
        'homework' => ['type' => 'int']
    ];

    $optionalFields = [
        'travel_description' => ['type' => 'string']
    ];

    $API_office_travel_reimburse->validateData($requiredFields, $optionalFields);
    $API_office_travel_reimburse->createTravelReimburse();

} elseif ($API_office_travel_reimburse->request_method === 'PUT') {
    # Only superuser can delete permission due to fact that the backend needs programming when setting a permission

    $API_office_travel_reimburse->checkPermissions('office-travel-reimburse', 'RW');
    $API_office_travel_reimburse->return_url = false;
    $API_office_travel_reimburse->setTravelReimburseCompany();
    $API_office_travel_reimburse->postedData['user_uuid'] = $_SESSION['user']['user_uuid'];

    $requiredFields = [
        'reimburse_uuid' => ['type' => 'uuid'],
        'user_uuid' => ['type' => 'uuid'],
        'departure_postcode' => ['type' => 'string'],
        'destination_postcode' => ['type' => 'string'],
        'travel_date' => ['type' => 'date'],
        'travel_distance' => ['type' => 'int'],
        'office_travelreimburse_company_uuid' => ['type' => 'uuid'],
        'office_travelreimburse_company_name' => ['type' => 'string'],
        'homework' => ['type' => 'boolean']
    ];

    $optionalFields = [
        'travel_description' => ['type' => 'string'],
    ];

    $API_office_travel_reimburse->validateData($requiredFields, $optionalFields);
    $API_office_travel_reimburse->updateTravelReimburse();

} elseif ($API_office_travel_reimburse->request_method === 'DELETE') {
    # Only superuser can delete permission due to fact that the backend needs programming when setting a permission
    $API_office_travel_reimburse->checkPermissions('office-travel-reimburse', 'RW');
    $API_office_travel_reimburse->return_url = false;

    $requiredFields = [
        'reimburse_uuid' => ['type' => 'uuid']
    ];

    $API_office_travel_reimburse->validateData($requiredFields);
    $API_office_travel_reimburse->deleteTravelReimburse();

}