<?php


use api\classes\API_office_travel_reimburse;
use api\classes\API_companies;

if (!defined('APP_INIT')) {
    exit;
}

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

# IDE Section

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

# Check permissions
$API = new API_office_travel_reimburse();
if (!$API->checkPermissions('office-travel-reimburse', 'RO', true)) {
    echo 'error 401 unauthorized';
    exit;
}

# Page functions

# JS Scripts to load for this page
$jsScriptLoadData['/src/js/plugin/fullcalendar/fullcalendar.global.js'] = true;
$jsScriptLoadData['/src/css/fullcalendar/theme/global.js'] = true;
$jsScriptLoadData['/src/js/sentri/travelReimburse.js'] = true;
$jsScriptLoadData['/src/js/sentri/companiesMap.js'] = true;

# PageClasses Setup

# Retrieve Information for the page
$API_companies = new API_companies();
$_GET['builder'] = [1 => ['where' => [0 => 'company_state', 1 => 'active']]];

# Retrieve Information for the page
$portal_settings = $GLOBALS['conn']->query("SELECT * FROM system_settings")->fetch_assoc();

$companies = $API_companies->getCompanies();

# Start page output
?>

<div class="card">
    <div class="card-body pt-2">
        <form id="defaultForNew" enctype="multipart/form-data" method="post" action="/api/v1/portal-management/users/">
            <div class="row align-items-end mt-0">
                <div class="col mt-0">
                    <label for="new_departure_postcode" class="form-label"><?php echo __('departure_postcode') ?></label>
                    <input type="text" id="new_departure_postcode" class="form-control" value="" placeholder="Departure Postcode" required>
                </div>

                <div class="col mt-0">
                    <label for="new_destination_postcode" class="form-label"><?php echo __('destination_postcode') ?></label>
                    <input type="text" id="new_destination_postcode" class="form-control" value="" placeholder="Destination Postcode" required>
                </div>

                <div class="col mt-0">
                    <label for="new_travel_distance" class="form-label"><?php echo __('kilometers') ?></label>
                    <input type="number" id="new_travel_distance" class="form-control" value="" placeholder="0" required>
                </div>

                <div class="col mt-0">
                    <label for="new_office_travelreimburse_company_uuid" class="form-label"><?php echo __('company') ?></label>
                    <select id="new_office_travelreimburse_company_uuid" class="form-select" required>
                        <option value=""></option>
                        <?php foreach ($companies as $company) { ?>
                            <option value="<?php echo $company['company_uuid'] ?>">
                                <?php echo $company['company_name'] ?>
                            </option>
                        <?php } ?>
                    </select>
                </div>
                <div class="col">
                    <label class="form-check-label" for="new_homework"><?php echo __('homework') ?></label>
                    <div class="form-check form-switch pb-3 mb-4">
                        <input class="form-check-input" type="checkbox" id="new_homework">
                    </div>
                </div>

                <div class="col">
                    <button type="submit" class="btn btn-success">
                        <?php echo __('save_default') ?>
                    </button>
                </div>
            </div>

        </form>
    </div>
</div>

<div class="container-fluid h-100 d-flex flex-column p-0">
    <div id="calendar" class="flex-grow-1"></div>
</div>

