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

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

// Sıralama seçimi
$sort = $_GET['sort'] ?? 'date_asc';
switch ($sort) {
    case 'date_desc':  $orderBy = 'date DESC, time DESC'; break;
    case 'odds_asc':   $orderBy = 'odds ASC'; break;
    case 'odds_desc':  $orderBy = 'odds DESC'; break;
    default:           $orderBy = 'date ASC, time ASC'; break;
}

// Maçları çek
$stmt = $pdo->prepare("SELECT * FROM matches WHERE status = 'WAITING' ORDER BY $orderBy");
$stmt->execute();
$matches = $stmt->fetchAll();

// Form gönderildiyse işle
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $combine_id = uniqid();
    $combine_name = $_POST['name'];
    $combine_date = $_POST['date'];
    $combine_type = $_POST['type'];
    $combine_price = $combine_type === 'VIP' ? intval($_POST['price']) : 0;
    $match_ids = $_POST['match_ids'] ?? [];

    $stmt = $pdo->prepare("INSERT INTO combines (id, name, date, type, price) VALUES (?, ?, ?, ?, ?)");
    $stmt->execute([$combine_id, $combine_name, $combine_date, $combine_type, $combine_price]);

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

    header("Location: combine_dashboard.php");
    exit;
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <title>Yeni Kupon Ekle</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
    <style>
        * { box-sizing: border-box; }
        body {
            font-family: 'Inter', sans-serif;
            background-color: #f8f9fa;
            padding: 40px;
            margin: 0;
        }
        .container {
            max-width: 850px;
            margin: auto;
            background-color: white;
            padding: 32px;
            border-radius: 10px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
        h2 {
            font-size: 24px;
            color: #333;
            border-bottom: 2px solid #0d6efd;
            padding-bottom: 8px;
            margin-bottom: 24px;
        }
        label {
            font-weight: 600;
            margin-top: 16px;
            display: block;
        }
        input[type="text"], input[type="date"], input[type="number"], select {
            width: 100%;
            padding: 10px;
            margin-top: 6px;
            border: 1px solid #ccc;
            border-radius: 6px;
            font-size: 15px;
        }
        #price-field {
            display: none;
        }
        .match-list {
            margin-top: 12px;
            border: 1px solid #ddd;
            border-radius: 6px;
            max-height: 300px;
            overflow-y: auto;
        }
        .match-item {
            padding: 12px 16px;
            border-bottom: 1px solid #eee;
            display: flex;
            align-items: center;
            font-size: 14px;
            background-color: #f9f9f9;
        }
        .match-item:hover {
            background-color: #e9f7ef;
        }
        .match-item input[type="checkbox"] {
            margin-right: 12px;
            transform: scale(1.2);
        }
        .button {
            padding: 12px;
            background-color: #0d6efd;
            color: white;
            border: none;
            font-weight: bold;
            border-radius: 6px;
            cursor: pointer;
            margin-top: 24px;
            width: 100%;
        }
        .button:hover {
            background-color: #0b5ed7;
        }
        .top-buttons {
            display: flex;
            gap: 12px;
            margin-bottom: 16px;
        }
        .top-buttons a {
            flex: 1;
            text-align: center;
            background-color: #6c757d;
            padding: 10px;
            color: white;
            text-decoration: none;
            border-radius: 6px;
            font-size: 14px;
        }
        .top-buttons a:hover {
            background-color: #5a6268;
        }
        .sort-select {
            margin-bottom: 20px;
        }
        .no-matches {
            padding: 12px;
            color: #999;
            font-style: italic;
        }
    </style>
    <script>
        function togglePriceField() {
            const type = document.getElementById('type').value;
            document.getElementById('price-field').style.display = type === 'VIP' ? 'block' : 'none';
        }
    </script>
</head>
<body>
<div class="container">
    <div class="top-buttons">
        <a href="dashboard.php">🏠 Ana Sayfa</a>
        <a href="combine_dashboard.php">🎫 Kuponlar</a>
    </div>

    <h2>Yeni Kupon Ekle</h2>

    <!-- Sıralama -->
    <form method="GET" class="sort-select">
        <label for="sort">Maçları sırala:</label>
        <select name="sort" id="sort" onchange="this.form.submit()">
            <option value="date_asc" <?= $sort === 'date_asc' ? 'selected' : '' ?>>Tarihe Göre (Eskiden Yeniye)</option>
            <option value="date_desc" <?= $sort === 'date_desc' ? 'selected' : '' ?>>Tarihe Göre (Yeniden Eskiye)</option>
            <option value="odds_asc" <?= $sort === 'odds_asc' ? 'selected' : '' ?>>Orana Göre (Düşükten Yükseğe)</option>
            <option value="odds_desc" <?= $sort === 'odds_desc' ? 'selected' : '' ?>>Orana Göre (Yüksekten Düşüğe)</option>
        </select>
    </form>

    <form method="POST">
        <label for="name">Kupon Adı:</label>
        <input type="text" name="name" id="name" required>

        <label for="date">Tarih:</label>
        <input type="date" name="date" id="date" required>

        <label for="type">Kupon Türü:</label>
        <select name="type" id="type" onchange="togglePriceField()" required>
            <option value="FREE">FREE</option>
            <option value="VIP">VIP</option>
        </select>

        <div id="price-field">
            <label for="price">Fiyat (kredi):</label>
            <input type="number" name="price" id="price" min="0" placeholder="VIP fiyatı girin">
        </div>

        <label>Maçlar:</label>
        <div class="match-list">
            <?php if (!empty($matches)): ?>
                <?php foreach ($matches as $match): ?>
                    <label class="match-item">
                        <input type="checkbox" name="match_ids[]" value="<?= $match['id'] ?>">
                        <?= htmlspecialchars($match['home']) ?> - <?= htmlspecialchars($match['away']) ?> |
                        <?= htmlspecialchars($match['league']) ?> |
                        <?= date("d.m.Y", strtotime($match['date'])) ?> <?= substr($match['time'], 0, 5) ?> |
                        Tahmin: <?= htmlspecialchars($match['prediction'] ?? '') ?> |
                        Oran: <?= number_format($match['odds'], 2) ?>
                    </label>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="no-matches">Bekleyen maç bulunamadı.</div>
            <?php endif; ?>
        </div>

        <button type="submit" class="button">✅ Kuponu Oluştur</button>
    </form>
</div>
</body>
</html>