<div class="row">
    <div class="col-12">
        <div class="tw-bg-white tw-border-0 tw-rounded-[2rem] tw-shadow-sm tw-overflow-hidden">
            
            <div class="tw-p-8 tw-border-b tw-border-gray-100 d-flex justify-content-between align-items-center flex-wrap tw-gap-4">
                <div>
                    <h5 class="tw-font-black tw-text-2xl tw-text-gray-900 tw-mb-1 d-flex align-items-center">
                        <div class="tw-w-10 tw-h-10 tw-bg-blue-50 tw-text-blue-600 tw-rounded-xl d-flex align-items-center justify-content-center tw-mr-3">
                            <i class="fa-solid fa-inbox"></i>
                        </div>
                        Müşteri Talepleri
                    </h5>
                    <p class="tw-text-gray-400 tw-text-xs tw-font-bold tw-uppercase tw-tracking-widest tw-mb-0">Web sitesinden gelen iletişim formları ve teklifler</p>
                </div>
                <div class="tw-bg-blue-50 tw-text-blue-600 tw-px-5 tw-py-2 tw-rounded-xl tw-text-sm tw-font-black tw-border tw-border-blue-100">
                    <?php echo count($messages); ?> Toplam Mesaj
                </div>
            </div>

            <div class="tw-bg-[#fcfdfe]">
                <?php if (!empty($messages)): ?>
                    <div class="tw-divide-y tw-divide-gray-100">
                        <?php foreach ($messages as $msg): ?>
                            
                            <div class="tw-p-8 hover:tw-bg-white tw-transition-all tw-duration-300 tw-group">
                                <div class="row g-4">
                                    
                                    <div class="col-lg-3">
                                        <div class="d-flex align-items-center tw-mb-4">
                                            <div class="tw-w-14 tw-h-14 tw-rounded-2xl tw-bg-gradient-to-br tw-from-blue-600 tw-to-indigo-700 tw-text-white d-flex align-items-center justify-content-center tw-mr-4 tw-font-black tw-text-xl tw-shadow-lg tw-shadow-blue-500/20 tw-flex-shrink-0">
                                                <?php echo strtoupper(mb_substr($msg->sender_name, 0, 1, 'UTF-8')); ?>
                                            </div>
                                            <div class="tw-overflow-hidden">
                                                <h6 class="tw-font-black tw-text-gray-900 tw-text-base tw-mb-1 tw-truncate" title="<?php echo htmlspecialchars($msg->sender_name); ?>">
                                                    <?php echo htmlspecialchars($msg->sender_name); ?>
                                                </h6>
                                                <div class="tw-flex tw-flex-col tw-gap-1">
                                                    <a href="mailto:<?php echo $msg->sender_email; ?>" class="tw-text-xs tw-font-bold tw-text-gray-400 hover:tw-text-blue-600 text-decoration-none d-flex align-items-center tw-transition-colors">
                                                        <i class="fa-regular fa-envelope tw-w-4 tw-mr-1"></i> <?php echo $msg->sender_email; ?>
                                                    </a>
                                                    <a href="tel:<?php echo $msg->sender_phone; ?>" class="tw-text-xs tw-font-bold tw-text-gray-400 hover:tw-text-emerald-500 text-decoration-none d-flex align-items-center tw-transition-colors">
                                                        <i class="fa-solid fa-phone tw-w-4 tw-mr-1"></i> <?php echo $msg->sender_phone; ?>
                                                    </a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="col-lg-5">
                                        <div class="tw-bg-gray-50 tw-p-5 tw-rounded-2xl tw-border tw-border-gray-100 tw-relative tw-h-full">
                                            <?php if(isset($msg->is_read) && $msg->is_read == 0): ?>
                                                <span class="tw-absolute tw-top-[-8px] tw-right-[-8px] tw-w-4 tw-h-4 tw-bg-red-500 tw-rounded-full tw-border-4 tw-border-white tw-shadow-sm"></span>
                                            <?php endif; ?>

                                            <p class="tw-text-sm tw-text-gray-600 tw-font-medium tw-leading-relaxed tw-mb-3">
                                                <?php echo nl2br(htmlspecialchars($msg->message_content)); ?>
                                            </p>
                                            
                                            <div class="tw-flex tw-justify-between tw-items-center tw-mt-auto tw-pt-3 tw-border-t tw-border-gray-200/50">
                                                <?php if (!empty($msg->tag)): ?>
                                                    <span class="tw-bg-blue-100 tw-text-blue-700 tw-text-[9px] tw-font-black tw-uppercase tw-tracking-widest tw-px-2.5 tw-py-1 tw-rounded-md">
                                                        <i class="fa-solid fa-tag tw-mr-1"></i><?php echo htmlspecialchars($msg->tag); ?>
                                                    </span>
                                                <?php else: ?>
                                                    <span></span>
                                                <?php endif; ?>
                                                <span class="tw-text-[10px] tw-font-bold tw-text-gray-400">
                                                    <i class="fa-regular fa-clock tw-mr-1"></i> <?php echo date('d.m.Y H:i', strtotime($msg->created_at)); ?>
                                                </span>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="col-lg-2">
                                        <?php if(!empty($msg->real_property_id)): ?>
                                            <a href="<?php echo BASE_URL; ?>/properties/detail?slug=<?php echo $msg->property_slug; ?>" target="_blank" class="text-decoration-none tw-block tw-h-full">
                                                <div class="tw-bg-white tw-border tw-border-gray-100 tw-rounded-2xl tw-p-3 tw-h-full hover:tw-border-blue-300 hover:tw-shadow-md tw-transition-all d-flex flex-column align-items-center text-center justify-content-center">
                                                    <img src="<?php echo !empty($msg->property_image) ? BASE_URL . '/' . $msg->property_image : 'https://placehold.co/100x100?text=İlan'; ?>" 
                                                         class="tw-w-full tw-h-20 tw-object-cover tw-rounded-xl tw-mb-3">
                                                    <div class="tw-text-[9px] tw-text-blue-600 tw-font-black tw-uppercase tw-tracking-widest tw-mb-1">İlgilenilen İlan</div>
                                                    <div class="tw-text-[11px] tw-font-bold tw-text-gray-800 tw-line-clamp-2">
                                                        <?php echo htmlspecialchars($msg->property_title); ?>
                                                    </div>
                                                </div>
                                            </a>
                                        <?php else: ?>
                                            <div class="tw-h-full d-flex align-items-center justify-content-center tw-text-xs tw-font-bold tw-text-gray-400 tw-bg-gray-50 tw-rounded-2xl tw-border tw-border-dashed tw-border-gray-200">
                                                GENEL MESAJ
                                            </div>
                                        <?php endif; ?>
                                    </div>

                                    <div class="col-lg-2">
                                        <div class="tw-flex tw-flex-col tw-gap-2">
                                            <div class="tw-flex tw-gap-2">
                                                <a href="mailto:<?php echo $msg->sender_email; ?>" class="tw-flex-1 btn tw-bg-blue-600 hover:tw-bg-blue-700 tw-text-white tw-rounded-xl tw-py-2.5 tw-transition-all shadow-sm" title="E-posta ile Yanıtla">
                                                    <i class="fa-solid fa-reply"></i>
                                                </a>
                                                <a href="https://wa.me/90<?php echo str_replace([' ', '(', ')', '-'], '', $msg->sender_phone); ?>" target="_blank" class="tw-flex-1 btn tw-bg-emerald-500 hover:tw-bg-emerald-600 tw-text-white tw-rounded-xl tw-py-2.5 tw-transition-all shadow-sm" title="WhatsApp">
                                                    <i class="fa-brands fa-whatsapp tw-text-lg"></i>
                                                </a>
                                                <a href="<?php echo BASE_URL; ?>/admin/message_delete/<?php echo $msg->id; ?>" onclick="return confirm('Bu mesajı silmek istediğinize emin misiniz?');" class="tw-flex-1 btn tw-bg-red-50 hover:tw-bg-red-500 tw-text-red-500 hover:tw-text-white tw-rounded-xl tw-py-2.5 tw-transition-all shadow-sm" title="Sil">
                                                    <i class="fa-regular fa-trash-can"></i>
                                                </a>
                                            </div>

                                            <form action="<?php echo BASE_URL; ?>/admin/message-update/<?php echo $msg->id; ?>" method="POST" class="tw-mt-2">
                                                <select name="tag" class="form-select form-select-sm tw-bg-gray-50 tw-border-gray-200 tw-rounded-lg tw-text-[11px] tw-font-bold tw-mb-2">
                                                    <option value="" <?php echo empty($msg->tag) ? 'selected' : ''; ?>>DURUM SEÇ</option>
                                                    <option value="Yeni" <?php echo ($msg->tag ?? '') === 'Yeni' ? 'selected' : ''; ?>>YENİ</option>
                                                    <option value="Aranacak" <?php echo ($msg->tag ?? '') === 'Aranacak' ? 'selected' : ''; ?>>ARANACAK</option>
                                                    <option value="İlgileniyor" <?php echo ($msg->tag ?? '') === 'İlgileniyor' ? 'selected' : ''; ?>>İLGİLENİYOR</option>
                                                    <option value="Kapandı" <?php echo ($msg->tag ?? '') === 'Kapandı' ? 'selected' : ''; ?>>KAPANDI</option>
                                                </select>
                                                <textarea name="note" rows="2" class="form-control form-control-sm tw-bg-gray-50 tw-border-gray-200 tw-rounded-lg tw-text-[11px] tw-mb-2" placeholder="Özel not ekleyin..."><?php echo htmlspecialchars($msg->note ?? ''); ?></textarea>
                                                <button type="submit" class="btn btn-sm tw-bg-gray-800 hover:tw-bg-black tw-text-white tw-w-full tw-rounded-lg tw-font-black tw-text-[10px] tw-py-2">GÜNCELLE</button>
                                            </form>
                                        </div>
                                    </div>

                                </div>
                            </div>

                        <?php endforeach; ?>
                    </div>
                <?php else: ?>
                    <div class="text-center tw-py-24">
                        <div class="tw-w-24 tw-h-24 tw-bg-gray-50 tw-rounded-full d-flex align-items-center justify-content-center tw-mx-auto tw-mb-6">
                            <i class="fa-regular fa-envelope-open tw-text-5xl tw-text-gray-300"></i>
                        </div>
                        <h4 class="tw-font-black tw-text-gray-900 tw-text-2xl tw-mb-2">Gelen kutusu bomboş</h4>
                        <p class="tw-text-gray-500 tw-font-medium">Şu an için yanıtlanmayı bekleyen bir müşteri talebi bulunmuyor.</p>
                    </div>
                <?php endif; ?>
            </div>

        </div>
    </div>
</div>