<?php
include_once('../config.php'); 
include_once('../header.php');

// 1. LÓGICA DE CRIAÇÃO DE SORTEIO
if (isset($_POST['btn_criar_sorteio'])) {
    $concurso = strtoupper(trim($_POST['concurso']));
    $data_sorteio = $_POST['data_sorteio'];
    $numeros = $_POST['numeros'] ?? ''; 
    
    if (!empty($concurso)) {
        $stmt = $conn->prepare("INSERT INTO loterias_sorteios (concurso, data_sorteio, numeros_sorteados) VALUES (?, ?, ?)");
        $stmt->bind_param("sss", $concurso, $data_sorteio, $numeros);
        $stmt->execute();
        
        // Redireciona para o novo nome de arquivo
        echo "<script>window.location.href='lotofacil.php';</script>";
        exit();
    }
}

// 2. LÓGICA DE EXCLUIR SORTEIO
if (isset($_GET['del'])) {
    $id = intval($_GET['del']);
    if ($id > 0) {
        $conn->query("DELETE FROM loterias_sorteios WHERE id = $id");
    }
    // Redireciona para o novo nome de arquivo
    echo "<script>window.location.href='lotofacil.php';</script>";
    exit();
}
?>

<style>
    body { font-family: 'Segoe UI', sans-serif; background: #f0f2f5; text-transform: uppercase; margin: 0; }
    .container-geral { display: flex; flex-wrap: wrap; gap: 20px; padding: 20px; justify-content: center; }
    .col-cadastro { flex: 0 0 350px; }
    .card-fixo { background: #fff; padding: 20px; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); position: sticky; top: 20px; }
    .col-salas { flex: 1; display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 20px; }
    .sala-box { background: #fff; border-radius: 10px; border-top: 6px solid #6f42c1; box-shadow: 0 4px 10px rgba(0,0,0,0.05); overflow: hidden; height: fit-content; }
    .sala-head { background: #222; color: #a29bfe; padding: 12px; display: flex; justify-content: space-between; align-items: center; font-weight: 900; }
    .numeros-container { padding: 20px; text-align: center; display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
    .bola-numero { 
        background: #6f42c1; color: white; width: 35px; height: 35px; 
        border-radius: 50%; display: flex; align-items: center; justify-content: center; 
        font-weight: 900; font-size: 14px; box-shadow: inset -2px -2px 4px rgba(0,0,0,0.3);
    }
    .info-footer { background: #f8f9fa; padding: 10px; font-size: 11px; text-align: center; border-top: 1px solid #eee; color: #666; font-weight: bold; }
    input[type="text"], input[type="date"] { width: 100%; padding: 12px; margin-bottom: 8px; border: 2px solid #eee; border-radius: 8px; box-sizing: border-box; font-weight: bold; }
    .btn-lilas { background: #6f42c1; color: white; border: none; padding: 15px; width: 100%; cursor: pointer; font-weight: 900; border-radius: 8px; margin-top: 10px; }
</style>

<div class="container-geral">
    <div class="col-cadastro">
        <div class="card-fixo">
            <h2 style="margin: 0 0 20px 0; font-size: 18px; text-align: center; color: #6f42c1;">🍀 LOTOFÁCIL - NOVO</h2>
            <form method="POST">
                <input type="text" name="concurso" placeholder="Nº DO CONCURSO" required>
                <input type="date" name="data_sorteio" value="<?php echo date('Y-m-d'); ?>" required>
                <input type="text" name="numeros" placeholder="NÚMEROS (EX: 01-02-05-...)">
                <small style="font-size: 9px; color: #888;">SEPARE OS NÚMEROS COM TRAÇO (-)</small>
                <button type="submit" name="btn_criar_sorteio" class="btn-lilas">SALVAR RESULTADO 🎲</button>
            </form>
        </div>
    </div>

    <div class="col-salas">
        <?php
        $sorteios = $conn->query("SELECT * FROM loterias_sorteios ORDER BY data_sorteio DESC, id DESC LIMIT 12");
        if($sorteios && $sorteios->num_rows > 0):
            while($st = $sorteios->fetch_assoc()):
                $id_sorteio = $st['id'];
                $lista_nums = explode('-', $st['numeros_sorteados']);
        ?>
        <div class="sala-box">
            <div class="sala-head">
                <span>🎫 CONCURSO: <?php echo $st['concurso']; ?></span>
                <div>
                    <a href="?del=<?php echo $id_sorteio; ?>" style="color:#ff4d4d; text-decoration:none;" onclick="return confirm('EXCLUIR ESTE RESULTADO?')">🗑️</a>
                </div>
            </div>
            <div class="numeros-container">
                <?php if(!empty($st['numeros_sorteados'])): ?>
                    <?php foreach($lista_nums as $n): if(trim($n) == "") continue; ?>
                        <div class="bola-numero"><?php echo trim($n); ?></div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
            <div class="info-footer">
                DATA DO SORTEIO: <?php echo date('d/m/Y', strtotime($st['data_sorteio'])); ?>
            </div>
        </div>
        <?php endwhile; endif; ?>
    </div>
</div>

<?php echo '</div>'; ?>