@extends('layouts.admin_layout')

@section('content')

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

        <x-alert/>

        <div class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800">
            <!-- Coupon Info -->
            <div class="p-4 mb-6 bg-gray-100 rounded-lg dark:bg-gray-700">
                <h3 class="mb-2 text-lg font-semibold text-gray-700 dark:text-gray-200">Coupon: {{ $coupon->code }}</h3>
                <div class="grid grid-cols-2 gap-4 text-sm">
                    <div>
                        <span class="text-gray-600 dark:text-gray-400">Type:</span>
                        <span class="ml-2 font-semibold">{{ ucfirst($coupon->type) }}</span>
                    </div>
                    <div>
                        <span class="text-gray-600 dark:text-gray-400">Value:</span>
                        @if($coupon->type === 'balance')
                            <span class="ml-2 font-semibold">USDT {{ number_format($coupon->amount, 2) }}</span>
                        @else
                            <span class="ml-2 font-semibold">{{ $coupon->product->name ?? 'N/A' }} x{{ $coupon->quantity }}</span>
                        @endif
                    </div>
                    <div>
                        <span class="text-gray-600 dark:text-gray-400">Created:</span>
                        <span class="ml-2">{{ $coupon->created_at->format('M j, Y') }}</span>
                    </div>
                    <div>
                        <span class="text-gray-600 dark:text-gray-400">Uses:</span>
                        <span class="ml-2">{{ $coupon->used_count }} / {{ $coupon->max_uses }}</span>
                    </div>
                </div>
            </div>

            <form method="POST" action="{{ route('admin.coupons.update', $coupon) }}">
                @csrf
                @method('PUT')

                <!-- Description -->
                <label class="block mt-4 text-sm">
                    <span class="text-gray-700 dark:text-gray-400">Description</span>
                    <textarea
                        name="description"
                        rows="3"
                        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-textarea"
                    >{{ old('description', $coupon->description) }}</textarea>
                    @error('description')
                        <span class="text-xs text-red-600 dark:text-red-400">{{ $message }}</span>
                    @enderror
                </label>

                <!-- Expiration Date -->
                <label class="block mt-4 text-sm">
                    <span class="text-gray-700 dark:text-gray-400">Expiration Date</span>
                    <input
                        type="datetime-local"
                        name="expires_at"
                        value="{{ $coupon->expires_at ? $coupon->expires_at->format('Y-m-d\TH:i') : '' }}"
                        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"
                    />
                    @error('expires_at')
                        <span class="text-xs text-red-600 dark:text-red-400">{{ $message }}</span>
                    @enderror
                </label>

                <!-- Max Uses -->
                <label class="block mt-4 text-sm">
                    <span class="text-gray-700 dark:text-gray-400">Maximum Uses *</span>
                    <input
                        type="number"
                        name="max_uses"
                        min="{{ $coupon->used_count }}"
                        value="{{ old('max_uses', $coupon->max_uses) }}"
                        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"
                        required
                    />
                    <span class="text-xs text-gray-600 dark:text-gray-400">
                        Must be at least {{ $coupon->used_count }} (current uses)
                    </span>
                    @error('max_uses')
                        <span class="text-xs text-red-600 dark:text-red-400">{{ $message }}</span>
                    @enderror
                </label>

                <!-- Active Status -->
                <label class="block mt-4 text-sm">
                    <span class="text-gray-700 dark:text-gray-400">Status</span>
                    <div class="mt-2">
                        <label class="inline-flex items-center">
                            <input
                                type="radio"
                                name="is_active"
                                value="1"
                                {{ old('is_active', $coupon->is_active) == 1 ? 'checked' : '' }}
                                class="text-blue-600 form-radio focus:border-blue-400 focus:outline-none dark:focus:shadow-outline-gray"
                            >
                            <span class="ml-2 text-gray-700 dark:text-gray-400">Active</span>
                        </label>
                        <label class="inline-flex items-center ml-6">
                            <input
                                type="radio"
                                name="is_active"
                                value="0"
                                {{ old('is_active', $coupon->is_active) == 0 ? 'checked' : '' }}
                                class="text-blue-600 form-radio focus:border-blue-400 focus:outline-none dark:focus:shadow-outline-gray"
                            >
                            <span class="ml-2 text-gray-700 dark:text-gray-400">Inactive</span>
                        </label>
                    </div>
                    @error('is_active')
                        <span class="text-xs text-red-600 dark:text-red-400">{{ $message }}</span>
                    @enderror
                </label>

                <!-- Submit Buttons -->
                <div class="flex mt-6 space-x-2">
                    <button
                        type="submit"
                        class="px-5 py-3 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;"
                    >
                        Update Coupon
                    </button>
                    
                    <a href="{{ route('admin.coupons') }}"
                        class="px-5 py-3 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"
                    >
                        Cancel
                    </a>
                </div>
            </form>
        </div>
    </div>
</main>

@endsection