<?php

$ilanlar = tabloOku("ilanlar", $conn);

$mesajID = @$_GET["mesajID"];
$sql = "SELECT * FROM mesajlar WHERE mesaj_id=$mesajID";
$ornek = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($ornek, MYSQLI_BOTH);

$ilanID = $row["ilan_id"];
$gonderenAd = $row["gonderen_ad"];
$gonderenEmail = $row["gonderen_email"];
$mesajIcerik = $row["mesaj_icerik"];
$tarih = $row["mesaj_tarih"];
$cevap = $row["cevap"];

if (isset($ilanlar[$ilanID]))
    $ilanBaslik = $ilanlar[$ilanID]["ilan_baslik"];
else
    $ilanBaslik = "<span class='text-muted'>Silinmiş Ürün</span>";
?>

<div class="card mt-3">
    <div class="card-header bg-dark text-white">
        <h5 class="mb-0"><i class="bi bi-envelope-open"></i> Sipariş / Bilgi Talebi Detayı</h5>
    </div>
    <div class="card-body">
        <table class="table table-bordered">
            <tr>
                <td style="width: 180px;" class="text-muted"><strong>İlgili Ürün:</strong></td>
                <td><strong><?php echo $ilanBaslik; ?></strong></td>
            </tr>
            <tr>
                <td class="text-muted"><strong>Gönderen Müşteri:</strong></td>
                <td><?php echo $gonderenAd; ?></td>
            </tr>
            <tr>
                <td class="text-muted"><strong>E-posta:</strong></td>
                <td><a href="mailto:<?php echo $gonderenEmail; ?>"><?php echo $gonderenEmail; ?></a></td>
            </tr>
            <tr>
                <td class="text-muted"><strong>Tarih:</strong></td>
                <td><?php echo $tarih; ?></td>
            </tr>
            <tr>
                <td class="text-muted"><strong>Müşteri Mesajı:</strong></td>
                <td><?php echo nl2br($mesajIcerik); ?></td>
            </tr>
        </table>
    </div>
</div>

<?php if (!is_null($cevap) && trim($cevap) != "") { ?>
<div class="card mt-3">
    <div class="card-header bg-success text-white">
        <h6 class="mb-0"><i class="bi bi-reply-all"></i> Gönderilen Cevap</h6>
    </div>
    <div class="card-body">
        <p class="mb-0"><?php echo nl2br($cevap); ?></p>
    </div>
</div>
<?php } ?>

<div class="card mt-3">
    <div class="card-header bg-dark text-white">
        <h6 class="mb-0"><i class="bi bi-reply"></i> Cevap Yaz</h6>
    </div>
    <div class="card-body">
        <form action="index.php" method="get">
            <input type="hidden" name="sayfa" value="mesajcevapla">
            <input type="hidden" name="mesajID" value="<?php echo $mesajID; ?>">
            <div class="mb-3">
                <label class="form-label fw-bold">Yanıtınız</label>
                <textarea name="cevapIcerik" id="cevapIcerik" class="form-control" rows="4" required placeholder="Cevabınızı yazın..."><?php if (!is_null($cevap)) echo $cevap; ?></textarea>
            </div>
            <button type="submit" class="btn btn-success"><i class="bi bi-telegram"></i> Yanıtı Kaydet / Gönder</button>
            <a href="?sayfa=mesajlar" class="btn btn-secondary">Geri Dön</a>
        </form>
    </div>
</div>
