<?php
require_once 'config/database.php';
require_once 'config/auth.php';
require_once 'includes/header.php';
require_once 'includes/sidebar.php';
requireLogin();

// Mengambil statistik untuk dashboard
$db = getDB();

// Total komponen per kategori
$stats = [
    'processors' => $db->query("SELECT COUNT(*) as count FROM processors")->fetch()['count'],
    'motherboards' => $db->query("SELECT COUNT(*) as count FROM motherboards")->fetch()['count'],
    'vga_cards' => $db->query("SELECT COUNT(*) as count FROM vga_cards")->fetch()['count'],
    'ram' => $db->query("SELECT COUNT(*) as count FROM ram")->fetch()['count'],
    'storage' => $db->query("SELECT COUNT(*) as count FROM storage")->fetch()['count'],
    'casing' => $db->query("SELECT COUNT(*) as count FROM casing")->fetch()['count']
];

// Total perhitungan yang sudah dilakukan
$totalCalculations = $db->query("SELECT COUNT(*) as count FROM calculations")->fetch()['count'];

// Perhitungan terbaru
$recentCalculations = $db->query("
    SELECT c.*, u.username 
    FROM calculations c 
    LEFT JOIN users u ON c.user_id = u.id 
    ORDER BY c.created_at DESC 
    LIMIT 5
")->fetchAll();
?>

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0">Dashboard</h1>
                </div>
            </div>
        </div>
    </div>

    <!-- Main content -->
    <section class="content">
        <div class="container-fluid">
            <!-- Small boxes (Stat box) -->
            <div class="row">
                <div class="col-lg-2 col-6">
                    <!-- small box -->
                    <div class="small-box bg-info">
                        <div class="inner">
                            <h3><?php echo $stats['processors']; ?></h3>
                            <p>Processor</p>
                        </div>
                        <div class="icon">
                            <i class="fas fa-microchip"></i>
                        </div>
                        <a href="modules/components/processor/" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
                    </div>
                </div>
                <!-- ./col -->
                <div class="col-lg-2 col-6">
                    <!-- small box -->
                    <div class="small-box bg-success">
                        <div class="inner">
                            <h3><?php echo $stats['motherboards']; ?></h3>
                            <p>Motherboard</p>
                        </div>
                        <div class="icon">
                            <i class="fas fa-server"></i>
                        </div>
                        <a href="modules/components/motherboard/" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
                    </div>
                </div>
                <!-- ./col -->
                <div class="col-lg-2 col-6">
                    <!-- small box -->
                    <div class="small-box bg-warning">
                        <div class="inner">
                            <h3><?php echo $stats['vga_cards']; ?></h3>
                            <p>VGA</p>
                        </div>
                        <div class="icon">
                            <i class="fas fa-tv"></i>
                        </div>
                        <a href="modules/components/vga/" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
                    </div>
                </div>
                <!-- ./col -->
                <div class="col-lg-2 col-6">
                    <!-- small box -->
                    <div class="small-box bg-danger">
                        <div class="inner">
                            <h3><?php echo $stats['ram']; ?></h3>
                            <p>RAM</p>
                        </div>
                        <div class="icon">
                            <i class="fas fa-memory"></i>
                        </div>
                        <a href="modules/components/ram/" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
                    </div>
                </div>
                <!-- ./col -->
                <div class="col-lg-2 col-6">
                    <!-- small box -->
                    <div class="small-box bg-primary">
                        <div class="inner">
                            <h3><?php echo $stats['storage']; ?></h3>
                            <p>Storage</p>
                        </div>
                        <div class="icon">
                            <i class="fas fa-hdd"></i>
                        </div>
                        <a href="modules/components/storage/" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
                    </div>
                </div>
                <!-- ./col -->
                <div class="col-lg-2 col-6">
                    <!-- small box -->
                    <div class="small-box bg-secondary">
                        <div class="inner">
                            <h3><?php echo $stats['casing']; ?></h3>
                            <p>Casing</p>
                        </div>
                        <div class="icon">
                            <i class="fas fa-box"></i>
                        </div>
                        <a href="modules/components/casing/" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
                    </div>
                </div>
                <!-- ./col -->
            </div>

            <!-- Main row -->
            <div class="row">
                <!-- Left col -->
                <section class="col-lg-12">
                    <!-- Custom tabs (Charts with tabs)-->
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">
                                <i class="fas fa-history mr-1"></i>
                                Perhitungan Terbaru
                            </h3>
                        </div>
                        <div class="card-body table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>Tanggal</th>
                                        <th>User</th>
                                        <th>Harga Kalkulasi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($recentCalculations as $calc): ?>
                                    <tr>
                                        <td><?php echo date('d/m/Y H:i', strtotime($calc['created_at'])); ?></td>
                                        <td><?php echo htmlspecialchars($calc['username']); ?></td>
                                        <td>Rp <?php echo number_format($calc['calculated_price'], 0, ',', '.'); ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </section>
            </div>
        </div>
    </section>
</div>

<?php require_once 'includes/footer.php'; ?>