@extends('layouts.admin')

@section('title', 'Messages Contact')
@section('page-title', 'Messages Contact')

@section('content')
<div class="card">
    <div class="card-header bg-white">
        <h5 class="mb-0">Liste des Messages Reçus</h5>
    </div>
    <div class="card-body">
        @if(session('success'))
            <div class="alert alert-success alert-dismissible fade show">
                {{ session('success') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        @endif

        <div class="table-responsive">
            <table class="table table-hover align-middle">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Nom</th>
                        <th>Email/Téléphone</th>
                        <th>Sujet</th>
                        <th>Message</th>
                        <th>Statut</th>
                        <th>Date</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($contacts as $contact)
                    <tr>
                        <td><strong>#{{ $contact->id }}</strong></td>
                        <td>{{ $contact->name }}</td>
                        <td>
                            <small>{{ $contact->email }}</small><br>
                            @if($contact->phone)
                                <small class="text-muted">{{ $contact->phone }}</small>
                            @endif
                        </td>
                        <td>
                            <span class="badge bg-info">{{ ucfirst($contact->subject) }}</span>
                        </td>
                        <td>
                            <small>{{ Str::limit($contact->message, 50) }}</small>
                        </td>
                        <td>
                            <form action="{{ route('admin.contacts.update-status', $contact) }}" method="POST" class="d-inline">
                                @csrf
                                @method('PUT')
                                <select name="status" class="form-select form-select-sm" onchange="this.form.submit()">
                                    <option value="new" {{ $contact->status == 'new' ? 'selected' : '' }}>Nouveau</option>
                                    <option value="in_progress" {{ $contact->status == 'in_progress' ? 'selected' : '' }}>En cours</option>
                                    <option value="resolved" {{ $contact->status == 'resolved' ? 'selected' : '' }}>Résolu</option>
                                </select>
                            </form>
                        </td>
                        <td>{{ $contact->created_at->format('d/m/Y H:i') }}</td>
                        <td>
                            <button type="button" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#messageModal{{ $contact->id }}" title="Voir le message">
                                <i class="bi bi-eye"></i>
                            </button>
                            <form action="{{ route('admin.contacts.destroy', $contact) }}" method="POST" class="d-inline" onsubmit="return confirm('Supprimer ce message ?')">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn btn-sm btn-outline-danger" title="Supprimer">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </form>
                        </td>
                    </tr>

                    <!-- Modal pour voir le message complet -->
                    <div class="modal fade" id="messageModal{{ $contact->id }}" tabindex="-1">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title">Message de {{ $contact->name }}</h5>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                </div>
                                <div class="modal-body">
                                    <p><strong>Email:</strong> {{ $contact->email }}</p>
                                    @if($contact->phone)
                                        <p><strong>Téléphone:</strong> {{ $contact->phone }}</p>
                                    @endif
                                    <p><strong>Sujet:</strong> {{ ucfirst($contact->subject) }}</p>
                                    <p><strong>Date:</strong> {{ $contact->created_at->format('d/m/Y à H:i') }}</p>
                                    <hr>
                                    <p><strong>Message:</strong></p>
                                    <p>{{ $contact->message }}</p>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fermer</button>
                                    <a href="mailto:{{ $contact->email }}" class="btn btn-primary">
                                        <i class="bi bi-envelope me-1"></i> Répondre par Email
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                    @empty
                    <tr>
                        <td colspan="8" class="text-center text-muted">Aucun message reçu</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div class="mt-3">
            {{ $contacts->links() }}
        </div>
    </div>
</div>
@endsection
