<?php
session_start();
require_once '/home/akgbilis/fi_sf/config.php';
if (!isset($_SESSION['logged_in'])) {
    header("Location: login.php");
    exit;
}

$combineId = $_GET['id'] ?? null;
if (!$combineId) {
    header("Location: combine_dashboard.php");
    exit;
}

$combineStmt = $pdo->prepare("SELECT * FROM combines WHERE id = ?");
$combineStmt->execute([$combineId]);
$combine = $combineStmt->fetch();

if (!$combine) {
    die("Kupon bulunamadı.");
}

$success = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'];
    $date = $_POST['date'];
    $active = isset($_POST['active']) ? 1 : 0;
    $canEveryoneSee = isset($_POST['canEveryoneSee']) ? 1 : 0;
    $matchIds = $_POST['matches'] ?? [];

    $pdo->prepare("UPDATE combines SET name = ?, date = ?, active = ?, canEveryoneSee = ? WHERE id = ?")
        ->execute([$name, $date, $active, $canEveryoneSee, $combineId]);

    $pdo->prepare("DELETE FROM combine_matches WHERE combine_id = ?")
        ->execute([$combineId]);

    $insertStmt = $pdo->prepare("INSERT INTO combine_matches (combine_id, match_id) VALUES (?, ?)");
    foreach ($matchIds as $matchId) {
        $insertStmt->execute([$combineId, $matchId]);
    }

    // Sayfa yenilemeden checkbox'ların güncellenmesini sağlamak için yeni değerleri tekrar çekiyoruz
    $combine['active'] = $active;
    $combine['canEveryoneSee'] = $canEveryoneSee;

    $success = true;
}

$currentStmt = $pdo->prepare("SELECT m.id FROM combine_matches cm JOIN matches m ON cm.match_id = m.id WHERE cm.combine_id = ?");
$currentStmt->execute([$combineId]);
$currentMatchIds = array_column($currentStmt->fetchAll(), 'id');

$allMatches = $pdo->query("SELECT * FROM matches ORDER BY id DESC")->fetchAll();
?>

<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kupon Düzenle</title>
    <style>
        body { font-family: sans-serif; background: #f5f5f5; margin: 0; padding: 20px; }
        .container { max-width: 700px; margin: auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.05); }
        .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
        .button { background: #3498db; color: white; padding: 10px 16px; border: none; border-radius: 4px; text-decoration: none; cursor: pointer; }
        .form-card label { display: block; margin-top: 12px; font-weight: bold; }
        .form-card input[type="text"],
        .form-card input[type="date"] {
            width: 100%; padding: 10px; margin-top: 6px; border: 1px solid #ccc; border-radius: 4px;
        }
        .match-list { margin-top: 12px; display: flex; flex-direction: column; gap: 10px; max-height: 300px; overflow-y: auto; }
        .match-option { background: #f9f9f9; padding: 10px; border-radius: 6px; border: 1px solid #ddd; }
        .success { background: #2ecc71; color: white; padding: 12px; border-radius: 6px; margin-bottom: 16px; text-align: center; }
        @media (max-width: 600px) {
            .container { padding: 14px; }
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h2>Kupon Düzenle</h2>
        <a href="combine_dashboard.php" class="button">&larr; Geri</a>
    </div>

    <?php if ($success): ?>
        <div class="success">✅ Kupon güncellendi.</div>
    <?php endif; ?>

    <form method="POST" class="form-card">
        <label>Kupon Adı:</label>
        <input type="text" name="name" value="<?= htmlspecialchars($combine['name']) ?>" required>

        <label>Tarih:</label>
        <input type="date" name="date" value="<?= htmlspecialchars($combine['date']) ?>" required>

        <label><input type="checkbox" name="active" <?= $combine['active'] ? 'checked' : '' ?>> Aktif (Satın alınabilir)</label>

        <label><input type="checkbox" name="canEveryoneSee" <?= $combine['canEveryoneSee'] ? 'checked' : '' ?>> Herkes görebilsin (VIP olmasa da)</label>

        <label>Maç Seç:</label>
        <div class="match-list">
            <?php foreach ($allMatches as $match): ?>
                <div class="match-option">
                    <label>
                        <input type="checkbox" name="matches[]" value="<?= $match['id'] ?>" <?= in_array($match['id'], $currentMatchIds) ? 'checked' : '' ?>>
                        <strong><?= htmlspecialchars($match['home']) ?> - <?= htmlspecialchars($match['away']) ?></strong><br>
                        <small><?= $match['date'] ?> • <?= $match['league'] ?> • Tahmin: <?= $match['prediction'] ?> @ <?= number_format((float)$match['odds'], 2) ?></small>
                    </label>
                </div>
            <?php endforeach; ?>
        </div>

        <button class="button" type="submit" style="margin-top: 16px;">💾 Güncelle</button>
    </form>
</div>
</body>
</html>