<?php
/**
 * Shared between the staff event page (events/show.php) and the event
 * organizer portal (organizer/events/show.php) — the QR/links/custom-
 * fields-manager/registrations-table block is identical in both places
 * apart from which URL prefix ($baseUrl: 'events' or 'organizer/events')
 * the field-management form actions post to. Kept as one partial rather
 * than two copies so it can't silently drift between the two views over
 * time.
 *
 * Expects: $event, $lookups, $customFields (EventRegistrationFieldModel
 * rows), $customFieldOptions (array field_id => decoded options list),
 * $registrations, $registerUrl, $checkinUrl, $qrDataUri, $baseUrl,
 * $canManageFields (bool).
 */
?>
<div class="card shadow-sm">
    <div class="card-header d-flex align-items-center gap-2">
        <i class="bi bi-qr-code" style="color: var(--brand);"></i> Registration &amp; Check-In
    </div>
    <div class="card-body">
        <?php if ($event->status !== 'confirmed'): ?>
            <div class="alert alert-warning small mb-3">Online registration only opens once this event is <strong>Confirmed</strong> — attendees can't sign up while it's <?= esc($lookups->eventStatuses[$event->status]['label'] ?? $event->status) ?>.</div>
        <?php endif; ?>

        <div class="row g-3">
            <div class="col-md-4 text-center">
                <img src="<?= $qrDataUri ?>" alt="Check-in QR code" style="width: 100%; max-width: 180px;" class="border rounded p-2">
                <p class="text-muted small mt-2 mb-0">Print or display this at the venue entrance — attendees scan it to check in.</p>
            </div>
            <div class="col-md-8">
                <label class="form-label small fw-semibold">Registration Link</label>
                <div class="input-group input-group-sm mb-3">
                    <input type="text" class="form-control" value="<?= esc($registerUrl) ?>" id="registerUrlInput" readonly>
                    <button type="button" class="btn btn-outline-secondary copy-btn" data-target="registerUrlInput"><i class="bi bi-clipboard"></i></button>
                </div>
                <label class="form-label small fw-semibold">Check-In Link (Kiosk)</label>
                <div class="input-group input-group-sm mb-3">
                    <input type="text" class="form-control" value="<?= esc($checkinUrl) ?>" id="checkinUrlInput" readonly>
                    <button type="button" class="btn btn-outline-secondary copy-btn" data-target="checkinUrlInput"><i class="bi bi-clipboard"></i></button>
                </div>
                <a href="<?= esc($checkinUrl) ?>" target="_blank" class="btn btn-sm btn-primary"><i class="bi bi-display me-1"></i>Open Kiosk Screen</a>
            </div>
        </div>

        <?php if ($canManageFields): ?>
        <hr>
        <h2 class="h6 mb-2">Custom Registration Questions</h2>
        <p class="text-muted small">Extra questions asked on the public registration form, in addition to name/phone/email.</p>

        <?php if (! empty($customFields)): ?>
        <div class="table-responsive mb-3">
            <table class="table table-sm mb-0 small align-middle">
                <thead><tr><th>Label</th><th>Type</th><th>Required</th><th class="text-end">Actions</th></tr></thead>
                <tbody>
                <?php foreach ($customFields as $i => $field): ?>
                    <tr>
                        <td><?= esc($field->label) ?></td>
                        <td><span class="badge bg-secondary"><?= esc($lookups->eventRegistrationFieldTypes[$field->field_type]['label'] ?? $field->field_type) ?></span></td>
                        <td><?= $field->is_required ? '<span class="text-success">Yes</span>' : '<span class="text-muted">No</span>' ?></td>
                        <td class="text-end">
                            <div class="d-flex justify-content-end gap-1">
                                <?php if ($i > 0): ?>
                                <form action="<?= site_url($baseUrl . '/' . $event->id . '/fields/' . $field->id . '/move-up') ?>" method="post" class="d-inline">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline-secondary" title="Move up"><i class="bi bi-arrow-up"></i></button>
                                </form>
                                <?php endif; ?>
                                <?php if ($i < count($customFields) - 1): ?>
                                <form action="<?= site_url($baseUrl . '/' . $event->id . '/fields/' . $field->id . '/move-down') ?>" method="post" class="d-inline">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline-secondary" title="Move down"><i class="bi bi-arrow-down"></i></button>
                                </form>
                                <?php endif; ?>
                                <form action="<?= site_url($baseUrl . '/' . $event->id . '/fields/' . $field->id . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Remove this question? Answers already collected for it are kept but will no longer show a matching label.');">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline-danger" title="Delete"><i class="bi bi-trash"></i></button>
                                </form>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>

        <form action="<?= site_url($baseUrl . '/' . $event->id . '/fields') ?>" method="post" class="row g-2 align-items-end">
            <?= csrf_field() ?>
            <div class="col-md-4">
                <label class="form-label small">Question</label>
                <input type="text" class="form-control form-control-sm" name="label" placeholder="e.g. Dietary restrictions" required>
            </div>
            <div class="col-md-3">
                <label class="form-label small">Type</label>
                <select class="form-select form-select-sm field-type-select" name="field_type" required>
                    <?php foreach ($lookups->eventRegistrationFieldTypes as $key => $meta): ?>
                        <option value="<?= esc($key) ?>"><?= esc($meta['label']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3 field-options-wrap" style="display: none;">
                <label class="form-label small">Choices <span class="text-muted fw-normal">(one per line)</span></label>
                <textarea class="form-control form-control-sm" name="options" rows="1" placeholder="Option A&#10;Option B"></textarea>
            </div>
            <div class="col-md-1 form-check">
                <input class="form-check-input" type="checkbox" name="is_required" value="1" id="fieldRequired">
                <label class="form-check-label small" for="fieldRequired">Req'd</label>
            </div>
            <div class="col-md-1">
                <button type="submit" class="btn btn-sm btn-primary w-100"><i class="bi bi-plus-lg"></i></button>
            </div>
        </form>
        <?php endif; ?>

        <hr>

        <h2 class="h6 mb-2">Registrations (<?= count($registrations) ?>)</h2>
        <div class="table-responsive">
            <table class="table table-sm mb-0 small">
                <thead><tr><th>Name</th><th>Phone</th><th>Status</th><th>Checked In</th></tr></thead>
                <tbody>
                <?php if (empty($registrations)): ?>
                    <tr><td colspan="4" class="text-center text-muted py-3">No registrations yet.</td></tr>
                <?php endif; ?>
                <?php foreach ($registrations as $r): ?>
                    <tr>
                        <td><a href="<?= site_url($baseUrl . '/' . $event->id . '/registrations/' . $r->id) ?>" class="text-decoration-none"><?= esc($r->full_name) ?></a></td>
                        <td class="text-muted"><?= esc($r->phone) ?></td>
                        <td>
                            <?php if ($r->status === 'checked_in'): ?>
                                <span class="badge bg-success">Checked In</span>
                            <?php elseif ($r->status === 'cancelled'): ?>
                                <span class="badge bg-secondary">Cancelled</span>
                            <?php else: ?>
                                <span class="badge bg-warning text-dark">Registered</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-muted"><?= $r->checked_in_at ? esc(date('d M, g:i a', strtotime($r->checked_in_at))) : '—' ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<script>
document.querySelectorAll('.copy-btn').forEach(function (btn) {
    btn.addEventListener('click', function () {
        const input = document.getElementById(this.dataset.target);
        navigator.clipboard.writeText(input.value).then(() => {
            const icon = this.querySelector('i');
            icon.className = 'bi bi-check2';
            setTimeout(() => { icon.className = 'bi bi-clipboard'; }, 2000);
        });
    });
});
document.querySelectorAll('.field-type-select').forEach(function (select) {
    const needsOptions = ['select', 'radio', 'checkbox'];
    const wrap = select.closest('form').querySelector('.field-options-wrap');
    const toggle = () => { wrap.style.display = needsOptions.includes(select.value) ? '' : 'none'; };
    select.addEventListener('change', toggle);
    toggle();
});
</script>
