@extends('layouts.landing')

@section('title', 'Anuncios')

@section('content')
<div class="container-fluid">
    <div class="card shadow mb-4">
        <div class="card-header py-3 d-flex align-items-center justify-content-between">
            <h6 class="m-0 font-weight-bold text-primary">
                <i class="fas fa-bullhorn mr-1"></i> Anuncios
            </h6>
            <a href="{{ route('anuncio.create') }}" class="btn btn-sm btn-primary shadow-sm">
                <i class="fas fa-plus fa-sm text-white-50 mr-1"></i> Nuevo anuncio
            </a>
        </div>
        <div class="card-body">

            @if(session('success'))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    {{ session('success') }}
                    <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
                </div>
            @endif

            <div class="table-responsive">
                <table class="table table-bordered table-hover" style="width:100%">
                    <thead class="thead-light">
                        <tr>
                            <th style="width:40px">#</th>
                            <th>Título</th>
                            <th style="width:130px">Tipo</th>
                            <th style="width:160px">Audiencia</th>
                            <th style="width:130px">Vence</th>
                            <th style="width:80px" class="text-center">Activo</th>
                            <th style="width:130px" class="text-center">Opciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($anuncios as $anuncio)
                            @php $cfg = $anuncio->typeConfig(); @endphp
                            <tr class="{{ $anuncio->isExpired() ? 'table-secondary text-muted' : '' }}">
                                <td>{{ $anuncio->id }}</td>
                                <td>
                                    <span class="badge badge-{{ $cfg['color'] }} mr-1">
                                        <i class="fas {{ $cfg['icon'] }}"></i>
                                    </span>
                                    {{ $anuncio->title }}
                                    @if($anuncio->image_path)
                                        <i class="fas fa-image text-secondary ml-1" title="Tiene imagen"></i>
                                    @endif
                                </td>
                                <td>
                                    <span class="badge badge-{{ $cfg['color'] }}">{{ $cfg['label'] }}</span>
                                </td>
                                <td>
                                    @foreach($anuncio->audience as $aud)
                                        <span class="badge badge-light border mr-1">{{ \App\Models\Anuncio::ROLES[$aud] ?? $aud }}</span>
                                    @endforeach
                                </td>
                                <td>
                                    @if($anuncio->expires_at)
                                        <span class="{{ $anuncio->isExpired() ? 'text-danger' : 'text-secondary' }}">
                                            <i class="fas fa-clock mr-1"></i>
                                            {{ $anuncio->expires_at->format('d/m/Y H:i') }}
                                        </span>
                                    @else
                                        <span class="text-muted">Sin vencimiento</span>
                                    @endif
                                </td>
                                <td class="text-center">
                                    <form action="{{ route('anuncio.toggleActive', $anuncio) }}" method="POST">
                                        @csrf
                                        @method('PATCH')
                                        <button type="submit" class="btn btn-sm {{ $anuncio->active ? 'btn-success' : 'btn-secondary' }}"
                                                title="{{ $anuncio->active ? 'Desactivar' : 'Activar' }}">
                                            <i class="fas {{ $anuncio->active ? 'fa-toggle-on' : 'fa-toggle-off' }}"></i>
                                        </button>
                                    </form>
                                </td>
                                <td class="text-center">
                                    <a href="{{ route('anuncio.edit', $anuncio) }}" class="btn btn-sm btn-primary">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <form action="{{ route('anuncio.destroy', $anuncio) }}" method="POST" class="d-inline delete-form">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-danger">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="text-center text-muted py-4">
                                    <i class="fas fa-bullhorn fa-2x mb-2 d-block"></i>
                                    No hay anuncios creados.
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <div class="d-flex justify-content-center mt-3">
                {{ $anuncios->links() }}
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
@include('layouts._partials.delete')
@endsection
