<x-app-layout>
    <x-slot name="header">
        <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 gradient-primary rounded-xl flex items-center justify-center shadow-lg">
                    <i class="fas fa-truck text-white text-sm"></i>
                </div>
                <div>
                    <h1 class="text-xl font-bold text-stone-800">Pengaturan Pengiriman</h1>
                    <p class="text-xs text-stone-500">Kelola zona, kurir, dan gratis ongkir</p>
                </div>
            </div>
        </div>
    </x-slot>

    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6 space-y-6">


        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
            {{-- General Settings Card --}}
            <div class="bg-white rounded-xl shadow-sm border border-stone-200 overflow-hidden hover:shadow-md transition-shadow duration-300">
                <div class="px-6 py-4 border-b border-stone-100 bg-gradient-to-r from-stone-50 to-white">
                    <div class="flex items-center gap-2">
                        <div class="w-8 h-8 bg-amber-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-cog text-amber-600 text-sm"></i>
                        </div>
                        <h3 class="font-semibold text-stone-800">Pengaturan Umum</h3>
                    </div>
                </div>

                <div class="p-6">
                    <form action="{{ route('admin.shipping.settings.save') }}" method="POST" class="space-y-5">
                        @csrf

                        {{-- Free Shipping --}}
                        <div class="bg-gradient-to-br from-emerald-50 to-green-50 rounded-xl p-5 border border-emerald-200">
                            <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3 mb-4">
                                <div>
                                    <p class="text-sm font-semibold text-stone-800">Gratis Ongkos Kirim</p>
                                    <p class="text-xs text-stone-500 mt-0.5">Otomatis ongkir = Rp 0 jika subtotal memenuhi</p>
                                </div>
                                <label class="relative inline-flex items-center cursor-pointer">
                                    <input type="checkbox" name="free_shipping_enabled" value="1"
                                        {{ ($settings['free_shipping_enabled']->value ?? '1') == '1' ? 'checked' : '' }}
                                        class="sr-only peer">
                                    <div class="w-11 h-6 bg-stone-300 peer-focus:outline-none rounded-full peer 
                                                peer-checked:after:translate-x-full peer-checked:bg-emerald-500
                                                after:content-[''] after:absolute after:top-[2px] after:left-[2px]
                                                after:bg-white after:rounded-full after:h-5 after:w-5 after:transition-all">
                                    </div>
                                </label>
                            </div>

                            <div>
                                <label class="block text-xs font-medium text-stone-700 mb-2">
                                    Minimum belanja gratis ongkir
                                </label>
                                <div class="relative">
                                    <span class="absolute left-3 top-1/2 -translate-y-1/2 text-stone-400 text-sm">Rp</span>
                                    <input type="number" name="free_shipping_minimum"
                                        value="{{ $settings['free_shipping_minimum']->value ?? 500000 }}"
                                        min="0" step="10000"
                                        class="w-full pl-10 pr-3 py-2.5 text-sm border border-stone-200 rounded-lg bg-white focus:ring-2 focus:ring-amber-500 focus:border-transparent transition">
                                </div>
                            </div>
                        </div>

                        {{-- Default Weight --}}
                        <div>
                            <label class="block text-xs font-medium text-stone-700 mb-2">
                                Berat default produk
                                <span class="text-stone-400 font-normal ml-1">— dipakai jika produk tidak punya berat</span>
                            </label>
                            <div class="relative">
                                <input type="number" name="default_product_weight"
                                    value="{{ $settings['default_product_weight']->value ?? 1000 }}"
                                    min="100" step="100"
                                    class="w-full pr-16 pl-3 py-2.5 text-sm border border-stone-200 rounded-lg bg-white focus:ring-2 focus:ring-amber-500 focus:border-transparent transition">
                                <span class="absolute right-3 top-1/2 -translate-y-1/2 text-stone-400 text-sm">gram</span>
                            </div>
                        </div>

                        {{-- Tracking Prefix --}}
                        <div>
                            <label class="block text-xs font-medium text-stone-700 mb-2">
                                Prefix nomor resi otomatis
                                <span class="text-stone-400 font-normal ml-1">— maks. 10 karakter huruf/angka</span>
                            </label>
                            <div class="flex flex-col sm:flex-row gap-2 items-start sm:items-center">
                                <input type="text" name="tracking_prefix"
                                    value="{{ $settings['tracking_prefix']->value ?? 'FRN' }}"
                                    maxlength="10" pattern="[A-Za-z0-9]+"
                                    class="flex-1 px-3 py-2.5 text-sm border border-stone-200 rounded-lg bg-white focus:ring-2 focus:ring-amber-500 font-mono uppercase transition"
                                    oninput="this.value = this.value.toUpperCase().replace(/[^A-Z0-9]/g,'')">
                                <span class="text-xs text-stone-400">+ YYYYMM + 8 karakter acak</span>
                            </div>
                        </div>

                        <button type="submit"
                            class="w-full py-3 btn-primary text-white text-sm font-semibold rounded-lg shadow-md hover:shadow-lg transition-all duration-200">
                            <i class="fas fa-save mr-2"></i>Simpan Pengaturan
                        </button>
                    </form>
                </div>
            </div>

            {{-- Couriers Card --}}
            <div class="bg-white rounded-xl shadow-sm border border-stone-200 overflow-hidden hover:shadow-md transition-shadow duration-300">
                <div class="px-6 py-4 border-b border-stone-100 bg-gradient-to-r from-stone-50 to-white">
                    <div class="flex items-center gap-2">
                        <div class="w-8 h-8 bg-amber-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-shipping-fast text-amber-600 text-sm"></i>
                        </div>
                        <h3 class="font-semibold text-stone-800">Layanan Kurir Aktif</h3>
                    </div>
                </div>

                <div class="divide-y divide-stone-100">
                    @foreach($couriers as $courier)
                    <div class="px-6 py-4 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3 hover:bg-stone-50 transition">
                        <div class="flex items-center gap-3">
                            <div class="w-10 h-10 bg-amber-50 rounded-xl flex items-center justify-center flex-shrink-0">
                                <i class="fas fa-truck text-amber-600 text-sm"></i>
                            </div>
                            <div>
                                <p class="text-sm font-semibold text-stone-800">{{ $courier['short_name'] }}</p>
                                <p class="text-xs text-stone-400">{{ $courier['etd'] }}</p>
                            </div>
                        </div>
                        <div class="text-left sm:text-right flex items-center justify-end gap-4">
                            <div>
                                <span class="text-xs text-stone-500">Multiplier</span>
                                <p class="text-lg font-bold text-amber-600">×{{ number_format($courier['price_multiplier'], 1) }}</p>
                            </div>
                            <button type="button" onclick="openEditCourier('{{ $courier['code'] }}', {{ json_encode($courier) }})"
                                class="inline-flex items-center justify-center w-8 h-8 bg-amber-50 hover:bg-amber-100 border border-amber-200 text-amber-700 rounded-lg transition duration-200">
                                <i class="fas fa-edit"></i>
                            </button>
                        </div>
                    </div>
                    @endforeach

                    <div class="px-6 py-4 bg-stone-50">
                        <p class="text-xs text-stone-400 text-center flex items-center justify-center gap-1">
                            <i class="fas fa-info-circle text-amber-400"></i>
                            Gunakan tombol edit di atas untuk merubah multiplier, ETD, atau nama kurir
                        </p>
                    </div>
                </div>
            </div>
        </div>

        {{-- Shipping Zones Card --}}
        <div class="bg-white rounded-xl shadow-sm border border-stone-200 overflow-hidden hover:shadow-md transition-shadow duration-300">
            <div class="px-6 py-4 border-b border-stone-100 bg-gradient-to-r from-stone-50 to-white">
                <div class="flex items-center gap-2">
                    <div class="w-8 h-8 bg-amber-100 rounded-lg flex items-center justify-center">
                        <i class="fas fa-map-marked-alt text-amber-600 text-sm"></i>
                    </div>
                    <h3 class="font-semibold text-stone-800">Zona Pengiriman</h3>
                </div>
            </div>

            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-stone-200">
                    <thead class="bg-stone-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-semibold text-stone-600 uppercase tracking-wider">Zona</th>
                            <th class="px-6 py-3 text-left text-xs font-semibold text-stone-600 uppercase tracking-wider">Harga/kg</th>
                            <th class="px-6 py-3 text-left text-xs font-semibold text-stone-600 uppercase tracking-wider">Min. tagih</th>
                            <th class="px-6 py-3 text-left text-xs font-semibold text-stone-600 uppercase tracking-wider">Pembulatan</th>
                            <th class="px-6 py-3 text-center text-xs font-semibold text-stone-600 uppercase tracking-wider">Status</th>
                            <th class="px-6 py-3 text-center text-xs font-semibold text-stone-600 uppercase tracking-wider">Aksi</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-stone-100 bg-white">
                        @forelse($zones as $zone)
                        <tr class="hover:bg-stone-50 transition duration-150">
                            <td class="px-6 py-4">
                                <p class="text-sm font-semibold text-stone-800">{{ $zone->zone_name }}</p>
                                <p class="text-xs text-stone-400 mt-0.5">{{ $zone->zone_code }}</p>
                                @if($zone->description)
                                <p class="text-xs text-stone-400 mt-1 truncate max-w-xs">{{ $zone->description }}</p>
                                @endif
                            </td>
                            <td class="px-6 py-4">
                                <span class="text-sm font-bold text-amber-600">
                                    Rp {{ number_format($zone->base_price_per_kg, 0, ',', '.') }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-sm text-stone-600">
                                {{ number_format($zone->min_charge_weight / 1000, 1) }} kg
                            </td>
                            <td class="px-6 py-4 text-sm text-stone-600">
                                {{ number_format($zone->weight_rounding / 1000, 1) }} kg
                            </td>
                            <td class="px-6 py-4 text-center">
                                <span class="inline-flex px-2.5 py-1 text-xs font-semibold rounded-full
                                    {{ $zone->is_active ? 'bg-emerald-100 text-emerald-700 border border-emerald-200' : 'bg-stone-100 text-stone-600 border border-stone-200' }}">
                                    {{ $zone->is_active ? 'Aktif' : 'Nonaktif' }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-center">
                                <button onclick="openEditZone({{ $zone->id }}, {{ json_encode($zone) }})"
                                    class="inline-flex items-center gap-1 px-3 py-1.5 bg-amber-50 hover:bg-amber-100 border border-amber-200 text-amber-700 text-xs font-semibold rounded-lg transition duration-200">
                                    <i class="fas fa-edit text-xs"></i>
                                    <span class="hidden sm:inline">Edit</span>
                                </button>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="px-6 py-8 text-center text-stone-500">
                                <i class="fas fa-map-marker-alt text-3xl mb-2 text-stone-300"></i>
                                <p>Belum ada zona pengiriman</p>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    {{-- Edit Zone Modal --}}
    <div id="edit-zone-modal" class="fixed inset-0 bg-black/50 z-50 hidden items-center justify-center p-4">
        <div class="bg-white rounded-xl shadow-2xl w-full max-w-md max-h-[90vh] overflow-y-auto animate-fade-in-up">
            <div class="sticky top-0 bg-white border-b border-stone-100 px-6 py-4 flex items-center justify-between">
                <h3 class="font-bold text-stone-800">Edit Zona Pengiriman</h3>
                <button onclick="closeEditZone()" class="w-8 h-8 rounded-lg hover:bg-stone-100 flex items-center justify-center transition">
                    <i class="fas fa-times text-stone-500"></i>
                </button>
            </div>

            <form id="edit-zone-form" method="POST" class="p-6 space-y-5">
                @csrf
                @method('PUT')

                <div>
                    <label class="block text-xs font-semibold text-stone-700 mb-2">Nama Zona</label>
                    <input type="text" name="zone_name" id="edit-zone-name" required
                        class="w-full px-3 py-2.5 text-sm border border-stone-200 rounded-lg bg-white focus:ring-2 focus:ring-amber-500 focus:border-transparent transition">
                </div>

                <div>
                    <label class="block text-xs font-semibold text-stone-700 mb-2">Deskripsi</label>
                    <input type="text" name="description" id="edit-zone-desc"
                        class="w-full px-3 py-2.5 text-sm border border-stone-200 rounded-lg bg-white focus:ring-2 focus:ring-amber-500 focus:border-transparent transition">
                </div>

                <div>
                    <label class="block text-xs font-semibold text-stone-700 mb-2">Harga dasar per kg (Rp)</label>
                    <input type="number" name="base_price_per_kg" id="edit-zone-price" min="1000" step="500" required
                        class="w-full px-3 py-2.5 text-sm border border-stone-200 rounded-lg bg-white focus:ring-2 focus:ring-amber-500 focus:border-transparent transition">
                </div>

                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-xs font-semibold text-stone-700 mb-2">Min. tagih (gram)</label>
                        <input type="number" name="min_charge_weight" id="edit-zone-minweight" min="100" step="100" required
                            class="w-full px-3 py-2.5 text-sm border border-stone-200 rounded-lg bg-white focus:ring-2 focus:ring-amber-500 focus:border-transparent transition">
                    </div>
                    <div>
                        <label class="block text-xs font-semibold text-stone-700 mb-2">Pembulatan (gram)</label>
                        <input type="number" name="weight_rounding" id="edit-zone-rounding" min="100" step="100" required
                            class="w-full px-3 py-2.5 text-sm border border-stone-200 rounded-lg bg-white focus:ring-2 focus:ring-amber-500 focus:border-transparent transition">
                    </div>
                </div>

                <div class="flex items-center gap-3 pt-2">
                    <input type="checkbox" name="is_active" value="1" id="edit-zone-active"
                        class="w-4 h-4 rounded border-stone-300 text-amber-600 focus:ring-amber-500">
                    <label for="edit-zone-active" class="text-sm font-medium text-stone-700">Zona aktif</label>
                </div>

                <div class="flex gap-3 pt-4">
                    <button type="button" onclick="closeEditZone()"
                        class="flex-1 py-2.5 bg-stone-100 text-stone-700 text-sm font-medium rounded-lg hover:bg-stone-200 transition border border-stone-200">
                        Batal
                    </button>
                    <button type="submit"
                        class="flex-1 py-2.5 btn-primary text-white text-sm font-semibold rounded-lg shadow-md transition">
                        <i class="fas fa-save mr-1"></i>Simpan
                    </button>
                </div>
            </form>
        </div>
    </div>

    {{-- Edit Courier Modal --}}
    <div id="edit-courier-modal" class="fixed inset-0 bg-black/50 z-50 hidden items-center justify-center p-4">
        <div class="bg-white rounded-xl shadow-2xl w-full max-w-md max-h-[90vh] overflow-y-auto animate-fade-in-up">
            <div class="sticky top-0 bg-white border-b border-stone-100 px-6 py-4 flex items-center justify-between z-10">
                <h3 class="font-bold text-stone-800">Edit Layanan Kurir</h3>
                <button onclick="closeEditCourier()" class="w-8 h-8 rounded-lg hover:bg-stone-100 flex items-center justify-center transition">
                    <i class="fas fa-times text-stone-500"></i>
                </button>
            </div>

            <form id="edit-courier-form" method="POST" class="p-6 space-y-5">
                @csrf
                @method('PUT')

                <div>
                    <label class="block text-xs font-semibold text-stone-700 mb-2">Nama Singkat Kurir</label>
                    <input type="text" name="short_name" id="edit-courier-name" required
                        class="w-full px-3 py-2.5 text-sm border border-stone-200 rounded-lg bg-white focus:ring-2 focus:ring-amber-500 focus:border-transparent transition">
                </div>

                <div>
                    <label class="block text-xs font-semibold text-stone-700 mb-2">Estimasi (ETD)</label>
                    <input type="text" name="etd" id="edit-courier-etd" required
                        class="w-full px-3 py-2.5 text-sm border border-stone-200 rounded-lg bg-white focus:ring-2 focus:ring-amber-500 focus:border-transparent transition"
                        placeholder="Contoh: 1-2 hari kerja">
                </div>

                <div>
                    <label class="block text-xs font-semibold text-stone-700 mb-2">Price Multiplier (Pengali Harga)</label>
                    <input type="number" name="price_multiplier" id="edit-courier-multiplier" min="0.1" max="10" step="0.01" required
                        class="w-full px-3 py-2.5 text-sm border border-stone-200 rounded-lg bg-white focus:ring-2 focus:ring-amber-500 focus:border-transparent transition">
                    <p class="text-xs text-stone-400 mt-1">Misal: 1 = harga zona normal, 2 = harga 2x lipat (YES/BEST).</p>
                </div>

                <div class="flex items-center gap-3 pt-2">
                    <input type="checkbox" name="is_active" value="1" id="edit-courier-active"
                        class="w-4 h-4 rounded border-stone-300 text-amber-600 focus:ring-amber-500">
                    <label for="edit-courier-active" class="text-sm font-medium text-stone-700">Kurir aktif</label>
                </div>

                <div class="flex gap-3 pt-4">
                    <button type="button" onclick="closeEditCourier()"
                        class="flex-1 py-2.5 bg-stone-100 text-stone-700 text-sm font-medium rounded-lg hover:bg-stone-200 transition border border-stone-200">
                        Batal
                    </button>
                    <button type="submit"
                        class="flex-1 py-2.5 btn-primary text-white text-sm font-semibold rounded-lg shadow-md transition">
                        <i class="fas fa-save mr-1"></i>Simpan
                    </button>
                </div>
            </form>
        </div>
    </div>

    <style>
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }

            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .animate-fade-in-up {
            animation: fadeInUp 0.3s ease-out;
        }
    </style>

    <script>
        function openEditZone(id, data) {
            document.getElementById('edit-zone-form').action = '/admin/shipping/zones/' + id;
            document.getElementById('edit-zone-name').value = data.zone_name;
            document.getElementById('edit-zone-desc').value = data.description || '';
            document.getElementById('edit-zone-price').value = data.base_price_per_kg;
            document.getElementById('edit-zone-minweight').value = data.min_charge_weight;
            document.getElementById('edit-zone-rounding').value = data.weight_rounding;
            document.getElementById('edit-zone-active').checked = !!data.is_active;

            const modal = document.getElementById('edit-zone-modal');
            modal.classList.remove('hidden');
            modal.classList.add('flex');
        }

        function closeEditZone() {
            const modal = document.getElementById('edit-zone-modal');
            modal.classList.add('hidden');
            modal.classList.remove('flex');
        }

        document.getElementById('edit-zone-modal').addEventListener('click', function(e) {
            if (e.target === this) closeEditZone();
        });

        function openEditCourier(code, data) {
            document.getElementById('edit-courier-form').action = '/admin/shipping/couriers/' + code;
            document.getElementById('edit-courier-name').value = data.short_name;
            document.getElementById('edit-courier-etd').value = data.etd;
            document.getElementById('edit-courier-multiplier').value = data.price_multiplier;
            document.getElementById('edit-courier-active').checked = !!data.is_active;

            const modal = document.getElementById('edit-courier-modal');
            modal.classList.remove('hidden');
            modal.classList.add('flex');
        }

        function closeEditCourier() {
            const modal = document.getElementById('edit-courier-modal');
            modal.classList.add('hidden');
            modal.classList.remove('flex');
        }

        document.getElementById('edit-courier-modal').addEventListener('click', function(e) {
            if (e.target === this) closeEditCourier();
        });
    </script>
</x-app-layout>