<?php
session_start();
if (!isset($_SESSION['user'])) {
    header("Location: /warungpojok/");
    exit;
}
include 'koneksi.php';

$cari = isset($_GET['cari']) ? mysqli_real_escape_string($conn, $_GET['cari']) : '';
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Dashboard Pembeli</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body{
    font-family: Arial, sans-serif;
    background: #f4f6f9;
    margin: 0;
    min-height: 100vh;
    position: relative;
}

body::before{
    content: "";
    position: fixed;
    inset: 0;
    background: url('/warungpojok/images/logo.png') no-repeat center center;
    background-size: 90% auto;
    opacity: 0.15;
    z-index: -1;
    pointer-events: none;
}

        .navbar-custom{
            background: #28a745;
        }

        .product-card{
            border-radius: 12px;
            transition: 0.3s;
        }

        .product-card:hover{
            transform: translateY(-5px);
        }

        .product-img{
            height: 180px;
            object-fit: cover;
            border-radius: 10px;
        }
    </style>
</head>
<body>

<!-- NAVBAR -->
<nav class="navbar navbar-expand-lg navbar-dark navbar-custom">
    <div class="container">
        <span class="navbar-brand">🛒 Halo, <?= $_SESSION['user']; ?></span>

        <div class="ms-auto">
            <a href="/warungpojok/keranjang.php" class="btn btn-light btn-sm">🛒 Keranjang</a>
            <a href="orders.php" class="btn btn-light btn-sm">🕘 Riwayat</a>
            <a href="/warungpojok/logout.php" class="btn btn-danger btn-sm">Logout</a>
        </div>
    </div>
</nav>

<!-- SEARCH -->
<div class="container mt-4">
    <form method="GET" class="d-flex justify-content-center">
        <input type="text" name="cari"
               class="form-control w-50 me-2"
               placeholder="🔍 Cari produk..."
               value="<?= $cari; ?>">
        <button type="submit" class="btn btn-success">Cari</button>
    </form>
</div>

<!-- HASIL SEARCH -->
<?php if($cari != '') { ?>
    <p class="text-center mt-2">
        Hasil pencarian: <b><?= $cari; ?></b>
    </p>
<?php } ?>

<!-- PRODUK -->
<div class="container mt-4">
    <div class="row">
        <?php
        if ($cari != '') {
            $data = mysqli_query($conn, "
                SELECT * FROM produk 
                WHERE nama_produk LIKE '%$cari%' 
                OR deskripsi LIKE '%$cari%'
            ");
        } else {
            $data = mysqli_query($conn, "SELECT * FROM produk");
        }

        while ($d = mysqli_fetch_assoc($data)) {
        ?>
        <div class="col-md-3 mb-4">
            <div class="card product-card shadow-sm text-center p-3">
                
                <img src="images/<?= $d['gambar']; ?>"
                     class="product-img mb-3"
                     data-bs-toggle="modal"
                     data-bs-target="#modal<?= $d['id']; ?>">

                <h6><?= $d['nama_produk']; ?></h6>
                <p class="text-success fw-bold">Rp <?= number_format($d['harga']); ?></p>

                <?php if ($d['stok'] > 0) { ?>
                    <a href="beli.php?id=<?= $d['id']; ?>" class="btn btn-success btn-sm">
                        Tambah
                    </a>
                <?php } else { ?>
                    <button class="btn btn-danger btn-sm" disabled>Stok Habis</button>
                <?php } ?>
            </div>
        </div>

        <!-- MODAL -->
        <div class="modal fade" id="modal<?= $d['id']; ?>" tabindex="-1">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">

                    <div class="modal-header">
                        <h5 class="modal-title"><?= $d['nama_produk']; ?></h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>

                    <div class="modal-body text-center">
                        <img src="images/<?= $d['gambar']; ?>"
                             class="img-fluid rounded mb-3"
                             style="max-height:250px; object-fit:cover;">

                        <p><b>Harga:</b> Rp <?= number_format($d['harga']); ?></p>
                        <p><b>Stok:</b> <?= $d['stok']; ?></p>

                        <p class="<?= $d['stok'] == 0 ? 'text-danger' : 'text-success' ?>">
                            <?= $d['deskripsi']; ?>
                        </p>
                    </div>

                    <div class="modal-footer">
                        <?php if ($d['stok'] > 0) { ?>
                            <a href="beli.php?id=<?= $d['id']; ?>" class="btn btn-success">
                                Tambah ke Keranjang
                            </a>
                        <?php } else { ?>
                            <button class="btn btn-danger" disabled>Stok Habis</button>
                        <?php } ?>
                    </div>

                </div>
            </div>
        </div>

        <?php } ?>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>