@extends('admin.layout')

@section('title', 'Edit Voucher')

@section('content')
<div class="max-w-3xl mx-auto">
    <div class="flex items-center gap-3 mb-6">
        <a href="{{ route('admin.vouchers.index') }}" class="text-gray-400 hover:text-gray-600 transition">
            <i class="fa-solid fa-arrow-left"></i>
        </a>
        <div>
            <h1 class="text-2xl font-bold text-gray-800">Edit Voucher</h1>
            <p class="text-gray-500 text-sm">
                <code class="bg-indigo-50 text-indigo-700 px-2 py-0.5 rounded font-mono">{{ $voucher->code }}</code>
            </p>
        </div>
    </div>

    <form method="POST" action="{{ route('admin.vouchers.update', $voucher) }}" class="space-y-6">
        @csrf @method('PUT')

        {{-- Kode Voucher --}}
        <div class="bg-white rounded-xl shadow-sm p-6">
            <h2 class="font-semibold text-gray-700 mb-4 flex items-center gap-2">
                <i class="fa-solid fa-barcode text-indigo-500"></i> Kode Voucher
            </h2>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Kode Voucher <span class="text-red-500">*</span></label>
                <input type="text" name="code" value="{{ old('code', $voucher->code) }}" required
                       class="w-full border border-gray-300 rounded-lg px-3 py-2.5 text-sm focus:ring-2 focus:ring-indigo-400 outline-none uppercase font-mono"
                       style="text-transform:uppercase">
                @error('code')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
            </div>
        </div>

        {{-- Info Voucher --}}
        <div class="bg-white rounded-xl shadow-sm p-6">
            <h2 class="font-semibold text-gray-700 mb-4 flex items-center gap-2">
                <i class="fa-solid fa-circle-info text-indigo-500"></i> Informasi Voucher
            </h2>

            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                <div class="sm:col-span-2">
                    <label class="block text-sm font-medium text-gray-700 mb-1">Nama Voucher <span class="text-red-500">*</span></label>
                    <input type="text" name="name" value="{{ old('name', $voucher->name) }}" required
                           class="w-full border border-gray-300 rounded-lg px-3 py-2.5 text-sm focus:ring-2 focus:ring-indigo-400 outline-none">
                    @error('name')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
                </div>

                <div class="sm:col-span-2">
                    <label class="block text-sm font-medium text-gray-700 mb-1">Deskripsi</label>
                    <textarea name="description" rows="2"
                              class="w-full border border-gray-300 rounded-lg px-3 py-2.5 text-sm focus:ring-2 focus:ring-indigo-400 outline-none resize-none">{{ old('description', $voucher->description) }}</textarea>
                </div>
            </div>
        </div>

        {{-- Tipe Diskon --}}
        <div class="bg-white rounded-xl shadow-sm p-6">
            <h2 class="font-semibold text-gray-700 mb-4 flex items-center gap-2">
                <i class="fa-solid fa-percent text-indigo-500"></i> Tipe Diskon
            </h2>

            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Tipe Diskon <span class="text-red-500">*</span></label>
                    <select name="type" id="type-select" onchange="toggleMaxDiscount(this.value)"
                            class="w-full border border-gray-300 rounded-lg px-3 py-2.5 text-sm focus:ring-2 focus:ring-indigo-400 outline-none">
                        <option value="percentage" {{ old('type', $voucher->type) === 'percentage' ? 'selected' : '' }}>Persentase (%)</option>
                        <option value="fixed" {{ old('type', $voucher->type) === 'fixed' ? 'selected' : '' }}>Nominal Tetap (Rp)</option>
                    </select>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1" id="discount-label">
                        Nilai Diskon <span class="text-red-500">*</span>
                    </label>
                    <input type="number" name="discount_value" value="{{ old('discount_value', $voucher->discount_value) }}" required step="0.01" min="0.01"
                           class="w-full border border-gray-300 rounded-lg px-3 py-2.5 text-sm focus:ring-2 focus:ring-indigo-400 outline-none">
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Minimum Pembelian (Rp)</label>
                    <input type="number" name="min_purchase" value="{{ old('min_purchase', $voucher->min_purchase) }}" step="0.01" min="0"
                           class="w-full border border-gray-300 rounded-lg px-3 py-2.5 text-sm focus:ring-2 focus:ring-indigo-400 outline-none">
                </div>

                <div id="max-discount-field">
                    <label class="block text-sm font-medium text-gray-700 mb-1">Maks. Nominal Diskon (Rp)</label>
                    <input type="number" name="max_discount" value="{{ old('max_discount', $voucher->max_discount) }}" step="0.01" min="0"
                           class="w-full border border-gray-300 rounded-lg px-3 py-2.5 text-sm focus:ring-2 focus:ring-indigo-400 outline-none">
                </div>
            </div>
        </div>

        {{-- Batas & Validitas --}}
        <div class="bg-white rounded-xl shadow-sm p-6">
            <h2 class="font-semibold text-gray-700 mb-4 flex items-center gap-2">
                <i class="fa-solid fa-calendar text-indigo-500"></i> Batas & Validitas
            </h2>

            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Maks. Penggunaan</label>
                    <input type="number" name="max_uses" value="{{ old('max_uses', $voucher->max_uses) }}" min="1"
                           class="w-full border border-gray-300 rounded-lg px-3 py-2.5 text-sm focus:ring-2 focus:ring-indigo-400 outline-none">
                </div>

                <div class="flex items-end">
                    <label class="flex items-center gap-2 cursor-pointer pb-1">
                        <input type="checkbox" name="is_active" value="1"
                               class="w-4 h-4 rounded text-indigo-600"
                               {{ old('is_active', $voucher->is_active) ? 'checked' : '' }}>
                        <span class="text-sm text-gray-700">Aktifkan voucher</span>
                    </label>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Berlaku Mulai</label>
                    <input type="datetime-local" name="valid_from"
                           value="{{ old('valid_from', $voucher->valid_from?->format('Y-m-d\TH:i')) }}"
                           class="w-full border border-gray-300 rounded-lg px-3 py-2.5 text-sm focus:ring-2 focus:ring-indigo-400 outline-none">
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Berlaku Sampai</label>
                    <input type="datetime-local" name="valid_until"
                           value="{{ old('valid_until', $voucher->valid_until?->format('Y-m-d\TH:i')) }}"
                           class="w-full border border-gray-300 rounded-lg px-3 py-2.5 text-sm focus:ring-2 focus:ring-indigo-400 outline-none">
                </div>
            </div>
        </div>

        <div class="flex justify-end gap-3">
            <a href="{{ route('admin.vouchers.index') }}"
               class="border border-gray-300 text-gray-600 px-6 py-2.5 rounded-lg hover:bg-gray-50 transition text-sm font-medium">
                Batal
            </a>
            <button type="submit"
                    class="bg-indigo-600 hover:bg-indigo-700 text-white px-6 py-2.5 rounded-lg transition text-sm font-medium">
                <i class="fa-solid fa-save mr-1"></i> Simpan Perubahan
            </button>
        </div>
    </form>
</div>

<script>
function toggleMaxDiscount(type) {
    const field = document.getElementById('max-discount-field');
    if (type === 'fixed') {
        field.classList.add('hidden');
    } else {
        field.classList.remove('hidden');
    }
}
toggleMaxDiscount(document.getElementById('type-select').value);
</script>
@endsection
