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

// Filter data
$filter_ruang = $_GET['ruang'] ?? '';
$filter_kondisi = $_GET['kondisi'] ?? '';

// Query dasar
$query = "SELECT * FROM pdt_barang WHERE 1=1";
$params = [];

// Tambahkan filter jika ada
if (!empty($filter_ruang)) {
    $query .= " AND nama_ruang = ?";
    $params[] = $filter_ruang;
}

if (!empty($filter_kondisi)) {
    $query .= " AND kondisi = ?";
    $params[] = $filter_kondisi;
}

$query .= " ORDER BY nama_ruang, nama_barang";

// Prepare statement
$stmt = $conn->prepare($query);

if (!empty($params)) {
    $types = str_repeat('s', count($params));
    $stmt->bind_param($types, ...$params);
}

$stmt->execute();
$result = $stmt->get_result();

// Ambil daftar ruang unik untuk dropdown filter
$ruang_query = "SELECT DISTINCT nama_ruang FROM pdt_barang WHERE nama_ruang IS NOT NULL ORDER BY nama_ruang";
$ruang_result = $conn->query($ruang_query);
?>

<div class="container mt-4">
    <div class="card shadow">
        <div class="card-header bg-primary text-white">
            <h3 class="card-title">Laporan Data Barang</h3>
            <div class="float-end">
                <a href="cetak.php?<?= http_build_query($_GET) ?>" 
                class="btn btn-light btn-sm me-2" target="_blank">
                    <i class="fas fa-print"></i> Cetak PDF
                </a>
            </div>
        </div>
        <div class="card-body">
            <!-- Filter Form -->
            <form method="get" class="mb-4">
                <div class="row g-3">
                    <div class="col-md-4">
                        <label for="ruang" class="form-label">Filter Ruang</label>
                        <select class="form-select" id="ruang" name="ruang">
                            <option value="">Semua Ruang</option>
                            <?php while ($row = $ruang_result->fetch_assoc()): ?>
                                <option value="<?= htmlspecialchars($row['nama_ruang']) ?>" 
                                    <?= $filter_ruang == $row['nama_ruang'] ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($row['nama_ruang']) ?>
                                </option>
                            <?php endwhile; ?>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label for="kondisi" class="form-label">Filter Kondisi</label>
                        <select class="form-select" id="kondisi" name="kondisi">
                            <option value="">Semua Kondisi</option>
                            <option value="Baik" <?= $filter_kondisi == 'Baik' ? 'selected' : '' ?>>Baik</option>
                            <option value="Rusak Ringan" <?= $filter_kondisi == 'Rusak Ringan' ? 'selected' : '' ?>>Rusak Ringan</option>
                            <option value="Rusak Berat" <?= $filter_kondisi == 'Rusak Berat' ? 'selected' : '' ?>>Rusak Berat</option>
                        </select>
                    </div>
                    <div class="col-md-4 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary me-2">
                            <i class="fas fa-filter"></i> Filter
                        </button>
                        <a href="index.php" class="btn btn-outline-secondary">
                            <i class="fas fa-sync-alt"></i> Reset
                        </a>
                    </div>
                </div>
            </form>

            <!-- Tabel Laporan -->
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>No</th>
                            <th>Nama Ruang</th>
                            <th>Nama Barang</th>
                            <th>Spesifikasi</th>
                            <th>Jumlah</th>
                            <th>Kondisi</th>
                            <th>Keterangan</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if ($result->num_rows > 0): ?>
                            <?php $no = 1; ?>
                            <?php while ($row = $result->fetch_assoc()): ?>
                                <tr>
                                    <td><?= $no++; ?></td>
                                    <td><?= htmlspecialchars($row['nama_ruang'] ?? '-') ?></td>
                                    <td><?= htmlspecialchars($row['nama_barang']) ?></td>
                                    <td><?= htmlspecialchars($row['spesifikasi'] ?? '-') ?></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><?= htmlspecialchars($row['keterangan'] ?? '-') ?></td>
                                </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="7" class="text-center py-4">Tidak ada data barang yang sesuai dengan filter</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="card-footer bg-light">
            <div class="row">
                <div class="col-md-6">
                    <?php
                    $total_query = "SELECT COUNT(*) as total, SUM(jumlah) as total_jumlah FROM pdt_barang WHERE 1=1";
                    if (!empty($filter_ruang)) {
                        $total_query .= " AND nama_ruang = '$filter_ruang'";
                    }
                    if (!empty($filter_kondisi)) {
                        $total_query .= " AND kondisi = '$filter_kondisi'";
                    }
                    $total_result = $conn->query($total_query);
                    $total_row = $total_result->fetch_assoc();
                    ?>
                    <p class="mb-0"><strong>Total Barang:</strong> <?= $total_row['total'] ?></p>
                    <p class="mb-0"><strong>Total Jumlah:</strong> <?= $total_row['total_jumlah'] ?></p>
                </div>
                <div class="col-md-6 text-end">
                    <small class="text-muted">Data diambil pada: <?= date('d/m/Y H:i:s') ?></small>
                </div>
            </div>
        </div>
    </div>
</div>

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