<?php include __DIR__ . '/layout/header.php'; ?>

<!-- ─── Stat Cards ─── -->
<div class="dashboard-stats">

    <a href="<?php echo $baseUrl; ?>/admin/products" style="text-decoration:none;">
        <div class="admin-card admin-card-metric">
            <div>
                <div class="metric-label">Toplam Ürün</div>
                <div class="metric-value"><?php echo $productsCount; ?></div>
            </div>
            <div class="metric-icon" style="color: var(--primary);">
                <i class="fa fa-print"></i>
            </div>
        </div>
    </a>

    <a href="<?php echo $baseUrl; ?>/admin/categories" style="text-decoration:none;">
        <div class="admin-card admin-card-metric">
            <div>
                <div class="metric-label">Kategoriler</div>
                <div class="metric-value"><?php echo $categoriesCount; ?></div>
            </div>
            <div class="metric-icon" style="color: var(--success);">
                <i class="fa fa-tags"></i>
            </div>
        </div>
    </a>

    <a href="<?php echo $baseUrl; ?>/admin/orders" style="text-decoration:none;">
        <div class="admin-card admin-card-metric">
            <div>
                <div class="metric-label">Siparişler</div>
                <div class="metric-value"><?php echo $ordersCount; ?></div>
            </div>
            <div class="metric-icon" style="color: var(--info);">
                <i class="fa fa-shopping-cart"></i>
            </div>
        </div>
    </a>

</div>

<!-- ─── Son Siparişler ─── -->
<div class="admin-card">
    <div class="admin-card-header">
        <h3 class="admin-card-title"><i class="fa fa-clock-o" style="color:var(--primary);margin-right:8px;"></i>Son Siparişler</h3>
        <a href="<?php echo $baseUrl; ?>/admin/orders" class="admin-btn admin-btn-sm">
            Tümünü Gör <i class="fa fa-arrow-right" style="margin-left:4px;"></i>
        </a>
    </div>

    <?php if (!empty($orders)): ?>

    <!-- Desktop Table -->
    <div class="admin-table-wrap desktop-only">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Sipariş ID</th>
                    <th>Müşteri</th>
                    <th>Tarih</th>
                    <th>Tutar</th>
                    <th>Durum</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($orders as $ord): ?>
                <?php
                    [$statusClass, $statusText] = orderStatusInfo($ord->status ?? '');
                ?>
                <tr>
                    <td><code style="font-weight:700;color:var(--primary);">#<?php echo htmlspecialchars($ord->id); ?></code></td>
                    <td style="font-weight:600;color:var(--text-1);"><?php echo htmlspecialchars($ord->customer_name ?? '-'); ?></td>
                    <td style="color:var(--text-3);font-size:12.5px;"><?php echo htmlspecialchars($ord->created_at ?? '-'); ?></td>
                    <td style="font-weight:700;"><?php echo \App\Services\CurrencyService::format($ord->total, $ord->currency); ?></td>
                    <td><span class="status-badge <?php echo $statusClass; ?>"><?php echo $statusText; ?></span></td>
                    <td style="text-align:right;">
                        <a href="<?php echo $baseUrl; ?>/admin/orders/edit/<?php echo $ord->id; ?>" class="admin-action-btn view">
                            <i class="fa fa-eye"></i>
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <!-- Mobile Cards -->
    <div class="mobile-only">
        <?php foreach ($orders as $ord): ?>
        <?php [$statusClass, $statusText] = orderStatusInfo($ord->status ?? ''); ?>
        <div class="m-card">
            <div class="m-card-header">
                <div class="m-card-title-wrap">
                    <div class="m-card-icon"><i class="fa fa-shopping-cart"></i></div>
                    <div>
                        <div class="m-card-title"><?php echo htmlspecialchars($ord->customer_name ?? '-'); ?></div>
                        <div class="m-card-subtitle">#<?php echo htmlspecialchars($ord->id); ?> · <?php echo htmlspecialchars($ord->created_at ?? '-'); ?></div>
                    </div>
                </div>
                <div class="m-card-value"><?php echo \App\Services\CurrencyService::format($ord->total, $ord->currency); ?></div>
            </div>
            <div class="m-card-footer">
                <span class="status-badge <?php echo $statusClass; ?>"><?php echo $statusText; ?></span>
                <a href="<?php echo $baseUrl; ?>/admin/orders/edit/<?php echo $ord->id; ?>" class="admin-btn admin-btn-sm">
                    <i class="fa fa-eye"></i> Görüntüle
                </a>
            </div>
        </div>
        <?php endforeach; ?>
    </div>

    <?php else: ?>
    <div style="text-align:center;padding:48px 20px;color:var(--text-3);">
        <i class="fa fa-shopping-cart" style="font-size:48px;opacity:0.2;display:block;margin-bottom:14px;"></i>
        <p>Henüz sipariş bulunmuyor.</p>
    </div>
    <?php endif; ?>
</div>

<?php
function orderStatusInfo($status) {
    $map = [
        'pending'          => ['badge-warning', 'Bekliyor'],
        'pending_payment'  => ['badge-warning', 'Ödeme Bekliyor'],
        'payment_uploaded' => ['badge-info',    'Dekont İnceleniyor'],
        'processing'       => ['badge-info',    'Hazırlanıyor'],
        'shipped'          => ['badge-purple',   'Kargoya Verildi'],
        'delivered'        => ['badge-success',  'Teslim Edildi'],
        'completed'        => ['badge-success',  'Tamamlandı'],
        'cancelled'        => ['badge-danger',   'İptal'],
        'returned'         => ['badge-muted',    'İade'],
    ];
    return $map[$status] ?? ['badge-muted', $status ?: '-'];
}
?>

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