<?php
/**
 * Admin Kupon Yönetimi - Liste
 * Konum: /magaza/admin/kuponlar/index.php
 */

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

adminCheck();

// Silme İşlemi
if (isset($_GET['sil']) && is_numeric($_GET['sil'])) {
    $sil_id = (int)$_GET['sil'];
    $db->prepare("DELETE FROM coupons WHERE id = ?")->execute([$sil_id]);
    header("Location: index.php?mesaj=silindi");
    exit;
}

if (isset($_GET['sil']) && is_numeric($_GET['sil'])) {
    $sil_id = (int)$_GET['sil'];
    $db->prepare("DELETE FROM coupons WHERE id = ?")->execute([$sil_id]);
    header("Location: index.php?mesaj=silindi");
    exit;
}

// Verileri Çek
$kuponlar = $db->query("SELECT * FROM coupons ORDER BY id DESC")->fetchAll();

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

<div class="row mb-4 align-items-center text-dark">
    <div class="col-md-6">
        <h2 class="fw-bold mb-0"><i class="bi bi-ticket-perforated text-primary me-2"></i>Kupon & Promosyonlar</h2>
        <p class="text-muted small">İndirim kodlarını ve kampanya performanslarını yönetin.</p>
    </div>
    <div class="col-md-6 text-md-end">
        <a href="ekle.php" class="btn btn-primary fw-bold shadow-sm px-4">
            <i class="bi bi-plus-lg me-2"></i> YENİ KUPON OLUŞTUR
        </a>
    </div>
</div>

<?php if(isset($_GET['mesaj']) && $_GET['mesaj'] == 'silindi'): ?>
    <div class="alert alert-success shadow-sm">Kupon başarıyla silindi.</div>
<?php endif; ?>

<div class="card shadow-sm border-0 mb-5 text-dark">
    <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">Kupon Kodu</th>
                        <th>İndirim</th>
                        <th>Min. Sepet</th>
                        <th class="text-center">Kullanım / Limit</th>
                        <th class="text-center">Bitiş Tarihi</th>
                        <th class="text-center">Durum</th>
                        <th class="text-end pe-3">İşlem</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if($kuponlar): foreach($kuponlar as $k): 
                        $yuzde = ($k['usage_limit'] > 0) ? ($k['used_count'] / $k['usage_limit']) * 100 : 0;
                        $is_expired = ($k['end_date'] && strtotime($k['end_date']) < time());
                    ?>
                    <tr>
                        <td class="ps-3">
                            <code class="fs-5 fw-bold text-primary bg-light px-2 py-1 rounded border"><?= $k['code'] ?></code>
                        </td>
                        <td>
                            <span class="fw-bold">
                                <?= $k['type'] == 'percentage' ? '%'.$k['discount_value'] : formatPrice($k['discount_value']) ?>
                            </span>
                        </td>
                        <td class="small text-muted"><?= formatPrice($k['min_order_amount']) ?></td>
                        <td class="text-center" style="min-width: 150px;">
                            <div class="small mb-1"><?= $k['used_count'] ?> / <?= $k['usage_limit'] ?: '∞' ?></div>
                            <?php if($k['usage_limit']): ?>
                            <div class="progress" style="height: 5px;">
                                <div class="progress-bar bg-success" style="width: <?= $yuzde ?>%"></div>
                            </div>
                            <?php endif; ?>
                        </td>
                        <td class="text-center small">
                            <?= $k['end_date'] ? date('d.m.Y', strtotime($k['end_date'])) : '<span class="text-muted">Süresiz</span>' ?>
                            <?= $is_expired ? '<br><span class="badge bg-danger mt-1">Süresi Doldu</span>' : '' ?>
                        </td>
                        <td class="text-center">
                            <?= getStatusBadge($k['status'], 'default') ?>
                        </td>
                        <td class="text-end pe-3">
                            <div class="btn-group shadow-sm">
                                <a href="duzenle.php?id=<?= $k['id'] ?>" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i></a>
                                <a href="?sil=<?= $k['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Bu kuponu tamamen silmek istediğinize emin misiniz?')"><i class="bi bi-trash"></i></a>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; else: ?>
                    <tr><td colspan="7" class="text-center py-5 text-muted">Henüz hiç kupon tanımlanmamış.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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