<?php
	/**
	 * @package     SP Soccer
	 * @author      JoomShaper http://www.joomshaper.com
	 * @copyright   Copyright (C) 2010 - 2016 JoomShaper. All rights reserved.
	 * @license     GNU General Public License version 2 or later; see LICENSE.txt
	 */

	defined('_JEXEC') or die('Restricted access');

	// Get match by id
	function getmatchById($match_id) {
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);

		// Select all records from the user profile table where key begins with "custom.".
		$query->select('*');
		$query->from($db->quoteName('#__spsoccer_matches'));
		$query->where($db->quoteName('enabled')." = 1");
		$query->where($db->quoteName('spsoccer_match_id')." = ".$db->quote($match_id));
		//Language
		$query->where('language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
		//Access
		$query->where($db->quoteName('access')." IN (" . implode( ',', JFactory::getUser()->getAuthorisedViewLevels() ) . ")");
		$db->setQuery($query);
		$result = $db->loadObject();
		return $result;
	}

	if($this->last_played_match){
		$last_match_info = getmatchById($this->last_played_match->spsoccer_match_id);
		$match_model = FOFModel::getTmpInstance('Matches', 'SpsoccerModel');
		$teamone_score_lists 	= json_decode($last_match_info->list_teamone_score);
		$teamtwo_score_lists 	= json_decode($last_match_info->list_teamtwo_score);

		$t1_g_players = array();
		// has team one player goal
		if (isset($teamone_score_lists->player_goal)) {
			//Team one Goals
			$teamone_playergoals = $teamone_score_lists->player_goal;
			// Team one player id
			$teamone_playenames	 = $teamone_score_lists->player_name;
			
			// Team one goal player
			foreach ($teamone_playergoals as $key => $teamone_playergoal) {
				if($teamone_playergoal) {
					$t1_g_players[] = $match_model->getPlayerInfo($teamone_playenames[$key]);
				}
			}
		}

		// has team two player goal
		$t2_g_players = array();
		if (isset($teamtwo_score_lists->player_goal)) {
			//Team one Goals
			$teamtwo_playergoals = $teamtwo_score_lists->player_goal;
			// Team one player id
			$teamtwo_playergoals	 = $teamtwo_score_lists->player_name;
			// Team one goal player
			foreach ($teamtwo_playergoals as $key => $teamtwo_playergoal) {
				if($teamtwo_playergoal) {
					$t2_g_players[] = $match_model->getPlayerInfo($teamtwo_playergoals[$key]);
				}
			}
		}

		//Has match time line
		if (isset($last_match_info->list_match_timeline)) {
			// Get Timeline
			$match_timeline_decode 	= json_decode($last_match_info->list_match_timeline);

			// Match goals
			$match_goals = array();

			if($match_timeline_decode){
				//Get values
				$match_timeline_times			= $match_timeline_decode->min;
				$match_timeline_types			= $match_timeline_decode->type;
				$match_timeline_teams			= $match_timeline_decode->team;
				$match_timeline_players			= $match_timeline_decode->player;

				$timeline_key = 0;
				$goal_key = 0;
				// Match timeline
				$this->match_timelines = array();
				foreach ($match_timeline_times as $key => $match_timeline_time) {
					//Replace type _ by space
					$match_timeline_name[$key] = $match_timeline_types[$key];
					$match_timeline_types[$key] = str_replace('_', ' ', $match_timeline_types[$key]);
					// Get player info
					$match_players_info[$key] = $match_model->getPlayerInfo($match_timeline_players[$key]);
					$match_players_name		  = $match_players_info[$key]->title;
					$match_players_squad_no	  = $match_players_info[$key]->squad_no;

					//Get team id
					$match_timeline_teams[$key] = ($match_timeline_teams[$key] == 'team_one') ? $last_match_info->teamone: $last_match_info->teamtwo;
					
					// Goals
					if ($match_timeline_types[$key] == 'goal') {
						$match_goals[$goal_key] = array(
							'time'  	=> $match_timeline_times[$key],
							'type' 		=> $match_timeline_types[$key],
							'team'   	=> $match_timeline_teams[$key],
							'player'   	=> $match_players_name,
						);
						$goal_key ++;
					}
					$timeline_key ++;
				} // END:: foreach $match_timelines	
			}
		}

		// before half time
		$match_goals_before45s = array();
		foreach ($match_goals as $match_goal) {
			if ($match_goal['time'] <= 45) {
				$match_goals_before45s[] = $match_goal;
			}
		}

		// after half time
		$match_goals_after45s = array();
		foreach ($match_goals as $match_goal) {
			if ($match_goal['time'] > 45) {
				$match_goals_after45s[] = $match_goal;
			}
		}
	}
	
