<?php
require_once 'config/database.php';
require_once 'includes/auth.php';

// Hitung total data
$total_barang = $conn->query("SELECT COUNT(*) as total FROM pdta_inventaris_barang")->fetch_assoc()['total'];
$total_jenis = $conn->query("SELECT COUNT(*) as total FROM pdta_jenis_barang")->fetch_assoc()['total'];
$total_lokasi = $conn->query("SELECT COUNT(*) as total FROM pdta_lokasi")->fetch_assoc()['total'];
$total_pengguna = $conn->query("SELECT COUNT(*) as total FROM pdta_pengguna")->fetch_assoc()['total'];

// Ambil data barang terbaru
$barang_terbaru = $conn->query("
    SELECT ib.*, jb.nama_jenis, l.nama_lokasi 
    FROM pdta_inventaris_barang ib
    JOIN pdta_jenis_barang jb ON ib.id_jenis = jb.id_jenis
    JOIN pdta_lokasi l ON ib.id_lokasi = l.id_lokasi
    ORDER BY ib.created_at DESC LIMIT 5
");
?>

<?php include 'includes/header.php'; ?>
<?php include 'includes/navbar.php'; ?>

<div class="container mt-4">
    <h3 class="mb-4">Dashboard</h3>
    
    <div class="row mb-4">
        <!-- Card Total Barang -->
        <div class="col-md-3">
            <div class="card bg-primary text-white shadow">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h5 class="card-title">Total Barang</h5>
                            <h2 class="mb-0"><?= number_format($total_barang) ?></h2>
                        </div>
                        <i class="bi bi-box-seam" style="font-size: 2rem;"></i>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Card Jenis Barang -->
        <div class="col-md-3">
            <div class="card bg-success text-white shadow">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h5 class="card-title">Jenis Barang</h5>
                            <h2 class="mb-0"><?= number_format($total_jenis) ?></h2>
                        </div>
                        <i class="bi bi-tags" style="font-size: 2rem;"></i>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Card Lokasi -->
        <div class="col-md-3">
            <div class="card bg-info text-white shadow">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h5 class="card-title">Lokasi</h5>
                            <h2 class="mb-0"><?= number_format($total_lokasi) ?></h2>
                        </div>
                        <i class="bi bi-geo-alt" style="font-size: 2rem;"></i>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Card Pengguna -->
        <div class="col-md-3">
            <div class="card bg-warning text-dark shadow">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h5 class="card-title">Pengguna</h5>
                            <h2 class="mb-0"><?= number_format($total_pengguna) ?></h2>
                        </div>
                        <i class="bi bi-people" style="font-size: 2rem;"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-8">
            <div class="card shadow mb-4">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0">Barang Terbaru</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Nama Barang</th>
                                    <th>Jenis</th>
                                    <th>Lokasi</th>
                                    <th>Tanggal</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while ($row = $barang_terbaru->fetch_assoc()): ?>
                                <tr>
                                    <td><?= htmlspecialchars($row['nama_barang']) ?></td>
                                    <td><?= htmlspecialchars($row['nama_jenis']) ?></td>
                                    <td><?= htmlspecialchars($row['nama_lokasi']) ?></td>
                                    <td><?= date('d M Y', strtotime($row['created_at'])) ?></td>
                                </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="text-end mt-3">
                        <a href="modules/inventaris/index.php" class="btn btn-primary">Lihat Semua Barang</a>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="card shadow">
                <div class="card-header bg-success text-white">
                    <h5 class="mb-0">Statistik Kondisi Barang</h5>
                </div>
                <div class="card-body">
                    <canvas id="kondisiChart" height="250"></canvas>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/chart.min.js"></script>
<script>
$(document).ready(function() {
    // Chart Kondisi Barang
    var ctx = document.getElementById('kondisiChart').getContext('2d');
    
    $.ajax({
        url: 'ajax/get_kondisi_stats.php',
        method: 'GET',
        dataType: 'json',
        success: function(data) {
            var kondisiChart = new Chart(ctx, {
                type: 'doughnut',
                data: {
                    labels: ['Baik', 'Rusak Ringan', 'Rusak Berat'],
                    datasets: [{
                        data: [data.baik, data.rusak_ringan, data.rusak_berat],
                        backgroundColor: [
                            '#28a745',
                            '#ffc107',
                            '#dc3545'
                        ],
                        borderWidth: 1
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    plugins: {
                        legend: {
                            position: 'bottom'
                        }
                    }
                }
            });
        }
    });
});
</script>

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