<?php
/**
 * Admin Abonelik Düzenleme
 * Konum: /magaza/admin/abonelikler/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['abonelik_guncelle'])) {
    $start_date = $_POST['start_date'];
    $end_date   = $_POST['end_date'];
    $status     = $_POST['status'];

    try {
        $sql = "UPDATE subscriptions SET start_date = ?, end_date = ?, status = ? WHERE id = ?";
        $stmt = $db->prepare($sql);
        $stmt->execute([$start_date, $end_date, $status, $id]);
        $mesaj = "<div class='alert alert-success shadow-sm'>Abonelik bilgileri güncellendi.</div>";
    } catch (Exception $e) {
        $mesaj = "<div class='alert alert-danger shadow-sm'>Hata: " . $e->getMessage() . "</div>";
    }
}

// Veriyi çek
$stmt = $db->prepare("
    SELECT s.*, u.name, u.surname, p.package_name 
    FROM subscriptions s 
    JOIN users u ON s.user_id = u.id 
    JOIN packages p ON s.package_id = p.id 
    WHERE s.id = ?
");
$stmt->execute([$id]);
$a = $stmt->fetch();

if (!$a) die("Kayıt bulunamadı.");

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">Abonelik Düzenle</h2>
        <span class="text-primary fw-bold"><?= htmlspecialchars($a['name'].' '.$a['surname']) ?> - <?= htmlspecialchars($a['package_name']) ?></span>
    </div>
    <div class="col-md-6 text-md-end">
        <a href="index.php" class="btn btn-sm btn-outline-secondary shadow-sm">Listeye Dön</a>
    </div>
</div>

<?= $mesaj; ?>

<div class="card shadow-sm border-0">
    <div class="card-body p-4 text-dark">
        <form action="duzenle.php?id=<?= $id ?>" method="POST">
            <div class="row mb-4">
                <div class="col-md-6">
                    <label class="form-label fw-bold small text-muted">BAŞLANGIÇ TARİHİ</label>
                    <input type="date" name="start_date" class="form-control border-2" value="<?= $a['start_date'] ?>" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label fw-bold small text-muted">BİTİŞ TARİHİ</label>
                    <input type="date" name="end_date" class="form-control border-2" value="<?= $a['end_date'] ?>" required>
                </div>
            </div>

            <div class="mb-4">
                <label class="form-label fw-bold small text-muted">DURUM</label>
                <select name="status" class="form-select border-2 fw-bold">
                    <option value="active" <?= $a['status'] == 'active' ? 'selected' : '' ?> class="text-success">AKTİF</option>
                    <option value="passive" <?= $a['status'] == 'passive' ? 'selected' : '' ?> class="text-danger">PASİF</option>
                </select>
            </div>

            <div class="d-flex justify-content-between align-items-center border-top pt-4">
                <span class="text-muted small">Kayıt: <?= date('d.m.Y H:i', strtotime($a['created_at'])) ?></span>
                <button type="submit" name="abonelik_guncelle" class="btn btn-primary px-5 fw-bold shadow">GÜNCELLE</button>
            </div>
        </form>
    </div>
</div>

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