<?php

use api\classes\API_office_stompjes;

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_stompjes.php";

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

# Page functions

# JS Scripts to load for this page
$jsScriptLoadData['/src/js/sentri/delete_confirmation.js'] = true;
$jsScriptLoadData['/src/js/plugin/chart.js/chart.min.js'] = true;
$jsScriptLoadData['/src/js/sentri/stompjes.js'] = true;
$jsScriptLoadData['/src/js/plugin/datatables/datatables.min.js'] = true;
$jsScriptLoadData['/src/js/sentri/multiFilterSelect.js'] = true;
$jsScriptLoadData['datepicker'] = true;

# PageClasses Setup

# Retrieve Information for the page
$stmt = $GLOBALS['conn']->query("SELECT user_uuid, user_full_name, user_first_name, user_profile_picture_thumbnail, user_stompable, user_email FROM system_users WHERE user_stompable = '1'");
$administrators = [];
while ($row = $stmt->fetch_assoc()) {
    $administrators[$row['user_uuid']] = $row;
    $administrators[$row['user_uuid']]['amount'] = 0;
}

if (!isset($_GET['fd'])) {
    $SelectFromDate = strtotime(date('Y-m-01'));
} else {
    $date = str_replace('/', '-', htmlspecialchars($_GET['fd'], ENT_QUOTES, 'UTF-8'));
    $SelectFromDate = strtotime($date . ' 00:00:00');
}

if (!isset($_GET['td'])) {
    $SelectTillDate = time();
} else {
    $date = str_replace('/', '-', htmlspecialchars($_GET['td'], ENT_QUOTES, 'UTF-8'));
    $SelectTillDate = strtotime($date . ' 23:59:59');
}

$stompjes = $API->getStomp(true);

foreach ($stompjes as $stompje) {
    $administrators[$stompje['user_uuid']]['amount']++;
}

# Start page output
?>
<div class="form-group form-show-validation row mb-3">
    <div class="col-5">
        <h2>
            <i class="<?php echo $GLOBALS['pages']['office']['stompjeslist']['page_icon'] ?>"></i> &nbsp; <?php echo __('stompjeslist') ?>
        </h2>
    </div>
    <div class="col d-flex justify-content-end px-1">
        <div class="d-flex align-items-center gap-2 justify-content-end">
            <h5><?php echo __('from') ?>: </h5>
        </div>
        <div class="col-lg-auto col-md-auto col-sm-auto">
            <div class="input-group">
                <label for="fd"></label>
                <input type="text" id="fd" class="form-control" data-datepicker="true" value="<?php echo date('d/m/Y', $SelectFromDate) ?>"/>
                <span class="input-group-text"><i class="fa fa-calendar-check"></i></span>
            </div>
        </div>
        <div class="col-lg-auto col-md-auto col-sm-auto mt-sm-1 px-2">
            <h5><?php echo __('to') ?>: </h5>
        </div>
        <div class="col-lg-auto col-md-auto col-sm-auto px-2">
            <div class="input-group">
                <label for="td"></label>
                <input type="text" id="td" class="form-control" data-datepicker="true" value="<?php echo date('d/m/Y', $SelectTillDate) ?>"/>
                <span class="input-group-text"><i class="fa fa-calendar-check"></i></span>
            </div>
        </div>
        <div class="col-lg-auto col-md-auto col-sm-auto">
            <a id="datePicker" class="btn btn-primary"><i class="fa-solid fa-arrow-rotate-right"></i></a>
        </div>
    </div>
</div>


<div class="card">
    <div class="card-body">
        <canvas id="stompjesChart" height="50"></canvas>
    </div>
</div>

<div class="row align-content-center">
    <?php foreach ($administrators as $administrator) {
        if ($administrator['user_email'] != 'superuser') { ?>
            <div class="col-sm-6 col-md-3 flex-shrink-0">
                <div class="card card-stats card-round">
                    <div class="card-body">
                        <div class="row align-items-center">
                            <div class="col-icon">
                                <div class="avatar-l">
                                    <img class="avatar-img rounded-circle" src="data:image/png;base64,<?php echo str_replace("'", '', $administrator['user_profile_picture_thumbnail']) ?>" height="50px" alt="">
                                </div>
                            </div>
                            <div class="col col-stats ms-3 ms-sm-0">
                                <div class="numbers">
                                    <p class="card-category"><?php echo $administrator['user_first_name'] ?></p>
                                    <h4 class="card-title" id="count-<?php echo $administrator['user_uuid'] ?>"><?php echo $administrator['amount'] ?></h4>
                                </div>
                            </div>
                            <div class="col col-stats ms-3 ms-sm-0">
                                <a href="#" class="btn btn-warning btn-lg btn-rounded stomp-btn w-100 <?php echo (!$API->checkPermissions('office-stompjes-canstomp', 'RW', true)) ? 'disabled' : '' ?>" data-item-uuid="<?php echo $administrator['user_uuid'] ?>" data-item-name="user_uuid" data-api-url="/api/v1/office/stompjes/"><i class="fa-solid fa-hand-fist"></i></a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        <?php }
    } ?>
</div>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="multi-filter-select display table table-striped table-hover" data-skip-columns="0,2,3" data-page-length="50">
                <thead>
                <tr>
                    <th></th>
                    <th><?php echo __('first_name') ?></th>
                    <th><?php echo __('time') ?></th>
                    <?php if ($API->checkPermissions('office-stompjes', 'RW', true)) { ?>
                        <th><?php echo __('actions') ?></th>
                    <?php } ?>
                </tr>
                </thead>
                <tfoot>
                <tr>
                    <th></th>
                    <th><?php echo __('first_name') ?></th>
                    <th><?php echo __('time') ?></th>
                    <?php if ($API->checkPermissions('office-stompjes', 'RW', true)) { ?>
                        <th><?php echo __('actions') ?></th>
                    <?php } ?>
                </tr>
                </tfoot>
                <tbody>
                <?php foreach ($stompjes as $stompje) {
                    if ($administrators[$stompje['user_uuid']]['user_email'] != 'superuser') { ?>
                        <tr data-item-id="<?php echo $stompje['stomp_uuid']; ?>" data-user-uuid=<?php echo $stompje['user_uuid']; ?>>
                            <td class="text-nowrap">
                                <div class="avatar-sm ">
                                    <img class="avatar-img rounded-circle" src="data:image/png;base64,<?php echo str_replace("'", '', $administrators[$stompje['user_uuid']]['user_profile_picture_thumbnail']) ?>" height="50px" alt="">
                                </div>
                            </td>
                            <td class="text-nowrap"><?php echo $administrators[$stompje['user_uuid']]['user_first_name'] ?></td>
                            <td class="text-nowrap"><?php echo date('d-m-y H:i:s', $stompje['stomp_timestamp']) ?></td>
                            <?php if ($API->checkPermissions('office-stompjes', 'RW', true)) { ?>
                                <td>
                                    <a href="#" class="btn btn-danger btn-sm btn-rounded stomp-delete-btn" data-item-uuid="<?php echo $stompje['stomp_uuid'] ?>" data-api-url="/api/v1/office/stompjes/" data-item-name="stomp_uuid"><i class="fas fa-trash-alt"></i></a>
                                </td>
                            <?php } ?>
                        </tr>
                    <?php }
                } ?>
                </tbody>
            </table>
            <br>
        </div>
    </div>
</div>
