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

requireAdminLogin();
ensureFeatureAndPaymentTables();

// Talepleri listele
$stmt = $pdo->query("
    SELECT r.*, s.title as service_title, s.price, s.image, c.company_name
    FROM service_feature_requests r
    INNER JOIN services s ON s.id = r.service_id
    INNER JOIN companies c ON c.id = r.company_id
    ORDER BY r.requested_at DESC
");
$requests = $stmt->fetchAll();

$page_title = 'İlan Talepleri';
include '../includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">İlan Talepleri</h1>
</div>

<div class="card">
    <div class="card-body">
        <?php if (empty($requests)): ?>
            <p class="text-muted mb-0">Henüz talep yok.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Firma</th>
                            <th>İlan/Hizmet</th>
                            <th>Durum</th>
                            <th>Talep</th>
                            <th>Onay Bitiş</th>
                            <th>İşlem</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($requests as $r): ?>
                            <tr>
                                <td><?= (int)$r['id'] ?></td>
                                <td><?= e($r['company_name']) ?></td>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <?php if (!empty($r['image'])): ?>
                                            <img src="<?= e(getImageUrl($r['image'])) ?>" style="width:48px;height:32px;object-fit:cover;border-radius:6px;" alt="">
                                        <?php endif; ?>
                                        <div>
                                            <div class="fw-semibold"><?= e($r['service_title']) ?></div>
                                            <?php if (!empty($r['price'])): ?>
                                                <div class="text-muted small"><?= number_format((float)$r['price'], 2) ?> ₺</div>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge bg-<?= $r['status'] === 'approved' ? 'success' : ($r['status'] === 'rejected' ? 'danger' : 'warning') ?>">
                                        <?= $r['status'] === 'approved' ? 'Onaylı' : ($r['status'] === 'rejected' ? 'Reddedildi' : 'Beklemede') ?>
                                    </span>
                                </td>
                                <td><?= formatDate($r['requested_at'], 'd.m.Y H:i') ?></td>
                                <td><?= $r['approved_until'] ? formatDate($r['approved_until'], 'd.m.Y') : '-' ?></td>
                                <td>
                                    <?php if ($r['status'] !== 'approved'): ?>
                                        <a class="btn btn-sm btn-success" href="approve.php?id=<?= (int)$r['id'] ?>" onclick="return confirm('15 gün onaylamak istiyor musunuz?')">
                                            <i class="fas fa-check"></i>
                                        </a>
                                    <?php endif; ?>
                                    <?php if ($r['status'] !== 'rejected'): ?>
                                        <a class="btn btn-sm btn-danger" href="reject.php?id=<?= (int)$r['id'] ?>" onclick="return confirm('Reddetmek istiyor musunuz?')">
                                            <i class="fas fa-times"></i>
                                        </a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

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


