<?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\HTML\HTMLHelper;
use Joomla\CMS\Uri\Uri;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;

// JHtml::_('behavior.formvalidation');
// JHtml::_('formbehavior.chosen', 'select');
?>
<script type="text/javascript">
	Joomla.submitbutton = function(task)
	{
		//if (task == 'cpanel' || document.formvalidator.isValid(document.id('rules-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?option=com_userpoints" method="post" name="adminForm" id="adminForm" class="form-validate">
<div id="filter-bar" class="btn-toolbar">
	<div class="pull-right">
		<?php echo Text::_('AUP_CATEGORY') . " " . $this->lists['filter_category']; ?>
	</div>
</div>
	<div class="clr"></div>
	<hr>
	<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="2%" class="title" nowrap="nowrap">&nbsp;
					<?php echo Text::_('AUP_ID'); ?>
				</th>
				<th width="3%" class="title" nowrap="nowrap">&nbsp;
					
				</th>
				<th width="3%" class="title" nowrap="nowrap">&nbsp;
					
				</th>
				<th width="12%" class="title">
					<?php echo Text::_('AUP_RULENAME'); ?>
				</th>
				<th class="title" >
					<?php echo Text::_('AUP_DESCRIPTION'); ?>
				</th>
				<th width="6%" class="title" nowrap="nowrap">
					<?php echo Text::_( 'AUP_PLUGIN' ); ?>
				</th>
				<th width="10%" class="title" nowrap="nowrap">
					<?php echo Text::_( 'AUP_USERLEVEL' ); ?>
				</th>
				<th width="8%" class="title">
					<div align="right"><?php echo Text::_( 'AUP_POINTS' ); ?></div>
				</th>				
				<th width="5%" class="title">
					<div align="center"><?php echo Text::_( 'AUP_MESSAGE' ); ?></div>
				</th>
				<th width="5%" class="title">
					<div align="center"><?php echo Text::_( 'AUP_EMAILNOTIFICATION' ); ?></div>
				</th>
				<th width="10%" class="title">
					<?php echo Text::_( 'AUP_EXPIRE' ); ?>
				</th>
				<th width="5%" class="title" nowrap="nowrap">
					<div align="center"><?php echo Text::_( 'AUP_PUBLISHED' ); ?></div>
				</th>
				<th width="5%" class="title" nowrap="nowrap">
					<div align="center"><?php echo Text::_( 'AUP_AUTOAPPROVED' ); ?></div>
				</th>				
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="14">
					<?php echo $this->pagination->getListFooter(); ?>
				</td>
			</tr>
		</tfoot>
		<tbody>
		<?php
			$i = $k = 0;
			foreach ($this->rules as $row )
			{
				$access = $row->groupname;
				
				$prefix = "";

				$img 	= $row->published ? 'publish_x.png' : 'tick.png';
				$link 	= 'index.php?option=com_userpoints&amp;task=editrule&amp;cid[]='. $row->id. '';
				
				$locked = $row->blockcopy ? '<img src="'.Uri::base(true).'/components/com_userpoints/assets/images/locked.png" alt="'.Text::_( 'AUP_THISRULECANTBECOPIED' ).'" />' : '';
				
				$published 	= HTMLHelper::_('jgrid.published', $row->published, $i );
				
				$imgA 	 = $row->autoapproved ? 'featured.png' :'disabled.png';
				$taskA 	 = $row->autoapproved ? 'unautoapprove' : 'autoapprove';
				$altA	 = $row->autoapproved ? Text::_( 'AUP_AUTOAPPROVED' ) : Text::_( 'AUP_NOTAUTOAPPROVED' );
				$actionA = $row->autoapproved ? Text::_( 'AUP_UNAUTOAPPROVEITEM' ) : Text::_( 'AUP_AUTOAPPROVEITEM' );
		
				$autoapproved = '<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>'
				;
				
				$showmessage = $row->displaymsg ? '<img src="'.Uri::base(true).'/components/com_userpoints/assets/images/megaphone.png" border="0" alt="'.Text::_( 'AUP_DISPLAY_MESSAGE_ON_FRONTEND' ).'" />' : '' ;
				$sendnotification = $row->notification ? '<img src="'.Uri::base(true).'/components/com_userpoints/assets/images/send_mail.png" border="0" alt="'.Text::_( 'AUP_EMAILNOTIFICATIONDESCRIPTION' ).'" />' : '' ;
				
				$category		= ($row->category!='') ? '<img src="../components/com_userpoints/assets/images/categories/'.$row->category.'.gif" alt="" />' : '';
				
			?>
			<tr class="<?php echo "row$k"; ?>">
				<td>
					<?php echo $i+1+$this->pagination->limitstart;?>
				</td>
				<td align="center">
					<?php echo HTMLHelper::_('grid.id', $i, $row->id ); ?>
				</td>
				<td align="center">
					<div align="center"><?php echo $row->id; ?></div>
				</td>	
				<td align="center" class="key"><span class="editlinktip hasTip" title="<?php echo Text::_('AUP_THISRULECANTBECOPIED');?>">
					<?php echo $locked; ?></span>
				</td>
				<td align="center">
					<?php echo $category; ?>
				</td>	
				<td>
				<?php 
				if (UserPoints::getUser()->authorise('core.edit', 'com_userpoints')) {
				?>
					<a href="<?php echo $link; ?>">
				<?php } ?>
						<?php echo Text::_( $row->rule_name ); ?>
				<?php 
				if (UserPoints::getUser()->authorise('core.edit', 'com_userpoints')) {
				?>
					</a>
					<?php } ?>
				</td>
				<td>
					<?php echo Text::_( $row->rule_description ); ?>
				</td>
				<td>
					<?php echo Text::_( $row->rule_plugin ); ?>
				</td>
				<td align="right">									
					<?php
					
					switch ( $row->plugin_function ) {
					
						case 'sysplgup_newregistered':
						case 'sysplgup_referralpoints':						
						case 'sysplgup_bonuspoints':
						case 'sysplgup_emailnotification':
						case 'sysplgup_winnernotification':
						case 'sysplgup_archive':		
						case 'sysplgup_changelevel1':		
						case 'sysplgup_changelevel2':		
						case 'sysplgup_changelevel3':										
							echo "<font color=\"grey\">-</font>";				
							break;							
						default:
							echo $access;		
					}					
					?>					
				</td>
				<td align="center">
				<div align="right">
					<?php
					switch ( $row->fixedpoints ) {
					
						case '0':
							echo  "-";
							break;
						case '1':
							if ($row->plugin_function!='sysplgup_changelevel1' && $row->plugin_function!='sysplgup_changelevel2' && $row->plugin_function!='sysplgup_changelevel3'){
								echo $this->getFormattedPointsAdm($row->points); 
								if ( $row->percentage ) echo "%";			
							}else{
								echo $this->getFormattedPointsAdm($row->points2); 
							}		
					}
					
					?>
					</div>
				</td>
				
				<td align="center">
					<div align="center"><?php echo $showmessage; ?></div>
				</td>
				<td align="center">
					<div align="center"><?php echo $sendnotification; ?></div>
				</td>
				
				<td align="center">
					<?php 
                                        echo $this->displayDateTime($row->rule_expire);
					?>
				</td>
				<td align="center">
					<div align="center"><?php echo $published; ?></div>
				</td>
				<td align="center">
				<div align="center">
					<?php
					switch ( $row->plugin_function ) {
						case 'sysplgup_newregistered':
						case 'sysplgup_emailnotification':
						case 'sysplgup_winnernotification':
						case 'sysplgup_archive':
							echo  "<img src=\"".Uri::base(true)."/components/com_userpoints/assets/images/checked_out.png\" border=\"0\" title=\"".Text::_('AUP_NOT_AVAILABLE')."\" alt=\"".Text::_('AUP_NOT_AVAILABLE')."\" /></a>";
							break;						
						default:
							echo $autoapproved;
					} 
					?>
					</div>
				</td>
			</tr>
			<?php
				$k = 1 - $k;
                                $i++;
				}
			?>
		</tbody>
	</table>
	<input type="hidden" name="option" value="com_userpoints" />
	<input type="hidden" name="task" value="rules" />
	<!--
	<input type="hidden" name="system" value="<?php echo $row->system; ?>" />
	<input type="hidden" name="duplicate" value="<?php echo $row->duplicate; ?>" />
	<input type="hidden" name="blockcopy" value="<?php echo $row->blockcopy; ?>" />	
	<input type="hidden" name="content_items" value="<?php echo $row->content_items; ?>" />	
	<input type="hidden" name="exclude_items" value="<?php echo $row->exclude_items; ?>" />
	-->
	<input type="hidden" name="table" value="alpha_userpoints_rules" />
	<input type="hidden" name="redirect" value="rules" />
	<input type="hidden" name="boxchecked" value="0" />
</form>
</div>