<?php 
include_once __DIR__ . '/webDbConnection.php';

$conn = new WebDbConnection( );

$countries  = $conn->getCountries();
$leagues = $conn->getLeagues();
?>

<head>
  <link rel="stylesheet" href="style.css">
</head>

<body>
<H2>Odds</H2>

<form id="selectionForm" action="matchTable.php" method="post">
    <label for="countrySelector">Select country: </label>
    
    <select name="countrySelector" id="countrySelector">
    <?php 
        foreach ($countries as $country) {
            echo "<option value='" . $country['id'] . "'>" . $country['name'] . "</option>";
        }
    ?>
    </select>
    
    <label for="leagueSelector">Select league: </label>
    
    <select name="leagueSelector" id="league">
    <?php 
        foreach ($leagues as $league) {
            echo "<option value='" . $league['id'] . "'>" . $league['name'] . " - " . $league['countryId'] ."</option>";
        }
    ?>
    </select>
    
	<input type='submit' id='getMatches' value='Se Matcher'/>
	
</form>
</br>
</br>

<h2>Betting Stats</h2>
  <?php 
    $betSummary = $conn->getBettingSummary();
    $result = array();
    foreach ($betSummary as $bs) {
        
        if ($bs['homeScore'] < 0) {
            $result[$bs['leagueName']]['pending'] = $result[$bs['leagueName']]['pending'] + 1;
            continue;
        }
        
        $result[$bs['leagueName']]['result'] = $result[$bs['leagueName']]['result'] - $bs['amount'];
        
        if ($bs['betOn'] === '1' && $bs['homeScore'] > $bs['awayScore']) {
            $result[$bs['leagueName']]['wins'] = $result[$bs['leagueName']]['wins'] + 1;
            $result[$bs['leagueName']]['result'] = $result[$bs['leagueName']]['result'] + ($bs['amount'] * $bs['odds']);
        } else if ($bs['betOn'] === 'X' && $bs['homeScore'] == $bs['awayScore']) {
            $result[$bs['leagueName']]['wins'] = $result[$bs['leagueName']]['wins'] + 1;
            $result[$bs['leagueName']]['result'] = $result[$bs['leagueName']]['result'] + ($bs['amount'] * $bs['odds']);
        } else if ($bs['betOn'] === '2' && $bs['homeScore'] < $bs['awayScore']) {
            $result[$bs['leagueName']]['wins'] = $result[$bs['leagueName']]['wins'] + 1;
            $result[$bs['leagueName']]['result'] = $result[$bs['leagueName']]['result'] + ($bs['amount'] * $bs['odds']);
        } else {
            $result[$bs['leagueName']]['losses'] = $result[$bs['leagueName']]['losses'] + 1;
        }
    }
    
    ?>
    <table>
    <thead>
    <tr>
    <th>League</th>
    <th>Wins</th>
    <th>Losses</th>
    <th>Percentage</th>
    <th>Result</th>
    <th>Pending</th>
    </tr>
    </thead>
    <tbody>
    <?php 
    foreach ($result as $name => $res) {
        echo "<tr>";
        echo "<td>" . $name . "</td>";
        echo "<td>" . $res['wins'] . "</td>";
        echo "<td>" . $res['losses'] . "</td>";
        echo "<td>" . round($res['wins'] / ($res['wins'] + $res['losses']),2) . "</td>";
        echo "<td>" . $res['result'] . "</td>";
        echo "<td>" . $res['pending'] . "</td>";
    }
    
  ?>
  </tbody>
    </table>


<h2>Day results</h2>

<?php 
$dayStats = $conn->getDayBettingsStats();

?>

<table>
<thead>
<tr>
<th>Datum</th>
<th>Resultat</th>
<th>Games Played</th>
<th>Bet amount</th>
<th>Wins</th>
<th>Unresolved</th>
</tr>
</thead>
<tbody>
<?php
$sum = 0;
foreach ($dayStats as $ds) {
    echo "<tr>";
    echo "<td>" . $ds['date'] . "</td>";
    echo "<td>" . $ds['sumAmount'] . "</td>";
    echo "<td>" . $ds['numGames'] . "</td>";
    echo "<td>" . ROUND($ds['betAmount'],2) . "</td>";
    echo "<td>" . $ds['wins'] . "</td>";
    echo "<td>" . $ds['pending'] . "</td>";
    echo "</tr>";
    $sum += $ds['sumAmount'];
}
?>
</tbody>
</table>

<p>Total: <?php echo $sum; ?></p>
</body>


<script type="text/javascript">


</script>