<?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_id = $_SESSION['userid'];
$user = check_user();

?>

<?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

// Benutzer aus der Datenbank erhalten
try {
    $users_stmt = $pdo->prepare("SELECT id,vorname, nachname FROM users WHERE zeiterfassung='1' ORDER BY nachname ASC");
    $users_stmt->execute();
    $users = $users_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
    echo "Datenbankfehler: " . $e->getMessage();
}


foreach($users AS $user){
	
	
	try {
		// Holen Sie die fehlerhaften Zeitbuchungen des Mitarbeiters aus der Datenbank
		$query = "
			SELECT
				DATE(timestamp_datetime) as datum, 
				SUM(CASE WHEN timestamp_type = 'KOMMEN' THEN 1 ELSE 0 END) as kommen_count, 
				SUM(CASE WHEN timestamp_type = 'GEHEN' THEN 1 ELSE 0 END) as gehen_count 
			FROM 
				timestamps 
			WHERE 
				employee_id = :employee_id 
			GROUP BY 
				DATE(timestamp_datetime)
			HAVING 
				kommen_count != gehen_count";

		$stmt = $pdo->prepare($query);
		$stmt->bindParam(':employee_id', $user["id"], PDO::PARAM_INT);
		$stmt->execute();
		$fehlerhafteTage = $stmt->fetchAll(PDO::FETCH_ASSOC);
	} catch(PDOException $e) {
		echo "Datenbankfehler: " . $e->getMessage();
	}
	
	
	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();
	}
	
	
	try {			
		$query1 = "
			SELECT 
				vorname,
				nachname
			FROM 
				users 
			WHERE 
				id = :employee_id
			ORDER BY
				nachname
				";

		$stmt = $pdo->prepare($query1);
		$stmt->bindParam(':employee_id', $user["id"], PDO::PARAM_INT);
		$stmt->execute();
		$userdaten = $stmt->fetchAll(PDO::FETCH_ASSOC);
	} catch(PDOException $e) {
		echo "Datenbankfehler: " . $e->getMessage();
	}
		

	foreach ($userdaten as $usertag){ 
		echo "<h5>" . $usertag["vorname"] . " " . $usertag["nachname"] . "</h5>";
	
	}
	
	
	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 Daten</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; ?>
	
<?php


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