<?php
ob_start();
include 'config.php';
checkLogin(); 
if (!has_access($conn, 'perm_manage_units')) {
    header("Location: index.php?status=restricted");
    exit;
}

if(isset($_POST['add_stick'])) {
    $nama = mysqli_real_escape_string($conn, $_POST['nama_stik']);
    mysqli_query($conn, "INSERT INTO sticks (nama_stik) VALUES ('$nama')");
    header("Location: sticks.php?status=added");
    exit;
}

if(isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    mysqli_query($conn, "DELETE FROM sticks WHERE id='$id'");
    header("Location: sticks.php?status=deleted");
    exit;
}

$page_title = "Management Stik";
$active_page = "sticks";
include 'layout.php';
?>

<div class="users-layout" style="display: grid; grid-template-columns: 350px 1fr; gap: 30px;">
    <!-- Form Tambah -->
    <div class="glass-panel" style="background: rgba(30, 41, 59, 0.7); border-radius: 20px; padding: 25px; border: 1px solid rgba(255,255,255,0.1);">
        <h3 style="color:#fff; margin-bottom:20px;"><i class='bx bx-plus-circle'></i> Tambah Stik Baru</h3>
        <form method="POST">
            <div class="form-group">
                <label style="color:#94a3b8; display:block; margin-bottom:10px;">Nama / Warna Stik</label>
                <input type="text" name="nama_stik" class="input-modern" placeholder="Contoh: DualSense Putih A" required style="width:100%; padding:12px; background:rgba(0,0,0,0.2); border:1px solid #334155; color:#fff; border-radius:10px;">
            </div>
            <button type="submit" name="add_stick" class="btn btn-primary" style="width:100%; margin-top:15px; background:#06b6d4; border:none; padding:12px; border-radius:10px; font-weight:700; cursor:pointer;">SIMPAN STIK</button>
        </form>
    </div>

    <!-- Daftar Stik -->
    <div class="glass-panel" style="background: rgba(30, 41, 59, 0.7); border-radius: 20px; padding: 25px; border: 1px solid rgba(255,255,255,0.1);">
        <h3 style="color:#fff; margin-bottom:20px;"><i class='bx bx-list-ul'></i> Inventory Stik</h3>
        <table class="custom-table" style="width:100%; border-collapse:collapse;">
            <thead>
                <tr style="text-align:left; color:#64748b; border-bottom:1px solid #334155;">
                    <th style="padding:10px;">Nama Stik</th>
                    <th style="padding:10px;">Status</th>
                    <th style="padding:10px; text-align:right;">Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php $res = mysqli_query($conn, "SELECT * FROM sticks ORDER BY id DESC"); 
                while($s = mysqli_fetch_assoc($res)): ?>
                <tr style="border-bottom:1px solid rgba(255,255,255,0.05); color:#fff;">
                    <td style="padding:15px; font-weight:700;"><?= $s['nama_stik'] ?></td>
                    <td style="padding:15px;">
                        <span style="padding:4px 8px; border-radius:6px; font-size:0.7rem; font-weight:800; background:<?= $s['status']=='available'?'rgba(16,185,129,0.2)':'rgba(239,68,68,0.2)' ?>; color:<?= $s['status']=='available'?'#10b981' : '#f43f5e' ?>;">
                            <?= strtoupper($s['status']) ?>
                        </span>
                    </td>
                    <td style="padding:15px; text-align:right;">
                        <a href="?delete=<?= $s['id'] ?>" onclick="return confirm('Hapus stik ini?')" style="color:#f43f5e; font-size:1.2rem;"><i class='bx bx-trash'></i></a>
                    </td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</div>
<?php echo "</div></body></html>"; ob_end_flush(); ?>