<div class="d-flex justify-content-between align-items-center tw-mb-10 flex-wrap tw-gap-4">
    <div>
        <h3 class="tw-font-black tw-text-2xl tw-text-gray-900 tw-mb-1 tw-tracking-tight">Toplu İçe Aktarma</h3>
        <p class="tw-text-gray-500 tw-text-sm tw-font-medium tw-mb-0">CSV dosyası kullanarak sisteminize toplu ilan girişi yapın.</p>
    </div>
    <div class="d-flex tw-gap-3">
        <button onclick="downloadCSVSample()" class="btn tw-bg-emerald-50 tw-border tw-border-emerald-200 tw-text-emerald-600 tw-rounded-xl tw-px-5 tw-py-2.5 tw-font-bold tw-text-sm hover:tw-bg-emerald-500 hover:tw-text-white tw-transition-all d-flex align-items-center shadow-sm">
            <i class="fa-solid fa-file-export tw-mr-2"></i> Örnek Şablon İndir
        </button>
        
        <a href="<?php echo BASE_URL; ?>/admin/properties" class="btn tw-bg-white tw-border tw-border-gray-200 tw-text-gray-600 tw-rounded-xl tw-px-5 tw-py-2.5 tw-font-bold tw-text-sm hover:tw-bg-gray-50 tw-transition-all d-flex align-items-center shadow-sm">
            <i class="fa-solid fa-arrow-left tw-mr-2"></i> Vazgeç ve Dön
        </a>
    </div>
</div>

<?php if (isset($_GET['imported'])): ?>
    <div class="tw-flex tw-items-center tw-p-5 tw-mb-8 tw-text-emerald-800 tw-rounded-2xl tw-bg-emerald-50 tw-border tw-border-emerald-100 tw-shadow-sm">
        <i class="fa-solid fa-circle-check tw-text-2xl tw-mr-4"></i>
        <div>
            <div class="tw-font-black tw-text-sm tw-uppercase tw-tracking-wider">İşlem Başarılı</div>
            <div class="tw-text-sm tw-font-medium">
                İçe aktarılan: <span class="tw-font-black"><?php echo (int)$_GET['imported']; ?></span> | 
                Atlanan: <span class="tw-font-black"><?php echo (int)($_GET['skipped'] ?? 0); ?></span>
            </div>
        </div>
    </div>
<?php elseif (isset($_GET['error'])): ?>
    <div class="tw-flex tw-items-center tw-p-5 tw-mb-8 tw-text-rose-800 tw-rounded-2xl tw-bg-rose-50 tw-border tw-border-rose-100 tw-shadow-sm">
        <i class="fa-solid fa-circle-exclamation tw-text-2xl tw-mr-4"></i>
        <div>
            <div class="tw-font-black tw-text-sm tw-uppercase tw-tracking-wider">Hata Oluştu</div>
            <div class="tw-text-sm tw-font-medium">CSV dosyası işlenirken bir sorun çıktı. Lütfen formatı kontrol edin.</div>
        </div>
    </div>
<?php endif; ?>

