@extends('layouts.app')

@section('title', 'My Custom Orders')

@section('styles')
<style>
    h1 {
        font-family: 'Abril Fatface', cursive;
        font-size: 2.5rem;
        color: var(--text-primary);
        margin-bottom: 0.5rem;
    }

    h2 {
        font-family: 'Abril Fatface', cursive;
        font-size: 1.6rem;
        color: var(--text-primary);
        margin-bottom: var(--spacing-md);
    }

    .page-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: var(--spacing-lg);
        gap: var(--spacing-md);
    }

    .page-header-content {
        flex: 1;
    }

    .page-header p {
        color: var(--text-secondary);
        font-size: 1rem;
    }

    .empty-state {
        background-color: var(--bg-secondary);
        border-radius: 8px;
        padding: var(--spacing-xl);
        text-align: center;
        margin-bottom: var(--spacing-lg);
    }

    .empty-state-icon {
        font-size: 3rem;
        margin-bottom: var(--spacing-md);
    }

    .empty-state h2 {
        margin-bottom: 0.5rem;
    }

    .empty-state p {
        color: var(--text-secondary);
        margin-bottom: var(--spacing-lg);
    }

    .orders-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
        gap: var(--spacing-lg);
        margin-bottom: var(--spacing-lg);
    }

    .order-card {
        overflow: hidden;
    }

    .order-card.card:hover {
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    }

    .order-card-header {
        padding: var(--spacing-lg);
        border-bottom: 1px solid var(--border-color);
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
    }

    .order-number {
        font-family: var(--font-sans);
        font-weight: 600;
        color: var(--text-primary);
        font-size: 1.1rem;
        margin-bottom: 0.25rem;
    }

    .order-date {
        color: var(--text-secondary);
        font-size: 0.9rem;
    }

    .order-card-body {
        padding: var(--spacing-lg);
    }

    .order-detail {
        margin-bottom: var(--spacing-md);
    }

    .order-detail:last-child {
        margin-bottom: 0;
    }

    .order-detail-label {
        font-weight: 600;
        color: var(--text-primary);
        font-size: 0.9rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin-bottom: 0.25rem;
    }

    .order-detail-value {
        color: var(--text-secondary);
        font-size: 1rem;
        text-transform: capitalize;
    }

    .order-details-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: var(--spacing-md);
    }

    .order-card-footer {
        padding: var(--spacing-lg);
        border-top: 1px solid var(--border-color);
        display: flex;
        gap: var(--spacing-md);
    }

    .order-card-footer a {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        text-decoration: none;
    }

    .progress-indicator {
        display: flex;
        gap: var(--spacing-sm);
        margin-bottom: var(--spacing-md);
    }

    .progress-step {
        flex: 1;
        height: 4px;
        background-color: var(--border-color);
        border-radius: 2px;
        overflow: hidden;
    }

    .progress-step.active {
        background-color: var(--accent-dark);
    }

    .progress-step.completed {
        background-color: #4caf50;
    }

    .status-flow {
        display: flex;
        align-items: center;
        gap: var(--spacing-sm);
        font-size: 0.85rem;
        color: var(--text-secondary);
        margin-bottom: var(--spacing-md);
        flex-wrap: wrap;
    }

    .status-flow-item {
        display: flex;
        align-items: center;
        gap: 0.25rem;
    }

    .status-flow-arrow {
        color: var(--border-color);
        margin: 0 0.25rem;
    }

    .alert {
        padding: var(--spacing-md);
        border-radius: 8px;
        margin-bottom: var(--spacing-lg);
        border: 1px solid;
    }

    .alert-success {
        background-color: #e8f5e9;
        border-color: #c8e6c9;
        color: #2e7d32;
    }

    @media (max-width: 768px) {
        h1 {
            font-size: 2rem;
        }

        .page-header {
            flex-direction: column;
        }

        .orders-grid {
            grid-template-columns: 1fr;
        }

        .order-details-grid {
            grid-template-columns: 1fr;
        }

        .order-card-footer {
            flex-direction: column;
        }

        .order-card-footer a {
            display: block;
        }

        .status-flow {
            flex-direction: column;
            align-items: flex-start;
        }
    }
</style>
@endsection

