<?php
$page_title = "Dashboard Owner";
$header_title = "Ringkasan Toko Kue Edelys";
require_once __DIR__ . '/../includes/admin_header.php';

// Metrik 1: Total Pendapatan dari pesanan yang sukses/completed
$rev_stmt = $db->query("SELECT SUM(total_price) FROM orders WHERE status = 'completed'");
$total_revenue = $rev_stmt->fetchColumn();
$total_revenue = $total_revenue ? $total_revenue : 0;

// Metrik 2: Jumlah Pesanan Selesai
$completed_stmt = $db->query("SELECT COUNT(*) FROM orders WHERE status = 'completed'");
$orders_completed = $completed_stmt->fetchColumn();

// Metrik 3: Jumlah Pesanan Pending
$pending_stmt = $db->query("SELECT COUNT(*) FROM orders WHERE status = 'pending'");
$orders_pending = $pending_stmt->fetchColumn();

// Metrik 4: Produk Stok Rendah (<= 5)
$low_stock_stmt = $db->query("SELECT COUNT(*) FROM products WHERE stock <= 5 AND is_active = 1");
$low_stock_count = $low_stock_stmt->fetchColumn();

// Ambil 5 Pesanan Terbaru
$recent_orders = $db->query("SELECT * FROM orders ORDER BY id DESC LIMIT 5")->fetchAll();

// Ambil Produk dengan Stok Terendah
$low_stock_products = $db->query("SELECT * FROM products WHERE stock <= 5 AND is_active = 1 ORDER BY stock ASC LIMIT 5")->fetchAll();
?>

<!-- Metrik Grid -->
<section class="metrics-grid">
    <div class="metric-card">
        <div class="metric-info">
            <h3>Total Pendapatan</h3>
            <div class="value">Rp <?php echo number_format($total_revenue, 0, ',', '.'); ?></div>
        </div>
        <div class="metric-icon green">
            <i class="fa-solid fa-money-bill-wave"></i>
        </div>
    </div>
    
    <div class="metric-card">
        <div class="metric-info">
            <h3>Pesanan Selesai</h3>
            <div class="value"><?php echo $orders_completed; ?></div>
        </div>
        <div class="metric-icon blue">
            <i class="fa-solid fa-circle-check"></i>
        </div>
    </div>

    <div class="metric-card">
        <div class="metric-info">
            <h3>Pesanan Pending</h3>
            <div class="value"><?php echo $orders_pending; ?></div>
        </div>
        <div class="metric-icon gold">
            <i class="fa-solid fa-hourglass-half"></i>
        </div>
    </div>

    <div class="metric-card">
        <div class="metric-info">
            <h3>Stok Menipis (<= 5)</h3>
            <div class="value"><?php echo $low_stock_count; ?></div>
        </div>
        <div class="metric-icon red">
            <i class="fa-solid fa-triangle-exclamation"></i>
        </div>
    </div>
</section>

<!-- Data Pesanan & Stok Rendah -->
<div style="display: grid; grid-template-columns: 1.6fr 1fr; gap: 2rem;">
    <!-- Pesanan Terbaru -->
    <div class="panel">
        <div class="panel-header">
            <h2 class="panel-title"><i class="fa-solid fa-receipt"></i> Pesanan Terbaru</h2>
            <a href="orders.php" class="btn-admin btn-admin-outline" style="font-size: 0.8rem; padding: 0.4rem 0.8rem;">Lihat Semua</a>
        </div>
        <div class="table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Invoice</th>
                        <th>Pelanggan</th>
                        <th>Tanggal</th>
                        <th>Total</th>
                        <th>Status</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($recent_orders) > 0): ?>
                        <?php foreach ($recent_orders as $ord): ?>
                            <tr>
                                <td style="font-family: monospace; font-weight: bold;">
                                    INV-<?php echo str_pad($ord['id'], 6, '0', STR_PAD_LEFT); ?>
                                </td>
                                <td>
                                    <strong><?php echo htmlspecialchars($ord['customer_name']); ?></strong>
                                    <div style="font-size: 0.8rem; color: var(--text-muted);"><?php echo htmlspecialchars($ord['customer_phone']); ?></div>
                                </td>
                                <td><?php echo date('d/m/Y H:i', strtotime($ord['created_at'])); ?></td>
                                <td style="font-weight: 600;">Rp <?php echo number_format($ord['total_price'], 0, ',', '.'); ?></td>
                                <td>
                                    <span class="badge badge-<?php echo $ord['status']; ?>">
                                        <?php 
                                        $status_labels = [
                                            'pending' => 'Menunggu',
                                            'processing' => 'Diproses',
                                            'completed' => 'Selesai',
                                            'cancelled' => 'Dibatalkan'
                                        ];
                                        echo isset($status_labels[$ord['status']]) ? $status_labels[$ord['status']] : $ord['status'];
                                        ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="orders.php?id=<?php echo $ord['id']; ?>" class="btn-admin btn-admin-primary" style="padding: 0.3rem 0.6rem; font-size: 0.8rem;">Kelola</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="6" style="text-align: center; color: var(--text-muted); padding: 2rem;">Belum ada pesanan masuk.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <!-- Peringatan Stok Rendah -->
    <div class="panel">
        <div class="panel-header">
            <h2 class="panel-title" style="color: var(--danger);"><i class="fa-solid fa-boxes-stacked"></i> Stok Hampir Habis</h2>
            <a href="stock.php" class="btn-admin btn-admin-outline" style="font-size: 0.8rem; padding: 0.4rem 0.8rem;">Mutasi Stok</a>
        </div>
        <div class="table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Nama Kue</th>
                        <th>Kategori</th>
                        <th>Sisa Stok</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($low_stock_products) > 0): ?>
                        <?php foreach ($low_stock_products as $p): ?>
                            <tr>
                                <td>
                                    <strong style="color: var(--text-dark);"><?php echo htmlspecialchars($p['name']); ?></strong>
                                </td>
                                <td>
                                    <span style="font-size: 0.85rem; color: var(--text-muted);"><?php echo htmlspecialchars($p['category']); ?></span>
                                </td>
                                <td>
                                    <span style="font-weight: bold; color: <?php echo $p['stock'] == 0 ? 'var(--danger)' : 'var(--warning)'; ?>;">
                                        <?php echo $p['stock']; ?> pcs
                                    </span>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="3" style="text-align: center; color: var(--success); padding: 2rem; font-weight: 500;">
                                <i class="fa-solid fa-circle-check"></i> Semua stok aman!
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/../includes/admin_footer.php'; ?>
