<?php
/**
 * Sınav Koçu Admin Paneli - Gelişmiş Müşteri Detay Merkezi (V3.0)
 * Konum: /magaza/admin/musteri/detay.php
 * * Bu sayfa; profil yönetimi, sipariş takibi, ödeme geçmişi, DENEME PAKETLERİ 
 * ve tüm iletişim kayıtlarını (SMS/Mail) tek bir dashboard üzerinde birleştirir.
 */

// 1. SİSTEM DOSYALARININ ÇAĞRILMASI
// -------------------------------------------------------------------------
require_once '../../includes/db.php';        // Veritabanı Bağlantısı
require_once '../../includes/functions.php'; // Genel Yardımcı Fonksiyonlar
require_once '../../includes/mail.php';      // E-Posta Gönderim Servisi
require_once '../../includes/sms.php';       // SMS Gönderim Servisi

/**
 * GÜVENLİK KONTROLÜ
 * Oturum açmamış veya yetkisi olmayan kullanıcıları login sayfasına atar.
 */
adminCheck();

// 2. BAŞLANGIÇ DEĞİŞKENLERİ VE PARAMETRELER
// -------------------------------------------------------------------------
$mesaj = '';
$hata  = '';

// URL'den gelen müşteri ID'sini kontrol et ve sanitize et
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header("Location: index.php");
    exit;
}
$musteri_id = (int)$_GET['id'];

/**
 * 3. POST İŞLEMLERİ: İLETİŞİM MOTORU
 * Modal pencerelerinden gelen SMS ve Mail gönderim taleplerini işler.
 */
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    // --- HIZLI SMS GÖNDERİMİ ---
    if (isset($_POST['detay_sms_gonder'])) {
        $u_id       = (int)$_POST['target_user_id'];
        $tel        = trim($_POST['target_phone']);
        $sms_icerik = trim($_POST['sms_message']);
        
        if (empty($sms_icerik)) {
            $mesaj = "<div class='alert alert-danger shadow-sm'>Hata: SMS içeriği boş olamaz!</div>";
        } else {
            // Loglama için $u_id (müşteri ID) mutlaka gönderilir
            $sonuc = sms_send_simple($tel, $sms_icerik, $u_id); 
            if ($sonuc['ok']) {
                $mesaj = "<div class='alert alert-success alert-dismissible fade show shadow-sm' role='alert'>
                            <i class='bi bi-check-circle-fill me-2'></i> SMS başarıyla gönderildi ve iletişim günlüğüne kaydedildi!
                            <button type='button' class='btn-close' data-bs-dismiss='alert'></button>
                          </div>";
            } else {
                $mesaj = "<div class='alert alert-warning shadow-sm'>SMS servisi hata verdi, ancak günlük kaydı oluşturulmuş olabilir.</div>";
            }
        }
    }
    
    // --- HIZLI E-POSTA GÖNDERİMİ ---
    if (isset($_POST['detay_mail_gonder'])) {
        $u_id         = (int)$_POST['target_user_id'];
        $email        = trim($_POST['target_email']);
        $konu         = trim($_POST['mail_subject']);
        $mail_icerik  = nl2br(trim($_POST['mail_message']));
        
        if (empty($mail_icerik) || empty($konu)) {
            $mesaj = "<div class='alert alert-danger shadow-sm'>Hata: Konu ve mesaj alanları zorunludur!</div>";
        } else {
            // sendSystemMail fonksiyonuna $u_id gönderilerek CRM kaydı sağlanır
            $mail_sonuc = sendSystemMail($email, $konu, $mail_icerik, $u_id);
            if ($mail_sonuc) {
                $mesaj = "<div class='alert alert-success alert-dismissible fade show shadow-sm' role='alert'>
                            <i class='bi bi-envelope-check-fill me-2'></i> E-posta başarıyla gönderildi ve iletişim günlüğüne kaydedildi!
                            <button type='button' class='btn-close' data-bs-dismiss='alert'></button>
                          </div>";
            } else {
                $mesaj = "<div class='alert alert-danger shadow-sm'>E-posta gönderimi sırasında bir hata oluştu!</div>";
            }
        }
    }
}

/**
 * 4. VERİ ÇEKME İŞLEMLERİ (SQL)
 * Sayfada gösterilecek tüm veriler ilişkisel tablolarla birlikte çekilir.
 */

