<?php

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

namespace BlackSheepResearch\Component\UserPoints\Administrator\Viewers;

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

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

class RaffleHTML extends AdminHTML {

	function displaylist($tpl = null) {
		$logo = '<img src="'. Uri::root() . 'administrator/components/com_userpoints/assets/images/icon-48-userpoints.png" />&nbsp;&nbsp;';

		ToolbarHelper::title( $logo . 'UserPoints :: ' .  Text::_( 'AUP_RAFFLE' ), 'featured' );
		$this->getCpanelToolbar();
		ToolbarHelper::publishList();
		ToolbarHelper::unpublishList();
		ToolbarHelper::divider();
		if (UserPoints::getUser()->authorise('core.edit', 'com_userpoints')) {
			ToolbarHelper::editList( 'editraffle' );
		}
		if (UserPoints::getUser()->authorise('core.create', 'com_userpoints')) {
			ToolbarHelper::addNew( 'editraffle' );
		}
		if (UserPoints::getUser()->authorise('core.delete', 'com_userpoints')) {
			ToolbarHelper::custom( 'deleteraffle', 'delete.png', 'delete.png', Text::_('AUP_DELETE') );
		}
		$this->getPrefHelpToolbar();

		$this->pagination = new Pagination( $this->total, $this->limitstart, $this->limit );

		parent::display( $tpl) ;
	}

	function edit_raffle($tpl = null) {

		$document	=  Factory::getDocument();
		$language 	= Factory::getLanguage();
		$tag 		= $language->getTag();

		Factory::getApplication()->input->set( 'hidemainmenu', 1 );

		$logo = '<img src="'. Uri::root() . 'administrator/components/com_userpoints/assets/images/icon-48-userpoints.png" />&nbsp;&nbsp;';

		ToolbarHelper::title( $logo . 'UserPoints :: ' .  Text::_( 'AUP_RAFFLE' ), 'featured' );
		ToolbarHelper::custom( 'cpanel', 'default.png', 'default.png', Text::_('AUP_CPANEL'), false );
		if (UserPoints::getUser()->authorise('core.edit.state', 'com_userpoints')) {
			ToolbarHelper::apply('applyraffle');
			ToolbarHelper::save( 'saveraffle' );
		}
		ToolbarHelper::cancel( 'cancelraffle' );
		ToolbarHelper::divider();
		ToolbarHelper::preferences('com_userpoints');
		if (file_exists( JPATH_COMPONENT . "/help/" . $tag . "/screen.userpoints.html")){
			ToolbarHelper::help( $tag . '/screen.userpoints', true );
		} else ToolbarHelper::help( 'en-GB/screen.userpoints', true );

  		//HTMLHelper::_('behavior.calendar');

		$this->lists = [];
		// build the html
		$this->lists['published'] = HTMLHelper::_('select.booleanlist', 'published', '', $this->row->published);
		$this->lists['inscription'] = HTMLHelper::_('select.booleanlist', 'inscription', '', $this->row->inscription);
		$this->lists['multipleentries'] = HTMLHelper::_('select.booleanlist', 'multipleentries', '', $this->row->multipleentries);

		$this->lists['removepointstoparticipate'] = HTMLHelper::_('select.booleanlist', 'removepointstoparticipate', '', $this->row->removepointstoparticipate);

		$javascript = 'onchange="return mxctoggleSystemRaffle(\'rafflesystemexpand\'+this.value);"';
		$rafflesys[] = HTMLHelper::_('select.option', '0', Text::_( 'AUP_POINTS' ));
		$rafflesys[] = HTMLHelper::_('select.option', '1', Text::_( 'AUP_COUPON_CODES' ));
		$rafflesys[] = HTMLHelper::_('select.option', '2', Text::_( 'AUP_EMAIL_WITH_A_LINK_TO_DOWNLOAD' ));
		$rafflesys[] = HTMLHelper::_('select.option', '3', Text::_( 'AUP_EMAIL_ONLY' ));
		$this->lists['rafflesystem'] = HTMLHelper::_( 'select.genericlist', $rafflesys, 'rafflesystem', 'class="inputbox" size="1" ' . $javascript, 'value', 'text', $this->row->rafflesystem );
		//$this->lists['rafflesystem'] = HTMLHelper::_( 'select.genericlist', $rafflesys, 'rafflesystem' );

		$numwin[] = HTMLHelper::_('select.option', '1', '1');
		$numwin[] = HTMLHelper::_('select.option', '2', '2');
		$numwin[] = HTMLHelper::_('select.option', '3', '3');
		$this->lists['numwinner'] = HTMLHelper::_( 'select.genericlist', $numwin, 'numwinner', 'class="inputbox" size="1"', 'value', 'text', $this->row->numwinner );

		$db			    = SQL::getInstance();
		$query = "SELECT * FROM #__alpha_userpoints_coupons";
		$db->setQuery($query);
		$couponcodes = $db->loadObjectlist();

		$selCouponCode[] = HTMLHelper::_('select.option', '0', Text::_( 'AUP_SELECT_A_COUPON_CODE_FOR_THIS_RANK' ));
		if ( $couponcodes )
		{
			foreach ($couponcodes as $couponcode)
			{
				$selCouponCode[] = HTMLHelper::_('select.option', $couponcode->id, $couponcode->couponcode . ' [ '.$couponcode->points . ' ' . Text::_( 'AUP_POINTS' )  . ' ]');
			}
		}

		$this->lists['couponcodeid1'] = HTMLHelper::_( 'select.genericlist', $selCouponCode, 'couponcodeid1', 'class="inputbox" size="1" ', 'value', 'text', $this->row->couponcodeid1 );
		$this->lists['couponcodeid2'] = HTMLHelper::_( 'select.genericlist', $selCouponCode, 'couponcodeid2', 'class="inputbox" size="1" ', 'value', 'text', $this->row->couponcodeid2 );
		$this->lists['couponcodeid3'] = HTMLHelper::_( 'select.genericlist', $selCouponCode, 'couponcodeid3', 'class="inputbox" size="1" ', 'value', 'text', $this->row->couponcodeid3 );

		$this->lists['sendcouponbyemail'] = HTMLHelper::_('select.booleanlist', 'sendcouponbyemail', '', $this->row->sendcouponbyemail);

		parent::display( "form" ) ;
	}
}
