<?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\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Session\Session;

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

$app = Factory::getApplication();
$jinput = $app->input;
$doc = Factory::getDocument();

$codeUpload = $jinput->get('codeupload', 0, 'uint');
$codeFile = $app->getUserState('com_cmgiftcard.tools.code_file', '');

if ($codeUpload == 1 && $codeFile != '')
{
	$token = '&' . Session::getFormToken() . '=1';

	$js = <<<JS
jQuery(document).ready(function() {
	jQuery('#codeImporting').show();
	jQuery.ajax({
		cache: false,
		url: 'index.php?option=com_cmgiftcard&task=tools.importCode&format=json{$token}',
		type: 'get',
		success:function(r) {
			if (r.success) {
				jQuery('#codeImporting').html('<div class="alert alert-success">' + r.message + '</div>');
			} else {
				jQuery('#codeImporting').html('<div class="alert alert-error">' + r.message + '</div>');
			}
		}
	});
})
JS;

$doc->addScriptDeclaration($js);
}

$doc->addStyleSheet('components/com_cmgiftcard/assets/css/cmgiftcard.css');
?>
<form enctype="multipart/form-data" method="post" action="index.php?option=com_cmgiftcard&task=tools.uploadCode" class="form-horizontal">
	<h2><?php echo Text::_('COM_CMGIFTCARD_TOOLS_CODE_UPLOAD_HEADING'); ?></h2>

	<?php if ($codeUpload == 1 && $codeFile != '') : ?>
	<div id="codeImporting" class=card">
		<div class="alert alert-success">
			<img class="loading" src="components/com_cmgiftcard/assets/images/loading.gif">
			<?php echo Text::_('COM_CMGIFTCARD_TOOLS_IMPORTING_CODES'); ?>
		</div>
	</div>
	<?php endif; ?>

	<p><?php echo Text::_('COM_CMGIFTCARD_TOOLS_CODE_UPLOAD_INSTRUCTION'); ?></p>
	<ul>
		<li><?php echo Text::_('COM_CMGIFTCARD_TOOLS_POST_MAX_SIZE') . ': ' . (int) ini_get('post_max_size'); ?>MB</li>
		<li><?php echo Text::_('COM_CMGIFTCARD_TOOLS_MEMORY_LIMIT') . ': ' . (int) ini_get('memory_limit'); ?>MB</li>
		<li><?php echo Text::_('COM_CMGIFTCARD_TOOLS_UPLOAD_MAX_FILESIZE') . ': ' . (int) ini_get('upload_max_filesize'); ?>MB</li>
	</ul>
	<?php echo $this->codeForm->renderField('code_file'); ?>
	<input type="submit" class="btn btn-primary" value="<?php echo Text::_('COM_CMGIFTCARD_BUTTON_UPLOAD_IMPORT'); ?>">
	<?php echo HTMLHelper::_('form.token'); ?>
</form>
