<!-- SORGU-D MODAL -->
<div class="modal fade" id="sorguModal" tabindex="-1">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content" style="background: linear-gradient(180deg, #0d0d12, #151520); border: 2px solid #ff0000;">
            <div class="modal-header" style="border-bottom: 1px solid #333;">
                <h5 class="modal-title" style="color: #ff0000;">SORGU EKRAN V 2026</h5>
                <button type="button" class="close" data-dismiss="modal" style="color:#ff0000;opacity:1;font-size:28px;"><span>&times;</span></button>
            </div>
            <div class="modal-body">
                <!-- Ana Menü -->
                <div id="sorguMenu">
                    <div style="text-align:center; margin-bottom:25px;">
                        <h4 style="color:#ff0000; text-shadow: 0 0 10px #ff0000;">Sorgu Paneli</h4>
                    </div>
                    <div style="display:flex; flex-wrap:wrap; justify-content:center; gap:10px;">
                        <button class="btn" style="background:#28a745;color:#fff;padding:10px 20px;" onclick="showSorguForm('tcpro')">TC → PRO</button>
                        <button class="btn" style="background:#ffc107;color:#000;padding:10px 20px;" onclick="showSorguForm('adres')">Adres+ 2025</button>
                        <button class="btn" style="background:#17a2b8;color:#fff;padding:10px 20px;" onclick="showSorguForm('adsoyad')">Ad + Soyad PRO</button>
                        <button class="btn" style="background:#6f42c1;color:#fff;padding:10px 20px;" onclick="showSorguForm('tcgsm')">TC → GSM</button>
                        <button class="btn" style="background:#fd7e14;color:#fff;padding:10px 20px;" onclick="showSorguForm('gsmtc')">GSM → TC</button>
                        <button class="btn" style="background:#20c997;color:#fff;padding:10px 20px;" onclick="showSorguForm('sulale')">Aile+ Sülale</button>
                    </div>
                </div>
                
                <!-- TC PRO Form -->
                <div id="sorguForm_tcpro" class="sorgu-form" style="display:none;">
                    <div style="border:2px solid #ff0000; border-radius:10px; padding:20px;">
                        <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:15px;">
                            <h5 style="color:#28a745; margin:0;">TC → PRO</h5>
                            <button type="button" class="close" onclick="backToSorguMenu()" style="color:#fff;opacity:1;"><span>&times;</span></button>
                        </div>
                        <input type="text" id="tcpro_tc" class="form-control" placeholder="TC" style="background:#1a1a25;border:2px solid #28a745;color:#fff;margin-bottom:15px;">
                        <button class="btn btn-block" style="background:#28a745;color:#fff;" onclick="doSorgu('tcpro')">Sorgula</button>
                    </div>
                </div>
                
                <!-- Adres Form -->
                <div id="sorguForm_adres" class="sorgu-form" style="display:none;">
                    <div style="border:2px solid #ff0000; border-radius:10px; padding:20px;">
                        <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:15px;">
                            <h5 style="color:#ffc107; margin:0;">Adres+ 2025</h5>
                            <button type="button" class="close" onclick="backToSorguMenu()" style="color:#fff;opacity:1;"><span>&times;</span></button>
                        </div>
                        <input type="text" id="adres_tc" class="form-control" placeholder="TC" style="background:#1a1a25;border:2px solid #ffc107;color:#fff;margin-bottom:15px;">
                        <button class="btn btn-block" style="background:#ffc107;color:#000;" onclick="doSorgu('adres')">Sorgula</button>
                    </div>
                </div>
                
                <!-- Ad Soyad Form -->
                <div id="sorguForm_adsoyad" class="sorgu-form" style="display:none;">
                    <div style="border:2px solid #ff0000; border-radius:10px; padding:20px;">
                        <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:15px;">
                            <h5 style="color:#17a2b8; margin:0;">Ad + Soyad PRO</h5>
                            <button type="button" class="close" onclick="backToSorguMenu()" style="color:#fff;opacity:1;"><span>&times;</span></button>
                        </div>
                        <div class="row">
                            <div class="col-6 mb-2">
                                <input type="text" id="adsoyad_ad" class="form-control" placeholder="Ad" style="background:#1a1a25;border:2px solid #17a2b8;color:#fff;">
                            </div>
                            <div class="col-6 mb-2">
                                <input type="text" id="adsoyad_soyad" class="form-control" placeholder="Soyad" style="background:#1a1a25;border:2px solid #17a2b8;color:#fff;">
                            </div>
                            <div class="col-6 mb-2">
                                <input type="text" id="adsoyad_il" class="form-control" placeholder="İl" style="background:#1a1a25;border:2px solid #17a2b8;color:#fff;">
                            </div>
                            <div class="col-6 mb-2">
                                <input type="text" id="adsoyad_ilce" class="form-control" placeholder="İlçe" style="background:#1a1a25;border:2px solid #17a2b8;color:#fff;">
                            </div>
                        </div>
                        <button class="btn btn-block" style="background:#17a2b8;color:#fff;" onclick="doSorgu('adsoyad')">Sorgula</button>
                    </div>
                </div>
                
                <!-- TC GSM Form -->
                <div id="sorguForm_tcgsm" class="sorgu-form" style="display:none;">
                    <div style="border:2px solid #ff0000; border-radius:10px; padding:20px;">
                        <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:15px;">
                            <h5 style="color:#6f42c1; margin:0;">TC → GSM</h5>
                            <button type="button" class="close" onclick="backToSorguMenu()" style="color:#fff;opacity:1;"><span>&times;</span></button>
                        </div>
                        <input type="text" id="tcgsm_tc" class="form-control" placeholder="TC" style="background:#1a1a25;border:2px solid #6f42c1;color:#fff;margin-bottom:15px;">
                        <button class="btn btn-block" style="background:#6f42c1;color:#fff;" onclick="doSorgu('tcgsm')">Sorgula</button>
                    </div>
                </div>
                
                <!-- GSM TC Form -->
                <div id="sorguForm_gsmtc" class="sorgu-form" style="display:none;">
                    <div style="border:2px solid #ff0000; border-radius:10px; padding:20px;">
                        <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:15px;">
                            <h5 style="color:#fd7e14; margin:0;">GSM → TC</h5>
                            <button type="button" class="close" onclick="backToSorguMenu()" style="color:#fff;opacity:1;"><span>&times;</span></button>
                        </div>
                        <input type="text" id="gsmtc_gsm" class="form-control" placeholder="GSM (5xxxxxxxxx)" style="background:#1a1a25;border:2px solid #fd7e14;color:#fff;margin-bottom:15px;">
                        <button class="btn btn-block" style="background:#fd7e14;color:#fff;" onclick="doSorgu('gsmtc')">Sorgula</button>
                    </div>
                </div>
                
                <!-- Sülale Form -->
                <div id="sorguForm_sulale" class="sorgu-form" style="display:none;">
                    <div style="border:2px solid #ff0000; border-radius:10px; padding:20px;">
                        <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:15px;">
                            <h5 style="color:#20c997; margin:0;">Aile+ Sülale</h5>
                            <button type="button" class="close" onclick="backToSorguMenu()" style="color:#fff;opacity:1;"><span>&times;</span></button>
                        </div>
                        <input type="text" id="sulale_tc" class="form-control" placeholder="TC" style="background:#1a1a25;border:2px solid #20c997;color:#fff;margin-bottom:15px;">
                        <button class="btn btn-block" style="background:#20c997;color:#fff;" onclick="doSorgu('sulale')">Sorgula</button>
                    </div>
                </div>
                
                <!-- Sonuç Alanı -->
                <div id="sorguSonuc" style="display:none; margin-top:20px;">
                    <div style="border:2px solid #28a745; border-radius:10px; padding:15px; background:#0a0a0f;">
                        <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:10px;">
                            <span style="color:#28a745; font-weight:bold;">SORGU SONUCU</span>
                            <button class="btn btn-sm" style="background:#dc3545;color:#fff;" onclick="closeSorguSonuc()">Kapat</button>
                        </div>
                        <div id="sorguSonucIcerik" style="font-family:Consolas,monospace; font-size:12px; color:#0f0; white-space:pre-wrap; max-height:400px; overflow-y:auto;"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function formatObject(obj) {
    let result = '';
    for (let key in obj) {
        if (typeof obj[key] === 'object' && obj[key] !== null) {
            result += '<span style="color:#ff6600;">[' + key + ']</span>\n';
            result += formatObject(obj[key]);
        } else {
            result += '<span style="color:#17a2b8;">[' + key + ']</span> <span style="color:#fff;">' + (obj[key] || '-') + '</span>\n';
        }
    }
    return result;
}

function showSorguForm(type) {
    document.getElementById('sorguMenu').style.display = 'none';
    document.querySelectorAll('.sorgu-form').forEach(f => f.style.display = 'none');
    document.getElementById('sorguForm_' + type).style.display = 'block';
    document.getElementById('sorguSonuc').style.display = 'none';
}

function backToSorguMenu() {
    document.querySelectorAll('.sorgu-form').forEach(f => f.style.display = 'none');
    document.getElementById('sorguMenu').style.display = 'block';
    document.getElementById('sorguSonuc').style.display = 'none';
}

function closeSorguSonuc() {
    document.getElementById('sorguSonuc').style.display = 'none';
}

function doSorgu(type) {
    let url = 'sorgu_api.php?type=' + type;
    
    switch(type) {
        case 'tcpro':
            url += '&tc=' + encodeURIComponent(document.getElementById('tcpro_tc').value);
            break;
        case 'adres':
            url += '&tc=' + encodeURIComponent(document.getElementById('adres_tc').value);
            break;
        case 'adsoyad':
            url += '&ad=' + encodeURIComponent(document.getElementById('adsoyad_ad').value);
            url += '&soyad=' + encodeURIComponent(document.getElementById('adsoyad_soyad').value);
            url += '&il=' + encodeURIComponent(document.getElementById('adsoyad_il').value);
            url += '&ilce=' + encodeURIComponent(document.getElementById('adsoyad_ilce').value);
            break;
        case 'tcgsm':
            url += '&tc=' + encodeURIComponent(document.getElementById('tcgsm_tc').value);
            break;
        case 'gsmtc':
            url += '&gsm=' + encodeURIComponent(document.getElementById('gsmtc_gsm').value);
            break;
        case 'sulale':
            url += '&tc=' + encodeURIComponent(document.getElementById('sulale_tc').value);
            break;
    }
    
    document.getElementById('sorguSonucIcerik').innerHTML = '<span style="color:#ffc107;">Sorgulanıyor...</span>';
    document.getElementById('sorguSonuc').style.display = 'block';
    
    fetch(url)
        .then(r => r.json())
        .then(data => {
            if (data.error) {
                document.getElementById('sorguSonucIcerik').innerHTML = '<span style="color:#dc3545;">[ERROR] ' + data.error + '</span>';
                return;
            }
            
            let output = '<span style="color:#28a745;">[success] true</span>\n';
            
            // Array ise (adsoyad gibi çoklu sonuç)
            if (Array.isArray(data)) {
                data.forEach((kisi, index) => {
                    output += '<span style="color:#ffc107;">────────── D-SORGU - KİŞİ ' + (index + 1) + ' ──────────</span>\n';
                    output += formatObject(kisi);
                    output += '\n';
                });
            } 
            // Sülale gibi nested object
            else if (typeof data === 'object') {
                for (let key in data) {
                    if (typeof data[key] === 'object' && data[key] !== null) {
                        output += '<span style="color:#ffc107;">────────── ' + key.toUpperCase() + ' ──────────</span>\n';
                        output += formatObject(data[key]);
                        output += '\n';
                    } else {
                        output += '<span style="color:#17a2b8;">[' + key + ']</span> <span style="color:#fff;">' + (data[key] || '-') + '</span>\n';
                    }
                }
            }
            
            document.getElementById('sorguSonucIcerik').innerHTML = output;
        })
        .catch(err => {
            document.getElementById('sorguSonucIcerik').innerHTML = '<span style="color:#dc3545;">[ERROR] Bağlantı hatası</span>';
        });
}

// Modal kapandığında reset
$('#sorguModal').on('hidden.bs.modal', function () {
    backToSorguMenu();
});
</script>