@extends('admin.layouts.app')

@section('title', 'Edit Coupon')
@section('page-title', 'Edit Coupon: ' . $coupon->code)

@section('content')
<div class="bg-white rounded-lg shadow p-6 max-w-3xl">
    <form method="POST" action="{{ route('admin.coupons.update', $coupon) }}">
        @csrf
        @method('PUT')

        <div class="space-y-6">
            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label for="code" class="block text-sm font-medium text-gray-700">Coupon Code *</label>
                    <input type="text" name="code" id="code" value="{{ old('code', $coupon->code) }}" required
                           class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 font-mono">
                    @error('code')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                </div>

                <div>
                    <label for="name" class="block text-sm font-medium text-gray-700">Coupon Name *</label>
                    <input type="text" name="name" id="name" value="{{ old('name', $coupon->name) }}" required
                           class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                </div>
            </div>

            <div>
                <label for="description" class="block text-sm font-medium text-gray-700">Description</label>
                <textarea name="description" id="description" rows="2"
                          class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">{{ old('description', $coupon->description) }}</textarea>
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label for="type" class="block text-sm font-medium text-gray-700">Discount Type *</label>
                    <select name="type" id="type" required
                            class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                        <option value="percentage" {{ old('type', $coupon->type) == 'percentage' ? 'selected' : '' }}>Percentage</option>
                        <option value="fixed" {{ old('type', $coupon->type) == 'fixed' ? 'selected' : '' }}>Fixed Amount</option>
                    </select>
                </div>

                <div>
                    <label for="discount_value" class="block text-sm font-medium text-gray-700">Discount Value *</label>
                    <input type="number" step="0.01" name="discount_value" id="discount_value" value="{{ old('discount_value', $coupon->discount_value) }}" required
                           class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                </div>
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label for="minimum_order_amount" class="block text-sm font-medium text-gray-700">Minimum Order Amount</label>
                    <input type="number" step="0.01" name="minimum_order_amount" id="minimum_order_amount" value="{{ old('minimum_order_amount', $coupon->minimum_order_amount) }}"
                           class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                </div>

                <div>
                    <label for="maximum_discount" class="block text-sm font-medium text-gray-700">Maximum Discount</label>
                    <input type="number" step="0.01" name="maximum_discount" id="maximum_discount" value="{{ old('maximum_discount', $coupon->maximum_discount) }}"
                           class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                </div>
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label for="usage_limit" class="block text-sm font-medium text-gray-700">Usage Limit</label>
                    <input type="number" name="usage_limit" id="usage_limit" value="{{ old('usage_limit', $coupon->usage_limit) }}"
                           class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                    <p class="mt-1 text-sm text-gray-500">Used: {{ $coupon->used_count }}</p>
                </div>

                <div class="flex items-center pt-6">
                    <input type="checkbox" name="is_active" id="is_active" value="1" {{ old('is_active', $coupon->is_active) ? 'checked' : '' }}
                           class="h-4 w-4 text-blue-600">
                    <label for="is_active" class="ml-2 block text-sm text-gray-900">Active</label>
                </div>
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label for="valid_from" class="block text-sm font-medium text-gray-700">Valid From *</label>
                    <input type="date" name="valid_from" id="valid_from" value="{{ old('valid_from', $coupon->valid_from->format('Y-m-d')) }}" required
                           class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                </div>

                <div>
                    <label for="valid_until" class="block text-sm font-medium text-gray-700">Valid Until *</label>
                    <input type="date" name="valid_until" id="valid_until" value="{{ old('valid_until', $coupon->valid_until->format('Y-m-d')) }}" required
                           class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3">
                </div>
            </div>

            <div class="flex justify-end space-x-4">
                <a href="{{ route('admin.coupons.index') }}" class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded">
                    Cancel
                </a>
                <button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
                    Update Coupon
                </button>
            </div>
        </div>
    </form>
</div>
@endsection
