
<?php
include('lib/mysql.inc');   // get the sql stuff happening

// TODO: sanitise bid. 
$bid=$_GET['bid'];

if (is_null($bid) ) {
    $bid=1;
}

// query regarding our ballot
    // LEFT JOIN means we get NULL if there was no vote made
$query = "select id,path,name,upvotes,downvotes,bal_time,voter_id,useragent,dpquote,bias,left_id,right_id,vote_time,won_id,lost_id,Xval,Yval,tied,skip,comment from Candidates INNER JOIN Ballots on Ballots.left_id = Candidates.id or Ballots.right_id=Candidates.id LEFT JOIN Votes ON Votes.ballot_id = Ballots.ballotid where ballotid=$bid";
$result = $db->query($query);
$error = $db->errorInfo();
if (!is_null($error[2])) {
        var_dump($error);
}

if ($result->rowcount() > 0 ) {
    // TODO: fix bug where ballots display left/right incorrect when rightid is < leftid
    $ballfound="true";
    $i=0;
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
	    // these things are same on both. Don't bother with array
	    $bdpquote = $row['dpquote'];
	    $vcomment = $row['comment'];
	    $vuseragent = $row['useragent'];
	    $voter_id = $row['voter_id'];
	    $bal_time = $row['bal_time'];
	    $bias = $row['bias'];
	    $left_id = $row['left_id'];
	    $right_id = $row['right_id'];
	    $vote_time = $row['vote_time'];
	    $won_id = $row['won_id'];
	    // these things are per-candidate. 
	    $cdate[$i]['id'] = $row['id'];
	    $cdate[$i]['path'] = $row['path'];
	    $cdate[$i]['name'] = $row['name'];
	    $cdate[$i]['upvotes'] = $row['upvotes'];
	    $cdate[$i]['downvotes'] = $row['downvotes'];
	    $i++;
    }
    // TODO: the following sets c0 and c1 in sql return order, not in left/right order
    if ($left_id == $cdate[0]['id']) {
	$clpathp=dirname($cdate[0]['path']);
	$clpathf=basename($cdate[0]['path']);
	$clpath=$clpathp ."/" .urlencode($clpathf);
	$clname=$cdate[0]['name'];
	$clid=$cdate[0]['id'];

	$crpathp=dirname($cdate[1]['path']);
	$crpathf=basename($cdate[1]['path']);
	$crpath=$crpathp ."/" .urlencode($crpathf);
	$crname=$cdate[1]['name'];
	$crid=$cdate[1]['id'];
    } else {
	$clpathp=dirname($cdate[1]['path']);
	$clpathf=basename($cdate[1]['path']);
	$clpath=$clpathp ."/" .urlencode($clpathf);
	$clname=$cdate[1]['name'];
	$clid=$cdate[1]['id'];

	$crpathp=dirname($cdate[0]['path']);
	$crpathf=basename($cdate[0]['path']);
	$crpath=$crpathp ."/" .urlencode($crpathf);
	$crname=$cdate[0]['name'];
	$crid=$cdate[0]['id'];
    }

    // echo "$clpath,$crpath,$bdpquote<br>\n";	// debug

    // TODO: dpquotes should be in the DB. (the numbers by which they're referenced here are stored in the DB afterall)
    $dpquote = [
	["First, it is slightly cheaper; and secondly it has the words", "inscribed in large friendly letters on its cover.", "The Hitchhiker's Guide to the Galaxy. Prologue", "0"],
	["...the snug plastic cover it fitted into had the words", "printed on it in large friendly letters.", "The Hitchhiker's Guide to the Galaxy. Ch 3", "1"],
	["...and written on it, in large friendly letters, the words", "", "So Long, and Thanks for All the Fish. Ch 20", "2"],
	["Feebly glowing on its cover, in response to her touch,<br>were some large friendly letters. They said", "", "Mostly Harmless. Ch 16", "3"],
	];
} else {
    $ballfound="false";
}


// close DB resources
$result->closeCursor();
$db = null;

// ////
// HTML
// ////

