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

// Bisa diakses oleh Staff maupun Admin
checkRole(['staff', 'admin']);

// Ambil daftar bahan baku untuk dropdown
$ingredients = $pdo->query("SELECT id, name, unit FROM ingredients ORDER BY name ASC")->fetchAll();
?>

<div class="container" style="max-width: 600px;">
    <header style="margin-bottom: 2rem;">
        <h2 style="margin:0; color: #dc2626;">INPUT BAHAN RUSAK (WASTE)</h2>
        <p style="color: #6b7280; margin:0;">Catat bahan yang basi, robek, atau jatuh agar stok akurat.</p>
    </header>

    <?php if (isset($_GET['status']) && $_GET['status'] == 'success'): ?>
        <div class="card" style="background: #ecfdf5; border-left: 5px solid #10b981; color: #065f46; padding: 15px; margin-bottom: 20px;">
            Bahan rusak berhasil dicatat! Stok gudang otomatis terpotong.
        </div>
    <?php endif; ?>

    <div class="card" style="border-top: 5px solid #dc2626;">
        <form action="../../api/submit_waste.php" method="POST" style="display: flex; flex-direction: column; gap: 15px;">
            
            <div style="display: flex; flex-direction: column; gap: 5px;">
                <label style="font-weight: bold; color: #374151;">Pilih Bahan Baku</label>
                <select name="ingredient_id" required style="padding: 12px; border-radius: 8px; border: 1px solid #ddd; outline: none;">
                    <option value="">-- Pilih Bahan --</option>
                    <?php foreach ($ingredients as $ing): ?>
                        <option value="<?= $ing['id'] ?>"><?= htmlspecialchars($ing['name']) ?> (<?= $ing['unit'] ?>)</option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div style="display: flex; flex-direction: column; gap: 5px;">
                <label style="font-weight: bold; color: #374151;">Jumlah Rusak</label>
                <input type="number" step="0.01" name="qty" required placeholder="Contoh: 5 (untuk 5 pcs) atau 100 (untuk 100 gram)" style="padding: 12px; border-radius: 8px; border: 1px solid #ddd; outline: none;">
                <small style="color: #6b7280;">Masukkan sesuai satuan bahan (Gram/Pcs).</small>
            </div>

            <div style="display: flex; flex-direction: column; gap: 5px;">
                <label style="font-weight: bold; color: #374151;">Alasan / Keterangan</label>
                <input type="text" name="reason" required placeholder="Contoh: Tortilla sobek saat dilipat, Daging jatuh" style="padding: 12px; border-radius: 8px; border: 1px solid #ddd; outline: none;">
            </div>

            <button type="submit" style="background: #dc2626; color: white; border: none; padding: 15px; border-radius: 8px; font-weight: bold; font-size: 1rem; cursor: pointer; margin-top: 10px;">
                SIMPAN & POTONG STOK
            </button>
        </form>
    </div>
</div>