

<?php $__env->startSection('content'); ?>
<div class="bg-white rounded-[40px] p-10 shadow-sm border border-slate-100 max-w-6xl mx-auto">
    
    <?php if(session('success')): ?>
        <div class="mb-6 p-4 bg-emerald-100 text-emerald-700 rounded-2xl font-bold flex items-center gap-3">
            <i data-lucide="check-circle"></i> <?php echo e(session('success')); ?>

        </div>
    <?php endif; ?>

    <div class="flex items-center gap-4 mb-8">
        <div class="w-12 h-12 bg-pink-500 rounded-2xl flex items-center justify-center shadow-lg shadow-pink-100">
            <i data-lucide="map-pin" class="text-white w-6 h-6"></i>
        </div>
        <div>
            <h1 class="text-2xl font-black text-slate-900">Konfigurasi Lokasi Toko</h1>
            <p class="text-slate-500 font-medium italic text-sm">Data untuk penentuan titik jemput & perhitungan ongkir.</p>
        </div>
    </div>

    <form action="<?php echo e(route('admin.address.update')); ?>" method="POST" class="grid grid-cols-1 lg:grid-cols-2 gap-10">
        <?php echo csrf_field(); ?> <?php echo method_field('PUT'); ?>

        
        <div class="space-y-5">
            <div class="p-6 bg-slate-50/50 rounded-[32px] border border-slate-100 space-y-5">
                <p class="text-[11px] font-black uppercase tracking-[0.2em] text-pink-500 mb-4">Informasi Wilayah</p>
                
                <div>
                    <label class="block text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2 ml-2">Provinsi</label>
                    <select name="province_id" id="province" class="w-full px-5 py-4 rounded-2xl bg-white border border-slate-100 focus:ring-2 focus:ring-pink-500 font-bold text-slate-700 shadow-sm">
                        <option value="">Pilih Provinsi</option>
                        <?php $__currentLoopData = \Laravolt\Indonesia\Models\Province::all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $prov): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($prov->code); ?>" <?php echo e($user->province_id == $prov->code ? 'selected' : ''); ?>><?php echo e($prov->name); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>

                <div>
                    <label class="block text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2 ml-2">Kota / Kabupaten</label>
                    <select name="regency_id" id="regency" class="w-full px-5 py-4 rounded-2xl bg-white border border-slate-100 focus:ring-2 focus:ring-pink-500 font-bold text-slate-700 shadow-sm">
                        <option value="">Pilih Kota</option>
                    </select>
                </div>

                <div>
                    <label class="block text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2 ml-2">Kecamatan</label>
                    <select name="district_id" id="district" class="w-full px-5 py-4 rounded-2xl bg-white border border-slate-100 focus:ring-2 focus:ring-pink-500 font-bold text-slate-700 shadow-sm">
                        <option value="">Pilih Kecamatan</option>
                    </select>
                </div>

                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2 ml-2">Desa / Kelurahan</label>
                        <select name="village_id" id="village" class="w-full px-5 py-4 rounded-2xl bg-white border border-slate-100 focus:ring-2 focus:ring-pink-500 font-bold text-slate-700 shadow-sm">
                            <option value="">Pilih Kelurahan</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-[10px] font-black uppercase tracking-widest text-slate-400 mb-2 ml-2">Kode Pos</label>
                        <input type="number" name="postal_code" value="<?php echo e($user->postal_code); ?>" placeholder="12345" class="w-full px-5 py-4 rounded-2xl bg-white border border-slate-100 focus:ring-2 focus:ring-pink-500 font-bold text-slate-700 shadow-sm">
                    </div>
                </div>
            </div>

            
            <div class="p-6 bg-slate-900 rounded-[32px] text-white">
                <div class="flex items-center justify-between mb-4">
                    <p class="text-[11px] font-black uppercase tracking-[0.2em] text-slate-400">Titik Koordinat (GPS)</p>
                    <button type="button" onclick="getLocation()" class="text-[9px] bg-pink-500 hover:bg-pink-600 px-3 py-1.5 rounded-full font-black uppercase transition-all">
                        Update GPS
                    </button>
                </div>
                
                <input type="hidden" name="lat" id="lat" value="<?php echo e($user->lat); ?>">
                <input type="hidden" name="lng" id="lng" value="<?php echo e($user->lng); ?>">

                <a href="https://www.google.com/maps?q=<?php echo e($user->lat); ?>,<?php echo e($user->lng); ?>" 
                   target="_blank" id="maps-link" 
                   class="flex items-center gap-4 p-4 bg-slate-800 rounded-2xl hover:bg-slate-700 transition-all border border-slate-700 group">
                    <div class="w-10 h-10 bg-blue-500 rounded-xl flex items-center justify-center shadow-lg group-hover:scale-110 transition-transform">
                        <i data-lucide="map" class="text-white w-5 h-5"></i>
                    </div>
                    <div>
                        <p class="text-[10px] font-bold text-slate-400 uppercase tracking-wider">Lihat di Google Maps</p>
                        <p class="text-xs font-mono text-blue-400" id="coords-text">
                            <?php echo e($user->lat ?? '0.000'); ?>, <?php echo e($user->lng ?? '0.000'); ?>

                        </p>
                    </div>
                    <i data-lucide="external-link" class="w-4 h-4 ml-auto text-slate-500"></i>
                </a>
            </div>
        </div>

        
        <div class="flex flex-col h-full">
            <div class="p-8 bg-pink-50/50 rounded-[40px] border border-pink-100 flex-grow">
                <label class="flex items-center gap-2 text-[11px] font-black uppercase tracking-widest text-pink-600 mb-4">
                    <i data-lucide="home" class="w-4 h-4"></i> Detail Alamat Lengkap
                </label>
                <textarea name="address" rows="10" placeholder="Nama Jalan, Nomor Rumah, Patokan, Blok, dsb..." 
                    class="w-full px-6 py-5 rounded-[28px] bg-white border-none focus:ring-2 focus:ring-pink-500 font-bold text-slate-700 shadow-sm placeholder:text-slate-300"><?php echo e($user->address); ?></textarea>
            </div>

            <button type="submit" class="mt-8 w-full py-6 bg-slate-900 text-white rounded-[30px] font-black text-sm shadow-2xl shadow-slate-300 hover:bg-pink-600 hover:-translate-y-1 transition-all duration-300 flex items-center justify-center gap-3">
                <i data-lucide="save" class="w-5 h-5"></i>
                SIMPAN PERUBAHAN LOKASI
            </button>
        </div>
    </form>
