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

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

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

// query regarding our candidate
$query = "select path,name,submission_time,enabled,dupe_of,pub_notes,upvotes,downvotes from Candidates where id=$cid";
$result = $db->query($query);
$error = $db->errorInfo();
if (!is_null($error[2])) {
        var_dump($error);
}

if ($result->rowcount() > 0 ) {
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
	    // these things are per-candidate. 
	    $cdate['path'] = $row['path'];
	    $cdate['name'] = $row['name'];
	    $cdate['submission_time'] = $row['submission_time'];
	    $cdate['enabled'] = $row['enabled'];
	    $cdate['dupe_of'] = $row['dupe_of'];
	    $cdate['pub_notes'] = $row['pub_notes'];
	    $cdate['upvotes'] = $row['upvotes'];
	    $cdate['downvotes'] = $row['downvotes'];
    }
    $cpathp=dirname($cdate['path']);
    $cpathf=basename($cdate['path']);
    $cpath=$cpathp ."/" .urlencode($cpathf);
    $cname=$cdate['name'];
    $nocand="false";
} else {
    $nocand = "true";
}

if ($nocand != "true") {
    // TODO: expand this sql with some joins and shenanigans so we have the details of the competition on each, so the ballot listing can be fleshed out with win/loss and maybe even thumbnails 
    // ...I think this SQL does it... 
    $ballistq = "select ballotid,path,name,bal_time,voter_id,won_id,lost_id,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 id != $cid and (left_id=$cid OR right_id=$cid) ORDER BY ballotid";
    //$ballistq = "select * from Ballots where left_id=$cid or right_id=$cid";

    $ballist = $db->prepare($ballistq);
    $ballist->execute();
    if (!is_null($error[2])) { header('Location: message.php?candballistfail1'); }
    $balcount=$ballist->rowcount();
    if ($balcount > 0 ) {
	$bcount=0;
	while ($row = $ballist->fetch(PDO::FETCH_ASSOC) ) {
	    $ballotid[$bcount]=$row['ballotid'];
	    $ballottime[$bcount]=$row['bal_time'];
	    if ($cid == $row['won_id']) { $ballotresult[$bcount]="WON"; } 
		elseif ($cid == $row['lost_id'] ) { $ballotresult[$bcount]="lost";}
		elseif ($row['tied'] == "1" ) { $ballotresult[$bcount]="tied";}
		elseif ($row['skip'] == "1" ) { $ballotresult[$bcount]="skipped";}
		else {$ballotresult[$bcount]="discarded"; }
	    $ballotoppname[$bcount]=$row['name'];
	    $ballotopppath[$bcount]=$row['path'];
	    $ballotvoter[$bcount]=$row['voter_id'];
	    $bcount++;
	}
    }
}

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

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

echo "<!DOCTYPE HTML SYSTEM>
<html>
  <head>
    <title>Don't Panic - Candidate $cid</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>Candidate 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 ($nocand != "true") {

    echo "<img class='tile tile2' type='image' name='left' title=\"$cname\" src='$cpath' height='$tsize' width='$tsize' alt=\"Don&apos;t Panic (font: $cname)\"><br>
<span class='dpquote'>$cname</span><br>
<br>";
    if ($cdate['pub_notes']) {
	echo "<span class='quote'>$cdate[pub_notes]</span><br>";
    }
echo "Candidate has been on the system since $cdate[submission_time]<br>
Enabled: $cdate[enabled] <br>
Ballots won: $cdate[upvotes] <br>
Ballots lost: $cdate[downvotes] <br>";
} else {
	echo "<h1>No candidate 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>";
}

echo "<span>(<a href='vote.php'>Go vote</a>)</span><br>
<br>
<hr width=42%>
<div style='width:84%;margin:auto'>
<h3>Ballots this candidate has featured in:</h3>\n";

if ($balcount > 0 ) {
    for ($b=0 ; $b<$bcount ; $b++) {
	if ( $b%10==0) {
	    echo "<div id=popup class='tile quote' style='text-align:center;vertical-align:top'><tt>\n";
	}
	// each span is a ballot. 
	// TODO: 
	// bold if the ballot was for the current user
	
	echo "  <span class='left'>" . gmdate('D, d M Y', strtotime($ballottime[$b])) ." <a href='ballot.php?bid=$ballotid[$b]'>Ballot&nbsp;#$ballotid[$b]<span class=pop><img class='tile2 tiletiny' width=105 title=\"$ballotoppname[$b]\" alt='$ballotoppname[$b]' src='$ballotopppath[$b]'></span></a></span> <span class='right resultinfo $ballotresult[$b]' > $ballotresult[$b]</span><br>\n";
	if ( ( ($b+1)%10==0) && (($b+1)<$bcount) ) {
	    echo "</tt></div>\n";
	}
    }

    echo "</tt></div>
    </div>\n";
} else {
    echo "This candidate has no ballots yet... why not go vote repeatedly till this is fixed?<br>\n";
}


include('footer.inc');
//include('lib/debug.inc');   // debug 
// TODO following data should be admin-only
if ($debug) {
    // though candidate ID is public - it's in the URL!
    echo "\n<div class=debug>Candidate ID: $cid<br></div><br>";
}
?>
  </body>
</html>


