<?php
$title = 'Produk Admin';
$errors = flash('errors', []);
?>
<section class="admin-section">
    <div class="container admin-grid">
        <div class="hero-panel">
            <span class="eyebrow">Manajemen Produk</span>
            <h1>Produk</h1>
            <p class="lead">Tambah, ubah status, dan hapus paket layanan yang tersedia untuk client.</p>

            <?php if ($status = flash('status')): ?>
                <div class="alert alert-success"><?= e($status) ?></div>
            <?php endif; ?>

            <?php if ($errors !== []): ?>
                <div class="alert alert-error">
                    <strong>Periksa input:</strong>
                    <ul class="field-errors">
                        <?php foreach ($errors as $messages): ?>
                            <?php foreach ((array) $messages as $message): ?>
                                <li><?= e($message) ?></li>
                            <?php endforeach; ?>
                        <?php endforeach; ?>
                    </ul>
                </div>
            <?php endif; ?>

            <form class="admin-form" action="<?= e(url('/admin/products')) ?>" method="post">
                <?= csrf() ?>
                <div class="field">
                    <label for="nama_produk">Nama Produk</label>
                    <input id="nama_produk" name="nama_produk" type="text" value="<?= e(old('nama_produk')) ?>" placeholder="Nama paket">
                </div>
                <div class="field">
                    <label for="harga">Harga</label>
                    <input id="harga" name="harga" type="number" min="0" step="1000" value="<?= e(old('harga')) ?>" placeholder="550000">
                </div>
                <div class="field">
                    <label for="deskripsi">Deskripsi</label>
                    <input id="deskripsi" name="deskripsi" type="text" value="<?= e(old('deskripsi')) ?>" placeholder="Deskripsi singkat">
                </div>
                <div class="field">
                    <label for="status">Status</label>
                    <select id="status" name="status" class="field-select">
                        <option value="aktif" <?= old('status', 'aktif') === 'aktif' ? 'selected' : '' ?>>Aktif</option>
                        <option value="nonaktif" <?= old('status') === 'nonaktif' ? 'selected' : '' ?>>Nonaktif</option>
                    </select>
                </div>
                <div class="admin-form-actions">
                    <button class="button-primary" type="submit">Simpan Produk</button>
                </div>
            </form>
        </div>

        <div class="card panel-stack">
            <h2 class="panel-title">Daftar Produk</h2>
            <div class="admin-table-wrap">
                <table class="admin-table">
                    <thead>
                    <tr class="admin-table-head">
                        <th>Nama</th>
                        <th>Harga</th>
                        <th>Status</th>
                        <th>Deskripsi</th>
                        <th>Aksi</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($products as $product): ?>
                        <tr>
                            <td><?= e($product['nama_produk']) ?></td>
                            <td>Rp<?= e(number_format((float) $product['harga'], 0, ',', '.')) ?></td>
                            <td><span class="status-badge status-<?= e($product['status']) ?>"><?= e($product['status']) ?></span></td>
                            <td><?= e($product['deskripsi'] ?? '-') ?></td>
                            <td>
                                <div class="admin-actions">
                                    <form action="<?= e(url('/admin/products/toggle')) ?>" method="post">
                                        <?= csrf() ?>
                                        <input type="hidden" name="product_id" value="<?= e((string) $product['id']) ?>">
                                        <button class="button-secondary" type="submit">Toggle</button>
                                    </form>
                                    <form action="<?= e(url('/admin/products/delete')) ?>" method="post">
                                        <?= csrf() ?>
                                        <input type="hidden" name="product_id" value="<?= e((string) $product['id']) ?>">
                                        <button class="button-secondary" type="submit">Hapus</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</section>
