<?php
/** 
 * @package     VikRentItems
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2022 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

JHtml::fetch('vrihtml.scripts.ajaxcsrf');

$rows = $this->rows;

$canEdit = JFactory::getUser()->authorise('core.admin', 'com_vikrentitems');
?>

<form action="index.php?option=com_vikrentitems" method="post" name="adminForm" id="adminForm">

<?php
if (count($rows) == 0)
{
	?>
	<p class="warn"><?php echo JText::translate('JGLOBAL_NO_MATCHING_RESULTS'); ?></p>
	<?php
}
else
{
	?>
	<div class="vri-list-form">
		<div class="table-responsive">
			<table cellpadding="4" cellspacing="0" border="0" width="100%" class="table table-striped vri-list-table">
				<thead>
					<tr>

						<th width="1%">
							<input type="checkbox" onclick="Joomla.checkAll(this)" value="" name="checkall-toggle">
						</th>

						<!-- DATE -->
						
						<th class="title left" width="20%" style="text-align: left;">
							<a href="index.php?option=com_vikrentitems&amp;view=backups&amp;filter_order=createdon&amp;filter_order_Dir=<?php echo ($this->ordering == "createdon" && $this->orderDir == "ASC" ? "DESC" : "ASC"); ?>" class="<?php echo ($this->ordering == "createdon" && $this->orderDir == "ASC" ? "vri-list-activesort" : ($this->ordering == "createdon" ? "vri-list-activesort" : "")); ?>">
								<?php echo JText::translate('VRPVIEWORDERSONE').($this->ordering == "createdon" && $this->orderDir == "ASC" ? '<i class="'.VikRentItemsIcons::i('sort-asc').'"></i>' : ($this->ordering == "createdon" ? '<i class="'.VikRentItemsIcons::i('sort-desc').'"></i>' : '<i class="'.VikRentItemsIcons::i('sort').'"></i>')); ?>
							</a>
						</th>

						<!-- TYPE -->
						
						<th class="title left" width="20%" style="text-align: left;">
							<?php echo JText::translate('VRI_CONFIG_BACKUP_TYPE'); ?>
						</th>

						<!-- SIZE -->
						
						<th class="title hidden-phone nowrap" width="8%" style="text-align: center;">
							<a href="index.php?option=com_vikrentitems&amp;view=backups&amp;filter_order=filesize&amp;filter_order_Dir=<?php echo ($this->ordering == "filesize" && $this->orderDir == "ASC" ? "DESC" : "ASC"); ?>" class="<?php echo ($this->ordering == "filesize" && $this->orderDir == "ASC" ? "vri-list-activesort" : ($this->ordering == "filesize" ? "vri-list-activesort" : "")); ?>">
								<?php echo JText::translate('VRI_BACKUP_SIZE').($this->ordering == "filesize" && $this->orderDir == "ASC" ? '<i class="'.VikRentItemsIcons::i('sort-asc').'"></i>' : ($this->ordering == "filesize" ? '<i class="'.VikRentItemsIcons::i('sort-desc').'"></i>' : '<i class="'.VikRentItemsIcons::i('sort').'"></i>')); ?>
							</a>
						</th>

						<!-- ACTIONS -->
						
						<?php
						if ($canEdit)
						{
							?>
							<th class="title hidden-phone nowrap" width="14%" style="text-align: center;" colspan="2">
								<?php echo JText::translate('VRICRONACTIONS'); ?>
							</th>
							<?php
						}
						?>
					
					</tr>
				</thead>
				
				<?php
				for ($i = 0, $n = count($rows); $i < $n; $i++)
				{
					$row = $rows[$i];
					?>
					<tr class="row<?php echo ($i % 2); ?>">

						<td>
							<input type="checkbox" id="cb<?php echo $i;?>" name="cid[]" value="<?php echo $this->escape($row->name); ?>" onclick="Joomla.isChecked(this.checked);">
						</td>

						<!-- NAME -->

						<td>
							<?php echo JHtml::fetch('date', $row->date, 'Y-m-d H:i:s'); ?>
						</td>

						<!-- TYPE -->

						<td>
							<?php echo $row->type->name; ?>
						</td>

						<!-- SIZE -->

						<td style="text-align: center;" class="hidden-phone">
							<?php echo JHtml::fetch('number.bytes', $row->size); ?>
						</td>

						<!-- ACTIONS -->
						
						<?php
						if ($canEdit)
						{
							?>
							<td style="text-align: right;" class="hidden-phone" width="7%">
								<a href="<?php echo VRIFactory::getPlatform()->getUri()->addCSRF('index.php?option=com_vikrentitems&task=backup.restore&cid[]=' . $this->escape($row->name), $xhtml = true); ?>" class="backup-restore-link btn btn-danger">
									<?php echo JText::translate('VRI_WIDGETS_RESTDEFAULTSHORT'); ?>
								</a>
							</td>

							<td style="text-align: left;" class="hidden-phone" width="7%">
								<a href="<?php echo $row->url; ?>" class="btn btn-primary">
									<?php echo JText::translate('VRI_BACKUP_DOWNLOAOD'); ?>
								</a>
							</td>
							<?php
						}
						?>

					</tr>
					<?php
				}
				?>
			</table>
		</div>
	</div>
	<?php
}
?>

	<input type="hidden" name="boxchecked" value="0" />
	<input type="hidden" name="task" value="" />
	<input type="hidden" name="view" value="backups" />

	<?php echo JHtml::fetch('form.token'); ?>
	<?php echo $this->navbut; ?>
</form>

<?php
// render modal script
echo VikRentitems::getVriApplication()->getJmodalScript();

// load create modal content
echo JHtml::fetch(
	'bootstrap.renderModal',
	'jmodal-newbackup',
	array(
		'title'       => JText::translate('VRIMAINTITLENEWBACKUP'),
		'closeButton' => true,
		'keyboard'    => false, 
		'bodyHeight'  => 80,
		'width'       => 60,
		'footer'      => '<button type="button" class="btn btn-success" data-role="backup.save">' . JText::translate('VRSAVE') . '</button>',
	),
	$this->loadTemplate('modal')
);

JText::script('VRIBACKUPRESTORECONF1');
JText::script('VRIBACKUPRESTORECONF2');
?>

<script>

	(function($) {
		'use strict';

		Joomla.submitbutton = (task) => {
			if (task === 'backup.add') {
				vriOpenJModal('newbackup');
			} else {
				Joomla.submitform(task, document.adminForm);
			}
		}

		$(function() {
			$('a.backup-restore-link').on('click', (event) => {
				let r = confirm(Joomla.JText._('VRIBACKUPRESTORECONF1'));

				if (!r) {
					return false;
				}

				r = confirm(Joomla.JText._('VRIBACKUPRESTORECONF2'));

				if (!r) {
					return false
				}

				return true;
			});
		});
	})(jQuery);

	function vriCloseJModal(id) {
		jQuery('#jmodal-' + id).modal('toggle');
	}

</script>
