<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/functions.php';

if (empty($_SESSION['admin_logged_in'])) {
    header('Location: login.php');
    exit;
}

expirePendingOrders();

$pageTitle = 'Admin Dashboard';
$orders = getOrders();
$products = getProducts();
$organizations = getOrganizations(false);

$stats = [
    'products' => count($products),
    'organizations' => count($organizations),
    'unconfirmed' => 0,
    'open' => 0,
    'partial' => 0,
    'processed' => 0,
    'cancelled' => 0,
];

foreach ($orders as $order) {
    if ($order['confirmation_status'] === 'pending') {
        $stats['unconfirmed']++;
    } elseif ($order['status'] === 'cancelled') {
        $stats['cancelled']++;
    } elseif ($order['status'] === 'processed') {
        $stats['processed']++;
    } elseif ($order['status'] === 'partial') {
        $stats['partial']++;
    } else {
        $stats['open']++;
    }
}

$recentOrders = $orders;
usort($recentOrders, function ($left, $right) {
    return strcmp($right['created_at'], $left['created_at']);
});
$recentOrders = array_slice($recentOrders, 0, 8);

$bodyClass = 'admin-page';
include __DIR__ . '/../includes/header.php';
?>

<div class="admin-header">
    <h2>Admin Dashboard</h2>
    <div class="admin-dashboard-actions">
        <a href="orders.php" class="btn">Bestellungen</a>
        <details class="admin-actions-dropdown">
            <summary class="btn btn-secondary">Verwaltung</summary>
            <div class="admin-actions-menu">
                <a href="products.php">Produkte verwalten</a>
                <a href="categories.php">Kategorien verwalten</a>
                <a href="organizations.php">Organisationen verwalten</a>
                <a href="settings.php">Einstellungen</a>
                <a href="faq.php">FAQ bearbeiten</a>
                <a href="admins.php">Admins verwalten</a>
                <form method="POST" action="login.php" class="inline-form">
                    <?php echo csrfField(); ?>
                    <button type="submit" name="logout" class="btn btn-secondary btn-small">Abmelden</button>
                </form>
            </div>
        </details>
    </div>
</div>

<div class="admin-stats">
    <div class="stat-card">
        <h3>Produkte</h3>
        <div class="stat-value"><?php echo $stats['products']; ?></div>
    </div>
    <div class="stat-card">
        <h3>Organisationen</h3>
        <div class="stat-value"><?php echo $stats['organizations']; ?></div>
    </div>
    <div class="stat-card">
        <h3>Unbestätigt</h3>
        <div class="stat-value"><?php echo $stats['unconfirmed']; ?></div>
    </div>
    <div class="stat-card">
        <h3>Offen</h3>
        <div class="stat-value"><?php echo $stats['open']; ?></div>
    </div>
    <div class="stat-card">
        <h3>Teilweise bearbeitet</h3>
        <div class="stat-value"><?php echo $stats['partial']; ?></div>
    </div>
    <div class="stat-card">
        <h3>Bearbeitet</h3>
        <div class="stat-value"><?php echo $stats['processed']; ?></div>
    </div>
    <div class="stat-card">
        <h3>Storniert</h3>
        <div class="stat-value"><?php echo $stats['cancelled']; ?></div>
    </div>
</div>

<h3 class="section-title mt-4">Letzte Bestellungen</h3>

<?php if (empty($recentOrders)): ?>
    <p>Keine Bestellungen vorhanden.</p>
<?php else: ?>
    <div class="table-responsive">
        <table class="responsive-table">
            <thead>
                <tr>
                    <th>Bestellnummer</th>
                    <th>Name</th>
                    <th>Organisation</th>
                    <th>Erstellt</th>
                    <th>Status</th>
                    <th>Aktionen</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($recentOrders as $order): ?>
                    <tr>
                        <td data-label="Bestellnummer"><strong><?php echo escape($order['id']); ?></strong></td>
                        <td data-label="Name"><?php echo escape($order['customer_name']); ?></td>
                        <td data-label="Organisation"><?php echo escape($order['organization_label']); ?></td>
                        <td data-label="Erstellt"><?php echo escape(formatDate($order['created_at'])); ?></td>
                        <td data-label="Status"><span class="status <?php echo escape(getOrderStatusClass($order)); ?>"><?php echo escape(getOrderStatusLabel($order)); ?></span></td>
                        <td data-label="Aktionen"><a href="orders.php?order_id=<?php echo urlencode($order['id']); ?>" class="btn btn-small">Details</a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>

<?php include __DIR__ . '/../includes/footer.php'; ?>
