@extends('layouts.admin_layout')

@section('content')

<main class="h-full pb-16 overflow-y-auto">
    <div class="container grid px-6 mx-auto">
        <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
            <span class="text-blue-500">Coupon</span> Management
        </h2>

        <a class="flex items-center justify-between p-4 mb-8 text-sm font-semibold bg-blue-600 shadow-md focus:outline-none"
            href="{{ url('/') }}" style="color: #fff;">
            <div class="flex items-center">
                <svg class="w-5 h-5 mr-2" fill="currentColor" viewBox="0 0 20 20">
                    <path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"></path>
                </svg>
                <span>Go to main website</span>
            </div>
            <span>View &RightArrow;</span>
        </a>

        <x-alert/>

        <!-- Create Button -->
        <div class="mb-6">
            <a href="{{ route('admin.coupons.create') }}"
                class="px-5 py-3 font-medium leading-5 text-white transition-colors duration-150 bg-green-600 border border-transparent hover:bg-green-700 focus:outline-none"
                style="color: #fff; display: inline-block;">
                Create New Coupon
            </a>
        </div>

        <!-- Filters -->
        <div class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800">
            <form method="GET" action="{{ route('admin.coupons') }}" class="grid grid-cols-1 gap-4 md:grid-cols-4">
                
                <!-- Search -->
                <div>
                    <label class="block text-sm">
                        <span class="text-gray-700 dark:text-gray-400">Search Code</span>
                        <input
                            type="text"
                            name="search"
                            value="{{ request('search') }}"
                            placeholder="Search by code..."
                            class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:border-blue-400 focus:outline-none dark:text-gray-300 form-input"
                        />
                    </label>
                </div>

                <!-- Type Filter -->
                <div>
                    <label class="block text-sm">
                        <span class="text-gray-700 dark:text-gray-400">Type</span>
                        <select
                            name="type"
                            class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:border-blue-400 focus:outline-none dark:text-gray-300 form-select"
                        >
                            <option value="all" {{ request('type') == 'all' ? 'selected' : '' }}>All Types</option>
                            <option value="balance" {{ request('type') == 'balance' ? 'selected' : '' }}>Balance</option>
                            <option value="product" {{ request('type') == 'product' ? 'selected' : '' }}>Product</option>
                        </select>
                    </label>
                </div>

                <!-- Status Filter -->
                <div>
                    <label class="block text-sm">
                        <span class="text-gray-700 dark:text-gray-400">Status</span>
                        <select
                            name="status"
                            class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:border-blue-400 focus:outline-none dark:text-gray-300 form-select"
                        >
                            <option value="">All Status</option>
                            <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>Active</option>
                            <option value="expired" {{ request('status') == 'expired' ? 'selected' : '' }}>Expired</option>
                            <option value="inactive" {{ request('status') == 'inactive' ? 'selected' : '' }}>Inactive</option>
                            <option value="maxed" {{ request('status') == 'maxed' ? 'selected' : '' }}>Max Uses Reached</option>
                        </select>
                    </label>
                </div>

                <!-- Submit -->
                <div class="flex items-end">
                    <button type="submit"
                        class="px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-blue-600 border border-transparent hover:bg-blue-700 focus:outline-none"
                        style="color: #fff;">
                        Filter
                    </button>
                    <a href="{{ route('admin.coupons') }}"
                        class="px-4 py-2 ml-2 text-sm font-medium leading-5 text-gray-700 transition-colors duration-150 bg-white border border-gray-300 hover:bg-gray-100 focus:outline-none dark:bg-gray-800 dark:text-gray-300">
                        Clear
                    </a>
                </div>
            </form>
        </div>

        <!-- Coupons Table -->
        <div class="w-full overflow-hidden rounded-lg shadow-xs">
            <div class="w-full overflow-x-auto">
                <table class="w-full whitespace-no-wrap">
                    <thead>
                        <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800">
                            <th class="px-4 py-3">Code</th>
                            <th class="px-4 py-3">Type</th>
                            <th class="px-4 py-3">Details</th>
                            <th class="px-4 py-3">Usage</th>
                            <th class="px-4 py-3">Expires</th>
                            <th class="px-4 py-3">Status</th>
                            <th class="px-4 py-3">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
                        @forelse($coupons as $coupon)
                            <tr class="text-gray-700 dark:text-gray-400">
                                <td class="px-4 py-3">
                                    <div class="flex flex-col">
                                        <p class="font-semibold">{{ $coupon->code }}</p>
                                        @if($coupon->description)
                                            <p class="text-xs text-gray-600 dark:text-gray-400">{{ Str::limit($coupon->description, 30) }}</p>
                                        @endif
                                    </div>
                                </td>
                                <td class="px-4 py-3 text-sm">
                                    @if($coupon->type === 'balance')
                                        <span class="px-2 py-1 font-semibold leading-tight text-green-700 bg-green-100 rounded-full dark:bg-green-700 dark:text-green-100">
                                            Balance
                                        </span>
                                    @else
                                        <span class="px-2 py-1 font-semibold leading-tight text-blue-700 bg-blue-100 rounded-full dark:bg-blue-700 dark:text-blue-100" style="color: #473d8d;">
                                            Product
                                        </span>
                                    @endif
                                </td>
                                <td class="px-4 py-3 text-sm">
                                    @if($coupon->type === 'balance')
                                        <span class="font-semibold">USDT {{ number_format($coupon->amount, 2) }}</span>
                                    @else
                                        <div class="flex items-center">
                                            @if($coupon->product)
                                                <img src="{{ asset('storage/products/'.$coupon->product->image1) }}" 
                                                     class="w-8 h-8 mr-2 rounded object-cover" 
                                                     alt="{{ $coupon->product->name }}">
                                                <div>
                                                    <p class="font-semibold">{{ $coupon->product->name }}</p>
                                                    <p class="text-xs text-gray-600 dark:text-gray-400">Qty: {{ $coupon->quantity }}</p>
                                                </div>
                                            @else
                                                <span class="text-red-600">Product Deleted</span>
                                            @endif
                                        </div>
                                    @endif
                                </td>
                                <td class="px-4 py-3 text-sm">
                                    <div class="flex flex-col">
                                        <span class="font-semibold">{{ $coupon->used_count }} / {{ $coupon->max_uses }}</span>
                                        <a href="{{ route('admin.coupons.redemptions', $coupon) }}" 
                                           class="text-xs text-blue-600 hover:underline">
                                            View Redemptions
                                        </a>
                                    </div>
                                </td>
                                <td class="px-4 py-3 text-xs">
                                    @if($coupon->expires_at)
                                        @if($coupon->isExpired())
                                            <span class="text-red-600">{{ $coupon->expires_at->format('M j, Y') }}</span>
                                        @else
                                            <span>{{ $coupon->expires_at->format('M j, Y') }}</span>
                                        @endif
                                    @else
                                        <span class="text-gray-500">Never</span>
                                    @endif
                                </td>
                                
                                <td class="px-4 py-3 text-xs">
                                    @if(!$coupon->is_active)
                                        <span
                                            class="px-2 py-1 font-semibold leading-tight rounded-full"
                                            style="color:#374151; background-color:#F3F4F6;"
                                        >
                                            Inactive
                                        </span>
                                    @elseif($coupon->isExpired())
                                        <span
                                            class="px-2 py-1 font-semibold leading-tight rounded-full"
                                            style="color:#B91C1C; background-color:#FEE2E2;"
                                        >
                                            Expired
                                        </span>
                                    @elseif($coupon->isMaxUsesReached())
                                        <span
                                            class="px-2 py-1 font-semibold leading-tight rounded-full"
                                            style="color:#C2410C; background-color:#FFEDD5;"
                                        >
                                            Max Uses
                                        </span>
                                    @else
                                        <span
                                            class="px-2 py-1 font-semibold leading-tight rounded-full"
                                            style="color:#15803D; background-color:#DCFCE7;"
                                        >
                                            Active
                                        </span>
                                    @endif
                                </td>

                                <td class="px-4 py-3">
                                    <div class="flex items-center space-x-2">
                                        <a href="{{ route('admin.coupons.edit', $coupon) }}"
                                            class="flex items-center justify-center px-2 py-2 text-sm font-medium leading-5 text-blue-600 rounded-lg hover:bg-blue-100 dark:text-blue-400 dark:hover:bg-blue-900"
                                            title="Edit">
                                            <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
                                                <path d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z"></path>
                                            </svg>
                                        </a>
                                        <form method="POST" action="{{ route('admin.coupons.destroy', $coupon) }}" style="display: inline;">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit"
                                                class="flex items-center justify-center px-2 py-2 text-sm font-medium leading-5 text-red-600 rounded-lg hover:bg-red-100 dark:text-red-400 dark:hover:bg-red-900"
                                                title="Delete"
                                                onclick="return confirm('Are you sure you want to delete this coupon?');">
                                                <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
                                                    <path fill-rule="evenodd" d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z" clip-rule="evenodd"></path>
                                                </svg>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
                                    <p class="text-lg font-semibold">No coupons found</p>
                                    <p class="mt-2 text-sm">Create your first coupon to get started</p>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            @if($coupons->hasPages())
                <div class="px-4 py-3 border-t dark:border-gray-700 bg-gray-50 dark:bg-gray-900">
                    {{ $coupons->links() }}
                </div>
            @endif
        </div>
    </div>
</main>

@endsection