<?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\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;
use BlackSheepResearch\Component\UserPoints\Site\Problems\PointsUser;
use BlackSheepResearch\Component\UserPoints\Site\Problems\RaffleData;
use BlackSheepResearch\Component\UserPoints\Site\Problems\SQL;

// JHtml::_('behavior.formvalidation');

$registration = function ($raffle, $i) {
    $taskA = $raffle->inscription ? 'unregistration' : 'registration';
    $actionA = $raffle->inscription ? Text::_('AUP_REGISTRATION') : Text::_('AUP_REGISTRATION');
    $imgA = $raffle->inscription ? Uri::base(true) . '/components/com_userpoints/assets/images/tick.png' : Uri::base(true) . '/components/com_userpoints/assets/images/publish_x.png';
    $altA = $raffle->inscription ? Text::_('AUP_REGISTRATION') : Text::_('AUP_REGISTRATION');

    return <<<REG_LINK

        <a href="javascript:void(0);"
            onclick="return Joomla.listItemTask('cb$i','$taskA')" title="$actionA">
            <img src="$imgA" border="0" alt="$altA" /></a>

REG_LINK;

};

$raffle_line = function ($raffle, $i, $k) use ($registration) {
    if (UserPoints::getUser()->authorise('core.edit', 'com_userpoints')) {
        $link = 'index.php?option=com_userpoints&amp;task=editraffle&amp;cid[]=' . $raffle->id . '';
        $linkhtml = "<a href=\"$link)\">{$this::Text_($raffle->description)}</a>";
    } else {
        $linkhtml = '';
    }
    if ($raffle->raffledate == SQL::nulldatetimeSQL()) {
        $drawdate = '-';
    } else {
        $drawdate = $this->presentDate('date', $raffle->raffledate, Text::_('DATE_FORMAT_LC2')) . '<br />' . SQL::nicetime($raffle->raffledate, 0);
    }

    return <<<RAFFLE_LINE

			<tr class="{$this::_("row$k")}">
				<td>
					{$this::_($i + 1 + $this->pagination->limitstart)}
				</td>
				<td align="center">
					{$this::_(HTMLHelper::_('grid.id', $i, $raffle->id))}
				</td>
				<td align="center">
					<b>{$raffle->id}</b>
				</td>
				<td>
                                        $linkhtml
				</td>
				<td>
					<div align="center">
					{$this::Text_($raffle->category_title)}
					</div>
				</td>
				<td align="center">
					{$registration($raffle,$i)}
				</td>
				<td align="center">
					{$this->getFormattedPointsAdm($raffle->pointstoparticipate)}
				</td>
				<td align="center">
                                        {$raffle->entriesListLink()}
				</td>
				<td align="center">
					{$raffle->numwinner}
				</td>
				<td align="center">
                                        {$raffle->raffleSystemText()}
				</td>
				<td align="center">
                                        $drawdate
				</td>
				<td align="center">
                                        {$raffle->winnerLinks()}
				</td>
				<td align="center">
					{$this::_(HTMLHelper::_('jgrid.published', $raffle->published, $i))}
				</td>
			</tr>

RAFFLE_LINE;
};

$i = $k = 0;
$lines = [];
foreach ($this->raffle as $row) {
    $lines[] = $raffle_line($row, $i, $k);
    $k = 1 - $k;
    $i++;
}

echo <<<RAFFLE_PAGE

<script type="text/javascript">
    Joomla.submitbutton = function(task)
    {
           Joomla.submitform(task, document.getElementById('adminForm'));
    }
</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">
	<table>
		<tr>
			<td align="left" width="100%">&nbsp;
                                {$this::Text_('Filter')}
                                <button onclick="this.form.submit();">{$this::Text_('Go')}</button>
				<button onclick="document.getElementById('search').value='';this.form.submit();"></button>-->
			</td>
			<td nowrap="nowrap" align="right">
				<select name="filter_category_id" class="inputbox" onchange="this.form.submit()">
                                        <option value="">{$this::Text_('JOPTION_SELECT_CATEGORY')}</option>
                                        {$this::_(HTMLHelper::_('select.options', HTMLHelper::_('category.options', 'com_userpoints'), 'value', 'text', $this->lists['filter_category_id']))}
				</select>
			</td>
		</tr>
	</table>
</div>
	<div class="clearfix"> </div>
	<table class="table table-striped">
		<thead>
			<tr>
				<th width="2%" class="title">
					{$this::Text_('NUM')}
				</th>
				<th width="3%" class="title">
					<input type="checkbox" name="checkall-toggle" value="" title="{$this::Text_('JGLOBAL_CHECK_ALL')}" onclick="Joomla.checkAll(this)" />
				</th>
				<th width="2%" class="title" >
					{$this::Text_('AUP_ID')}
				</th>
				<th class="title" >
					{$this::Text_('AUP_DESCRIPTION')}
				</th>
				<th width="10%" class="title" nowrap="nowrap">
					{$this::Text_('JCATEGORY')}
				</th>
				<th width="10%" class="title" nowrap="nowrap">
					{$this::Text_('AUP_REGISTRATION')}
				</th>
				<th width="10%" class="title" nowrap="nowrap">
					{$this::Text_('AUP_POINTS_TO_PARTICIPATE')}
				</th>
				<th width="8%" class="title" nowrap="nowrap">
					{$this::Text_('AUP_REGISTERED')}
				</th>
				<th width="10%" class="title" nowrap="nowrap">
					{$this::Text_('AUP_NUM_OF_WINNER')}
				</th>
				<th width="10%" class="title">
					{$this::Text_('AUP_RAFFLE_SYSTEM')}
				</th>
				<th width="10%" class="title">
					{$this::Text_('AUP_RAFFLE_DATE')}
				</th>
				<th class="title" nowrap="nowrap">
					{$this::Text_('AUP_WINNERS')}
				</th>
				<th width="5%" class="title" nowrap="nowrap">
					{$this::Text_('AUP_PUBLISHED')}
				</th>
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="14">
					{$this::_($this->pagination->getListFooter())}
				</td>
			</tr>
		</tfoot>

		<tbody>
                     {$this::_x($lines)}
                </tbody>
            </table>
        <input type="hidden" name="option" value="com_userpoints" />
        <input type="hidden" name="task" value="raffle" />
        <input type="hidden" name="table" value="alpha_userpoints_raffle" />
        <input type="hidden" name="redirect" value="raffle" />
        <input type="hidden" name="boxchecked" value="0" />
    </form>
</div>

RAFFLE_PAGE;

