<?php
/*
 * @component UserPoints
 * @copyright Copyright (C) 2015-2022 Bernard Gilly - Adrien Roussel - Martin Brampton
 * @license : GNU/GPL
 * @Website : https://remository.com
 */

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

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\HTML\HTMLHelper;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;
use BlackSheepResearch\Component\UserPoints\Site\Problems\SQL;

// JHtml::_('behavior.formvalidation');
// JHtml::_('formbehavior.chosen', 'select');
?>
<script type="text/javascript">
	Joomla.submitbutton = function(task)
	{
		//if (task == 'cpanel' || task == 'cancelusers??? details???' || document.formvalidator.isValid(document.id('users-form'))) {
			Joomla.submitform(task, document.getElementById('adminForm'));
		//}
		//else {
			//alert('<?php echo $this->escape(Text::_('JGLOBAL_VALIDATION_FORM_FAILED'));?>');
		//}
	}
</script>
<div id="j-main-container">
<form action="index.php" method="post" name="adminForm" id="adminForm" class="form-validate">
	<table class="table table-striped table-hover">
		<thead>
			<tr>
				<th width="2%" class="title">
					<?php echo Text::_( 'NUM' ); ?>
				</th>
				<th width="3%" class="title">
					<input type="checkbox" name="checkall-toggle" value="" title="<?php echo Text::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this)" />
				</th>
				<th width="20%" class="title" nowrap="nowrap">
					<?php echo Text::_( 'AUP_DATE' ); ?>
				</th>
				<th width="20%" class="title">
					<?php echo Text::_('AUP_RULE'); ?>
				</th>
				<th width="5%" class="title" >
					<?php echo Text::_('AUP_POINTS'); ?>
				</th>
				<th width="20%" class="title" nowrap="nowrap">
					<?php echo Text::_( 'AUP_EXPIRE' ); ?>
				</th>
				<th width="5%" class="title" >
					<?php echo Text::_('AUP_APPROVED'); ?>
				</th>
				<th class="title" nowrap="nowrap">
					<?php echo Text::_( 'AUP_DATA' ); ?>
				</th>
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="8">
					<?php echo $this->pagination->getListFooter(); ?>
				</td>
			</tr>
		</tfoot>
		<tbody>
		<?php
			$i = $k = 0;
			foreach ($this->userDetails as $row)
			{
				if (UserPoints::getUser()->authorise('core.edit', 'com_userpoints')) {
					$link 	= 'index.php?option=com_userpoints&amp;task=edituserdetails&amp;cid[]='. $row->id. '&amp;name=' . $this->name . '';
				} else {
                                    $link 	= '';
                                }
				
				$prefix = "";
				$imgA 	 = $row->approved ? 'icon-16-add.png' :'publish_x.png';
				$taskA 	 = $row->approved ? 'unapprove' : 'approve';
				$altA	 = $row->approved ? Text::_( 'AUP_APPROVE' ) : Text::_( 'AUP_NOTAPPROVE' );
				$actionA = $row->approved ? Text::_( 'AUP_NOTAPPROVE' ) : Text::_( 'AUP_APPROVE' );
		
				$approved = '<a href="javascript:void(0);" onclick="return Joomla.listItemTask(\'cb'. $i .'\',\''. $prefix.$taskA .'\')" title="'. $actionA .'">				
				<img src="'.Uri::base(true).'/components/com_userpoints/assets/images/'. $imgA .'" border="0" alt="'. $altA .'" /></a>'
				;
			
				if ( $row->status ) {
					// already approved !
					$approved = "<img src=\"".Uri::base(true)."/components/com_userpoints/assets/images/icon-16-allowinactive.png\" border=\"0\" title=\"".Text::_('AUP_ALREADY_APPROVED')."\" alt=\"".Text::_('AUP_ALREADY_APPROVED')."\" /></a>";
				}
				
				$style  = ( $row->points < 0 ) ? 'style="color:red;"' : '' ;
				
			?>
			<tr class="<?php echo "row$k"; ?>" <?php echo "$style"; ?>>
				<td>
					<?php echo $i+1+$this->pagination->limitstart;?>
				</td>
				<td align="center">
					<?php echo HTMLHelper::_('grid.id', $i, $row->id ); ?>
				</td>
				<td>
				<a href="<?php echo $link; ?>">
				<i class="icon-calendar"></i>
					<?php 
					if ( $row->insert_date == SQL::nulldatetimeSQL()) {
						echo '-';
					} else {
						echo $this->presentDate('date', $row->insert_date,  Text::_('DATE_FORMAT_LC2') );
					}
					?>
				</a>
				</td>				
				<td>
					<?php echo Text::_( $row->rule_name ); ?>					
				</td>
				<td align="center">
					<?php echo $this->getFormattedPointsAdm( $row->points ); ?>
				</td>
				<td align="center">
				<i class="icon-calendar"></i>
					<?php 
                                        echo $this->displayDateTime($row->expire_date);
					?>
				</td>
				<td align="center">
					<?php echo $approved; ?>
				</td>
				<td>				
					<?php echo $row->datareference; ?>
				</td>
			</tr>
			<?php
				$k = 1 - $k;
                                $i++;
				}
			?>
		</tbody>
	</table>
	<input type="hidden" name="option" value="com_userpoints" />
	<input type="hidden" name="task" value="showdetails" />
	<input type="hidden" name="c2id" value="<?php echo isset($row) ? $row->referreid : 0; ?>" />
	<input type="hidden" name="name" value="<?php echo $this->name; ?>" />
	<input type="hidden" name="table" value="alpha_userpoints_details" />
	<input type="hidden" name="redirect" value="showdetails&cid=<?php echo isset($row) ? $row->referreid : 0; ?>&name=<?php echo $this->name; ?>" />
	<input type="hidden" name="boxchecked" value="0" />
