<?php require_once __DIR__.'/guard.php'; require_login(); $db=new PDO('sqlite:'.dirname(__DIR__).'/data/app.db'); $msg=''; if($_SERVER['REQUEST_METHOD']==='POST'){ if(isset($_POST['open'])){ $o1=trim($_POST['opt1']??''); $o2=trim($_POST['opt2']??''); if($o1&&$o2){ $st=$db->prepare('INSERT INTO bets(status,option1,option2,created_ts) VALUES(?,?,?,?)'); $st->execute(['open',$o1,$o2,gmdate('c')]); $msg='Bet åbnet.'; } } if(isset($_POST['close'])){ $db->exec("UPDATE bets SET status='closed', close_ts='".gmdate('c')."' WHERE status='open'"); $msg='Bet lukket.'; } if(isset($_POST['resolve'])){ $win=(int)($_POST['winner']??1); $last=$db->query("SELECT * FROM bets WHERE status='closed' ORDER BY id DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC); if($last){ $st=$db->prepare('SELECT user_login, amount FROM bet_entries WHERE bet_id=? AND option=?'); $st->execute([$last['id'],$win]); $wins=$st->fetchAll(PDO::FETCH_ASSOC); foreach($wins as $w){ $db->prepare('INSERT INTO points(user_login,display_name,points) VALUES(?,?,?) ON CONFLICT(user_login) DO UPDATE SET points=points+excluded.points')->execute([$w['user_login'],$w['user_login'],(int)$w['amount']*2]); } $db->prepare('UPDATE bets SET status="resolved", resolved_option=? WHERE id=?')->execute([$win,$last['id']]); $msg='Bet resolved, vinder: option '.$win; } } } $last=$db->query('SELECT * FROM bets ORDER BY id DESC LIMIT 1')->fetch(PDO::FETCH_ASSOC); $entries=$last?$db->query('SELECT * FROM bet_entries WHERE bet_id='.(int)$last['id'])->fetchAll(PDO::FETCH_ASSOC):[]; ?><!DOCTYPE html><html><head><meta charset="utf-8"><title>Bets</title><link rel="stylesheet" href="style.css"></head><body><div class="wrap"><div class="card"><h2>💸 Bets</h2><p><a class="btn" href="index.php">← Tilbage</a></p><?php if($msg): ?><p class="notice"><?php echo htmlspecialchars($msg); ?></p><?php endif; ?><form method="post" style="margin-bottom:1rem"><label>Option 1<input name="opt1" placeholder="Team A"></label><br><label>Option 2<input name="opt2" placeholder="Team B"></label><br><button class="btn" name="open" value="1">Åbn nyt bet</button> <button class="btn" name="close" value="1">Luk indskud</button> <label>Vinder (1/2)<input name="winner" value="1"></label> <button class="btn" name="resolve" value="1">Afslut & udbetal</button></form><?php if($last): ?><p>Status: <span class="badge"><?php echo htmlspecialchars($last['status']); ?></span> – #<?php echo (int)$last['id']; ?> (<?php echo htmlspecialchars($last['option1'].' vs '.$last['option2']); ?>)</p><h3>Indskud</h3><table><tr><th>Bruger</th><th>Beløb</th><th>Option</th></tr><?php foreach($entries as $e){ echo '<tr><td>@'.htmlspecialchars($e['user_login']).'</td><td>'.(int)$e['amount'].'</td><td>'.(int)$e['option'].'</td></tr>'; } ?></table><?php endif; ?></div></div></body></html>