<?php
// ===========================
// = Configuration Variables =
// ===========================

$error_report = false;
require_once($_SERVER['DOCUMENT_ROOT'].'/_config.php');

?>

 
<?php

	$query = "SELECT UNIX_TIMESTAMP() time_loaded, DATE_FORMAT(a.date_created,'%H:%i') AS date_created, f.fixture_id, a.audio_id, a.audio_type_id, CONCAT(f.team_one_score, ' - ', f.team_two_score) as score, a.file, at.name as audio_type_name,a.duration,a.auxiliary_info,f.kick_off,c.name, concat(t1.name, ' vs ', t2.name) as teams, CONCAT(f.team_one_score, ' - ', f.team_two_score) as score_ht
		FROM fixture f, audio_type at, competition c, team t1, team t2, audio a 
		WHERE a.audio_type_id = at.audio_type_id 
		AND a.audio_type_id IN (2,3,4,5,7) 
		AND TO_DAYS(f.kick_off) = TO_DAYS(NOW() + INTERVAL -0 DAY)
		AND a.source_id = f.fixture_id 
		AND f.competition_id = c.competition_id 
		AND f.team_one_id = t1.team_id 
		AND f.team_two_id = t2.team_id 
		ORDER BY date_created DESC;";




	$stmt = $mdb2->query($query);
	if (PEAR::isError($stmt)) { die($stmt->getMessage()); }
	$audioresult = $stmt->fetchAll();

	$timeloaded = time();

	$audiocount = count($audioresult);
?>

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta value="viewport" content="width=device-width, initial-scale=1.0">
	<title>Player</title>
	
	<link rel="stylesheet" href="css/normalize.css">
	<link rel="stylesheet" href="css/main.css">
	<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css'>
	<link href='http://fonts.googleapis.com/css?family=Oswald:400,300' rel='stylesheet' type='text/css'>
	<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">

	<script src="//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.3/fastclick.min.js"></script>
	<script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.7.2.js"></script>
	<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<script type="text/javascript" src="js/jquery.cookie.js"></script>
	
</head>

<body data-updated="<?php echo $timeloaded ?>">

<!-- <div class="menu-bar">
	<div class="btn--primary  cookie-button--delete">Forget played files</div>
</div> -->

<div class="dummy" style="display: none;">
	<img src="assets/wh-logo-white.png">
	<img src="assets/spinner.png">
</div>

<div id="overlay">

	<div class="overlay__content-container">
		
		<!--[if lt IE 9]>
		  	<img class="overlay__logo" style="position: relative; bottom: 30px;" src="assets/wh-logo-white.png">
		  	<p style="color: white; display: block; text-align: center; margin-top: 30px;">Unfortunately your browser is ancient. Please download a more modern browser to use this player.</p>
		<![endif]-->
	</div>

</div>

<!-- <div class="cookie-container" style="position: fixed; right: 0;">
	<button class="cookie-button">
		(dev) <br> Show cookie <br> contents
	</button>

	<br>

	<button class="cookie-button--delete">
		(dev) <br> Delete cookie <br> contents 
	</button>
</div> -->

<div class="container">
	<div class="logo-container">
		<img class="logo" src="assets/wh-logo.png" alt="william hill logo">
	</div>

	<div class="player">

	<?php 
		if ($audiocount == 0) {
			echo '<div class="notice-container" style="text-align: center; margin-top: 110px;"><span class="notice" style="font-family: \'oswald\';font-weight: 300;font-size: 1.86em; padding: 0.7em 1.8em;">No audio available for today</span></div>';
		}
	?>


	<?php 	foreach($audioresult as $key => $row) { 

		// all the files with {audio_type_id = 7} go in the OTHER folder.
		$audio_id = $row['audio_id'];
		$filepath = $row['file'];
		$fixture_id = $row['fixture_id'];

		/*
		 * Lineups:		3 - No score
		 * Half Time: 	4 - Keep score
		 * Full Time: 	5 - Keep score
		 * Preview: 	2 - ?
		 * Update: 		7 - Lose score
		*/


		if ($row['audio_type_id'] == 4) {
			$ht_query = 'SELECT CONCAT(fe.team_one_score,"-",fe.team_two_score) AS score_ht FROM fixture_event fe WHERE fixture_id='.$fixture_id.' AND event_type=2';
			$ht_stmt = $mdb2->query($ht_query);
			if (PEAR::isError($ht_stmt)) { die($ht_stmt->getMessage()); }
			while($ht_result = $ht_stmt -> fetchrow()) {
				$score = $ht_result['score_ht'];
			}

			 //
		} else {
			$score = $row['score'];
		}

		if ($row['audio_type_id'] == 3 || $row['audio_type_id'] == 2 || $row['audio_type_id'] == 7) {
			$score = "&nbsp";
		}


		if ($row['audio_type_id'] == 7) {
			$localfilepath = str_replace('D:\\Audio\\Match\\','../../audio/other/', $filepath);
		} else {
			$localfilepath = str_replace('D:\\Audio\\Match\\','../../audio/match/', $filepath);
		}

	?>


		<section class="item">
			<div class="item__panel-1  u-cf">

				<div class="item__timecontainer ">
					<div class="item__timebox">
						<span class="item__time  u-textCenter  u-block"><?php echo $row['date_created'] ?></span>
						<span class="item__audio-type  u-textCenter  u-block"><?php echo $row['audio_type_name'] ?></span>
					</div>
				</div>

				<div class="item__infocontainer  u-textCenter">

					<span class="item__score"><?php echo (empty($score) ? "&nbsp;" : $score)  ?></span>

					<span class="item__team"><?php echo $row['teams'] ?></span>
				</div>

				<div class="item__iconcontainer">
					<!-- <div class="item__infobutton">
						<i class="item__toggle-button fa fa-info-circle fa-2x"></i>
					</div> -->
				</div>
			</div>

			<div class="item__panel-2">
				<div class="item__list-pocket  is-not-active">
					<?php if (!empty($row['auxiliary_info'])) { ?>
						<li><i class="fa fa-info-circle"></i><span class="list-pocket__key">Info: </span><?php echo $row['auxiliary_info'] ?></li>
						
					<?php } ?>
						<li><i class="fa fa-futbol-o"></i><span class="list-pocket__key">League: </span><?php echo $row['name'] ?></li>
						<li><i class="fa fa-clock-o"></i><span class="list-pocket__key">Audio duration: </span><?php echo $row['duration'] ?> seconds</li>
					
				</div>

				<div class="item__audio-pocket">	
					<audio controls id="<?php echo $audio_id ?>"><source src="<?php echo $localfilepath ?>" type="audio/mpeg"><source src="<?php echo $localfilepath ?>" type="audio/ogg">Your browser does not support the audio element.</audio>
				</div>

			</div>
		</section>
	<?php } ?>

	</div> <!-- /player -->
</div> <!-- /container -->


<script type="text/javascript" src="js/application.js"></script>
<script type="text/javascript" src="js/fetch.js"></script>

</body>
</html>