@section('content')
<div class="container" style="margin-top:20px;">
    @if (session('success'))
        <div class="alert alert-success">
            {{ session('success') }}
        </div>
    @endif

    <div class="page-header">
        <div class="page-header-content">
            <h1>My Custom Orders</h1>
            <p>Track and manage your custom printing orders</p>
        </div>
        <a href="{{ route('custom-orders.create') }}" class="btn">Create New Order</a>
    </div>

    @if ($customOrders->isEmpty())
        <div class="empty-state">
            <div class="empty-state-icon">📋</div>
            <h2>No Custom Orders Yet</h2>
            <p>You haven't created any custom orders yet. Start by designing your unique print today!</p>
            <a href="{{ route('custom-orders.create') }}" class="btn">Create Your First Order</a>
        </div>
    @else
        <div class="orders-grid">
            @foreach ($customOrders as $order)
                <div class="order-card card">
                    <div class="order-card-header">
                        <div>
                            <div class="order-number">Order #{{ $order->order_number }}</div>
                            <div class="order-date">{{ $order->created_at->format('d M Y') }}</div>
                        </div>
                        <span class="status-badge {{ $order->status }}">
                            {{ str_replace('_', ' ', ucfirst($order->status)) }}
                        </span>
                    </div>

                    <div class="order-card-body">
                        <!-- Progress Indicator -->
                        <div class="progress-indicator">
                            @php
                                $statuses = ['submitted', 'approved', 'in_production', 'proof_ready', 'completed'];
                                $currentIndex = array_search($order->status, $statuses);
                            @endphp
                            @foreach ($statuses as $index => $status)
                                <div class="progress-step {{ $index <= $currentIndex ? 'completed' : ($index === $currentIndex + 1 ? 'active' : '') }}"></div>
                            @endforeach
                        </div>

                        <!-- Order Details Grid -->
                        <div class="order-details-grid">
                            <div class="order-detail">
                                <div class="order-detail-label">Type</div>
                                <div class="order-detail-value">{{ ucfirst($order->type) }}</div>
                            </div>

                            <div class="order-detail">
                                <div class="order-detail-label">Status</div>
                                <div class="order-detail-value">
                                    {{ str_replace('_', ' ', ucfirst($order->status)) }}
                                </div>
                            </div>

                            @if ($order->specifications)
                                <div class="order-detail">
                                    <div class="order-detail-label">Quantity</div>
                                    <div class="order-detail-value">{{ $order->specifications->quantity }}</div>
                                </div>

                                <div class="order-detail">
                                    <div class="order-detail-label">Stock</div>
                                    <div class="order-detail-value">
                                        {{ $order->specifications->printStock ? $order->specifications->printStock->name : 'N/A' }}
                                    </div>
                                </div>
                            @endif
                        </div>

                        <!-- Payment Status -->
                        @if ($order->total_cost)
                            <div class="order-detail">
                                <div class="order-detail-label">Total Cost</div>
                                <div class="order-detail-value" style="font-weight: 600; color: var(--accent-dark); font-size: 1.1rem;">
                                    R{{ number_format($order->total_cost, 2) }}
                                </div>
                            </div>
                        @endif

                        <!-- Status Flow -->
                        <div class="status-flow">
                            <div class="status-flow-item">
                                <span>{{ $order->deposit_status === 'paid' ? '✓' : '○' }}</span>
                                <span>Deposit</span>
                            </div>
                            <div class="status-flow-arrow">→</div>
                            <div class="status-flow-item">
                                <span>{{ $order->status === 'approved' ? '✓' : '○' }}</span>
                                <span>Approved</span>
                            </div>
                            <div class="status-flow-arrow">→</div>
                            <div class="status-flow-item">
                                <span>{{ in_array($order->status, ['in_production', 'proof_ready', 'completed']) ? '✓' : '○' }}</span>
                                <span>Production</span>
                            </div>
                        </div>
                    </div>

                    <div class="order-card-footer">
                        <a href="{{ route('custom-orders.show', $order->uuid) }}">View Details</a>
                        @if ($order->deposit_status === 'unpaid')
                            <a href="{{ route('custom-orders.show', $order->uuid) }}" class="secondary">Pay Deposit</a>
                        @endif
                    </div>
                </div>
            @endforeach
        </div>
    @endif
</div>
@endsection
