<?php include 'header.php'; ?>

<?php
    // SQL sorgusunu tek satırda yaz
    $results = $db->query("SELECT * FROM iletisim ORDER BY id DESC LIMIT 250")->fetchAll(PDO::FETCH_ASSOC);

    function ipv6Toipv4($ipv6) {
        $ipv4 = '';

        if (filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
            $ipv6 = inet_pton($ipv6);

            // IPv6 adresi olduğundan emin olduktan sonra IPv4'e dönüştür
            if (strlen($ipv6) == 16) {
                $ipv4 = unpack('C*', $ipv6);
                $ipv4 = array_slice($ipv4, 7, 4);
                $ipv4 = implode('.', $ipv4);
            }
        } elseif (filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
            // Eğer zaten IPv4 ise işlem yapma
            $ipv4 = $ipv6;
        }

        return $ipv4;
    }
?>

<div class="app-content container center-layout mt-2">
    <div class="content-overlay"></div>
    <div class="content-wrapper">
        <div class="content-header row">
        </div>
        <div class="content-body">
            <div class="row">
                <div id="recent-transactions" class="col-12">
                    <div class="card">
                        <div class="card-header">
                            <h4 class="card-title">İletişim Formu</h4>
                            <a class="heading-elements-toggle"><i class="la la-ellipsis-v font-medium-3"></i></a>
                        </div>
                        <div class="card-content">
                            <div class="table-responsive">
                                <table id="recent-orders" class="table table-hover mb-0">
                                    <thead>
                                        <tr>
                                            <th>User ID</th>
                                            <th>Ad Soyad<br>Email</th>
                                            <th>İçerik</th>
                                            <th>Zaman</th>
                                            <th>IP</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($results as $result): ?>
                                            <tr>
                                                <td><?php echo $result['user_id']; ?></td>
                                                <td>
                                                    <?php
                                                        // Ödemeler tablosunda ilgili user_id var mı kontrol et
                                                        $user_id = $result['user_id'];
                                                        $check_payment_query = $db->query("SELECT COUNT(*) FROM odemeler WHERE user_id = $user_id")->fetchColumn();

                                                        // Eğer ödeme varsa ad-soyad bilgisini bold yap
                                                        if ($check_payment_query > 0) {
                                                            echo '<b>' . $result['user_fullname'] . '</b><br>';
                                                        } else {
                                                            echo $result['user_fullname'] . '<br>';
                                                        }

                                                        echo $result['user_email'];
                                                    ?>
                                                </td>
                                                <td><?php echo $result['icerik']; ?></td>
                                                <td><?php echo date('d.m.Y - H:i:s', $result['zaman']); ?></td>
                                                <td><?php echo ipv6Toipv4($result['ip']); ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
