@extends('bo.layouts.backend')

@section('title', 'Missions BO · LineUp France')

@section('content')
    <div class="bg-body-light bo-page-top">
        <div class="content content-full bo-page-top-content">
            <div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center py-2 bo-page-head">
                <div class="flex-grow-1">
                    <h1 class="h3 fw-bold mb-1 bo-page-title">Missions BO</h1>
                    <h2 class="fs-base lh-base fw-medium text-muted mb-0 bo-page-subtitle">Gestion complete et supervision des missions.</h2>
                </div>
                <nav class="flex-shrink-0 mt-3 mt-sm-0 ms-sm-3" aria-label="breadcrumb">
                    <ol class="breadcrumb breadcrumb-alt bo-breadcrumb">
                        <li class="breadcrumb-item">LineUp France</li>
                        <li class="breadcrumb-item" aria-current="page">Missions</li>
                    </ol>
                </nav>
            </div>
            <div class="bo-hero-actions">
                <a href="{{ route('bo.dashboard') }}" class="btn btn-sm btn-alt-secondary">
                    <i class="si si-speedometer me-1"></i> Tableau de bord
                </a>
                <a href="{{ route('bo.users.index') }}" class="btn btn-sm btn-alt-secondary">
                    <i class="si si-users me-1"></i> Utilisateurs
                </a>
                <a href="{{ route('bo.finance') }}" class="btn btn-sm btn-alt-secondary">
                    <i class="si si-wallet me-1"></i> Finance
                </a>
                <div class="ms-auto">
                    <a href="{{ route('bo.missions.create') }}" class="btn btn-alt-primary">
                        <i class="fa fa-plus opacity-50 me-1"></i> Nouvelle mission
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="content bo-page-content">
        <div class="block block-rounded">
            <div class="block-header block-header-default">
                <h3 class="block-title"><i class="si si-equalizer me-1"></i>Filtres avances</h3>
                <div class="block-options">
                    <a href="{{ route('bo.missions.index') }}" class="btn btn-sm btn-alt-secondary">
                        <i class="fa fa-rotate-left opacity-50 me-1"></i> Reinitialiser
                    </a>
                </div>
            </div>
            <div class="block-content">
                <form method="GET" action="{{ route('bo.missions.index') }}" class="row g-3 align-items-end bo-filter-form">
                    <div class="col-md-3">
                        <label class="form-label" for="q">Recherche</label>
                        <div class="input-group">
                            <span class="input-group-text bg-body-light border-0"><i class="fa fa-search text-muted"></i></span>
                            <input id="q" type="text" name="q" class="form-control form-control-alt" value="{{ $search }}" placeholder="Titre, evenement, lieu">
                        </div>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label" for="status">Statut</label>
                        <select id="status" name="status" class="form-select">
                            <option value="">Tous</option>
                            @foreach($statuses as $statusOption)
                                @php
                                    $statusLabel = match ($statusOption) {
                                        'published' => 'Publiee',
                                        'accepted' => 'Acceptee',
                                        'en_route' => 'En route',
                                        'arrived' => 'Arrivee',
                                        'in_queue' => 'Dans la file',
                                        'client_arrived' => 'Client arrive',
                                        'spot_transmitted' => 'Place transmise',
                                        'completed' => 'Terminee',
                                        'failed' => 'Echec',
                                        'cancelled' => 'Annulee',
                                        default => ucfirst(str_replace('_', ' ', $statusOption)),
                                    };
                                @endphp
                                <option value="{{ $statusOption }}" @selected($status === $statusOption)>{{ $statusLabel }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label" for="client_id">Client</label>
                        <select id="client_id" name="client_id" class="form-select">
                            <option value="">Tous</option>
                            @foreach($clients as $client)
                                <option value="{{ $client->id }}" @selected($clientId === $client->id)>#{{ $client->id }} · {{ $client->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label" for="liner_id">Liner</label>
                        <select id="liner_id" name="liner_id" class="form-select">
                            <option value="">Tous</option>
                            @foreach($liners as $liner)
                                <option value="{{ $liner->id }}" @selected($linerId === $liner->id)>#{{ $liner->id }} · {{ $liner->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label" for="date_from">Periode debut</label>
                        <div class="input-group">
                            <input id="date_from" type="date" name="date_from" class="form-control form-control-alt" value="{{ $dateFrom }}">
                            <input id="date_to" type="date" name="date_to" class="form-control form-control-alt" value="{{ $dateTo }}">
                        </div>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label" for="budget_min">Budget min/max</label>
                        <div class="input-group">
                            <input id="budget_min" type="number" step="0.01" min="0" name="budget_min" class="form-control form-control-alt" value="{{ $budgetMin }}">
                            <input id="budget_max" type="number" step="0.01" min="0" name="budget_max" class="form-control form-control-alt" value="{{ $budgetMax }}">
                        </div>
                    </div>
                    <div class="col-md-3 d-flex gap-2 bo-filter-actions">
                        <button type="submit" class="btn btn-alt-primary w-100">Filtrer</button>
                    </div>
                </form>
            </div>
        </div>

        <div class="block block-rounded mb-0">
            <div class="block-header block-header-default">
                <h3 class="block-title"><i class="si si-briefcase me-1"></i>Liste missions</h3>
                <div class="block-options">
                    <span class="badge bg-primary">{{ $missions->total() }} resultat(s)</span>
                </div>
            </div>
            <div class="block-content p-0">
                <div class="table-responsive">
                    <table class="table table-vcenter table-striped table-hover fs-sm mb-0 bo-table">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>Mission</th>
                            <th>Client / Liner</th>
                            <th>Date</th>
                            <th>Budget</th>
                            <th>Statut</th>
                            <th style="width: 160px;">Actions</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($missions as $mission)
                            <tr>
                                <td>#{{ $mission->id }}</td>
                                <td>
                                    <div class="fw-semibold">{{ $mission->title }}</div>
                                    <div class="text-muted">{{ $mission->event_name ?: '-' }}</div>
                                    <small class="text-muted">{{ $mission->location }}</small>
                                </td>
                                <td>
                                    <div>Client: {{ $mission->client?->name ?? '-' }}</div>
                                    <div>Liner: {{ $mission->liner?->name ?? '-' }}</div>
                                </td>
                                <td>{{ $mission->expected_start_at?->format('d/m/Y H:i') }}</td>
                                <td>{{ number_format((float) $mission->budget_amount, 2, ',', ' ') }} {{ $mission->currency }}</td>
                                @php
                                    $missionStatusLabel = match ($mission->status) {
                                        'published' => 'Publiee',
                                        'accepted' => 'Acceptee',
                                        'en_route' => 'En route',
                                        'arrived' => 'Arrivee',
                                        'in_queue' => 'Dans la file',
                                        'client_arrived' => 'Client arrive',
                                        'spot_transmitted' => 'Place transmise',
                                        'completed' => 'Terminee',
                                        'failed' => 'Echec',
                                        'cancelled' => 'Annulee',
                                        default => ucfirst(str_replace('_', ' ', (string) $mission->status)),
                                    };
                                @endphp
                                <td><span class="badge bg-info">{{ $missionStatusLabel }}</span></td>
                                <td>
                                    <div class="btn-group">
                                        <a href="{{ route('bo.missions.show', $mission) }}" class="btn btn-sm btn-alt-secondary">
                                            <i class="fa fa-eye me-1"></i>Voir
                                        </a>
                                        <a href="{{ route('bo.missions.edit', $mission) }}" class="btn btn-sm btn-alt-primary">
                                            <i class="fa fa-pen me-1"></i>Modifier
                                        </a>
                                        <form method="POST" action="{{ route('bo.missions.destroy', $mission) }}" onsubmit="return confirm('Supprimer cette mission ?');">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-alt-danger">
                                                <i class="fa fa-trash me-1"></i>Supprimer
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="text-center text-muted">Aucune mission.</td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="block-content">
                {{ $missions->links() }}
            </div>
        </div>
    </div>
@endsection
