<?php
require_once '../../config/config.php';
require_once '../../includes/auth.php';

requireAdminLogin();
ensureFeatureAndPaymentTables();

$status = $_GET['status'] ?? '';
if (!in_array($status, ['', 'pending', 'processed'], true)) {
    $status = '';
}

$where = '';
$params = [];
if ($status) {
    $where = "WHERE p.status = ?";
    $params[] = $status;
}

$stmt = $pdo->prepare("
    SELECT p.*, c.company_name, c.email
    FROM payment_notifications p
    INNER JOIN companies c ON c.id = p.company_id
    $where
    ORDER BY p.created_at DESC
");
$stmt->execute($params);
$payments = $stmt->fetchAll();

$page_title = 'Ödeme Bildirimleri';
include '../includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">Ödeme Bildirimleri</h1>
    <form method="GET" class="d-flex gap-2">
        <select class="form-select" name="status" style="max-width:200px;">
            <option value="" <?= $status === '' ? 'selected' : '' ?>>Tümü</option>
            <option value="pending" <?= $status === 'pending' ? 'selected' : '' ?>>Beklemede</option>
            <option value="processed" <?= $status === 'processed' ? 'selected' : '' ?>>İşlendi</option>
        </select>
        <button class="btn btn-primary" type="submit">Filtrele</button>
    </form>
</div>

<div class="card">
    <div class="card-body">
        <?php if (empty($payments)): ?>
            <p class="text-muted mb-0">Henüz bildirim yok.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Firma</th>
                            <th>Tip</th>
                            <th>Tutar</th>
                            <th>Gönderen</th>
                            <th>Açıklama</th>
                            <th>Durum</th>
                            <th>Tarih</th>
                            <th>İşlem</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($payments as $p): ?>
                            <tr>
                                <td><?= (int)$p['id'] ?></td>
                                <td>
                                    <div class="fw-semibold"><?= e($p['company_name']) ?></div>
                                    <div class="text-muted small"><?= e($p['email']) ?></div>
                                </td>
                                <td>
                                    <span class="badge bg-secondary">
                                        <?= $p['type'] === 'gold' ? 'Gold' : ($p['type'] === 'featured_service' ? 'İlan' : 'Diğer') ?>
                                    </span>
                                </td>
                                <td class="fw-semibold"><?= number_format((float)$p['amount'], 2) ?> ₺</td>
                                <td><?= e($p['sender_name']) ?></td>
                                <td style="max-width: 320px;"><?= e($p['description']) ?></td>
                                <td>
                                    <span class="badge bg-<?= $p['status'] === 'processed' ? 'success' : 'warning' ?>">
                                        <?= $p['status'] === 'processed' ? 'İşlendi' : 'Beklemede' ?>
                                    </span>
                                </td>
                                <td><?= formatDate($p['created_at'], 'd.m.Y H:i') ?></td>
                                <td>
                                    <?php if ($p['status'] !== 'processed'): ?>
                                        <a class="btn btn-sm btn-success" href="mark-processed.php?id=<?= (int)$p['id'] ?>" onclick="return confirm('İşlendi olarak işaretlensin mi?')">
                                            <i class="fas fa-check"></i>
                                        </a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php include '../includes/footer.php'; ?>


