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

$conn = new WebDbConnection( );

$countryId = $_POST['countrySelector'];
$leagueId = $_POST['leagueSelector'];

$conn->getBettingStats($leagueId);

$matches = $conn->getMatches($countryId, $leagueId);
$leagueInfo = $conn->getLeagueInfo($leagueId)[0];
$betMarginHome = 1 + ($leagueInfo['betMarginHome'] / 100);
$betMarginDraw = 1 + ($leagueInfo['betMarginDraw'] / 100);
$betMarginAway = 1 + ($leagueInfo['betMarginAway'] / 100);

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

<iframe name="hiddenFrame" class="hide"></iframe>
<h1><?php echo $leagueInfo['name']; ?></h1>
<table id="matchTable">
    <thead>
        <tr>
        	<th hidden/>
            <th>Datum</th>
            <th>Hemma lag</th>
            <th>Borta lag</th>
            <th>Odds 1</th>
            <th>Odds X</th>
            <th>Odds 2</th>
        	<th>1, X , 2</th>
        	<th>Bet Odds</th>
        	<th>Bet Amount</th>
        	<th>&nbsp;</th>
        </tr>
    </thead>
    <tbody>
<?php 
    foreach ($matches as $match) {
        echo "<form id='makeBetFormId' action='makeBet.php' method='post' target='hiddenFrame'>";
        echo "<tr>";
        echo "<td hidden><input hidden id='gameId' name='gameIdName' value='" . $match['id'] . "'/>";
        echo "<td>".$match['gameDate'] . "</td>";
        echo "<td>".$match['homeTeamName'] . "</td>";
        echo "<td>".$match['awayTeamName'] . "</td>";
        echo "<td>". round((100 / $match['homeWinPercent']) * $betMarginHome, 2) . "</td>";
        echo "<td>". round((100 / $match['drawPercent']) * $betMarginDraw, 2) . "</td>";
        echo "<td>". round((100 / $match['awayWinPercent']) * $betMarginAway, 2) . "</td>";
        echo "<td><select name='betTypeName'>
                <option value='1'>1</option>
                <option value='X'>X</option>
                <option value='2'>2</option>
            </select></td>";
        echo "<td><input id='betOdds' name='betOddsName' type='number' step='.01'/></td>";
        echo "<td><input id='betAmount' name='betAmountName' type='number' step='.01'/></td>";
        echo "<td><input id='button' type='submit'/></td>";
        echo "</form>";
        echo "</tr>";
    }
?>
</tbody>
</table>

<h2>Stats</h2>

<?php 

$betStats = $conn->getBettingStats($leagueId);

$wins = 0;
$losses = 0;
$result = 0;
$pendingResult = 0;
foreach ($betStats as $betStat) {
    if ($betStat['homeScore'] < 0) {
        $pendingResult++;
    }
    if ($betStat['betOn'] === "1") {
        $result -= $betStat['amount'];
        if ($betStat['homeScore'] > $betStat['awayScore']) {
            $wins++;
            $result = $result + ($betStat['amount'] * $betStat['odds']);
        } else {
            $losses++;
        }
    }
    if ($betStat['betOn'] === "X") {
        $result -= $betStat['amount'];
        if ($betStat['homeScore'] == $betStat['awayScore']) {
            $wins++;
            $result = $result + ($betStat['amount'] * $betStat['odds']);
        } else {
            $losses++;
        }
    }
    if ($betStat['betOn'] === "2") {
        $result -= $betStat['betAmount'];
        if ($betStat['homeScore'] < $betStat['awayScore']) {
            $wins++;
            $result = $result + ($betStat['amount'] * $betStat['odds']);
        } else {
            $losses++;
        }
    }
}


echo "<p>Pending Result: $pendingResult</p>";

echo "<p>Wins: $wins</p>";
echo "<p>Losses: $losses</p>";
echo "<p>Result: $result</p>";
?>

</body>