<?php
session_start();
include('../includes/config.php');


if(!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../login_admin.php");
    exit;
}


$produk = mysqli_query($conn, "SELECT * FROM produk ORDER BY id DESC");
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Kelola Stok Produk - Alfateeh Sepatuku</title>
    <style>
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #f0f2f5; padding: 20px; }
        .container { background: white; padding: 30px; border-radius: 12px; box-shadow: 0 4px 20px rgba(0,0,0,0.08); }
        h2 { color: #333; margin-top: 0; }
        
        table { width: 100%; border-collapse: collapse; margin-top: 20px; background: #fff; }
        th, td { padding: 15px; border-bottom: 1px solid #eee; text-align: left; }
        th { background: #f8f9fa; color: #555; text-transform: uppercase; font-size: 12px; letter-spacing: 1px; }
        
        .stok-box { 
            background: #f1f3f4; 
            padding: 5px 10px; 
            border-radius: 4px; 
            display: inline-block; 
            margin: 2px; 
            font-size: 13px;
        }
        .stok-habis { color: #dc3545; font-weight: bold; }
        .stok-ada { color: #28a745; font-weight: bold; }

        .btn { padding: 8px 15px; text-decoration: none; border-radius: 5px; font-size: 13px; transition: 0.2s; }
        .btn-tambah { background: #007bff; color: white; display: inline-block; margin-bottom: 20px; font-weight: bold; }
        .btn-tambah:hover { background: #0056b3; }
        .btn-edit { border: 1px solid #ffc107; color: #856404; margin-right: 5px; }
        .btn-edit:hover { background: #ffc107; color: white; }
        .btn-hapus { border: 1px solid #dc3545; color: #dc3545; }
        .btn-hapus:hover { background: #dc3545; color: white; }
        
        img { border-radius: 8px; object-fit: cover; border: 1px solid #ddd; }
    </style>
</head>
<body>

<div class="container">
    <h2>Daftar Produk & Stok Per Ukuran</h2>
    <a href="tambah_produk.php" class="btn btn-tambah">+ Tambah Produk Baru</a>

    <table>
        <thead>
            <tr>
                <th>Produk</th>
                <th>Harga</th>
                <th>Rincian Stok & Ukuran</th>
                <th>Aksi</th>
            </tr>
        </thead>
        <tbody>
            <?php while($p = mysqli_fetch_assoc($produk)): ?>
            <tr>
                <td>
                    <div style="display: flex; align-items: center;">
                        <img src="../assets/img/<?= $p['gambar'] ?>" width="70" height="70" style="margin-right: 15px;">
                        <strong><?= htmlspecialchars($p['nama']) ?></strong>
                    </div>
                </td>
                <td>Rp <?= number_format($p['harga'], 0, ",", ".") ?></td>
                <td>
                    <?php 
                    $id_produk = $p['id'];
                    $detail = mysqli_query($conn, "SELECT * FROM produk_detail WHERE id_produk = '$id_produk' ORDER BY ukuran ASC");
                    
                    if(mysqli_num_rows($detail) > 0) {
                        while($d = mysqli_fetch_assoc($detail)) {
                            $status_kelas = ($d['stok'] > 0) ? 'stok-ada' : 'stok-habis';
                            echo "<div class='stok-box'>";
                            echo "Size " . htmlspecialchars($d['ukuran']) . ": <span class='$status_kelas'>" . $d['stok'] . "</span>";
                            echo "</div>";
                        }
                    } else {
                        echo "<em style='color: #999;'>Belum ada data ukuran</em>";
                    }
                    ?>
                </td>
                <td>
                    <a href="edit_produk.php?id=<?= $p['id'] ?>" class="btn btn-edit">Edit</a>
                    <a href="hapus_produk.php?id=<?= $p['id'] ?>" class="btn btn-hapus" onclick="return confirm('Hapus produk ini?')">Hapus</a>
                </td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
</div>

</body>
</html>