@extends('layouts.landing')

@section('title', 'Promociones')

@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="fab fa-whatsapp mr-1"></i> Promociones WhatsApp
            </h6>
            <div>
                <a href="{{ route('promocion.analytics') }}" class="btn btn-sm btn-info shadow-sm mr-1">
                    <i class="fas fa-chart-bar fa-sm mr-1"></i> Analytics
                </a>
                <a href="{{ route('promocion.chat') }}" class="btn btn-sm btn-success shadow-sm mr-1">
                    <i class="fas fa-comments fa-sm mr-1"></i> Chat
                </a>
                @can('promocion.create')
                <a href="{{ route('promocion.create') }}" class="btn btn-sm btn-primary shadow-sm">
                    <i class="fas fa-plus fa-sm text-white-50 mr-1"></i> Nueva promoción
                </a>
                @endcan
            </div>
        </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:50px">#</th>
                            <th style="width:70px">Imagen</th>
                            <th>Título</th>
                            <th style="width:180px">Planteles</th>
                            <th style="width:120px">Inicio</th>
                            <th style="width:120px">Fin</th>
                            <th style="width:90px" class="text-center">Status</th>
                            <th style="width:160px" class="text-center">Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($promociones as $promo)
                            <tr>
                                <td>{{ $promo->id }}</td>
                                <td class="text-center">
                                    @if($promo->imagen)
                                        <img src="{{ asset('storage/'.$promo->imagen) }}"
                                             alt="img" class="rounded" style="width:50px;height:50px;object-fit:cover">
                                    @else
                                        <span class="text-muted"><i class="fas fa-image"></i></span>
                                    @endif
                                </td>
                                <td>
                                    <strong>{{ $promo->titulo }}</strong>
                                    @if($promo->descripcion)
                                        <div class="small text-muted text-truncate" style="max-width:260px">
                                            {{ $promo->descripcion }}
                                        </div>
                                    @endif
                                </td>
                                <td>
                                    @forelse($promo->planteles as $p)
                                        <span class="badge badge-light border mr-1">{{ $p->name }}</span>
                                    @empty
                                        <span class="text-muted">—</span>
                                    @endforelse
                                </td>
                                <td>{{ $promo->fecha_inicio->format('d/m/Y') }}</td>
                                <td>{{ $promo->fecha_fin->format('d/m/Y') }}</td>
                                <td class="text-center">
                                    <form action="{{ route('promocion.toggle', $promo) }}" method="POST">
                                        @csrf
                                        @method('PATCH')
                                        <button type="submit"
                                                class="btn btn-sm {{ $promo->status === 'activo' ? 'btn-success' : 'btn-secondary' }}"
                                                title="{{ $promo->status === 'activo' ? 'Desactivar' : 'Activar' }}">
                                            <i class="fas {{ $promo->status === 'activo' ? 'fa-toggle-on' : 'fa-toggle-off' }}"></i>
                                        </button>
                                    </form>
                                </td>
                                <td class="text-center">
                                    @can('promocion.enviar')
                                    <form action="{{ route('promocion.enviar', $promo) }}" method="POST" class="d-inline enviar-promo-form">
                                        @csrf
                                        <button type="button" class="btn btn-sm btn-success btn-enviar-promo" title="Enviar por WhatsApp"
                                                data-titulo="{{ $promo->titulo }}">
                                            <i class="fab fa-whatsapp"></i>
                                        </button>
                                    </form>
                                    @endcan
                                    @can('promocion.edit')
                                    <a href="{{ route('promocion.edit', $promo) }}" class="btn btn-sm btn-primary">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    @endcan
                                    @can('promocion.destroy')
                                    <form action="{{ route('promocion.destroy', $promo) }}" 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>
                                    @endcan
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="text-center text-muted py-4">
                                    <i class="fab fa-whatsapp fa-2x mb-2 d-block"></i>
                                    No hay promociones creadas.
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

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

@section('scripts')
@include('layouts._partials.delete')
<script>
document.addEventListener('click', function (e) {
    const btn = e.target.closest('.btn-enviar-promo');
    if (!btn) return;

    const form   = btn.closest('.enviar-promo-form');
    const titulo = btn.dataset.titulo;

    Swal.fire({
        title: '¿Enviar promoción?',
        html:  `Se enviará <strong>${titulo}</strong> a todos los prospectos activos de los planteles asignados.`,
        icon:  'question',
        showCancelButton:   true,
        confirmButtonColor: '#28a745',
        confirmButtonText:  '<i class="fab fa-whatsapp mr-1"></i> Enviar',
        cancelButtonText:   'Cancelar',
    }).then(result => {
        if (result.isConfirmed) {
            btn.disabled = true;
            btn.innerHTML = '<span class="spinner-border spinner-border-sm"></span>';
            form.submit();
        }
    });
});
</script>
@endsection
