<?php
session_start();
require_once '/home/akgbilis/fi_sf/config.php';

if (!isset($_SESSION['logged_in'])) {
    header("Location: login.php");
    exit;
}

// Prefill values from GET or defaults
$prefill = [
    'country' => isset($_GET['country']) ? $_GET['country'] : '',
    'league'  => isset($_GET['league'])  ? $_GET['league']  : '',
    'date'    => isset($_GET['date'])    ? $_GET['date']    : date('Y-m-d'),
    'time'    => isset($_GET['time'])    ? $_GET['time']    : '19:00',
    'home'    => isset($_GET['home'])    ? $_GET['home']    : '',
    'away'    => isset($_GET['away'])    ? $_GET['away']    : '',
];

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $id = uniqid();
    $country = $_POST['country'];
    $league = $_POST['league'];
    $date = $_POST['date'];
    $time = $_POST['time'];
    $home = $_POST['home'];
    $away = $_POST['away'];
    $home_score = 0;
    $away_score = 0;
    $prediction = $_POST['prediction'];
    $odds = $_POST['odds'];
    $status = $_POST['status'];
    $type = $_POST['type'];
    $can_generate = $_POST['can_generate'] ?? 1;

    $stmt = $pdo->prepare("INSERT INTO matches (id, country, league, date, time, home, away, home_score, away_score, prediction, odds, status, type, can_generate)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

    $stmt->execute([
        $id, $country, $league, $date, $time,
        $home, $away, $home_score, $away_score,
        $prediction, $odds, $status, $type, $can_generate
    ]);

    header("Location: dashboard.php");
    exit;
}
?>

<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <title>Yeni Maç Ekle</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: system-ui, sans-serif;
            background-color: #f8f9fa;
            margin: 0;
            padding: 20px;
        }

        .container {
            max-width: 500px;
            margin: auto;
            background: white;
            padding: 25px;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }

        h2 {
            margin-top: 0;
            color: #2c3e50;
            text-align: center;
        }

        label {
            font-weight: 600;
            display: block;
            margin-bottom: 6px;
            margin-top: 14px;
        }

        input[type="text"],
        input[type="date"],
        input[type="time"],
        select {
            width: 100%;
            padding: 10px;
            font-size: 15px;
            border: 1px solid #ccc;
            border-radius: 8px;
            box-sizing: border-box;
        }

        button {
            margin-top: 24px;
            width: 100%;
            padding: 12px;
            background: #28a745;
            color: white;
            font-size: 16px;
            font-weight: bold;
            border: none;
            border-radius: 10px;
            cursor: pointer;
        }

        button:hover {
            background: #218838;
        }

        @media (max-width: 600px) {
            .container {
                margin: 10px;
                padding: 18px;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <h2>⚽ Yeni Maç Ekle</h2>
    <form method="POST">
        <label>Ülke:</label>
        <input type="text" name="country" required value="<?= htmlspecialchars($prefill['country']) ?>">

        <label>Lig:</label>
        <input type="text" name="league" required value="<?= htmlspecialchars($prefill['league']) ?>">

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

        <label>Saat:</label>
        <input type="time" name="time" required value="<?= htmlspecialchars($prefill['time']) ?>">

        <label>Ev Sahibi:</label>
        <input type="text" name="home" required value="<?= htmlspecialchars($prefill['home']) ?>">

        <label>Deplasman:</label>
        <input type="text" name="away" required value="<?= htmlspecialchars($prefill['away']) ?>">

        <label>Tahmin:</label>
        <input type="text" name="prediction" required>

        <label>Oran:</label>
        <input type="number" step="0.01" name="odds" required>

        <label>Durum:</label>
        <select name="status">
            <option value="WAITING">⏳ WAITING</option>
            <option value="WON">✅ WON</option>
            <option value="LOST">❌ LOST</option>
        </select>

        <label>Kupon Türü:</label>
        <select name="type">
            <option value="FREE">FREE</option>
            <option value="VIP">VIP</option>
        </select>

        <label>Kuponda kullanılabilir mi?</label>
        <select name="can_generate">
            <option value="1" selected>Evet</option>
            <option value="0">Hayır</option>
        </select>

        <button type="submit">💾 Kaydet</button>
    </form>
</div>
</body>
</html>