<div class="modal fade" id="travelModal">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <h5><?php echo __('edit') . ' ' . __('travelReimburse') ?></h5>
            </div>

            <div class="modal-body">
                <input type="hidden" id="reimburse_uuid">
                <input type="hidden" id="travel_date">

                <div class="mb-3">
                    <label><?php echo __('departure_postcode') ?></label>
                    <label for="departure_postcode"></label><input class="form-control" id="departure_postcode" required>
                </div>

                <div class="mb-3">
                    <label><?php echo __('destination_postcode') ?></label>
                    <label for="destination_postcode"></label><input class="form-control" id="destination_postcode" required>
                </div>

                <div class="mb-3">
                    <label><?php echo __('kilometers') ?></label>
                    <label for="travel_distance"></label><input type="number" class="form-control" id="travel_distance" required>
                </div>

                <div class="mb-3">
                    <label><?php echo __('company') ?></label>
                    <select id="office_travelreimburse_company_uuid" aria-label="office_travelreimburse_company_uuid" aria-describedby="office_travelreimburse_company_uuid" class="form-control" required>
                        <option value=""></option>
                        <?php foreach ($companies as $company) { ?>
                            <option value="<?php echo $company['company_uuid'] ?>"><?php echo $company['company_name'] ?></option>
                        <?php } ?>
                    </select>
                </div>

                <div class="mb-3">
                    <label><?php echo __('description') ?></label>
                    <label for="travel_description"></label><textarea class="form-control" id="travel_description"></textarea>
                </div>

                <div class="form-check form-switch pb-3 mb-3">
                    <label class="form-check-label" for="homework"><?php echo __('total_travel_distance_homework') ?></label>
                    <input class="form-check-input" type="checkbox" id="homework">
                </div>

            </div>

            <div class="modal-footer">
                <button class="btn btn-danger" id="deleteTravelBtn"><?php echo __('delete') ?></button>
                <button class="btn btn-primary" id="saveTravelBtn"><?php echo __('save') ?></button>
            </div>
        </div>
    </div>
</div>

<div class="card mt-3">
    <div class="card-body">
        <div class="row">
            <div class="col-6">
                <table class="table table-sm table-responsive">
                    <tr>
                        <td><?php echo __('office_travelreimburse_cents_homework_incl') ?>:</td>
                        <td>
                            <span id="office_travelreimburse_cents_homework_incl">
                                € <?php echo number_format($portal_settings['office_travelreimburse_cents_homework_incl'] / 100, 2, ',', '.') ?>
                            </span>
                        </td>
                        <td>
                            <span id="total_travel_distance_homework_incl"></span>
                        </td>
                        <td>
                            <span id="office_travelreimburse_cents_homework_incl_total">

                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td><?php echo __('office_travelreimburse_cents_homework_excl') ?>:</td>
                        <td>
                            <span id="office_travelreimburse_cents_homework_excl">
                                € <?php echo number_format($portal_settings['office_travelreimburse_cents_homework_excl'] / 100, 2, ',', '.') ?>
                            </span>
                        </td>
                        <td>
                            <span id="total_travel_distance_homework_excl"></span>
                        </td>
                        <td>
                            <span id="office_travelreimburse_cents_homework_excl_total">

                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td><?php echo __('office_travelreimburse_cents_business_incl') ?>:</td>
                        <td>
                            <span id="office_travelreimburse_cents_business_incl">
                                € <?php echo number_format($portal_settings['office_travelreimburse_cents_business_incl'] / 100, 2, ',', '.') ?>
                            </span>
                        </td>
                        <td>
                            <span id="total_travel_distance_business_incl"></span>
                        </td>
                        <td>
                            <span id="office_travelreimburse_cents_business_incl_total">

                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td><?php echo __('office_travelreimburse_cents_business_excl') ?>:</td>
                        <td>
                            <span id="office_travelreimburse_cents_business_excl">
                                € <?php echo number_format($portal_settings['office_travelreimburse_cents_business_excl'] / 100, 2, ',', '.') ?>
                            </span>
                        </td>
                        <td>
                            <span id="total_travel_distance_business_excl"></span>
                        </td>
                        <td>
                            <span id="office_travelreimburse_cents_business_excl_total">

                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td><?php echo __('total_travel_incl') ?>:</td>
                        <td></td>
                        <td></td>
                        <td>
                            <span id="total_travel_incl">

                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td><?php echo __('total_travel_excl') ?>:</td>
                        <td></td>
                        <td></td>
                        <td>
                            <span id="total_travel_excl">

                            </span>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col-auto">
                <form method="POST" action="/api/v1/office/travel-reimburse/send/">
                    <input type="hidden" name="calender_month" id="calender_month" value="">
                    <button class="btn btn-success" type="submit">
                        <i class="fa-regular fa-envelope"></i> <?php echo __('mail_travelreimburse') ?>
                        <br> (<?php echo $_SESSION['user']['user_email'] ?>)
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