// A. Müşteri Temel Profil Bilgileri
$stmt = $db->prepare("SELECT * FROM users WHERE id = ? AND role = 'customer'");
$stmt->execute([$musteri_id]);
$m = $stmt->fetch();

if (!$m) {
    die("<div class='alert alert-danger m-5 text-center fw-bold shadow-lg py-5'>
            <i class='bi bi-exclamation-octagon fs-1 d-block mb-3'></i>
            KRİTİK HATA: MÜŞTERİ VERİTABANINDA BULUNAMADI!<br>
            <a href='index.php' class='btn btn-light mt-4 px-4'>Listeye Geri Dön</a>
         </div>");
}

// B. Sipariş Geçmişi
$orders_stmt = $db->prepare("SELECT * FROM orders WHERE user_id = ? ORDER BY id DESC");
$orders_stmt->execute([$musteri_id]);
$siparisler = $orders_stmt->fetchAll();

// C. Ödeme Kayıtları (Sipariş Numarası ile Join)
$pay_stmt = $db->prepare("
    SELECT p.*, o.order_number 
    FROM payments p 
    JOIN orders o ON p.order_id = o.id 
    WHERE o.user_id = ? 
    ORDER BY p.id DESC
");
$pay_stmt->execute([$musteri_id]);
$odemeler = $pay_stmt->fetchAll();

// D. İletişim Günlüğü (Tüm SMS ve Mail Arşivi)
$log_stmt = $db->prepare("SELECT * FROM communication_logs WHERE user_id = ? ORDER BY id DESC");
$log_stmt->execute([$musteri_id]);
$iletisim_gecmisi = $log_stmt->fetchAll();

// E. YENİ: DENEME PAKETLERİ BİLGİLERİ
// Müşterinin satın aldığı 'exam_package' türündeki ürünleri ve order_deliveries tablosundaki lojistik sürecini çeker.
$paketler_sorgu = $db->prepare("
    SELECT 
        o.order_number, o.created_at as siparis_tarihi,
        oi.product_id as package_id,
        p.name as package_name,
        (SELECT COUNT(id) FROM order_deliveries WHERE order_id = o.id AND package_id = p.id) as toplam_kutu,
        (SELECT COUNT(id) FROM order_deliveries WHERE order_id = o.id AND package_id = p.id AND status IN('shipped','delivered')) as kargolanan_kutu,
        (SELECT planned_date FROM order_deliveries WHERE order_id = o.id AND package_id = p.id AND status = 'pending' ORDER BY delivery_sequence ASC LIMIT 1) as siradaki_kargo_tarihi
    FROM orders o
    JOIN order_items oi ON o.id = oi.order_id
    JOIN products p ON oi.product_id = p.id
    WHERE o.user_id = ? AND p.type = 'exam_package' AND o.status != 'cancelled'
    ORDER BY o.id DESC
");
$paketler_sorgu->execute([$musteri_id]);
$deneme_paketleri = $paketler_sorgu->fetchAll(PDO::FETCH_ASSOC);

// F. Harcama İstatistikleri
$total_spent = $db->prepare("SELECT SUM(total_amount) FROM orders WHERE user_id = ? AND status != 'cancelled'");
$total_spent->execute([$musteri_id]);
$spent_val = $total_spent->fetchColumn() ?: 0;

/**
 * 5. YARDIMCI GÖRSEL FONKSİYONLAR
 */
function getBadgeByStatus($status) {
    switch ($status) {
        case 'delivered': return '<span class="badge bg-success rounded-pill px-3">Teslim Edildi</span>';
        case 'pending':   return '<span class="badge bg-warning text-dark rounded-pill px-3">Onay Bekliyor</span>';
        case 'processing':return '<span class="badge bg-info text-dark rounded-pill px-3">İşlemde/Parçalı</span>';
        case 'cancelled': return '<span class="badge bg-danger rounded-pill px-3">İptal Edildi</span>';
        case 'shipped':   return '<span class="badge bg-primary rounded-pill px-3">Kargolandı</span>';
        default:          return '<span class="badge bg-secondary rounded-pill px-3">'.$status.'</span>';
    }
}

// Tasarımın baş kısmını dahil et
require_once '../header.php';
?>

<style>
    /* Pasif sekmelerin beyaz fonda görünmesi için renk tanımları */
    #musteriTab .nav-link {
        color: #495057 !important; 
        font-weight: 700;
        margin-right: 8px;
        padding: 12px 25px;
        border: 1px solid #e9ecef;
        transition: all 0.3s ease;
        background: #fff;
    }
    /* Aktif olan sekme tasarımı */
    #musteriTab .nav-link.active {
        color: #fff !important;
        background-color: #0d6efd !important;
        border-color: #0d6efd !important;
        box-shadow: 0 4px 10px rgba(13, 110, 253, 0.2);
    }
    /* Üzerine gelme efekti */
    #musteriTab .nav-link:hover:not(.active) {
        background-color: #f8f9fa;
        border-color: #dee2e6;
        color: #000 !important;
    }
    /* Kart başlıkları için ince ayar */
    .card-title-small {
        font-size: 0.8rem;
        letter-spacing: 1px;
        color: #6c757d;
    }
    .avatar-circle {
        width: 100px;
        height: 100px;
        line-height: 100px;
        font-size: 2.5rem;
    }
