@extends('layouts.admin')
@section('title', 'Orders')
@section('page-title', 'Orders')

@section('content')

{{-- Filters --}}
<form action="{{ route('admin.orders.index') }}" method="GET" style="margin-bottom:20px;">
    <div style="display:flex;flex-wrap:wrap;gap:8px;align-items:center;">
        <input type="text" name="search" value="{{ request('search') }}" placeholder="Search order, name, email…"
               class="tm-input" style="width:220px;flex-shrink:1;">
        <select name="status" class="tm-select" style="width:140px;">
            <option value="">All Payment</option>
            @foreach(['pending','paid','failed','refunded'] as $s)
            <option value="{{ $s }}" {{ request('status') === $s ? 'selected' : '' }}>{{ strtoupper($s) }}</option>
            @endforeach
        </select>
        <select name="work_status" class="tm-select" style="width:160px;">
            <option value="">All Work Status</option>
            @foreach(\App\Models\Order::WORK_STATUSES as $s)
            <option value="{{ $s }}" {{ request('work_status') === $s ? 'selected' : '' }}>{{ strtoupper(str_replace('_',' ',$s)) }}</option>
            @endforeach
        </select>
        <button type="submit" class="tm-btn tm-btn-primary">FILTER</button>
        @if(request()->anyFilled(['status','work_status','search']))
        <a href="{{ route('admin.orders.index') }}" class="tm-btn tm-btn-ghost">CLEAR ×</a>
        @endif
    </div>
</form>

{{-- Success flash --}}
@if(session('success'))
<div style="background:rgba(34,197,94,0.1);border:1px solid rgba(34,197,94,0.25);border-radius:10px;padding:12px 16px;margin-bottom:16px;font-size:13px;color:#86efac;display:flex;align-items:center;gap:8px;">
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"/></svg>
    {{ session('success') }}
</div>
@endif

<div class="glass-card">
    <div class="section-header" style="flex-wrap:wrap;gap:8px;">
        <div class="section-title">All Orders</div>
        <span style="font-family:'JetBrains Mono',monospace;font-size:10px;color:var(--muted);">{{ $orders->total() }} TOTAL</span>
    </div>
    <div class="table-wrap">
    <table class="data-table" style="min-width:700px;">
        <thead>
            <tr>
                <th>Order #</th>
                <th>Customer</th>
                <th>Total</th>
                <th>Payment</th>
                <th>Work Status</th>
                <th>Assigned</th>
                <th>Date</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @forelse($orders as $order)
            <tr>
                <td><span class="mono" style="color:#94a3b8;font-size:11px;">{{ $order->order_number }}</span></td>
                <td>
                    <div style="font-weight:500;color:var(--text);font-size:13px;">{{ $order->full_name }}</div>
                    <div style="color:var(--muted);font-size:11px;">{{ $order->email }}</div>
                </td>
                <td><span class="mono" style="color:#86efac;font-weight:600;">${{ number_format($order->total, 2) }}</span></td>
                <td>
                    <span class="badge badge-{{ $order->status }}">
                        <span class="badge-dot"></span>{{ $order->status }}
                    </span>
                </td>
                <td>
                    <span class="badge badge-{{ $order->work_status ?? 'open' }}">
                        <span class="badge-dot"></span>{{ strtoupper(str_replace('_',' ',$order->work_status ?? 'open')) }}
                    </span>
                </td>
                <td>
                    @forelse($order->employees as $emp)
                    <span style="display:inline-block;background:rgba(255,255,255,0.06);border:1px solid var(--border);border-radius:4px;padding:2px 7px;font-size:10px;color:var(--muted);margin:1px;">{{ $emp->name }}</span>
                    @empty
                    <span style="color:var(--muted);font-size:11px;">—</span>
                    @endforelse
                </td>
                <td style="color:var(--muted);font-size:12px;" class="mono">{{ $order->created_at->format('M j, Y') }}</td>
                <td style="text-align:right;white-space:nowrap;"><a href="{{ route('admin.orders.show', $order) }}" class="link-red">OPEN →</a></td>
            </tr>
            @empty
            <tr><td colspan="8" style="padding:48px;text-align:center;color:var(--muted);font-family:'JetBrains Mono',monospace;font-size:12px;letter-spacing:2px;">NO ORDERS FOUND</td></tr>
            @endforelse
        </tbody>
    </table>
    </div>
    @if($orders->hasPages())
    <div style="padding:16px 20px;border-top:1px solid var(--border);">{{ $orders->links() }}</div>
    @endif
</div>
@endsection
