<?php
session_start();
require_once('./../admin/tcpdf/tcpdf.php');
require_once("inc/config.inc.php");
require_once("inc/functions.inc.php");

// Überprüfen, ob eine Benutzer-ID in der Session vorhanden ist
if (!isset($_SESSION['userid'])) {
    die("Kein Benutzer angemeldet.");
}

$user = check_user();

$user_id = $_SESSION['userid'];




try {
	$query2 = "
	SELECT 
		DATE(timestamp_datetime) AS datum,
		GROUP_CONCAT(timestamp_type ORDER BY timestamp_datetime) AS day_sequence
	FROM 
		timestamps
	WHERE 
		employee_id = :employee_id
	GROUP BY 
		DATE(timestamp_datetime);";
		
	$stmt = $pdo->prepare($query2);
	$stmt->bindParam(':employee_id', $user["id"], PDO::PARAM_INT);
	$stmt->execute();
	$result = $stmt->fetchAll();

	$invalidDates = [];

} catch(PDOException $e) {
	echo "Datenbankfehler: " . $e->getMessage();
}



?>

<?php include 'header.php'; ?>
<div class="container">
    <div class="row">
        <div class="col-md-12">
		
		
<div class="container">
    <h2 class="mb-4">Zeitbuchungsfehler Auswertung</h2>
<?php

	foreach ($result as $row) {
		if (!isValidSequence($row["day_sequence"])) {
			$invalidDates[] = $row["datum"];
		}
	}

	
	?>
	
	




		<?php if (!empty($invalidDates)): ?>
			<table class="table table-striped">
				<thead class="thead-dark">
					<tr>
						<th>Datum</th>
						<th>Fehler</th>
						<th>Aktionen</th>
					</tr>
				</thead>
				<tbody>
				<?php foreach ($invalidDates as $date): ?>
					<tr>
						<td><?php echo date('d.m.Y', strtotime($date)); ?></td>
						<td>Fehlerhafte KOMMEN/GEHEN Buchung</td>
						<td>
							<a href="editDayEntries.php?employee_id=<?php echo $user['id']; ?>&datum=<?php echo $date; ?>" class="btn btn-warning">Anpassen</a>
						</td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>
		<?php else: ?>
			<div class="alert alert-info" role="alert">
				Keine Zeitbuchungsfehler gefunden.
			</div>
		<?php endif; ?>

		</div>
	</div>
</div>
<?php include 'footer.php'; ?>