<?php

/**
 * Crew Portal — Workspace Selector Hub View
 * Location: app/views/operation/crew_home.php
 */

/** @var array $workspaces */
/** @var string|null $error */

?>

<div class="max-w-xl mx-auto px-4 sm:px-6 lg:px-8 py-12 fade-in">
    <!-- Header Summary Block -->
    <div class="text-center mb-8">
        <div class="inline-flex items-center justify-center w-12 h-12 rounded-full mb-4" style="background-color: var(--brand-ivory-warm); border: 1px solid var(--brand-ivory-dark); color: var(--brand-rose-gold);">
            <i class="fa-solid fa-user-group text-xl"></i>
        </div>
        <h2 class="text-2xl font-black text-slate-900 tracking-tight">
            Crew Workspace Hub
        </h2>
        <p class="text-xs text-slate-500 mt-1">Select an assigned client workspace to start wedding day check-in operations.</p>
    </div>

    <!-- Error State -->
    <?php if ($error): ?>
        <div class="bg-rose-50 border border-rose-150 text-rose-800 p-6 rounded-2xl text-center space-y-3">
            <i class="fa-solid fa-triangle-exclamation text-rose-600 text-3xl"></i>
            <p class="text-xs font-bold"><?= escape_html($error) ?></p>
            <div class="pt-2">
                <a href="<?= url('/logout') ?>" onclick="event.preventDefault(); document.getElementById('logout-form').submit();" class="text-xs font-bold text-rose-650 hover:underline">
                    Log out
                </a>
                <form id="logout-form" action="<?= url('/logout') ?>" method="POST" style="display: none;">
                    <?= csrf_field() ?>
                </form>
            </div>
        </div>
    <?php else: ?>
        <!-- Workspace List -->
        <div class="bg-white border border-slate-150 rounded-2xl shadow-sm overflow-hidden divide-y divide-slate-100">
            <?php foreach ($workspaces as $ws): 
                $wsName = "{$ws['groom_name']} & {$ws['bride_name']}";
            ?>
                <form method="POST" action="<?= url('/app/crew/select-workspace') ?>" class="m-0">
                    <?= csrf_field() ?>
                    <input type="hidden" name="workspace_id" value="<?= $ws['id'] ?>">
                    
                    <button type="submit" class="w-full text-left p-5 hover:bg-slate-50/50 transition-colors flex items-center justify-between group focus:outline-none">
                        <div class="space-y-1">
                            <span class="text-slate-800 font-bold text-sm block group-hover:text-slate-900 transition-colors">
                                <?= escape_html($wsName) ?>
                            </span>
                            <div class="flex items-center gap-2 text-xxs text-slate-400">
                                <span class="font-mono"><?= escape_html($ws['slug']) ?></span>
                                <span>•</span>
                                <span class="font-mono"><?= escape_html($ws['wedding_date']) ?></span>
                                <span>•</span>
                                <span class="uppercase font-bold text-[9px] px-1.5 py-0.5 rounded bg-slate-100 text-slate-600 border border-slate-200"><?= escape_html($ws['workspace_status']) ?></span>
                            </div>
                        </div>
                        <div class="w-8 h-8 rounded-full bg-slate-50 text-slate-400 flex items-center justify-center group-hover:bg-[#FAF8F3] group-hover:text-[#D4A28C] transition-colors">
                            <i class="fa-solid fa-chevron-right text-xs"></i>
                        </div>
                    </button>
                </form>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>