echo "<!DOCTYPE HTML SYSTEM>
<html>
  <head>
    <title>Don't Panic - Ballot $bid</title>
<link rel='stylesheet' type='text/css' href='css/dp.css'>
  </head>
  <body>

<div class='navbar'>
	<ul class='left'>
	    <li>://
	    <li><a href='/'>sub-ethr.net</a>
	    <li><a href='/DontPanic'>Don't Panic</a>
	    <li><a href='vote.php'>Vote</a>
        </ul>
	<ul class='right'>
	    <li>Ballot details
	    <li><a href='results.php'>Results</a>
	    <li><a href='ident-i-eeze.php'>Ident-i-Eeze</a>
        </ul>
    </div>
<br clear=all>
";

$tsize=420;	// our image tile size

if ($ballfound == "true") {

    echo "<div class=dpquote>" . $dpquote[$bdpquote][0] . "</div>";

    // TODO: wrap win/lose info in admin check
    if ($won_id == $clid) {
	$resultclass="tile tile2 tilewin";
    } else {
	$resultclass="tile";
    }
    echo "<img class='$resultclass' type='image' name='left' title=\"$clname\" src='$clpath' height='$tsize' width='$tsize' alt=\"Don&apos;t Panic (font: $clname)\">";

    // TODO: wrap win/lose info in admin check
    if ($won_id == $crid) {
	$resultclass="tile tile2 tilewin";
    } else {
	$resultclass="tile";
    }
    echo "<img class='$resultclass' type='image' name='right' title=\"$crname\" src='$crpath' height='$tsize' width='$tsize' alt=\"Don&apos;t Panic (font: $crname)\"><br>

    <div class=dpquote>" . $dpquote[$bdpquote][1] . " </div>
    <cite>&mdash;" . $dpquote[$bdpquote][2] . "</cite><br>";
    # TODO: wrap navigation in admin? maybe? maybe not?
    # improve how it looks? 
    # TODO: add indication of
    # * tied
    # * skipped
    # * no vote
    echo "
    <span style='display:inline-block;width:50%;margin:none;border:none;text-align:right'>
	<span class='right'><a href='candidate.php?cid=$clid'>$clname</a></span>
    </span><span style='display:inline-block;width:50%;margin:none;border:none;text-align:left'>
	<span class='left'><a href='candidate.php?cid=$crid'>$crname</a></span>
    </span>
    <br clear=all>
    <span class='quote' style='text-align: center'> " . gmdate('D, d M Y, H:m:s T', strtotime($bal_time)) ."<br>(voting took " . ((strtotime($vote_time))-(strtotime($bal_time))) ." seconds)</span>
    <br clear=all>
    <br clear=all>
    ";


    echo "<span class=nodecoration><a href='ballot.php?bid=" . ($bid-1) . "'><span class='ballotnav'>&lt;</span></a><span style='display:inline-block;width:20px;'>&nbsp;</span><a href='ballot.php?bid=" . ($bid+1) . "'><span class='ballotnav'>&gt;</span></a></span>
    <br>
    <br>";
} else {
        echo "<h1>No ballot found</h1>
<img alt='We apologise for the inconvenience' src='inconvenience.png'><br>
<br>
oh dear, I think you'll find reality is on the blink again
<br>
<br>";
}

// TODO: add a "report this ballot" function? 
echo "<span>(<a href='vote.php'>Go vote</a>)</span><br>
<br>";

include('footer.inc');
//include('lib/debug.inc');   // debug 
// TODO following data should be admin-only
if ($debug) {
    echo "\n<div class=debug>Ballot ID: $bid<br>Selection bias: $bias<br>Voter ID: $voter_id<br>User agent: $vuseragent<br>Ballot and vote time: $bal_time ... $vote_time (" .(strtotime($vote_time)-strtotime($bal_time)) ." seconds)<br>Voter's submitted comment: <span class='vcomment'>$vcomment</span><br>Left/Right/Winning IDs: $left_id / $right_id / $won_id<br></div><br>";
}
?>
  </body>
</html>
