<?php
/**
 * Admin İletişim Günlüğü - Tüm SMS ve Mailler (Müşteri Bağlantılı)
 * Konum: /magaza/admin/iletisim_gunlugu.php
 */

require_once '../includes/db.php';
require_once '../includes/functions.php';

adminCheck();

// Filtreleme
$type_filtre = isset($_GET['type']) ? $_GET['type'] : '';
$where = "";
$params = [];

if ($type_filtre) {
    $where = "WHERE type = ?";
    $params[] = $type_filtre;
}

// SQL Sorgusu: u.id'yi de çekiyoruz ki detay sayfasına link verebilelim
$sql = "SELECT l.*, u.id as customer_id, u.name, u.surname 
        FROM communication_logs l 
        LEFT JOIN users u ON l.user_id = u.id 
        $where 
        ORDER BY l.id DESC LIMIT 100";
$stmt = $db->prepare($sql);
$stmt->execute($params);
$loglar = $stmt->fetchAll();

require_once 'header.php';
?>

<div class="row mb-4 align-items-center">
    <div class="col-md-8">
        <h2 class="fw-bold text-dark"><i class="bi bi-journal-richtext text-primary me-2"></i>İletişim Günlüğü</h2>
        <p class="text-muted small mb-0">Sistem üzerinden gönderilen son 100 SMS ve E-posta kaydı.</p>
    </div>
    <div class="col-md-4 text-md-end mt-3 mt-md-0">
        <div class="btn-group shadow-sm">
            <a href="iletisim_gunlugu.php" class="btn btn-sm btn-outline-dark <?= !$type_filtre ? 'active' : '' ?>">Hepsi</a>
            <a href="?type=email" class="btn btn-sm btn-outline-primary <?= $type_filtre == 'email' ? 'active' : '' ?>">Mailler</a>
            <a href="?type=sms" class="btn btn-sm btn-outline-success <?= $type_filtre == 'sms' ? 'active' : '' ?>">SMS'ler</a>
        </div>
    </div>
</div>

<div class="card shadow-sm border-0 mb-5">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0 text-dark">
                <thead class="table-dark">
                    <tr>
                        <th class="ps-3" width="160">Tarih / Saat</th>
                        <th width="100">Tür</th>
                        <th>Müşteri / Hedef</th>
                        <th>Mesaj Özeti</th>
                        <th class="text-center" width="100">Durum</th>
                        <th class="text-end pe-3" width="80">İncele</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(count($loglar) > 0): ?>
                        <?php foreach($loglar as $l): ?>
                        <tr>
                            <td class="ps-3 small">
                                <div class="fw-bold"><?= date('d.m.Y', strtotime($l['created_at'])) ?></div>
                                <div class="text-muted"><?= date('H:i:s', strtotime($l['created_at'])) ?></div>
                            </td>
                            <td>
                                <?php if($l['type'] == 'email'): ?>
                                    <span class="badge bg-primary-subtle text-primary border border-primary-subtle px-3 w-100">E-Posta</span>
                                <?php else: ?>
                                    <span class="badge bg-success-subtle text-success border border-success-subtle px-3 w-100">SMS</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if($l['name']): ?>
                                    <a href="musteri/detay.php?id=<?= $l['customer_id'] ?>" class="text-decoration-none fw-bold text-dark">
                                        <i class="bi bi-person-circle me-1 text-primary"></i> <?= htmlspecialchars($l['name'].' '.$l['surname']) ?>
                                    </a>
                                    <div class="small text-muted"><?= htmlspecialchars($l['recipient']) ?></div>
                                <?php else: ?>
                                    <div class="fw-bold text-secondary">
                                        <i class="bi bi-robot me-1"></i> Kayıtsız / Sistem
                                    </div>
                                    <div class="small text-muted"><?= htmlspecialchars($l['recipient']) ?></div>
                                <?php endif; ?>
                            </td>
                            <td>
                                <div class="text-truncate" style="max-width: 350px;">
                                    <?php if($l['subject']): ?>
                                        <span class="fw-bold text-primary">[<?= htmlspecialchars($l['subject']) ?>]</span> 
                                    <?php endif; ?>
                                    <span class="text-muted"><?= strip_tags($l['message']) ?></span>
                                </div>
                            </td>
                            <td class="text-center">
                                <?php if($l['status'] == 'sent'): ?>
                                    <i class="bi bi-check2-all text-success fs-5" title="Gönderildi"></i>
                                <?php else: ?>
                                    <i class="bi bi-exclamation-triangle text-danger fs-5" title="Hata"></i>
                                <?php endif; ?>
                            </td>
                            <td class="text-end pe-3">
                                <button type="button" class="btn btn-sm btn-light border shadow-sm" onclick='alert(<?= json_encode($l["message"]) ?>)'>
                                    <i class="bi bi-eye-fill"></i>
                                </button>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="6" class="text-center py-5 text-muted">
                                <i class="bi bi-inbox fs-1 d-block mb-2"></i>
                                Henüz bir iletişim kaydı bulunmuyor.
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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