<div class="row g-5">
    <div class="col-lg-6">
        <div class="tw-bg-white tw-border tw-border-gray-100 tw-rounded-[2rem] tw-shadow-sm tw-overflow-hidden tw-h-full">
            <div class="tw-p-6 tw-px-8 tw-bg-gray-50/50 tw-border-b tw-border-gray-100">
                <h6 class="tw-font-black tw-text-sm tw-text-blue-600 tw-mb-0 tw-uppercase tw-tracking-widest">
                    <i class="fa-solid fa-file-csv tw-mr-2"></i>DOSYA SEÇİMİ
                </h6>
            </div>
            <div class="tw-p-8 md:tw-p-10">
                <form action="<?php echo BASE_URL; ?>/admin/property-import-save" method="POST" enctype="multipart/form-data">
                    <div class="tw-mb-8">
                        <label class="tw-text-xs tw-font-extrabold tw-text-gray-400 tw-uppercase tw-tracking-widest tw-mb-4 tw-block tw-px-1">CSV Veri Dosyası</label>
                        <div id="dropZone" class="tw-border-2 tw-border-dashed tw-border-gray-200 tw-bg-gray-50 tw-rounded-2xl tw-p-12 tw-text-center hover:tw-border-blue-400 hover:tw-bg-blue-50/30 tw-transition-all tw-cursor-pointer tw-relative group">
                            <input type="file" name="csv_file" id="csv_file" class="tw-absolute tw-inset-0 tw-w-full tw-h-full tw-opacity-0 tw-cursor-pointer" accept=".csv" required onchange="updateFileName(this)">
                            <div class="tw-text-gray-400 group-hover:tw-scale-110 tw-transition-transform">
                                <i class="fa-solid fa-cloud-arrow-up tw-text-5xl tw-mb-4 tw-text-blue-500"></i>
                                <p id="fileName" class="tw-font-black tw-text-sm tw-text-gray-600 tw-uppercase tw-tracking-widest">Dosyayı Buraya Bırakın veya Seçin</p>
                                <p class="tw-text-[10px] tw-font-bold tw-mt-2">Sadece .csv formatında dosyalar kabul edilir.</p>
                            </div>
                        </div>
                    </div>
                    <button type="submit" class="btn tw-bg-blue-600 hover:tw-bg-blue-700 tw-text-white tw-w-full tw-py-4 tw-rounded-xl tw-font-black tw-text-sm tw-tracking-widest tw-shadow-xl tw-shadow-blue-500/30 tw-transition-all hover:tw--translate-y-1">
                        <i class="fa-solid fa-upload tw-mr-2"></i> YÜKLE VE İÇE AKTAR
                    </button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-6">
        <div class="tw-bg-white tw-border tw-border-gray-100 tw-rounded-[2rem] tw-shadow-sm tw-overflow-hidden">
            <div class="tw-p-6 tw-px-8 tw-bg-gray-50/50 tw-border-b tw-border-gray-100">
                <h6 class="tw-font-black tw-text-sm tw-text-gray-800 tw-mb-0 tw-uppercase tw-tracking-widest">
                    <i class="fa-solid fa-book-open tw-mr-2 tw-text-blue-500"></i>VERİ FORMATI KILAVUZU
                </h6>
            </div>
            <div class="tw-p-8">
                <div class="tw-mb-6">
                    <div class="tw-flex tw-items-center tw-mb-2">
                        <span class="tw-w-2 tw-h-2 tw-bg-red-500 tw-rounded-full tw-mr-2"></span>
                        <h6 class="tw-text-xs tw-font-black tw-text-gray-700 tw-uppercase tw-tracking-wider tw-mb-0">Zorunlu Kolonlar</h6>
                    </div>
                    <div class="tw-bg-gray-50 tw-p-3 tw-rounded-xl tw-border tw-border-gray-100">
                        <code class="tw-text-blue-600 tw-font-bold">title</code>
                    </div>
                </div>

                <div class="tw-mb-6">
                    <div class="tw-flex tw-items-center tw-mb-2">
                        <span class="tw-w-2 tw-h-2 tw-bg-blue-500 tw-rounded-full tw-mr-2"></span>
                        <h6 class="tw-text-xs tw-font-black tw-text-gray-700 tw-uppercase tw-tracking-wider tw-mb-0">Önerilen Kolonlar</h6>
                    </div>
                    <div class="tw-flex tw-wrap tw-gap-2">
                        <?php 
                        $cols = ['price', 'type', 'category', 'location', 'description', 'oda', 'banyo', 'm2', 'yas'];
                        foreach($cols as $col): ?>
                            <code class="tw-bg-gray-100 tw-text-gray-600 tw-px-2 tw-py-1 tw-rounded-md tw-text-[11px] tw-font-bold"><?php echo $col; ?></code>
                        <?php endforeach; ?>
                    </div>
                </div>

                <div class="tw-bg-blue-50 tw-rounded-2xl tw-p-5 tw-border tw-border-blue-100">
                    <div class="tw-flex tw-items-start">
                        <i class="fa-solid fa-lightbulb tw-text-blue-500 tw-text-lg tw-mr-3 tw-mt-0.5"></i>
                        <div>
                            <h6 class="tw-text-[11px] tw-font-black tw-text-blue-700 tw-uppercase tw-tracking-wider tw-mb-2">Önemli Hatırlatma</h6>
                            <p class="tw-text-xs tw-text-blue-600 tw-font-medium tw-mb-0 tw-leading-relaxed">
                                Şablon dosyasında başlıklar İngilizce karakterlerle ve küçük harflerle yazılmalıdır. Örnek şablonu indirerek bu yapıyı görebilirsiniz.
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function downloadCSVSample() {
    // CSV kolon başlıkları
    const headers = ["title", "price", "type", "category", "location", "description", "oda", "banyo", "m2", "yas"];
    // Örnek bir satır veri
    const sampleRow = ["Boğaz Manzaralı Lüks Daire", "7500000", "apartment", "sale", "Beşiktaş, İstanbul", "Harika konumda deniz manzaralı.", "3+1", "2", "145", "5"];
    
    // CSV içeriğini oluşturma
    let csvContent = "data:text/csv;charset=utf-8," 
        + headers.join(",") + "\n" 
        + sampleRow.join(",");

    // İndirme linki oluşturma
    const encodedUri = encodeURI(csvContent);
    const link = document.createElement("a");
    link.setAttribute("href", encodedUri);
    link.setAttribute("download", "emlak_ilan_sablonu.csv");
    document.body.appendChild(link);

    // İndirmeyi başlatma
    link.click();
    document.body.removeChild(link);
}

function updateFileName(input) {
    const fileNameDisplay = document.getElementById('fileName');
    if (input.files && input.files.length > 0) {
        fileNameDisplay.innerText = "Seçilen Dosya: " + input.files[0].name;
        fileNameDisplay.classList.add('tw-text-blue-600');
    } else {
        fileNameDisplay.innerText = "Dosyayı Buraya Bırakın veya Seçin";
        fileNameDisplay.classList.remove('tw-text-blue-600');
    }
}
</script>