</style>

<div class="row mb-4 align-items-center">
    <div class="col-md-7 text-dark">
        <h2 class="fw-bold mb-0">
            <a href="index.php" class="btn btn-sm btn-outline-secondary me-3 shadow-sm border-2">
                <i class="bi bi-arrow-left"></i>
            </a>
            Müşteri: <span class="text-primary"><?= htmlspecialchars($m['name'].' '.$m['surname']) ?></span>
        </h2>
        <p class="text-muted mb-0 ms-5 ps-3 small">Sistem ID: #<?= $musteri_id ?> | Müşteri dosyası inceleniyor.</p>
    </div>
    <div class="col-md-5 text-md-end mt-3 mt-md-0">
        <div class="btn-group shadow-sm">
            <a href="duzenle.php?id=<?= $musteri_id ?>" class="btn btn-warning fw-bold px-4 py-2 border-2">
                <i class="bi bi-pencil-square me-2"></i> PROFİLİ GÜNCELLE
            </a>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-12"><?= $mesaj; ?></div>
</div>

<div class="row text-dark">
    
    <div class="col-lg-4 col-xl-3">
        
        <div class="card shadow-sm border-0 mb-4 overflow-hidden">
            <div class="card-body p-4 text-center">
                <div class="mb-3 d-flex justify-content-center">
                    <div class="bg-primary text-white rounded-circle fw-bold d-flex align-items-center justify-content-center shadow-lg avatar-circle">
                        <?= strtoupper(mb_substr($m['name'],0,1).mb_substr($m['surname'],0,1)) ?>
                    </div>
                </div>
                <h4 class="fw-bold mb-1 text-dark"><?= htmlspecialchars($m['name'].' '.$m['surname']) ?></h4>
                <p class="text-muted small mb-0">Kayıt: <?= date('d.m.Y', strtotime($m['created_at'])) ?></p>
                <div class="mt-4">
                    <span class="badge <?= $m['status'] == 1 ? 'bg-success' : 'bg-danger' ?> w-100 py-3 shadow-sm rounded-3">
                        <i class="bi <?= $m['status'] == 1 ? 'bi-check-circle-fill' : 'bi-person-x-fill' ?> me-2"></i>
                        <?= $m['status'] == 1 ? 'HESAP ŞU AN AKTİF' : 'HESAP ENGELLENDİ' ?>
                    </span>
                </div>
            </div>
        </div>

        <div class="card shadow-sm border-0 mb-4">
            <div class="card-header bg-white py-3 border-bottom">
                <h6 class="mb-0 fw-bold card-title-small fw-bold text-uppercase">
                    <i class="bi bi-chat-quote-fill me-2 text-primary"></i>Hızlı İletişim Kanalları
                </h6>
            </div>
            <div class="card-body">
                <div class="mb-3">
                    <small class="text-muted d-block small fw-bold text-uppercase mb-1">E-Posta Adresi:</small>
                    <span class="fw-bold text-dark small"><?= htmlspecialchars($m['email']) ?></span>
                </div>
                <div class="mb-4">
                    <small class="text-muted d-block small fw-bold text-uppercase mb-1">Telefon Numarası:</small>
                    <span class="fw-bold text-dark small"><?= htmlspecialchars($m['phone'] ?: 'Girilmemiş') ?></span>
                </div>
                
                <div class="d-grid gap-3">
                    <button type="button" class="btn btn-outline-primary fw-bold py-2 border-2 shadow-sm" data-bs-toggle="modal" data-bs-target="#mailModal">
                        <i class="bi bi-envelope-at-fill me-2"></i> E-POSTA GÖNDER
                    </button>
                    
                    <?php if($m['phone']): ?>
                        <button type="button" class="btn btn-outline-success fw-bold py-2 border-2 shadow-sm" data-bs-toggle="modal" data-bs-target="#smsModal">
                            <i class="bi bi-chat-left-dots-fill me-2"></i> SMS MESAJI AT
                        </button>
                        <a href="https://wa.me/<?= preg_replace('/[^0-9]/', '', $m['phone']) ?>" target="_blank" class="btn btn-success fw-bold py-2 shadow-sm border-2">
                            <i class="bi bi-whatsapp me-2"></i> WHATSAPP'TAN YAZ
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <div class="card shadow-sm border-0 bg-white p-4 mb-4">
            <div class="row text-center">
                <div class="col-6 border-end">
                    <small class="text-muted d-block card-title-small fw-bold mb-1">SİPARİŞ</small>
                    <span class="h4 fw-bold text-dark"><?= count($siparisler) ?></span>
                </div>
                <div class="col-6">
                    <small class="text-muted d-block card-title-small fw-bold mb-1">CİRO</small>
                    <span class="h4 fw-bold text-success"><?= number_format($spent_val, 2, ',', '.') ?>₺</span>
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-8 col-xl-9">
        
        <ul class="nav nav-pills mb-4 bg-white p-2 rounded shadow-sm border" id="musteriTab" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" data-bs-toggle="pill" data-bs-target="#orders" type="button" role="tab">
                    <i class="bi bi-cart3 me-2"></i>SİPARİŞLER (<?= count($siparisler) ?>)
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" data-bs-toggle="pill" data-bs-target="#denemepaketleri" type="button" role="tab">
                    <i class="bi bi-box-seam me-2"></i>DENEME PAKETLERİ (<?= count($deneme_paketleri) ?>)
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" data-bs-toggle="pill" data-bs-target="#payments" type="button" role="tab">
                    <i class="bi bi-wallet2 me-2"></i>ÖDEMELER
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" data-bs-toggle="pill" data-bs-target="#history" type="button" role="tab">
                    <i class="bi bi-journal-text me-2"></i>İLETİŞİM GEÇMİŞİ
                </button>
            </li>
        </ul>

        <div class="tab-content shadow-sm bg-white rounded border overflow-hidden" id="tabContent">
            
            <div class="tab-pane fade show active" id="orders" role="tabpanel">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0 text-dark">
                        <thead class="table-light">
                            <tr>
                                <th class="ps-3 py-3" width="150">Sipariş No</th>
                                <th>Satın Alım Tarihi</th>
                                <th>Toplam Tutar</th>
                                <th class="text-center">İşlem Durumu</th>
                                <th class="text-end pe-3">Detay</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if($siparisler): foreach($siparisler as $s): ?>
                            <tr>
                                <td class="ps-3 fw-bold text-primary">#<?= $s['order_number'] ?></td>
                                <td class="small"><?= date('d.m.Y H:i', strtotime($s['created_at'])) ?></td>
                                <td class="fw-bold"><?= number_format($s['total_amount'],2,',','.') ?> ₺</td>
                                <td class="text-center"><?= getBadgeByStatus($s['status']) ?></td>
                                <td class="text-end pe-3">
                                    <a href="../siparisler/detay.php?id=<?= $s['id'] ?>" class="btn btn-sm btn-outline-dark" title="İncele">
                                        <i class="bi bi-search"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; else: ?>
                            <tr><td colspan="5" class="text-center py-5 text-muted small">Henüz bir sipariş kaydı bulunmuyor.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="tab-pane fade" id="denemepaketleri" role="tabpanel">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0 text-dark">
                        <thead class="table-light border-bottom">
                            <tr>
                                <th class="ps-3 py-3">Özel Paket Adı</th>
                                <th>Sipariş No & Tarih</th>
                                <th class="text-center">Lojistik İlerlemesi</th>
                                <th class="text-center">Sıradaki Kargo</th>
                                <th class="text-center">Aksiyon</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if($deneme_paketleri): foreach($deneme_paketleri as $pkg): 
                                $yuzde = ($pkg['toplam_kutu'] > 0) ? round(($pkg['kargolanan_kutu'] / $pkg['toplam_kutu']) * 100) : 0;
                            ?>
                            <tr>
                                <td class="ps-3 py-3">
                                    <div class="fw-bold text-primary"><i class="bi bi-gift-fill text-warning me-1"></i> <?= htmlspecialchars($pkg['package_name']) ?></div>
                                </td>
                                <td>
                                    <div class="fw-bold text-dark">#<?= $pkg['order_number'] ?></div>
                                    <small class="text-muted"><?= date('d.m.Y', strtotime($pkg['siparis_tarihi'])) ?></small>
                                </td>
                                <td class="text-center">
                                    <div class="progress mb-1 mx-auto" style="height: 6px; width: 80%;">
                                        <div class="progress-bar bg-success" style="width: <?= $yuzde ?>%"></div>
                                    </div>
                                    <span class="badge bg-light text-dark border" style="font-size:0.65rem;"><?= $pkg['kargolanan_kutu'] ?> / <?= $pkg['toplam_kutu'] ?> Kutu Yola Çıktı</span>
                                </td>
                                <td class="text-center">
                                    <?php if($pkg['siradaki_kargo_tarihi']): ?>
                                        <span class="badge bg-warning text-dark"><i class="bi bi-clock-history me-1"></i> <?= date('d.m.Y', strtotime($pkg['siradaki_kargo_tarihi'])) ?></span>
                                    <?php else: ?>
                                        <span class="badge bg-success"><i class="bi bi-check-all"></i> Tamamlandı</span>
                                    <?php endif; ?>
                                </td>
                                <td class="text-center">
                                    <a href="../siparisler/detay.php?id=<?= $pkg['order_number'] /* order_number ile query yapılıyor varsayılır, ya da siparişi order_id ile çekmek daha güvenli */ ?>" class="btn btn-sm btn-primary py-1 px-3 rounded-pill fw-bold" style="font-size:0.7rem;">Takvim ></a>
                                </td>
                            </tr>
                            <?php endforeach; else: ?>
                            <tr><td colspan="5" class="text-center py-5 text-muted small">Müşterinin satın aldığı bir deneme paketi bulunmamaktadır.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="tab-pane fade" id="payments" role="tabpanel">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0 text-dark">
                        <thead class="table-light">
                            <tr>
                                <th class="ps-3 py-3">Ödeme ID</th>
                                <th>Sipariş No</th>
                                <th>Ödeme Yöntemi</th>
                                <th>İşlem Tarihi</th>
                                <th class="text-end pe-3">Net Tutar</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if($odemeler): foreach($odemeler as $p): ?>
                            <tr>
                                <td class="ps-3 text-muted small">#TR-<?= $p['id'] ?></td>
                                <td class="fw-bold">#<?= $p['order_number'] ?></td>
                                <td>
                                    <span class="small border px-2 py-1 rounded bg-light text-uppercase fw-bold">
                                        <?= $p['payment_method'] == 'credit_card' ? 'Kredi Kartı' : 'EFT / Havale' ?>
                                    </span>
                                </td>
                                <td class="small"><?= date('d.m.Y H:i', strtotime($p['created_at'])) ?></td>
                                <td class="text-end pe-3 fw-bold text-success"><?= number_format($p['amount'],2,',','.') ?> ₺</td>
                            </tr>
                            <?php endforeach; else: ?>
                            <tr><td colspan="5" class="text-center py-5 text-muted small">Henüz onaylı bir ödeme kaydı bulunmuyor.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="tab-pane fade" id="history" role="tabpanel">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0 text-dark">
                        <thead class="table-light">
                            <tr>
                                <th class="ps-3 py-3">Gönderim Tarihi</th>
                                <th>Tür</th>
                                <th>Alıcı / Hedef</th>
                                <th>İçerik Özeti</th>
                                <th class="text-end pe-3">İncele</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if($iletisim_gecmisi): foreach($iletisim_gecmisi as $log): ?>
                            <tr>
                                <td class="ps-3 small text-muted"><?= date('d.m.Y H:i:s', strtotime($log['created_at'])) ?></td>
                                <td>
                                    <?= $log['type'] == 'email' 
                                        ? '<span class="badge bg-primary-subtle text-primary border border-primary-subtle px-3">E-POSTA</span>' 
                                        : '<span class="badge bg-success-subtle text-success border border-success-subtle px-3">SMS</span>' ?>
                                </td>
                                <td class="small fw-bold"><?= $log['recipient'] ?></td>
                                <td class="small text-truncate" style="max-width: 300px;">
                                    <?php if($log['subject']): ?><span class="text-primary fw-bold">[<?= $log['subject'] ?>]</span> <?php endif; ?>
                                    <?= htmlspecialchars(strip_tags($log['message'])) ?>
                                </td>
                                <td class="text-end pe-3">
                                    <button type="button" class="btn btn-sm btn-light border shadow-sm" onclick='alert(`<?= addslashes($log["message"]) ?>`)'>
                                        <i class="bi bi-eye-fill"></i>
                                    </button>
                                </td>
                            </tr>
                            <?php endforeach; else: ?>
                            <tr><td colspan="5" class="text-center py-5 text-muted small">Müşteriye daha önce sistemden mesaj gönderilmemiş.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="smsModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <form action="" method="POST" class="modal-content border-0 shadow-lg">
            <input type="hidden" name="target_user_id" value="<?= $musteri_id ?>">
            <input type="hidden" name="target_phone" value="<?= $m['phone'] ?>">
            
            <div class="modal-header bg-success text-white py-3">
                <h5 class="modal-title fw-bold"><i class="bi bi-chat-left-dots-fill me-2"></i>Hızlı SMS Gönder</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body p-4 text-dark text-start">
                <div class="mb-3">
                    <label class="form-label small fw-bold text-muted text-uppercase">Hedef Numara</label>
                    <input type="text" class="form-control bg-light fw-bold" value="<?= $m['phone'] ?>" readonly disabled>
                </div>
                <div class="mb-0">
                    <label class="form-label small fw-bold text-muted text-uppercase">SMS Mesaj Metni</label>
                    <textarea name="sms_message" class="form-control border-2 shadow-none" rows="6" placeholder="Müşteriye iletilecek mesajı buraya yazın..." required></textarea>
                    <div class="form-text text-end small">Karakter sınırı operatöre göre değişebilir.</div>
                </div>
            </div>
            <div class="modal-footer bg-light">
                <button type="button" class="btn btn-secondary fw-bold px-4" data-bs-dismiss="modal">İPTAL</button>
                <button type="submit" name="detay_sms_gonder" class="btn btn-success fw-bold px-4 shadow">
                    <i class="bi bi-send-fill me-2"></i> SMS'İ GÖNDER VE GÜNLÜĞE İŞLE
                </button>
            </div>
        </form>
    </div>