</form>
</div>
<!-- Modal Form Custom Points-->
<div class="modal hide fade" id="generatorModal" data-toggle="modal">
	<form action="index.php" method="post" name="adminFormCustomPoints" id="adminFormCustomPoints" autocomplete="off" style="margin:0;">
		<div class="modal-header">
			<button type="button" role="presentation" class="close" data-dismiss="modal">&times;</button>
			<h3><?php echo Text::_('AUP_CUSTOM_POINTS');?></h3>
		</div>
		<div class="modal-body">
			<div class="form-horizontal">
				<fieldset class="adminform">
					<div class="control-group">
						<div class="control-label">
							<span class="editlinktip hasTip" title="<?php echo Text::_( 'AUP_POINTS' ); ?>::<?php echo Text::_('AUP_CUSTOM_POINTS_DESCRIPTION'); ?>">
								<?php echo Text::_( 'AUP_POINTS' ); ?>:
							</span>
						</div>
						<div class="controls">							
							<input class="inputbox" type="text" name="points" id="points" size="20" maxlength="255" value="" />
						</div>
					</div>
					<div class="control-group">
						<div class="control-label">
							<span class="editlinktip hasTip" title="<?php echo Text::_( 'AUP_DESCRIPTION' ); ?>::<?php echo Text::_('AUP_DESCRIPTION'); ?>">
								<?php echo Text::_( 'AUP_DESCRIPTION' ); ?>:
							</span>
						</div>
						<div class="controls">							
							<input class="inputbox" type="text" name="reason" id="reason" size="80" maxlength="255" value="" />
						</div>
					</div>
				</fieldset>
				<input type="hidden" name="option" value="com_userpoints" />
				<input type="hidden" name="task" value="savecustompoints" />
				<input type="hidden" name="cid" value="<?php echo $this->cid; ?>" />
				<input type="hidden" name="name" value="<?php echo $this->name; ?>" />
			</div>
		</div>
		<div class="modal-footer">
			<button class="btn" type="button" data-dismiss="modal">
				<?php echo Text::_( 'Cancel' ); ?>
			</button>
			<button class="btn btn-primary" type="submit" onclick="Joomla.submitform('savecustompoints', this.form);">
				<?php 				
					echo Text::_( 'Save' ); 
				?>
			</button>
		</div>
	</form>
</div>