<?php
/**
 * Admin Kupon Düzenleme
 * Konum: /magaza/admin/kuponlar/duzenle.php
 */

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

adminCheck();

$mesaj = '';

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header("Location: index.php");
    exit;
}
$id = (int)$_GET['id'];

// --- GÜNCELLEME İŞLEMİ ---
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['kupon_guncelle'])) {
    $code           = strtoupper(trim($_POST['code']));
    $type           = $_POST['type'];
    $discount       = $_POST['discount_value'];
    $min_amount     = $_POST['min_order_amount'];
    $usage_limit    = !empty($_POST['usage_limit']) ? (int)$_POST['usage_limit'] : NULL;
    $end_date       = !empty($_POST['end_date']) ? $_POST['end_date'] : NULL;
    $status         = (int)$_POST['status'];

    try {
        // GÜNCELLEME: Sütun adları düzeltildi
        $sql = "UPDATE coupons SET 
                code = ?, discount_type = ?, discount_value = ?, min_cart_amount = ?, 
                usage_limit = ?, valid_until = ?, status = ? 
                WHERE id = ?";
        $stmt = $db->prepare($sql);
        $stmt->execute([$code, $type, $discount, $min_amount, $usage_limit, $end_date, $status, $id]);
        
        $mesaj = "<div class='alert alert-success shadow-sm'><i class='bi bi-check-circle me-2'></i> Kupon başarıyla güncellendi! <a href='index.php' class='fw-bold'>Listeye Dön</a></div>";
    } catch (Exception $e) {
        $mesaj = "<div class='alert alert-danger shadow-sm'>Hata: Bu kod başka bir kupon tarafından kullanılıyor olabilir.</div>";
    }
}

// Mevcut veriyi çek
$stmt = $db->prepare("SELECT * FROM coupons WHERE id = ?");
$stmt->execute([$id]);
$k = $stmt->fetch();

if (!$k) die("<div class='alert alert-danger m-4'>Kupon bulunamadı!</div>");

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">Kupon Düzenle</h2>
        <span class="badge bg-primary px-3 mt-1">Kod: <?= htmlspecialchars($k['code']) ?></span>
    </div>
    <div class="col-md-6 text-md-end">
        <a href="index.php" class="btn btn-sm btn-outline-secondary fw-bold shadow-sm"><i class="bi bi-arrow-left"></i> Geri Dön</a>
    </div>
</div>

<?= $mesaj; ?>

<div class="row text-dark">
    <div class="col-lg-8">
        <div class="card shadow-sm border-0 p-4">
            <form action="" method="POST">
                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label fw-bold small text-muted">KUPON KODU</label>
                        <input type="text" name="code" class="form-control border-2 fw-bold" value="<?= htmlspecialchars($k['code']) ?>" required style="text-transform: uppercase;">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label fw-bold small text-muted">DURUM</label>
                        <select name="status" class="form-select border-2">
                            <option value="1" <?= $k['status'] == 1 ? 'selected' : '' ?>>AKTİF (Kullanılabilir)</option>
                            <option value="0" <?= $k['status'] == 0 ? 'selected' : '' ?>>PASİF (Durduruldu)</option>
                        </select>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label fw-bold small text-muted">İNDİRİM TÜRÜ</label>
                        <select name="type" class="form-select border-2">
                            <option value="percentage" <?= $k['discount_type'] == 'percentage' ? 'selected' : '' ?>>Yüzde (%)</option>
                            <option value="fixed" <?= $k['discount_type'] == 'fixed' ? 'selected' : '' ?>>Sabit Tutar (₺)</option>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label fw-bold small text-muted">İNDİRİM DEĞERİ</label>
                        <input type="number" step="0.01" name="discount_value" class="form-control border-2" value="<?= htmlspecialchars($k['discount_value']) ?>" required>
                    </div>
                </div>

                <div class="row mb-4">
                    <div class="col-md-4">
                        <label class="form-label fw-bold small text-muted">MİN. SEPET TUTARI</label>
                        <input type="number" step="0.01" name="min_order_amount" class="form-control border-2" value="<?= htmlspecialchars($k['min_cart_amount']) ?>">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label fw-bold small text-muted">KULLANIM LİMİTİ</label>
                        <input type="number" name="usage_limit" class="form-control border-2" value="<?= htmlspecialchars($k['usage_limit'] ?? '') ?>" placeholder="Sınırsız için boş bırakın">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label fw-bold small text-muted">SON KULLANMA TARİHİ</label>
                        <input type="date" name="end_date" class="form-control border-2" value="<?= !empty($k['valid_until']) ? date('Y-m-d', strtotime($k['valid_until'])) : '' ?>">
                    </div>
                </div>

                <div class="d-flex justify-content-between align-items-center mt-4 border-top pt-3">
                    <span class="text-muted small">Şu ana kadar <strong><?= (int)$k['used_count'] ?></strong> kez kullanıldı.</span>
                    <button type="submit" name="kupon_guncelle" class="btn btn-primary px-5 fw-bold shadow">GÜNCELLEMELERİ KAYDET</button>
                </div>
            </form>
        </div>
    </div>
</div>

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