@extends('layouts.member')

@section('title', 'Mes Commandes')
@section('page-title', 'Mes Commandes')

@section('content')
@php
    $pageOrders = collect($orders->items());
    $totalOrders = $orders->total() ?? $pageOrders->count();
    $inProgress = $pageOrders->whereIn('status', ['pending','processing'])->count();
    $delivered = $pageOrders->where('status', 'delivered')->count();
    $cancelled = $pageOrders->where('status', 'cancelled')->count();
    $pageTotalAmount = $pageOrders->sum('total');
@endphp

<div class="row g-4 mb-4">
    <div class="col-lg-3 col-md-6">
        <div class="stat-card modern shadow-sm border-0 rounded-3">
            <p class="text-muted small mb-1">Total commandes</p>
            <h4 class="fw-700 mb-0">{{ $totalOrders }}</h4>
        </div>
    </div>
    <div class="col-lg-3 col-md-6">
        <div class="stat-card modern shadow-sm border-0 rounded-3">
            <p class="text-muted small mb-1">En cours</p>
            <h4 class="text-primary fw-700 mb-0">{{ $inProgress }}</h4>
        </div>
    </div>
    <div class="col-lg-3 col-md-6">
        <div class="stat-card modern shadow-sm border-0 rounded-3">
            <p class="text-muted small mb-1">Livrées</p>
            <h4 class="text-success fw-700 mb-0">{{ $delivered }}</h4>
        </div>
    </div>
    <div class="col-lg-3 col-md-6">
        <div class="stat-card modern shadow-sm border-0 rounded-3">
            <p class="text-muted small mb-1">Montant (page)</p>
            <h4 class="fw-700 mb-0">{{ number_format($pageTotalAmount, 0, ',', ' ') }} XOF</h4>
        </div>
    </div>
</div>

<div class="card modern shadow-sm border-0 rounded-3">
    <div class="card-header bg-white d-flex flex-column flex-lg-row align-items-lg-center justify-content-between gap-3 rounded-top-3">
        <div>
            <h5 class="mb-1">Historique des commandes</h5>
            <small class="text-muted">Filtrez, recherchez et exportez vos commandes</small>
        </div>
        <div class="d-flex flex-wrap gap-2">
            <a class="btn btn-sm btn-outline-secondary" href="{{ request()->fullUrlWithQuery(['export' => 'csv']) }}">
                <i class="bi bi-download me-1"></i> Export CSV
            </a>
            <a class="btn btn-sm btn-outline-secondary" href="{{ request()->fullUrlWithQuery(['export' => 'pdf']) }}">
                <i class="bi bi-file-earmark-pdf me-1"></i> Export PDF
            </a>
        </div>
    </div>

    <div class="card-body border-bottom">
        <form action="{{ route('member.orders.index') }}" method="GET" class="row g-3 align-items-end">
            <div class="col-6 col-md-3">
                <label class="form-label small">Statut</label>
                <select name="status" class="form-select form-select-sm">
                    <option value="">Tous</option>
                    <option value="pending" {{ request('status')==='pending' ? 'selected' : '' }}>En attente</option>
                    <option value="processing" {{ request('status')==='processing' ? 'selected' : '' }}>En cours</option>
                    <option value="shipped" {{ request('status')==='shipped' ? 'selected' : '' }}>Expédiée</option>
                    <option value="delivered" {{ request('status')==='delivered' ? 'selected' : '' }}>Livrée</option>
                    <option value="cancelled" {{ request('status')==='cancelled' ? 'selected' : '' }}>Annulée</option>
                </select>
            </div>
            <div class="col-6 col-md-3">
                <label class="form-label small">Du</label>
                <input type="date" name="from" class="form-control form-control-sm" value="{{ request('from') }}">
            </div>
            <div class="col-6 col-md-3">
                <label class="form-label small">Au</label>
                <input type="date" name="to" class="form-control form-control-sm" value="{{ request('to') }}">
            </div>
            <div class="col-12 col-md-6">
                <label class="form-label small">Recherche</label>
                <input type="text" name="q" class="form-control form-control-sm" placeholder="Commande, référence, client..." value="{{ request('q') }}">
            </div>
            <div class="col-12 col-md-6 d-flex gap-2 justify-content-end">
                <a href="{{ route('member.orders.index') }}" class="btn btn-outline-secondary btn-sm">Réinitialiser</a>
                <button type="submit" class="btn btn-primary btn-sm">Filtrer</button>
            </div>
        </form>
    </div>

    <div class="table-responsive">
        <table class="table table-hover table-modern mb-0">
            <thead class="table-light">
                <tr>
                    <th class="small text-muted fw-600">Commande</th>
                    <th class="small text-muted fw-600">Date</th>
                    <th class="small text-muted fw-600">Articles</th>
                    <th class="small text-muted fw-600">Total</th>
                    <th class="small text-muted fw-600">Statut</th>
                    <th class="small text-muted fw-600">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($orders as $order)
                    <tr class="align-middle">
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <span class="fw-600">#{{ $order->order_number ?? $order->id }}</span>
                                @if($order->order_type === 'investment')
                                    <span class="badge bg-primary-subtle text-primary" title="Produit d'investissement">
                                        <i class="bi bi-gem"></i>
                                    </span>
                                @endif
                            </div>
                        </td>
                        <td class="small">{{ $order->created_at->format('d/m/Y') }}</td>
                        <td class="small">
                            @if($order->order_type === 'investment')
                                <span class="text-primary fw-500">Investissement</span>
                            @else
                                {{ $order->items->count() }} article(s)
                            @endif
                        </td>
                        <td class="fw-600">{{ number_format($order->total_amount ?? $order->total, 0, ',', ' ') }} XOF</td>
                        <td>
                            @php
                                $st = $order->status;
                                $map = [
                                    'pending' => ['warning','En attente'],
                                    'processing' => ['info','En cours'],
                                    'shipped' => ['primary','Expédiée'],
                                    'delivered' => ['success','Livrée'],
                                    'cancelled' => ['danger','Annulée']
                                ];
                                $badge = $map[$st] ?? ['secondary', ucfirst($st)];
                            @endphp
                            <span class="badge rounded-pill bg-{{ $badge[0] }} bg-opacity-75">{{ $badge[1] }}</span>
                        </td>
                        <td>
                            <a href="{{ route('member.orders.show', $order) }}" class="btn btn-sm btn-outline-primary">
                                <i class="bi bi-eye me-1"></i>Détails
                            </a>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="6" class="text-center text-muted py-4">
                            <i class="bi bi-inbox fs-3 d-block mb-2 opacity-50"></i>
                            Aucune commande trouvée
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="card-footer bg-light d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-2">
        <small class="text-muted">Total: {{ $orders->total() ?? 0 }} commandes</small>
        <div>{{ $orders->appends(request()->query())->links() }}</div>
    </div>
</div>
@endsection
