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

<div class="d-flex justify-content-between align-items-start mb-3">
    <div>
        <h1 class="h3 mb-1"><?= esc($event->event_name) ?></h1>
        <span class="badge bg-<?= esc($lookups->eventStatuses[$event->status]['badge'] ?? 'secondary') ?>"><?= esc($lookups->eventStatuses[$event->status]['label'] ?? $event->status) ?></span>
    </div>
    <a href="<?= site_url('events/' . $event->id . '/edit') ?>" class="btn btn-outline-secondary"><i class="bi bi-pencil me-1"></i>Edit</a>
</div>

<div class="row g-3 mb-3">
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Event Date</div>
            <div class="fs-5 fw-bold"><?= esc(date('d M Y', strtotime($event->event_date))) ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1"><?= $daysUntil >= 0 ? 'Days Until Event' : 'Days Since Event' ?></div>
            <div class="fs-3 fw-bold"><?= abs($daysUntil) ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Expected Headcount</div>
            <div class="fs-3 fw-bold"><?= esc($event->expected_headcount ?: '—') ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Venue</div>
            <div class="fs-5 fw-bold"><?= esc($event->venue_space ?: '—') ?></div>
        </div></div>
    </div>
</div>

<div class="row">
    <div class="col-lg-8 mb-3">
        <div class="card shadow-sm mb-3">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-calendar-event" style="color: var(--brand);"></i> Event Details
            </div>
            <div class="card-body">
                <div class="row row-cols-2 g-3 small">
                    <div class="col"><span class="text-muted">Event Type</span><br><span class="fw-semibold"><?= esc($event->event_type ?: '—') ?></span></div>
                    <div class="col"><span class="text-muted">Status</span><br><span class="badge bg-<?= esc($lookups->eventStatuses[$event->status]['badge'] ?? 'secondary') ?>"><?= esc($lookups->eventStatuses[$event->status]['label'] ?? $event->status) ?></span></div>
                    <div class="col"><span class="text-muted">Venue / Space</span><br><span class="fw-semibold"><?= esc($event->venue_space ?: '—') ?></span></div>
                    <div class="col"><span class="text-muted">Expected Headcount</span><br><span class="fw-semibold"><?= esc($event->expected_headcount ?: '—') ?></span></div>
                    </div>
                <?php if ($event->description): ?>
                <hr>
                <div class="text-muted small mb-1">Description</div>
                <p class="mb-0 small"><?= nl2br(esc($event->description)) ?></p>
                <?php endif; ?>
            </div>
        </div>

        <div class="card shadow-sm mb-3">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-journal-text" style="color: var(--brand);"></i> Contract / Notes
            </div>
            <div class="card-body">
                <p class="mb-0 small"><?= $event->contract_notes ? nl2br(esc($event->contract_notes)) : '<span class="text-muted">No contract notes on file.</span>' ?></p>
            </div>
        </div>

        <?php echo view('events/_registrations_panel', [
            'event' => $event, 'lookups' => $lookups, 'customFields' => $customFields,
            'registrations' => $registrations, 'registerUrl' => $registerUrl,
            'checkinUrl' => $checkinUrl, 'qrDataUri' => $qrDataUri,
            'baseUrl' => 'events', 'canManageFields' => true,
        ]); ?>
    </div>

    <div class="col-lg-4">
        <div class="card shadow-sm mb-3">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-person-vcard" style="color: var(--brand);"></i> Client Info
            </div>
            <div class="card-body">
                <?php if ($guest): ?>
                    <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Name</span><a href="<?= site_url('guests/' . $guest->id) ?>" class="fw-semibold text-decoration-none"><?= esc($guest->full_name) ?></a></div>
                    <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Phone</span><span class="fw-semibold"><?= esc($guest->phone) ?></span></div>
                    <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Email</span><span class="fw-semibold"><?= esc($guest->email ?: '—') ?></span></div>
                    <a href="<?= site_url('guests/' . $guest->id) ?>" class="btn btn-sm btn-outline-primary w-100 mt-2">View Guest Profile</a>
                <?php elseif ($eventClient): ?>
                    <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Company</span><a href="<?= site_url('event-clients/' . $eventClient->id) ?>" class="fw-semibold text-decoration-none"><?= esc($eventClient->company_name) ?></a></div>
                    <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Contact</span><span class="fw-semibold"><?= esc($eventClient->contact_name ?: '—') ?></span></div>
                    <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Phone</span><span class="fw-semibold"><?= esc($eventClient->phone ?: '—') ?></span></div>
                    <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Email</span><span class="fw-semibold"><?= esc($eventClient->email ?: '—') ?></span></div>
                    <?php if ($eventClient->catering_preferences): ?>
                        <div class="small mt-2"><span class="text-muted">Catering Preferences</span><p class="mb-0"><?= nl2br(esc($eventClient->catering_preferences)) ?></p></div>
                    <?php endif; ?>
                    <a href="<?= site_url('event-clients/' . $eventClient->id) ?>" class="btn btn-sm btn-outline-primary w-100 mt-2">View Company Profile</a>
                <?php else: ?>
                    <p class="text-muted small mb-0">No client on file.</p>
                <?php endif; ?>
            </div>
        </div>

        <div class="card shadow-sm mb-3">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-person-badge" style="color: var(--brand);"></i> Event Organizer
            </div>
            <div class="card-body">
                <?php if ($organizer): ?>
                    <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Name</span><span class="fw-semibold"><?= esc($organizer->full_name) ?></span></div>
                    <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Email</span><span class="fw-semibold"><?= esc($organizer->email) ?></span></div>
                    <div class="d-flex justify-content-between small mb-3"><span class="text-muted">Status</span><span class="badge bg-<?= $organizer->password_hash ? 'success' : 'warning text-dark' ?>"><?= $organizer->password_hash ? 'Active' : 'Invite Pending' ?></span></div>
                    <form action="<?= site_url('events/' . $event->id . '/resend-organizer-invite') ?>" method="post">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-secondary btn-sm w-100"><i class="bi bi-arrow-repeat me-1"></i><?= $organizer->password_hash ? 'Resend Invite' : 'Resend Set-Password Link' ?></button>
                    </form>
                <?php else: ?>
                    <p class="text-muted small">Give this event's client their own login to manage registration questions and see who's registered.</p>
                    <form action="<?= site_url('events/' . $event->id . '/invite-organizer') ?>" method="post">
                        <?= csrf_field() ?>
                        <div class="mb-2">
                            <input type="text" class="form-control form-control-sm" name="organizer_name" placeholder="Full name" required>
                        </div>
                        <div class="mb-2">
                            <input type="email" class="form-control form-control-sm" name="organizer_email" placeholder="Email" required>
                        </div>
                        <div class="mb-2">
                            <input type="tel" class="form-control form-control-sm" name="organizer_phone" placeholder="Phone (024XXXXXXX)" required>
                        </div>
                        <button type="submit" class="btn btn-primary btn-sm w-100"><i class="bi bi-send me-1"></i>Invite Organizer</button>
                    </form>
                <?php endif; ?>
            </div>
        </div>

        <div class="card shadow-sm">
            <div class="card-header">Quick Actions</div>
            <div class="card-body d-flex flex-column gap-2">
                <a href="<?= site_url('events/' . $event->id . '/edit') ?>" class="btn btn-outline-primary btn-sm">Edit Event</a>
                <a href="<?= site_url('events') ?>" class="btn btn-outline-secondary btn-sm">Back to Events</a>
            </div>
        </div>
    </div>
</div>

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