@extends('layout.admin')

@section('content')
@php
    $offersEnabled = (bool)($offerState['enabled'] ?? true);
    $reopenAt = \App\Support\OfferProjectState::formatForDisplay($offerState['reopen_at'] ?? null);
@endphp

<div class="container py-3">
    <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
        <h2 class="fw-bold mb-0">
            <i class="fas fa-tags text-primary me-2"></i>অফার তালিকা
            <span class="ms-2 badge {{ $offersEnabled ? 'bg-success' : 'bg-danger' }}">
                {{ $offersEnabled ? 'প্রজেক্ট চালু' : 'প্রজেক্ট বন্ধ' }}
            </span>
        </h2>

        <div class="d-flex gap-2">
            <a href="{{ route('admin.offers.settings') }}" class="btn btn-outline-primary shadow-sm px-3 py-2 rounded">
                <i class="fas fa-toggle-on me-1"></i>কন্ট্রোল
            </a>
            <a href="{{ route('admin.offers.create') }}" class="btn btn-success shadow-sm px-4 py-2 rounded">
                <i class="fas fa-plus-circle me-1"></i>নতুন অফার
            </a>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="fas fa-check-circle me-1"></i>{{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    @if(!$offersEnabled)
        <div class="alert alert-warning">
            <strong>ড্রাইভ অফার প্রজেক্ট বর্তমানে বন্ধ।</strong>
            @if($reopenAt)
                <div class="mt-1">পুনরায় চালু হবে: {{ $reopenAt }}</div>
            @endif
        </div>
    @endif

    @if($offers->isEmpty())
        <div class="alert alert-warning text-center">
            <i class="fas fa-exclamation-circle me-1"></i>কোনো অফার পাওয়া যায়নি।
        </div>
    @else
        <div class="row row-cols-1 row-cols-md-2 g-4">
            @foreach($offers as $offer)
                <div class="col">
                    <div class="card border-0 shadow-sm h-100" style="border-radius:14px; overflow:hidden;">
                        <div class="card-body p-3">
                            <div class="d-flex mb-2">
                                <img src="{{ asset($offer->logo) }}" alt="Logo"
                                     style="height:50px; width:50px; object-fit:contain; border-radius:8px; background-color:#f5f5f5;"
                                     class="me-3">
                                <div>
                                    <h5 class="mb-1 fw-bold text-dark" style="font-size:16px;">{{ $offer->title }}</h5>
                                    <small class="text-muted d-block">
                                        <i class="fas fa-calendar-alt me-1 text-secondary"></i>মেয়াদ: {{ $offer->validity }}
                                    </small>
                                </div>
                            </div>

                            <p class="text-muted small mb-2" style="line-height:1.5;">{{ $offer->description }}</p>

                            <div class="mb-2">
                                <span class="badge bg-light text-dark me-1">
                                    <i class="fas fa-sim-card text-primary me-1"></i>{{ $offer->operator->name }}
                                </span>
                                <span class="badge bg-light text-dark">
                                    <i class="fas fa-layer-group text-info me-1"></i>{{ $offer->category->name }}
                                </span>
                            </div>

                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <span class="text-success fw-bold">
                                    <i class="fas fa-money-bill-wave me-1"></i>৳{{ number_format($offer->price, 2) }}
                                </span>
                                <span class="text-primary fw-bold">
                                    <i class="fas fa-sync-alt me-1"></i>৳{{ number_format($offer->cashback, 2) }}
                                </span>
                            </div>

                            <div class="d-flex gap-2">
                                <a href="{{ route('admin.offers.edit', $offer->id) }}" class="btn btn-sm btn-outline-warning w-50">
                                    <i class="fas fa-edit me-1"></i>এডিট
                                </a>

                                <form method="POST" action="{{ route('admin.offers.destroy', $offer->id) }}"
                                      onsubmit="return confirm('আপনি কি নিশ্চিতভাবে অফারটি ডিলিট করতে চান?');" class="w-50">
                                    @csrf
                                    <button type="submit" class="btn btn-sm btn-outline-danger w-100">
                                        <i class="fas fa-trash-alt me-1"></i>ডিলিট
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            @endforeach
        </div>
    @endif
</div>
@endsection