?>
<div id="spsoccer" class="spsoccer view-spsoccer-tournament">
	<div class="spsoccer-tournament">
		<div class="spsoccer-row">
			<div class="tournament-details-top">
				<div class="spsoccer-container">
					<?php if (!empty($this->last_played_match)) { ?>
						<div class="match-time">
							<span><?php echo $this->last_played_match->title; ?> - <?php echo JHtml::date($this->last_played_match->date, 'd M Y'); ?></span>
							<span><?php echo JHTML::date($this->last_played_match->time, 'h:i a'); ?></span>
						</div>
						<div class="match-result">
							<!-- goal-timeline -->
							<div class="goal-timeline hidden-sm hidden-xs">
								<span class="timeline-titme">00"</span>
								<ul class="goal-timeline1">
									<?php
										$total_playtime = str_replace(':00', '', $last_match_info->play_time);
										$half_time 		= round($total_playtime/2);
									?>
									<?php foreach ($match_goals_before45s as $goal_before45) {
										$time_percentage = round(($goal_before45['time']/$half_time)*100);
									?>
									<li style="left:<?php echo $time_percentage; ?>%" class="team1 goal"><span class="time"><?php echo $goal_before45['player']. ' ' . $goal_before45['time']; ?>"</span></li>
									<?php } ?>
								</ul>
								<span class="timeline-titme"><?php echo $half_time; ?>"</span>
								<ul class="goal-timeline2">
									<?php foreach ($match_goals_after45s as $goal_after45) {
										$time_percentage = round((($goal_after45['time']-$half_time)/$half_time)*100);
									?>
									<li style="left:<?php echo $time_percentage; ?>%" class="team1 goal"><span class="time"><?php echo $goal_after45['player']. ' ' . $goal_after45['time']; ?>"</span></li>
									<?php } ?>

								</ul>
								<span class="timeline-titme"><?php echo $total_playtime; ?>"</span>
							</div> <!-- END:: goal-timeline -->
							<div class="spsoccer-row">
								<div class="match-goal-content-wrap">
									<div class="bg-white"></div>
									<div class="match-team-left spsoccer-col-md-3 text-left hidden-sm hidden-xs">
										<div class="goal-scorer">
											<div class="goal-football"></div>
											<?php foreach ($t1_g_players as $t1_g_player) { ?>
												<p><?php echo $t1_g_player->title . ' ' . $t1_g_player->squad_no.'`'; ?></p>
											<?php } ?>
										</div>
									</div>
									<div class="spsoccer-col-md-6 spsoccer-col-sm-12">
										<div class="match-goal-count">
											<div class="match-title">
												<img class="match-result-logo-thumb" src="<?php echo $this->last_played_match->teamone_logo; ?>">
												<h4><?php echo $this->last_played_match->teamone_name; ?></h4>	
											</div>
											<div class="match-result-content-wrap">
												<h3><?php echo $this->last_played_match->teamone_score . ' - ' . $this->last_played_match->teamtwo_score; ?></h3>
											</div>
											<div class="match-title">
												<img class="match-result-logo-thumb" src="<?php echo $this->last_played_match->teamtwo_logo; ?>">
												<h4><?php echo $this->last_played_match->teamtwo_name; ?></h4>
											</div>
										</div>
									</div>
									<div class="match-team-right spsoccer-col-md-3 text-right hidden-sm hidden-xs">
										<div class="goal-scorer">
											<div class="goal-football"></div>
											<?php foreach ($t2_g_players as $t2_g_player) { ?>
												<p><?php echo $t2_g_player->title . ' ' . $t2_g_player->squad_no.'`'; ?></p>
											<?php } ?>
										</div>
									</div>
								</div> <!-- /.match-goal-content-wrap -->
							</div> <!-- /.row -->
						</div>
						<?php } else{ ?>
							<div class="match-result text-center">
								<h2><?php echo JText::_('COM_SPSOOCER_NO_MATCH_PLAYED'); ?></h2>
							</div>
						<?php } ?>
				</div>
			</div>
		</div>
		<div class="spsoccer-tournament-tab-content">
			<div class="spsoccer-container">
				<div class="tournament-details-tab" role="tabpanel">
				<!-- Nav tabs -->
					<ul class="nav nav-tabs" role="tablist">

						<li role="presentation" class="active">
							<a href="#tournament-group" aria-controls="tournament-group" role="tab" data-toggle="tab">
								<?php echo JText::_('COM_SPSOOCER_GROUPS'); ?>
							</a>
						</li>

						<li role="presentation">
							<a href="#tournament-fixture" aria-controls="tournament-fixture" role="tab" data-toggle="tab">
								<?php echo JText::_('COM_SPSOOCER_FIXTURE'); ?>
							</a>
						</li>
						<li role="presentation">
							<a href="#tournament-result" aria-controls="tournament-result" role="tab" data-toggle="tab">
								<?php echo JText::_('COM_SPSOOCER_RESULTS'); ?>
							</a>
						</li>
						<li role="presentation">
							<a href="#tournament-standing" aria-controls="tournament-standing" role="tab" data-toggle="tab">
								<?php echo JText::_('COM_SPSOOCER_STANDINGS'); ?>
							</a>
						</li>
					</ul>

				<!-- Tab panes -->
					<div class="tab-content">

						<div role="tabpanel" class="tab-pane active" id="tournament-group">
							<!-- has Groups -->
							<?php if(!empty($this->groups) && $this->show_group) {
								$i = 1;
								foreach ($this->groups as $this->group) { ?>
								<div class="spsoccer-group-wrapper <?php echo $this->group->ungroup; ?>">
									<h3 class="text-center title"><span><?php echo $this->group->title; ?></span></h3>
									<div class="item-list-wrap">
										<?php foreach ($this->group->groups_teams_info as $this->group_team_infos) {
											$this->group_team_infos['team_logo'] = SpsoccerHelper::hasImg($this->group_team_infos['team_logo']);
											?>

											<div class="spsoccer-col-md-4 spsoccer-col-sm-4 child-<?php echo $i; ?>">

												<a href="<?php echo $this->group_team_infos['team_url']; ?>">
												<?php if ($i != 3) { ?>
													<div class="club-name">
														<img class="spsoccer-img-thumbnail spsoccer-img-responsive" src="<?php echo $this->group_team_infos['team_logo']; ?>" alt="<?php echo $this->group_team_infos['team_name']; ?>">
														<span class="spsoccer-team-name"><?php echo $this->group_team_infos['team_name']; ?></span>
													</div>
												<?php } elseif ($i == 3) {?>
													<div class="club-name">
														<img class="spsoccer-img-thumbnail spsoccer-img-responsive" src="<?php echo $this->group_team_infos['team_logo']; ?>" alt="<?php echo $this->group_team_infos['team_name']; ?>">
														<span class="spsoccer-team-name"><?php echo $this->group_team_infos['team_name']; ?></span>
													</div>

												<?php } ?>
												</a>

											</div> <!-- /.spsoccer-col-lg -->

										<?php

											if ($i == 3){ $i = 0; }
												$i++;
											} // END:: forech $group_teams_infos ?>
									</div> <!-- ./spsoccer-row -->
								</div> <!-- ./spsoccer-group-wrapper -->
								<?php } // END:: forech $this->groups ?>
							<?php } // END:: !empty groups ?>
						</div> <!-- ./tournament-group -->

						<div role="tabpanel" class="tab-pane" id="tournament-fixture">
							<!-- has Sessions -->
								<?php if(!empty($this->sessions)) {?>

									<?php
										foreach ($this->sessions as $session) {
										// has session matches
										if (!empty($session->session_matches)) {
									?>
									<div class="spsoccer-fixture-wrapper">
										<h3 class="text-center title"><span><?php echo $session->title; ?></span></h3>

										<?php foreach ($session->session_matches as $session_match) {
											$session_match->team_two_info->logo = SpsoccerHelper::hasImg($session_match->team_two_info->logo);

											?>

											<!-- START:: fixture -->
											<div class="item-list-wrap">

												<div class="spsoccer-col-md-4 spsoccer-col-sm-4 text-left">
													<img class="spsoccer-img-thumbnail spsoccer-img-responsive" src="<?php echo $session_match->team_one_info->logo; ?>" alt="<?php echo $session_match->team_one_info->title; ?>">
													<span class="spsoccer-team-name"><?php echo $session_match->team_one_info->title; ?></span>
												</div> <!-- /.spsoccer-col-lg -->

												<div class="spsoccer-col-md-4 spsoccer-col-sm-4 text-center">
													<div class="spsoccer-match-date-time">
														<span><?php echo $session_match->match_date; ?></span>,
														<span><?php echo $session_match->match_time; ?></span>
													</div>
													<p class="spsoccer-match-location">
														<?php echo $session_match->match_location->title; ?>
													</p>
												</div><!-- /.spsoccer-col-lg -->

												<div class="spsoccer-col-md-4 spsoccer-col-sm-4 text-right">
													<span class="spsoccer-team-name"><?php echo $session_match->team_two_info->title; ?></span>
													<img class="spsoccer-img-thumbnail spsoccer-img-responsive" src="<?php echo $session_match->team_two_info->logo; ?>" alt="<?php echo $session_match->team_two_info->title; ?>">
												</div><!-- /.spsoccer-col-lg -->

											</div>
											<!-- /.spsoccer-row -->

											<?php //print_r($session_match); ?>

										<?php } // END:: forech $session_matches ?>
									</div> <!-- ./spsoccer-group-wrapper -->
									<?php } // END:: has session match  ?>
									<?php } // END:: forech $this->sessions  ?>
								<?php } // END:: !empty sessions ?>

						</div> <!-- /.tournament-fixture -->


						<div role="tabpanel" class="tab-pane" id="tournament-result">
							<!-- has match is played -->
							<?php if(!empty($this->sessions)) {?>

								<?php foreach ($this->sessions as $session) {
									// Has match session
									if (!empty($session->played_matches)) {

										$session->title = (strpos($this->group->slug, 'uncategory') !== FALSE) ? JText::_('COM_SPSOOCER_RESULTS') : $session->title;
								?>
								<div class="spsoccer-result-wrapper">
									<h3 class="text-center title"><span><?php echo $session->title; ?></span></h3>
									<?php foreach ($session->played_matches as $session_played_match) {
										$session_played_match->team_one_info->logo = SpsoccerHelper::hasImg($session_played_match->team_one_info->logo);
										$session_played_match->team_two_info->logo = SpsoccerHelper::hasImg($session_played_match->team_two_info->logo);
									?>

									<!-- START:: fixture -->
									<div class="item-list-wrap">

										<div class="spsoccer-col-md-4 spsoccer-col-sm-4 text-left">
											<a href="<?php echo $session_played_match->url; ?>">
												<img class="spsoccer-img-thumbnail spsoccer-img-responsive" src="<?php echo $session_played_match->team_one_info->logo; ?>" alt="<?php echo $session_played_match->team_one_info->title; ?>">
												<span class="spsoccer-team-name"><?php echo $session_played_match->team_one_info->title; ?></span>
											</a>
										</div> <!-- /.spsoccer-col-lg -->

										<div class="spsoccer-col-md-4 spsoccer-col-sm-4 text-center">

											<h2><?php echo $session_played_match->teamone_score . ' : ' . $session_played_match->teamtwo_score; ?></h2>

											<div class="spsoccer-match-date-time">
												<span><?php echo $session_played_match->match_date; ?></span>,
												<span><?php echo $session_played_match->match_time; ?></span>
											</div>
											<p class="spsoccer-match-location">
												<?php echo $session_played_match->match_location->title; ?>
											</p> <!-- /.spsoccer-match-location-->

										</div><!-- /.spsoccer-col-lg -->

										<div class="spsoccer-col-md-4 spsoccer-col-sm-4 text-right">
											<a href="<?php echo $session_played_match->url; ?>">
												<span class="spsoccer-team-name"><?php echo $session_played_match->team_two_info->title; ?></span>
												<img class="spsoccer-img-thumbnail spsoccer-img-responsive" src="<?php echo $session_played_match->team_two_info->logo; ?>" alt="<?php echo $session_played_match->team_two_info->title; ?>">
											</a>
										</div><!-- /.spsoccer-col-lg -->

									</div>
									<!-- END:: fixture -->
									<!-- /.spsoccer-row -->

									<?php //print_r($session_played_match); ?>

									<?php } // END:: forech $session_matches ?>
								</div> <!-- ./spsoccer-group-wrapper -->
									<?php } // END:: has session match  ?>
									<?php } // END:: forech $this->sessions  ?>
									<?php } // END:: !empty sessions ?>
						</div>	<!-- /.tournament-result -->



						<div role="tabpanel" class="tab-pane" id="tournament-standing">

							<!-- has Standings -->
							<div class="spsoccer-standings-wrapper">

								<div class="pointboard-header">
									<div class="spsoccer-col-md-4 spsoccer-col-sm-4 header-left spsoccer-col-xs-5">
										<ul>
											<li><?php echo JText::_('COM_SPSOOCER_POSITION'); ?></li>
											<li><?php echo JText::_('COM_SPSOOCER_TEAM'); ?></li>
										</ul>
									</div>
									<div class="spsoccer-col-md-8 spsoccer-col-sm-8 header-right spsoccer-col-xs-7">
										<ul>
											<li><?php echo JText::_('COM_SPSOOCER_STANDINGS_P'); ?></li>
											<li><?php echo JText::_('COM_SPSOOCER_STANDINGS_W'); ?></li>
											<li><?php echo JText::_('COM_SPSOOCER_STANDINGS_D'); ?></li>
											<li><?php echo JText::_('COM_SPSOOCER_STANDINGS_L'); ?></li>
											<li><?php echo JText::_('COM_SPSOOCER_STANDINGS_GF'); ?></li>
											<li><?php echo JText::_('COM_SPSOOCER_STANDINGS_GA'); ?></li>
											<li><?php echo JText::_('COM_SPSOOCER_STANDINGS_AVG'); ?></li>
											<li><?php echo JText::_('COM_SPSOOCER_STANDINGS_PTS'); ?></li>
										</ul>
									</div>
								</div>

								<?php
								// start countig team
								foreach ($this->groups as $sgroup) {
									if(!empty($sgroup->point_tables )){

									if ($this->sgroup->uncategory == false) { ?>
									<div class="item-list-wrap text-center">
										<h3><?php echo $sgroup->title; ?></h3>
									</div>
								<?php
									} // END:: $this->sgroup->uncategory
									// has point table
									if (!empty($sgroup->point_tables)) {
									$item_no = 1;
									foreach ($sgroup->point_tables as $point_table){

									$point_table['team_logo'] = SpsoccerHelper::hasImg($point_table['team_logo']);
								?>

								<div class="item-list-wrap">

									<div class="spsoccer-col-md-4 spsoccer-col-sm-4 spsoccer-col-xs-5 team-position">
										<div class="teamthumb">
											<div class="inline-block">
												<p><?php echo $item_no; ?></p>
											</div>
											<img class="spsoccer-img-thumbnail spsoccer-img-responsive" src="<?php echo $point_table['team_logo']; ?>" alt="<?php echo $point_table['team_name']; ?>">
											<span class="spsoccer-team-name"><?php echo $point_table['team_name']; ?></span>
										</div>
									</div> <!-- /.spsoccer-col-lg -->

									<div class="spsoccer-col-md-8 spsoccer-col-sm-8 spsoccer-col-xs-7 team-stats">
										<div class="team-point-table">
											<ul>
												<li> <span><?php echo $point_table['plays']; ?></span> </li>
												<li> <span><?php echo $point_table['wons']; ?></span> </li>
												<li> <span><?php echo $point_table['draws']; ?></span> </li>
												<li> <span><?php echo $point_table['losts']; ?></span> </li>
												<li> <span><?php echo $point_table['gforwards']; ?></span> </li>
												<li> <span><?php echo $point_table['gagainsts']; ?></span> </li>
												<li> <span><?php echo $point_table['avgs']; ?></span> </li>
												<li> <span><?php echo $point_table['ptss']; ?></span> </li>
											</ul>
										</div>
									</div> <!-- /.spsoccer-col-lg -->

								</div> <!-- ./spsoccer-row -->

								<?php $item_no ++; } } // END:: forech $group_teams_infos // END:: has point table  ?>
							<?php } }// END:: foreach $this->point_tables ?>

							</div> <!-- ./spsoccer-standings-wrapper -->
							<?php  // END:: !empty groups ?>
						</div> <!-- /.tournament-standing -->

					</div> <!-- /.tab-content -->

				</div> <!-- tabpanel -->

			</div> <!-- /.spsoccer-container -->
		</div> <!-- /.spsoccer-tournament-tab-content -->

	</div> <!-- /.spsoccer-tournament -->
</div> <!-- /#spsoccer .spsoccer -->
