<?php
$tenant = service('tenantContext')->hasTenant() ? service('tenantContext')->getTenant() : null;
$primaryColor   = safe_hex_color($tenant->primary_color ?? null, '#4f46e5');
$secondaryColor = safe_hex_color($tenant->secondary_color ?? null, '#f59e0b');
$hotelName      = $tenant->hotel_name ?? 'MaxYields PMS';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include APPPATH . 'Views/public/_shell_head.php'; ?>
</head>
<body>
<div class="public-shell">
    <div class="d-flex align-items-center gap-2 mb-4">
        <div class="brand-mark"><i class="bi bi-buildings-fill"></i></div>
        <div class="brand-name"><?= esc($hotelName) ?></div>
    </div>

    <div class="public-card mb-3 text-center">
        <i class="bi bi-calendar-event" style="font-size: 1.8rem; color: var(--brand);"></i>
        <h1 class="h5 mt-2 mb-1"><?= esc($event->event_name) ?></h1>
        <p class="text-muted small mb-0"><?= esc(date('l, d F Y', strtotime($event->event_date))) ?><?= $event->venue_space ? ' · ' . esc($event->venue_space) : '' ?></p>
    </div>

    <div class="public-card">
        <h2 class="h6 mb-3">Register to Attend</h2>
        <?php if (session()->getFlashdata('error')): ?>
            <div class="alert alert-danger py-2 small"><?= esc(session()->getFlashdata('error')) ?></div>
        <?php endif; ?>
        <?php if (isset($validation)): ?>
            <div class="alert alert-danger py-2 small"><?= $validation->listErrors() ?></div>
        <?php endif; ?>
        <form action="<?= site_url('event-register/' . $token) ?>" method="post">
            <?= csrf_field() ?>
            <div class="mb-3">
                <label class="form-label small fw-semibold">Full Name</label>
                <input type="text" class="form-control" name="full_name" value="<?= esc(old('full_name')) ?>" required>
            </div>
            <div class="mb-3">
                <label class="form-label small fw-semibold">Phone</label>
                <input type="tel" class="form-control" name="phone" placeholder="024XXXXXXX" value="<?= esc(old('phone')) ?>" required>
            </div>
            <div class="mb-4">
                <label class="form-label small fw-semibold">Email <span class="text-muted fw-normal">(optional)</span></label>
                <input type="email" class="form-control" name="email" value="<?= esc(old('email')) ?>">
            </div>

            <?php foreach ($fields as $field): ?>
                <?php $opts = $fieldOptions[$field->id] ?? []; ?>
                <div class="mb-3">
                    <label class="form-label small fw-semibold">
                        <?= esc($field->label) ?>
                        <?php if ($field->is_required): ?><span class="text-danger">*</span><?php endif; ?>
                    </label>
                    <?php if ($field->field_type === 'textarea'): ?>
                        <textarea class="form-control" name="custom_fields[<?= $field->id ?>]" rows="2" <?= $field->is_required ? 'required' : '' ?>><?= esc(old("custom_fields.{$field->id}")) ?></textarea>
                    <?php elseif ($field->field_type === 'select'): ?>
                        <select class="form-select" name="custom_fields[<?= $field->id ?>]" <?= $field->is_required ? 'required' : '' ?>>
                            <option value="">Select&hellip;</option>
                            <?php foreach ($opts as $opt): ?>
                                <option value="<?= esc($opt) ?>"><?= esc($opt) ?></option>
                            <?php endforeach; ?>
                        </select>
                    <?php elseif ($field->field_type === 'radio'): ?>
                        <?php foreach ($opts as $i => $opt): ?>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="custom_fields[<?= $field->id ?>]" value="<?= esc($opt) ?>" id="cf<?= $field->id ?>_<?= $i ?>" <?= $field->is_required ? 'required' : '' ?>>
                                <label class="form-check-label small" for="cf<?= $field->id ?>_<?= $i ?>"><?= esc($opt) ?></label>
                            </div>
                        <?php endforeach; ?>
                    <?php elseif ($field->field_type === 'checkbox'): ?>
                        <?php foreach ($opts as $i => $opt): ?>
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" name="custom_fields[<?= $field->id ?>][]" value="<?= esc($opt) ?>" id="cf<?= $field->id ?>_<?= $i ?>">
                                <label class="form-check-label small" for="cf<?= $field->id ?>_<?= $i ?>"><?= esc($opt) ?></label>
                            </div>
                        <?php endforeach; ?>
                    <?php elseif ($field->field_type === 'number'): ?>
                        <input type="number" class="form-control" name="custom_fields[<?= $field->id ?>]" value="<?= esc(old("custom_fields.{$field->id}")) ?>" <?= $field->is_required ? 'required' : '' ?>>
                    <?php elseif ($field->field_type === 'date'): ?>
                        <input type="date" class="form-control" name="custom_fields[<?= $field->id ?>]" value="<?= esc(old("custom_fields.{$field->id}")) ?>" <?= $field->is_required ? 'required' : '' ?>>
                    <?php else: ?>
                        <input type="text" class="form-control" name="custom_fields[<?= $field->id ?>]" value="<?= esc(old("custom_fields.{$field->id}")) ?>" <?= $field->is_required ? 'required' : '' ?>>
                    <?php endif; ?>
                </div>
            <?php endforeach; ?>

            <button type="submit" class="btn btn-brand w-100 mt-1"><i class="bi bi-check2 me-1"></i>Register</button>
            <p class="text-muted small text-center mt-2 mb-0">We'll text you a check-in code to use at the venue.</p>
        </form>
    </div>
</div>
</body>
</html>
