<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once 'config/database.php';
require_once 'includes/functions.php';
require_once 'includes/header.php';
require_once 'includes/navbar.php';
?>

<div class="container mt-5">
    <div class="row">
        <div class="col-lg-12">
            <div class="jumbotron bg-light p-5 rounded-3 shadow">
                <h1 class="display-4">Selamat Datang di Sistem Manajemen Barang</h1>
                <p class="lead">Aplikasi ini membantu Anda mengelola data barang inventaris dengan mudah dan efisien.</p>
                <hr class="my-4">
                <p>Fitur yang tersedia:</p>
                <ul>
                    <li>Manajemen data barang (tambah, edit, hapus)</li>
                    <li>Pencarian dan filter data</li>
                    <li>Laporan data barang</li>
                    <li>Cetak data dalam format PDF</li>
                </ul>
                <div class="d-grid gap-2 d-md-flex justify-content-md-start">
                    <a href="modules/barang/index.php" class="btn btn-primary btn-lg px-4 me-md-2">
                        <i class="fas fa-boxes"></i> Kelola Barang
                    </a>
                    <a href="modules/laporan/index.php" class="btn btn-outline-secondary btn-lg px-4">
                        <i class="fas fa-file-alt"></i> Lihat Laporan
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="row mt-4">
        <div class="col-lg-4 mb-4">
            <div class="card h-100 shadow border-start-0 border-bottom-0 border-end-0 border-top-primary border-4">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="me-3">
                            <i class="fas fa-box fa-2x text-primary"></i>
                        </div>
                        <div class="text-end">
                            <h5 class="card-title text-primary">Total Barang</h5>
                            <?php
                            $query = "SELECT COUNT(*) as total FROM pdt_barang";
                            $result = $conn->query($query);
                            $row = $result->fetch_assoc();
                            ?>
                            <h2 class="mb-0"><?= $row['total']; ?></h2>
                        </div>
                    </div>
                </div>
                <div class="card-footer bg-transparent">
                    <a href="modules/barang/index.php" class="text-primary text-decoration-none">
                        Lihat detail <i class="fas fa-arrow-right"></i>
                    </a>
                </div>
            </div>
        </div>

        <div class="col-lg-4 mb-4">
            <div class="card h-100 shadow border-start-0 border-bottom-0 border-end-0 border-top-success border-4">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="me-3">
                            <i class="fas fa-check-circle fa-2x text-success"></i>
                        </div>
                        <div class="text-end">
                            <h5 class="card-title text-success">Barang Baik</h5>
                            <?php
                            $query = "SELECT COUNT(*) as baik FROM pdt_barang WHERE kondisi = 'Baik'";
                            $result = $conn->query($query);
                            $row = $result->fetch_assoc();
                            ?>
                            <h2 class="mb-0"><?= $row['baik']; ?></h2>
                        </div>
                    </div>
                </div>
                <div class="card-footer bg-transparent">
                    <a href="modules/barang/index.php?filter=kondisi&value=Baik" class="text-success text-decoration-none">
                        Lihat detail <i class="fas fa-arrow-right"></i>
                    </a>
                </div>
            </div>
        </div>

        <div class="col-lg-4 mb-4">
            <div class="card h-100 shadow border-start-0 border-bottom-0 border-end-0 border-top-warning border-4">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="me-3">
                            <i class="fas fa-exclamation-triangle fa-2x text-warning"></i>
                        </div>
                        <div class="text-end">
                            <h5 class="card-title text-warning">Barang Rusak</h5>
                            <?php
                            $query = "SELECT COUNT(*) as rusak FROM pdt_barang WHERE kondisi != 'Baik'";
                            $result = $conn->query($query);
                            $row = $result->fetch_assoc();
                            ?>
                            <h2 class="mb-0"><?= $row['rusak']; ?></h2>
                        </div>
                    </div>
                </div>
                <div class="card-footer bg-transparent">
                    <a href="modules/barang/index.php?filter=kondisi&value=Rusak" class="text-warning text-decoration-none">
                        Lihat detail <i class="fas fa-arrow-right"></i>
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="row mt-4">
        <div class="col-lg-12">
            <div class="card shadow">
                <div class="card-header bg-primary text-white">
                    <h5 class="card-title mb-0">Barang Terakhir Ditambahkan</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Nama Barang</th>
                                    <th>Ruang</th>
                                    <th>Jumlah</th>
                                    <th>Kondisi</th>
                                    <th>Tanggal Input</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $query = "SELECT * FROM pdt_barang ORDER BY id_barang DESC LIMIT 5";
                                $result = $conn->query($query);
                                
                                if ($result->num_rows > 0) {
                                    while ($row = $result->fetch_assoc()) {
                                        echo '<tr>
                                            <td>'.htmlspecialchars($row['nama_barang']).'</td>
                                            <td>'.htmlspecialchars($row['nama_ruang']).'</td>
                                            <td>'.$row['jumlah'].'</td>
                                            <td><span class="badge bg-'.($row['kondisi'] == 'Baik' ? 'success' : ($row['kondisi'] == 'Rusak Ringan' ? 'warning' : 'danger')).'">'.$row['kondisi'].'</span></td>
                                            <td>'.date('d/m/Y', strtotime($row['created_at'] ?? 'now')).'</td>
                                        </tr>';
                                    }
                                } else {
                                    echo '<tr><td colspan="5" class="text-center">Tidak ada data barang</td></tr>';
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="card-footer bg-transparent">
                    <a href="modules/barang/index.php" class="btn btn-primary btn-sm">
                        <i class="fas fa-list"></i> Lihat Semua Barang
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

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