<?php
use Joomla\CMS\Language\Text;


$config				= $this->config;
$bootstrapHelper	= $this->bootstrapHelper;
$rowFluidCss		= $bootstrapHelper->getClassMapping('row-fluid');
$span12Css			= $bootstrapHelper->getClassMapping('span12');
$span6Css			= $bootstrapHelper->getClassMapping('span6');
$rating				= $this->rating;
$rating_width		= (int)($rating['rating']*140/5);
?>
<div class="<?php echo $rowFluidCss; ?>">
    <div class="<?php echo $span12Css; ?>">
		<div itemscope itemtype="http://schema.org/Article">
			<div class="avg-rating"  itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
			<div class="vr_avg_rating">
			<strong class="hasTip" title="<?php echo Text::_('EDOCMAN_AVERAGE_DOCUMENT');?>"><?php echo Text::_('EDOCMAN_AVERAGE');?>:</strong>
				<span class="avg-stars">
					<label class="hasTip" for="title" title="<?php echo number_format($rating['rating'],2);?>">
						<span class="rating-foreground" style="width:<?php echo $rating_width; ?>px; ">
						<meta itemprop="ratingValue" content="<?php echo number_format($rating['rating'],2);?>"/>
						<meta itemprop="ratingCount" content="<?php echo $rating['numberVotes'];?>" />
						</span>
					</label>    
				</span>
				</div>
			</div>
		</div>
    </div>
</div>
<?php
if(EdocmanHelper::canRate($this->item->id))
{
	?>
	<div class="<?php echo $rowFluidCss; ?> ratingform">
		<div class="<?php echo $span12Css; ?>">
			<div class="<?php echo $rowFluidCss; ?>">
				<div class="<?php echo $span6Css; ?>">
					<span class="label label-warning">
						<?php echo number_format($rating['rating'],2);?>
					</span>
					<?php
					echo sprintf(Text::_('EDOCMAN_BASED_ON_VOTES'), $rating['numberVotes']);
					?>
				</div>
				<div class="<?php echo $span6Css; ?> alignright">
					<a href="javascript:rateDocument();" class="rateDocumentBtn" id="btnRate"><?php echo Text::_('EDOCMAN_RATE_DOCUMENT');?></a>
				</div>
			</div>
			<div class="<?php echo $rowFluidCss; ?> ratingformStars">
				<div class="<?php echo $span12Css; ?>">
					<?php
					for ($i=1;$i<=5;$i++)
					{
						?>
						<div class="ratingformStar clearfix">
							<div class="ratingformStarLabel">
								<?php
								echo $i." ".Text::_('EDOCMAN_STAR');
								?>
							</div>
							<div class="ratingformStarOptions">
								<input type="radio" name="ratingOpt" id="rating_<?php echo $i?>" value="<?php echo $i;?>" />
								&nbsp;
								<?php
								for($j=1;$j<=$i;$j++)
								{
									?>
									<i class="icon icon-star fa fa-star" style="color:orange;"></i>
									<?php
								}
								?>
							</div>
						</div>
						<?php
					}
					?>
				</div>
			</div>
		</div>
	</div>
	<div class="<?php echo $rowFluidCss; ?>" id="ratingComplete">
	</div>
	<script type="text/javascript">
	function rateDocument()
	{
		var alreadyCheck = 0;
		var rateOpt = '';
		for (var i = 1; i <= 5; i++) 
		{
			if (document.getElementById('rating_' + i).checked == true) 
			{
				alreadyCheck = 1;
				rateOpt = document.getElementById('rating_' + i).value;  
			}
		}

		if(alreadyCheck == 0)
		{
			alert("<?php echo Text::_('EDOCMAN_PLEASE_SELECT_YOUR_RATING');?>");
		}
		else
		{

			jQuery('#btnRate').before('<span class="wait">&nbsp;<img src="components/com_edocman/assets/images/loading.gif" alt="" /></span>');
			var data = {
				'task'	:	'document.rate_document',
				'document_id' : <?php echo $this->item->id;?>,
				'rateOpt': rateOpt
			};
			jQuery.ajax({
				type: 'POST',
				url: 'index.php?option=com_edocman',
				data: data,
				dataType: 'html',
				success: function(htmltext) {
					jQuery('.ratingform').hide();
					jQuery('.wait').remove();
					jQuery('#ratingComplete').html(htmltext);
				},
				error: function(jqXHR, textStatus, errorThrown) {
					alert(errorThrown);
				}
			});

		}
	}
	</script>
	<?php
}
elseif(EdocmanHelper::isAlreadyRate($this->item->id))
{
	
	$myRate = EdocmanHelper::getYourRating($this->item->id);
	$rated_on = date($config->date_format,strtotime($myRate->rated_on));
	?>
	<div class="ratedNotification">
	<?php
		echo sprintf(Text::_('EDOCMAN_YOU_HAVE_RATED_FOR_THIS_DOCUMENT'),$myRate->rate, $this->item->title, $rated_on);
	?>
	</div>
	<?php
}
?>
