@extends('layouts.admin')

@section('content')
<div class="container-fluid">
    <div class="card">
        <div class="card-header">
            <div class="d-flex justify-content-between align-items-center">
                <h4 class="card-title mb-0">Flash Sales</h4>
                <a href="{{ route('admin.flash-sales.create') }}" class="btn btn-primary">
                    <i class="bi bi-plus"></i> Add New Flash Sale
                </a>
            </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="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
            @endif

            @if(session('error'))
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                {{ session('error') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
            @endif

            <div class="table-responsive">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Starts At</th>
                            <th>Ends At</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($flashSales as $flashSale)
                        <tr>
                            <td>{{ $flashSale->name }}</td>
                            <td>{{ $flashSale->starts_at?->format('M d, Y H:i') ?? 'N/A' }}</td>
                            <td>{{ $flashSale->ends_at?->format('M d, Y H:i') ?? 'N/A' }}</td>
                            <td>
                                @php
                                    $now = now();
                                    $isActive = $flashSale->is_active && $flashSale->starts_at <= $now && $flashSale->ends_at >= $now;
                                @endphp
                                <span class="badge bg-{{ $isActive ? 'success' : 'secondary' }}">
                                    {{ $isActive ? 'Active' : 'Inactive' }}
                                </span>
                            </td>
                            <td>
                                <a href="{{ route('admin.flash-sales.edit', $flashSale) }}" class="btn btn-sm btn-info">Edit</a>
                                <form action="{{ route('admin.flash-sales.destroy', $flashSale) }}" method="POST" class="d-inline">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')">Delete</button>
                                </form>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            {{ $flashSales->links('pagination::bootstrap-5') }}
        </div>
    </div>
</div>
@endsection