</div>

<div class="modal fade" id="mailModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg">
        <form action="" method="POST" class="modal-content border-0 shadow-lg">
            <input type="hidden" name="target_user_id" value="<?= $musteri_id ?>">
            <input type="hidden" name="target_email" value="<?= $m['email'] ?>">
            
            <div class="modal-header bg-primary text-white py-3">
                <h5 class="modal-title fw-bold"><i class="bi bi-envelope-paper-fill me-2"></i>Hızlı E-posta Gönder</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body p-4 text-dark text-start">
                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label small fw-bold text-muted text-uppercase">Alıcı Adresi</label>
                        <input type="text" class="form-control bg-light fw-bold" value="<?= $m['email'] ?>" readonly disabled>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label small fw-bold text-muted text-uppercase">E-posta Konusu</label>
                        <input type="text" name="mail_subject" class="form-control border-2 fw-bold" placeholder="Örn: Paket Aktivasyon Bilgisi" required>
                    </div>
                </div>
                <div class="mb-0">
                    <label class="form-label small fw-bold text-muted text-uppercase">Mesaj İçeriği</label>
                    <textarea name="mail_message" class="form-control border-2 shadow-none" rows="10" placeholder="Detaylı e-posta metnini buraya giriniz..." required></textarea>
                </div>
            </div>
            <div class="modal-footer bg-light border-top-0">
                <button type="button" class="btn btn-secondary fw-bold px-4" data-bs-dismiss="modal">VAZGEÇ</button>
                <button type="submit" name="detay_mail_gonder" class="btn btn-primary fw-bold px-4 shadow">
                    <i class="bi bi-envelope-check-fill me-2"></i> E-POSTAYI GÖNDER VE ARŞİVLE
                </button>
            </div>
        </form>
    </div>
</div>

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