@extends('layouts.appback')

@section('title', 'Elenco Promozioni')

@section('content')
    <div class="container-fluid py-2">
        <div class="row">
            <div class="col-12">
                <div class="card my-4">
                    {{-- Header --}}
                    <div class="card-header p-0 position-relative mt-n4 mx-3 z-index-2">
                        <div
                            class="bg-gradient-dark shadow-dark border-radius-lg pt-4 pb-3 d-flex justify-content-between align-items-center">
                            <h6 class="text-white text-capitalize ps-3 mb-0">Promozioni</h6>
                            <a href="{{ route('promotions.create') }}" class="btn btn-primary me-3">+ Nuova Promozione</a>
                        </div>
                    </div>

                    {{-- Corpo tabella --}}
                    <div class="card-body px-0 pb-2">
                        <div class="table-responsive p-0">
                            @if ($promotions->isEmpty())
                                <div class="alert alert-info m-3">Non ci sono promozioni. Crea la prima!</div>
                            @else
                                <table class="table align-items-center mb-0">
                                    <thead>
                                        <tr>
                                            <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">
                                                Anteprima</th>
                                            <th
                                                class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">
                                                Titolo</th>
                                            <th
                                                class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">
                                                Stato</th>
                                            <th
                                                class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">
                                                Azienda</th>
                                            <th
                                                class="text-end text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">
                                                Azioni</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach ($promotions as $promo)
                                            <tr>
                                                {{-- Anteprima immagine --}}
                                                <td>
                                                    <div class="d-flex px-2 py-1">
                                                        <div>
                                                            <img src="{{ asset('storage/' . $promo->image) }}"
                                                                width="100" class="rounded shadow-sm me-2"
                                                                alt="{{ $promo->title }}">
                                                        </div>
                                                    </div>
                                                </td>

                                                {{-- Titolo --}}
                                                <td class="text-sm">
                                                    {{ $promo->title }}
                                                </td>

                                                {{-- Stato (esempio: se hai un campo status nel DB) --}}
                                                <td class="align-middle text-center text-sm">
                                                    @if ($promo->status === 'online')
                                                        <span class="badge badge-sm bg-gradient-success">Online</span>
                                                    @else
                                                        <span class="badge badge-sm bg-gradient-success">Attiva</span>
                                                    @endif
                                                </td>

                                                {{-- Azienda associata --}}
                                                <td class="align-middle text-center text-sm">
                                                    <span
                                                        class="badge badge-sm bg-gradient-info">{{ $promo->azienda?->nome ?? 'Amazon' }}</span>
                                                </td>

                                                {{-- Azioni --}}
                                                <td class="align-middle text-end">
                                                    <a href="{{ route('promotions.edit', $promo) }}"
                                                        class="btn btn-sm btn-outline-primary">Modifica</a>
                                                    <form action="{{ route('promotions.destroy', $promo) }}" method="POST"
                                                        class="d-inline"
                                                        onsubmit="return confirm('Eliminare questa promozione?');">
                                                        @csrf
                                                        @method('DELETE')
                                                        <button type="submit"
                                                            class="btn btn-sm btn-outline-danger">Elimina</button>
                                                    </form>
                                                </td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            @endif
                        </div>
                    </div>
                </div>

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