<?php
session_start();
require 'config.php';
require 'db.php';

if ($dbhandle->connect_error) {
    die("Verbindung fehlgeschlagen: " . $dbhandle->connect_error);
}

// Query
$sql = "SELECT CONCAT(u.discord_name) AS clientname, 
               SUM(c.preis) AS gesamt_einnahmen, 
               ROUND(SUM(c.preis) * 0.1, 2) AS abschlag
        FROM contracts c
        INNER JOIN users u ON c.user_id = u.id
        GROUP BY u.id, u.discord_name
        ORDER BY gesamt_einnahmen DESC";


$result = $dbhandle->query($sql);
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <title>Übersicht</title>
    <style>
        table { border-collapse: collapse; width: 80%; margin: auto; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: center; }
        th { background-color: #4CAF50; color: white; }
    </style>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <?php include "header.php"?>
    <h1 style="text-align:center;">Übersicht der Einnahmen</h1>
    <table>
        <tr>
            <th>Dienstnummer + Name</th>
            <th>Gesamteinnahmen (€)</th>
            <th>10 % Abschlag (€)</th>
        </tr>
        <?php while($row = $result->fetch_assoc()): ?>
        <tr>
            <td><?php echo htmlspecialchars($row['clientname']); ?></td>
            <td><?php echo number_format($row['gesamt_einnahmen'], 2, ',', '.'); ?> €</td>
            <td><?php echo number_format($row['abschlag'], 2, ',', '.'); ?> €</td>
        </tr>
        <?php endwhile; ?>
    </table>
</body>
</html>
<?php
$dbhandle->close();
?>