<?php

namespace api\classes;

use api\classes\API;
use JetBrains\PhpStorm\NoReturn;

require_once 'API.php';

class API_office_stompjes extends API
{
    #[NoReturn]
    public function addStomp()
    {
        $query = "INSERT INTO office_stompjes (stomp_uuid, user_uuid, stomp_timestamp) VALUES (UUID(), ?, ?)";
        $stmt = $this->prepareStatement($query);
        $stmt->bind_param('si', $this->data['user_uuid'], time());
        $this->executeStatement($stmt);
        $stmt->close();

        $this->apiOutput(200, ['success' => 'Stomp added.']);
    }

    #[NoReturn]
    public function deleteStomp()
    {
        $query = "DELETE FROM office_stompjes WHERE stomp_uuid = ?";
        $stmt = $this->prepareStatement($query);
        $stmt->bind_param('s', $this->data['stomp_uuid']);
        $this->executeStatement($stmt);
        $stmt->close();

        $this->apiOutput(200, ['success' => 'Stomp removed.']);
    }

    public function getStomp($frontend = false)
    {
        if (!$frontend) {
            list($query, $types, $params) = $this->buildDynamicQuery('office_stompjes');

            $items = $this->generalGetFunction($query, $types, $params, false, 'Stompjes');

            return ($items);

        } else {
            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 = [];
            $stmt = $this->conn->query("SELECT stomp_uuid, office_stompjes.user_uuid, user_full_name, user_first_name, stomp_timestamp 
            FROM office_stompjes 
            INNER JOIN system_users ON office_stompjes.user_uuid = system_users.user_uuid  
            WHERE stomp_timestamp BETWEEN '$SelectFromDate' AND '$SelectTillDate'
            AND user_stompable = '1'
            ORDER BY stomp_timestamp DESC");
            while ($row = $stmt->fetch_assoc()) {
                $stompjes[] = $row;
            }

            return $stompjes;
        }
    }
}