<?php
/**
 * Admin Destek Talepleri - Liste Sayfası
 * Konum: /magaza/admin/destek/index.php
 */

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

/**
 * GÜVENLİK KONTROLÜ
 */
adminCheck();

// --- FİLTRELEME VE ARAMA ---
$where_clauses = [];
$params = [];

$arama = isset($_GET['q']) ? trim($_GET['q']) : '';
if ($arama !== '') {
    $where_clauses[] = "(u.name LIKE ? OR u.surname LIKE ? OR s.subject LIKE ?)";
    $search_val = "%$arama%";
    $params = array_merge($params, [$search_val, $search_val, $search_val]);
}

$status_filtre = isset($_GET['status']) ? $_GET['status'] : '';
if ($status_filtre !== '') {
    $where_clauses[] = "s.status = ?";
    $params[] = $status_filtre;
}

$where_sql = "";
if (count($where_clauses) > 0) {
    $where_sql = "WHERE " . implode(" AND ", $where_clauses);
}

// --- VERİ ÇEKME ---
$sql = "
    SELECT s.*, u.name, u.surname, u.email
    FROM support_tickets s
    JOIN users u ON s.user_id = u.id
    $where_sql
    ORDER BY FIELD(s.status, 'open', 'in_progress', 'answered', 'closed'), s.id DESC
";

$stmt = $db->prepare($sql);
$stmt->execute($params);
$talepler = $stmt->fetchAll();

// Durum Badge Fonksiyonu
function getTicketStatusBadge($status) {
    switch ($status) {
        case 'open': return '<span class="badge bg-danger">Açık</span>';
        case 'in_progress': return '<span class="badge bg-info text-dark">İşlemde</span>';
        case 'answered': return '<span class="badge bg-success">Cevaplandı</span>';
        case 'closed': return '<span class="badge bg-secondary">Kapandı</span>';
        default: return '<span class="badge bg-dark">Bilinmiyor</span>';
    }
}

require_once '../header.php';
?>

<div class="row mb-4 align-items-center">
    <div class="col-md-6">
        <h2 class="fw-bold mb-0 text-dark"><i class="bi bi-headset text-primary me-2"></i>Destek Talepleri</h2>
        <span class="badge bg-secondary shadow-sm"><?= count($talepler) ?> Talep Mevcut</span>
    </div>
</div>

<div class="card shadow-sm border-0 mb-4">
    <div class="card-body bg-light rounded-3 p-4 text-dark">
        <form action="index.php" method="GET" class="row g-3 align-items-end">
            <div class="col-md-5">
                <label class="form-label small fw-bold text-muted text-uppercase">Müşteri veya Konu Ara</label>
                <input type="text" name="q" class="form-control border-0 shadow-sm" value="<?= htmlspecialchars($arama) ?>" placeholder="İsim veya konu başlığı...">
            </div>
            <div class="col-md-4">
                <label class="form-label small fw-bold text-muted text-uppercase">Talep Durumu</label>
                <select name="status" class="form-select border-0 shadow-sm">
                    <option value="">Tümü</option>
                    <option value="open" <?= $status_filtre == 'open' ? 'selected' : '' ?>>Açık Talepler</option>
                    <option value="answered" <?= $status_filtre == 'answered' ? 'selected' : '' ?>>Cevaplananlar</option>
                    <option value="closed" <?= $status_filtre == 'closed' ? 'selected' : '' ?>>Kapananlar</option>
                </select>
            </div>
            <div class="col-md-3">
                <button type="submit" class="btn btn-primary w-100 fw-bold shadow">
                    <i class="bi bi-funnel me-1"></i> Filtrele
                </button>
            </div>
        </form>
    </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">
                <thead class="table-dark">
                    <tr>
                        <th class="ps-3" width="80">ID</th>
                        <th>Müşteri</th>
                        <th>Konu Başlığı</th>
                        <th class="text-center">Durum</th>
                        <th class="text-center">Son Güncelleme</th>
                        <th class="text-end pe-3">İşlem</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($talepler) > 0): ?>
                        <?php foreach ($talepler as $t): ?>
                        <tr>
                            <td class="ps-3 text-muted">#<?= $t['id'] ?></td>
                            <td>
                                <div class="fw-bold"><?= htmlspecialchars($t['name'] . ' ' . $t['surname']) ?></div>
                                <div class="text-muted small"><?= htmlspecialchars($t['email']) ?></div>
                            </td>
                            <td>
                                <div class="fw-bold text-truncate" style="max-width: 250px;"><?= htmlspecialchars($t['subject']) ?></div>
                            </td>
                            <td class="text-center">
                                <?= getTicketStatusBadge($t['status']) ?>
                            </td>
                            <td class="text-center small">
                                <?= date('d.m.Y', strtotime($t['updated_at'])) ?><br>
                                <span class="text-muted"><?= date('H:i', strtotime($t['updated_at'])) ?></span>
                            </td>
                            <td class="text-end pe-3">
                                <a href="cevapla.php?id=<?= $t['id'] ?>" class="btn btn-sm <?= $t['status'] == 'open' ? 'btn-primary' : 'btn-outline-secondary' ?> shadow-sm px-3 fw-bold">
                                    <i class="bi bi-chat-left-text me-1"></i> Görüntüle
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="6" class="text-center py-5 text-muted fw-bold italic">
                                <i class="bi bi-emoji-smile fs-1 d-block mb-2"></i>
                                Şu an cevaplanması gereken bir destek talebi yok.
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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