<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<h1 class="h3 mb-3">Dining Tables</h1>

<?php if (empty($areas)): ?>
    <div class="alert alert-warning">Add a dining area first, then come back to add tables. <a href="<?= site_url('dining/areas') ?>">Go to Dining Areas &rarr;</a></div>
<?php endif; ?>

<div class="row">
    <div class="col-md-7 mb-3">
        <div class="card shadow-sm">
            <div class="table-scroll">
                <table class="table table-hover mb-0 align-middle">
                    <thead class="table-light"><tr><th>Table #</th><th>Area</th><th>Seats</th><th class="text-end">Actions</th></tr></thead>
                    <tbody>
                        <?php if (empty($tables)): ?>
                            <tr><td colspan="4" class="text-center text-muted py-4">No tables yet.</td></tr>
                        <?php endif; ?>
                        <?php foreach ($tables as $table): ?>
                            <tr>
                                <td class="fw-semibold"><?= esc($table->table_number) ?></td>
                                <td><?= esc($table->area_name) ?></td>
                                <td><?= (int) $table->seating_capacity ?></td>
                                <td class="text-end">
                                    <form action="<?= site_url('dining/tables/' . $table->id . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this table?');">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-md-5 mb-3">
        <div class="card shadow-sm">
            <div class="card-body">
                <h2 class="h6">New Table</h2>
                <form action="<?= site_url('dining/tables') ?>" method="post">
                    <?= csrf_field() ?>
                    <div class="mb-3">
                        <label class="form-label">Table Number</label>
                        <input type="text" class="form-control" name="table_number" required placeholder="e.g. T1">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Area</label>
                        <select class="form-select" name="dining_area_id" required>
                            <?php foreach ($areas as $area): ?>
                                <option value="<?= $area->id ?>"><?= esc($area->name) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Seating Capacity</label>
                        <input type="number" class="form-control" name="seating_capacity" min="1" value="4" required>
                    </div>
                    <button type="submit" class="btn btn-primary" <?= empty($areas) ? 'disabled' : '' ?>>Add Table</button>
                </form>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
