<?php
/**
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Layout\FileLayout;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;

HTMLHelper::_('jquery.framework');
HTMLHelper::_('bootstrap.modal');

$doc = Factory::getDocument();
$params = $this->params;
$session = Factory::getSession();
$cards = $session->get('scanned_cards', [], 'com_cmgiftcard');

$layout = new FileLayout('scanned_cards', JPATH_ROOT . '/administrator/components/com_cmgiftcard/layouts');
$recent = $layout->render(['cards' => $cards]);

$doc->addStyleSheet('components/com_cmgiftcard/assets/css/styles.css');

$doc->addScript('components/com_cmgiftcard/assets/js/html5-qrcode.min.js');
$doc->addScript('components/com_cmgiftcard/assets/js/scanner.js');

$doc->addScriptDeclaration('var token = "' . $session->getFormToken() . '";');
?>
<div class="cmgiftcard<?php echo $this->pageclass_sfx?>">
	<?php if ($params->get('show_page_heading')) : ?>
		<h1><?php echo $this->escape($params->get('page_heading')); ?></h1>
	<?php endif; ?>

	<div class="row">
		<div class="col-md-6">
			<div class="scanner-container">
				<div id="scanner"></div>
			</div>
		</div>

		<div class="col-md-6 px-4 pt-4 pt-md-0">
		<div id="recent"><?php echo $recent; ?></div>
		</div>
	</div>

	<div id="loader" class="loader"><div class="spinner"></div></div>
</div>

<div id="cardModal" class="modal" tabindex="-1">
	<div class="modal-dialog modal-fullscreen">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title"></h5>
				<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<?php echo Text::_('COM_CMGIFTCARD_CLOSEN'); ?>"></button>
			</div>
			<div class="modal-body"></div>
			<div class="modal-footer">
				<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo Text::_('COM_CMGIFTCARD_CLOSE'); ?></button>
				<button type="button" class="btn btn-primary" id="redeemButton"><?php echo Text::_('COM_CMGIFTCARD_REDEEM'); ?></button>
				<button type="button" class="btn btn-danger" id="cancelRedemptionButton"><?php echo Text::_('COM_CMGIFTCARD_CANCEL_REDEMPTION'); ?></button>
			</div>
		</div>
	</div>
</div>

<div id="errorModal" class="modal" tabindex="-1">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title"><?php echo Text::_('COM_CMGIFTCARD_ERROR'); ?></h5>
				<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<?php echo Text::_('COM_CMGIFTCARD_CLOSE'); ?>"></button>
			</div>
			<div class="modal-body"><p class="m-2"></p></div>
			<div class="modal-footer">
				<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo Text::_('COM_CMGIFTCARD_CLOSE'); ?></button>
			</div>
		</div>
	</div>
</div>