</div>

<script>
    // Fungsi Fetch Global
    async function loadDropdownData(endpoint, id, targetEl, selectedValue = null) {
        if (!id || id === "") return;
        
        try {
            const res = await fetch(`${endpoint}/${id}`);
            if (!res.ok) throw new Error('Data gagal dimuat');
            
            const data = await res.json();
            let options = `<option value="">Pilih...</option>`;
            
            data.forEach(item => {
                const isSelected = item.code == selectedValue ? 'selected' : '';
                options += `<option value="${item.code}" ${isSelected}>${item.name}</option>`;
            });
            
            targetEl.innerHTML = options;
        } catch (err) {
            console.error('Fetch error:', err);
            targetEl.innerHTML = `<option value="">Error memuat data</option>`;
        }
    }

    const provinceEl = document.getElementById('province');
    const regencyEl = document.getElementById('regency');
    const districtEl = document.getElementById('district');
    const villageEl = document.getElementById('village');

    // Event Listeners
    provinceEl.addEventListener('change', async () => {
        regencyEl.innerHTML = '<option value="">Memuat...</option>';
        await loadDropdownData('/indoregion/regencies', provinceEl.value, regencyEl);
        districtEl.innerHTML = '<option value="">Pilih Kecamatan</option>';
        villageEl.innerHTML = '<option value="">Pilih Kelurahan</option>';
    });

    regencyEl.addEventListener('change', async () => {
        districtEl.innerHTML = '<option value="">Memuat...</option>';
        await loadDropdownData('/indoregion/districts', regencyEl.value, districtEl);
        villageEl.innerHTML = '<option value="">Pilih Kelurahan</option>';
    });

    districtEl.addEventListener('change', async () => {
        villageEl.innerHTML = '<option value="">Memuat...</option>';
        await loadDropdownData('/indoregion/villages', districtEl.value, villageEl);
    });

    // AUTO LOAD SAAT PAGE REFRESH (Old Data)
    window.addEventListener('DOMContentLoaded', async () => {
        const oldRegency = "<?php echo e($user->regency_id); ?>";
        const oldDistrict = "<?php echo e($user->district_id); ?>";
        const oldVillage = "<?php echo e($user->village_id); ?>";

        if (provinceEl.value) {
            // Load Regency
            await loadDropdownData('/indoregion/regencies', provinceEl.value, regencyEl, oldRegency);
            
            if (oldRegency) {
                // Load District
                await loadDropdownData('/indoregion/districts', oldRegency, districtEl, oldDistrict);
                
                if (oldDistrict) {
                    // Load Village
                    await loadDropdownData('/indoregion/villages', oldDistrict, villageEl, oldVillage);
                }
            }
        }
    });

    // GPS Functionality
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(pos => {
                const lat = pos.coords.latitude;
                const lng = pos.coords.longitude;
                document.getElementById('lat').value = lat;
                document.getElementById('lng').value = lng;
                document.getElementById('coords-text').innerText = `${lat.toFixed(6)}, ${lng.toFixed(6)}`;
                document.getElementById('maps-link').href = `https://www.google.com/maps?q=${lat},${lng}`;
                alert('GPS Berhasil Diperbarui!');
            }, (error) => {
                alert('Gagal mengambil lokasi. Pastikan izin lokasi aktif.');
            });
        }
    }
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\SwiftCart\resources\views/admin/profile/address.blade.php ENDPATH**/ ?>