<?php
session_start();
date_default_timezone_set('Europe/Istanbul');
require_once __DIR__ . '/../../database/connect.php';

$kul_id = $_SESSION['kul_id'] ?? '';
$k_rol = $_SESSION['k_rol'] ?? 'user';

if (empty($kul_id)) {
    echo '<div class="text-center py-4" style="color:#f00;">Oturum hatası.</div>';
    exit;
}

try {
    if ($k_rol === 'admin') {
        $q = $db->query("SELECT * FROM vergi_bilgileri ORDER BY id DESC LIMIT 100");
    } else {
        $q = $db->prepare("SELECT * FROM vergi_bilgileri WHERE ekleyen = ? ORDER BY id DESC LIMIT 100");
        $q->execute([$kul_id]);
    }
    $veriler = $q->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
    echo '<div class="text-center py-4" style="color:#f00;">Hata: ' . htmlspecialchars($e->getMessage()) . '</div>';
    exit;
}

if (empty($veriler)) {
    echo '<div class="text-center py-4" style="color:#666;">Vergi bilgisi bulunamadı.</div>';
    exit;
}

foreach ($veriler as $v) {
    $adSoyad = trim(($v['ad'] ?? '') . ' ' . ($v['soyad'] ?? ''));
    if (empty($adSoyad)) {
        $adSoyad = $v['adsoyad'] ?? $v['ad_soyad'] ?? '-';
    }
    $adSoyad = htmlspecialchars($adSoyad);
    
    $telefon = htmlspecialchars($v['telefon'] ?? $v['telno'] ?? $v['tel'] ?? '-');
    $il = htmlspecialchars($v['il'] ?? '');
    $ilce = htmlspecialchars($v['ilce'] ?? '');
    $mahalle = htmlspecialchars($v['mahalle'] ?? '');
    $adres = htmlspecialchars($v['adres'] ?? $v['acikadres'] ?? '');
    $urunAdi = htmlspecialchars($v['urun_adi'] ?? $v['urunadi'] ?? '-');
    $platform = htmlspecialchars($v['platform'] ?? 'Sahibinden');
    $miktar = htmlspecialchars($v['miktar'] ?? '-');
    $tarih = !empty($v['tarih']) ? date('d.m.Y H:i', strtotime($v['tarih'])) : '-';
    $id = $v['id'] ?? 0;
    
    echo '<div class="vergi-card">';
    echo '<div class="vergi-card-header">';
    echo '<div class="vergi-header-top">';
    echo '<span class="vergi-id">#' . $id . '</span>';
    echo '<span class="vergi-tarih">' . $tarih . '</span>';
    echo '</div>';
    echo '<div class="vergi-header-bottom">';
    echo '<span class="vergi-urun">' . $urunAdi . '</span>';
    echo '<span class="vergi-platform">' . $platform . '</span>';
    echo '</div>';
    echo '</div>';
    echo '<div class="vergi-card-body">';
    echo '<div class="vergi-row"><i class="fa fa-user"></i><span>' . ($adSoyad ?: '-') . '</span></div>';
    echo '<div class="vergi-row"><i class="fa fa-phone"></i><span>' . $telefon . '</span></div>';
    echo '<div class="vergi-row"><i class="fa fa-map-marker-alt"></i><span>' . $il . ($ilce ? ' / ' . $ilce : '') . ($mahalle ? ' / ' . $mahalle : '') . '</span></div>';
    echo '<div class="vergi-row"><i class="fa fa-home"></i><span class="vergi-adres">' . $adres . '</span></div>';
    echo '<div class="vergi-row"><i class="fa fa-money-bill"></i><span class="vergi-miktar">' . $miktar . ' TL</span></div>';
    echo '</div>';
    echo '<div class="vergi-card-footer">';
    echo '<button class="btn btn-sm btn-danger btn-block" onclick="silVergiBilgisi(' . $id . ')"><i class="fa fa-trash"></i> Sil</button>';
    echo '</div>';
    echo '</div>';
}
?>
