@extends('layouts.admin')

@section('title', 'Coupons')

@section('content')
<div class="admin-page-header">
    <h1>Coupons</h1>
    @can('coupons.create')
    <a href="{{ route('admin.coupons.create') }}" class="admin-btn-primary">
        <i class="bi bi-plus"></i> Add Coupon
    </a>
    @endcan
</div>

<div class="admin-card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="admin-table table table-hover">
                <thead>
                    <tr>
                        <th>Code</th>
                        <th>Type</th>
                        <th>Value</th>
                        <th>Valid Period</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($coupons as $coupon)
                        <tr>
                            <td><strong>{{ $coupon->code }}</strong></td>
                            <td><span class="badge-modern badge-modern-info">{{ ucfirst($coupon->type) }}</span></td>
                            <td>
                                @if($coupon->type === 'percentage')
                                    {{ $coupon->value }}%
                                @else
                                    ₹{{ number_format($coupon->value, 2) }}
                                @endif
                            </td>
                            <td>
                                <small>{{ $coupon->starts_at->format('M d, Y') }} - {{ $coupon->ends_at->format('M d, Y') }}</small>
                            </td>
                            <td>
                                <span class="badge-modern {{ $coupon->is_active && $coupon->ends_at->isFuture() ? 'badge-modern-success' : 'badge-modern-danger' }}">
                                    {{ $coupon->is_active && $coupon->ends_at->isFuture() ? 'Active' : 'Inactive' }}
                                </span>
                            </td>
                            <td>
                                <div class="d-flex gap-2">
                                    @can('coupons.update')
                                    <a href="{{ route('admin.coupons.edit', $coupon->id) }}" class="admin-action-btn admin-action-btn-sm admin-btn-primary" title="Edit Coupon">
                                        <i class="bi bi-pencil"></i> 
                                    </a>
                                    @endcan
                                    @can('coupons.delete')
                                    <form action="{{ route('admin.coupons.destroy', $coupon->id) }}" method="POST" class="d-inline" onsubmit="return confirm('Are you sure?')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="admin-action-btn admin-action-btn-sm" style="background: #ef4444; height: -webkit-fill-available; color: white;" title="Delete Coupon">
                                            <i class="bi bi-trash"></i> 
                                        </button>
                                    </form>
                                    @endcan
                                </div>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>

        @if($coupons->count() > 0)
            <!-- Pagination - Right Aligned -->
            @if($coupons->hasPages())
            <div class="pagination-wrapper mt-4 m-4">
                <div class="d-flex justify-content-end align-items-center gap-3 flex-wrap">
                    <div class="pagination-info-text">
                        Showing <strong>{{ $coupons->firstItem() ?? 0 }}</strong>–<strong>{{ $coupons->lastItem() ?? 0 }}</strong> 
                        of <strong>{{ $coupons->total() }}</strong> coupons
                    </div>
                    <div class="pagination-controls">
                        {{ $coupons->links('vendor.pagination.admin-categories') }}
                    </div>
                </div>
            </div>
            @endif
        @else
            <div class="admin-empty-state">
                <i class="bi bi-ticket-perforated"></i>
                <h3>No Coupons Found</h3>
                @can('coupons.create')
                <p>Get started by creating your first coupon.</p>
                <a href="{{ route('admin.coupons.create') }}" class="admin-btn-primary">
                    <i class="bi bi-plus"></i> Add Coupon
                </a>
                @endcan
            </div>
        @endif
    </div>
</div>
@endsection
