@extends('layouts.app')

@section('content')

<div class="row mb-4 align-items-center">
    <div class="col-md-6">
        <h2 class="fw-bold text-dark mb-1">Visitor Messages</h2>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb mb-0">
                <li class="breadcrumb-item"><a href="{{ route('dashboard') }}" class="text-decoration-none text-muted">Dashboard</a></li>
                <li class="breadcrumb-item active fw-medium text-primary" aria-current="page">Messages</li>
            </ol>
        </nav>
    </div>
</div>

@if(session('success'))
    <div class="alert alert-success alert-dismissible fade show rounded-4 shadow-sm mb-4 border-0 p-3" role="alert">
        <div class="d-flex align-items-center">
            <div class="bg-success text-white rounded-circle p-2 me-3 d-flex align-items-center justify-content-center" style="width: 32px; height: 32px;">
                <i class="bi bi-check-lg"></i>
            </div>
            <div>
                <strong class="d-block">Success!</strong>
                <span class="small">{{ session('success') }}</span>
            </div>
        </div>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
@endif

<div class="card border-0 shadow-sm rounded-4 overflow-hidden">
    <div class="card-header bg-white border-bottom py-3 px-4">
        <h6 class="m-0 fw-bold text-dark">Recent Messages</h6>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="text-secondary small text-uppercase fw-bold" style="background-color: #f8fafc;">
                    <tr>
                        <th class="px-4 py-3" style="width: 80px;">SL.NO</th>
                        <th class="py-3">Sender Details</th>
                        <th class="py-3">Subject</th>
                        <th class="py-3">Date Received</th>
                        <th class="px-4 py-3 text-end">Management</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($messages as $message)
                        <tr class="transition-all">
                            <td class="px-4 py-4 text-secondary fw-medium">{{ $loop->iteration }}</td>
                            <td class="py-4">
                                <h6 class="fw-bold text-dark mb-1">{{ $message->name }}</h6>
                                <div class="text-secondary small">
                                    <i class="bi bi-envelope me-1"></i> <a href="mailto:{{ $message->email }}" class="text-decoration-none">{{ $message->email }}</a>
                                </div>
                            </td>
                            <td class="py-4">
                                <span class="fw-semibold text-dark">{{ $message->subject }}</span>
                            </td>
                            <td class="py-4 text-secondary">
                                <span class="d-block small fw-bold text-dark mb-1">
                                    {{ $message->created_at->format('d M, Y') }}
                                </span>
                                <span class="text-secondary small">
                                    {{ $message->created_at->diffForHumans() }}
                                </span>
                            </td>
                            <td class="px-4 py-4 text-end">
                                <div class="d-flex justify-content-end gap-2">
                                    <a href="{{ route('visitors-messages.show', $message->id) }}" class="btn btn-light btn-sm rounded-3 border text-info" title="View Message">
                                        <i class="bi bi-eye"></i> View
                                    </a>
                                    <button
                                        type="button"
                                        class="btn btn-light btn-sm rounded-3 border text-danger"
                                        title="Delete Message"
                                        data-bs-toggle="modal"
                                        data-bs-target="#deleteMessageModal"
                                        data-message-subject="{{ $message->subject }}"
                                        data-delete-url="{{ route('visitors-messages.destroy', $message->id) }}"
                                    >
                                        <i class="bi bi-trash3"></i> Delete
                                    </button>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="text-center py-5">
                                <img src="https://illustrations.popsy.co/gray/success.svg" alt="Empty" style="width: 150px;" class="mb-4 opacity-50">
                                <h5 class="text-dark fw-bold">No Messages Yet</h5>
                                <p class="text-secondary mb-4">When visitors submit the contact form, their messages will appear here.</p>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Delete Modal -->
<div class="modal fade" id="deleteMessageModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow-lg rounded-4 overflow-hidden">
            <div class="modal-body p-4 p-md-5">
                <div class="delete-modal-icon mx-auto mb-4" style="width: 72px; height: 72px; display: flex; align-items: center; justify-content: center; border-radius: 24px; background: linear-gradient(145deg, #fee2e2 0%, #fecaca 100%); color: #dc2626; font-size: 1.75rem;">
                    <i class="bi bi-trash3"></i>
                </div>
                <div class="text-center">
                    <h5 class="fw-bold text-dark mb-2">Delete this Message?</h5>
                    <p class="text-secondary mb-2">This action will permanently remove the message.</p>
                    <p class="text-dark fw-semibold mb-0" id="deleteMessageSubject"></p>
                </div>
            </div>
            <div class="modal-footer border-0 pt-0 px-4 px-md-5 pb-4 pb-md-5 d-flex justify-content-center gap-2">
                <button type="button" class="btn btn-light border rounded-3 px-4" data-bs-dismiss="modal">Cancel</button>
                <form id="deleteMessageForm" method="POST" class="m-0">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger rounded-3 px-4 shadow-sm">
                        Delete Message
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@section('styles')
<style>
    .transition-all { transition: all 0.2s ease-in-out; }
    .table tbody tr:hover { background-color: #f8fafc !important; transform: scale(1.002); }
    .breadcrumb-item + .breadcrumb-item::before { content: "\2022"; color: #cbd5e1; }
</style>
@endsection

@section('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const deleteMessageModal = document.getElementById('deleteMessageModal');
        const deleteMessageForm = document.getElementById('deleteMessageForm');
        const deleteMessageSubject = document.getElementById('deleteMessageSubject');

        if (deleteMessageModal) {
            deleteMessageModal.addEventListener('show.bs.modal', function (event) {
                const button = event.relatedTarget;
                const url = button.getAttribute('data-delete-url');
                const subject = button.getAttribute('data-message-subject');

                deleteMessageForm.setAttribute('action', url);
                deleteMessageSubject.textContent = '"' + subject + '"';
            });
        }
    });
</script>
@endsection
