<?php
/**
 * Crew Portal — Gateway Dashboard
 * Location: app/views/operation/dashboard.php
 *
 * Variables:
 *   $crewName          — string — Crew display name.
 *   $totalAssigned     — int — Count of assigned workspaces.
 *   $incomingWeddings  — array — List of active weddings within 30 days.
 */
?>
<div class="max-w-5xl mx-auto space-y-8 py-4">

    <!-- ── 1. Welcome & Greeting ── -->
    <div class="bg-white rounded-2xl border border-slate-100 p-6 sm:p-8 shadow-sm flex flex-col md:flex-row md:items-center justify-between gap-6" style="border-left: 4px solid var(--brand-rose-gold);">
        <div class="space-y-2">
            <h1 class="text-2xl sm:text-3xl font-extrabold text-slate-900 tracking-tight">
                Hello, <?= escape_html($crewName) ?>
            </h1>
            <p class="text-sm text-slate-500 max-w-xl leading-relaxed">
                Welcome back to your Crew Portal. Here you can access and manage the guest check-ins for the wedding workspaces you are assigned to.
            </p>
        </div>
        <div class="bg-[#FAF8F3] px-6 py-4 rounded-xl border border-slate-150 flex flex-col items-center text-center justify-center flex-shrink-0">
            <span class="text-3xl font-black text-slate-800 leading-none">
                <?= (int)$totalAssigned ?>
            </span>
            <span class="text-[10px] font-bold uppercase tracking-wider text-slate-400 mt-2">
                Assigned Weddings
            </span>
        </div>
    </div>

    <!-- ── 2. Incoming Weddings (Next 30 Days) ── -->
    <div class="bg-white rounded-2xl border border-slate-100 shadow-sm overflow-hidden">
        <div class="px-6 py-5 border-b border-slate-100 flex items-center justify-between">
            <h2 class="text-sm font-bold text-slate-800 uppercase tracking-wider flex items-center gap-2">
                <i class="fa-solid fa-clock text-[#D4A28C]"></i> Incoming Weddings (Next 30 Days)
            </h2>
            <a href="<?= url('/app/crew/weddings') ?>" class="text-xs font-bold text-[#D4A28C] hover:text-[#c4927c] transition-colors flex items-center gap-1">
                View All <i class="fa-solid fa-arrow-right"></i>
            </a>
        </div>

        <?php if (empty($incomingWeddings)): ?>
            <div class="p-12 text-center space-y-3">
                <span class="text-4xl block">📅</span>
                <h3 class="text-sm font-bold text-slate-700">No Incoming Weddings</h3>
                <p class="text-xs text-slate-400 max-w-xs mx-auto">You do not have any weddings scheduled within the next 30 days.</p>
            </div>
        <?php else: ?>
            <div class="overflow-x-auto">
                <table class="w-full text-left border-collapse" role="table">
                    <thead>
                        <tr class="bg-slate-55 border-b border-slate-100">
                            <th class="px-6 py-3 text-[10px] font-black uppercase tracking-wider text-slate-400">Couple Name</th>
                            <th class="px-6 py-3 text-[10px] font-black uppercase tracking-wider text-slate-400">Wedding Date</th>
                            <th class="px-6 py-3 text-[10px] font-black uppercase tracking-wider text-slate-400">Venue</th>
                            <th class="px-6 py-3 text-[10px] font-black uppercase tracking-wider text-slate-400">Status</th>
                            <th class="px-6 py-3 text-right"></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100">
                        <?php foreach ($incomingWeddings as $w): 
                            $coupleName = "{$w['groom_name']} & {$w['bride_name']}";
                            $statusColor = 'bg-slate-100 text-slate-700';
                            if ($w['workspace_status'] === 'live') {
                                $statusColor = 'bg-green-100 text-green-800';
                            } elseif ($w['workspace_status'] === 'active') {
                                $statusColor = 'bg-blue-100 text-blue-800';
                            }
                        ?>
                            <tr class="hover:bg-slate-50/50 transition-colors">
                                <td class="px-6 py-4 text-sm font-bold text-slate-800">
                                    <?= escape_html($coupleName) ?>
                                </td>
                                <td class="px-6 py-4 text-xs font-semibold text-slate-500">
                                    <?= date('M d, Y', strtotime($w['wedding_date'])) ?>
                                </td>
                                <td class="px-6 py-4 text-xs text-slate-500">
                                    <?= escape_html($w['venue_name'] ?: 'TBD') ?>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="inline-flex items-center px-2 py-0.5 rounded-full text-[10px] font-bold uppercase tracking-wider <?= $statusColor ?>">
                                        <?= escape_html($w['workspace_status']) ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-right">
                                    <form action="<?= url('/app/crew/select-workspace') ?>" method="POST" class="inline m-0">
                                        <?= csrf_field() ?>
                                        <input type="hidden" name="workspace_id" value="<?= (int)$w['id'] ?>">
                                        <button type="submit" class="btn btn-secondary py-1.5 px-3 text-[10px] font-bold tracking-wider uppercase inline-flex items-center gap-1.5 hover:bg-slate-100">
                                            Open Workspace <i class="fa-solid fa-arrow-right-to-bracket text-[8px]"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>

</div>
