<?php
/**
 * @package    VP Email Manager
 * @copyright  Copyright (C) 2012-2021 VirtuePlanet Services LLP. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @author     Abhishek Das <info@virtueplanet.com>
 * @link       https://www.virtueplanet.com
 */

defined('_JEXEC') or die;

use \Joomla\CMS\HTML\HTMLHelper;
use \Joomla\CMS\Factory;
use \Joomla\CMS\Uri\Uri;
use \Joomla\CMS\Router\Route;
use \Joomla\CMS\Layout\LayoutHelper;
use \Joomla\CMS\Language\Text;

HTMLHelper::addIncludePath(JPATH_COMPONENT . '/helpers/');
HTMLHelper::_('bootstrap.tooltip');
HTMLHelper::_('behavior.multiselect');
HTMLHelper::_('formbehavior.chosen', 'select');

$user      = Factory::getUser();
$userId    = $user->get('id');
$listOrder = $this->state->get('list.ordering');
$listDirn  = $this->state->get('list.direction');
?>
<form action="<?php echo Route::_('index.php?option=com_vpem&view=outbox'); ?>" method="post" name="adminForm" id="adminForm">
	<?php if (!empty($this->sidebar)): ?>
	<div id="j-sidebar-container" class="span2">
		<?php echo $this->sidebar; ?>
	</div>
	<div id="j-main-container" class="span10">
		<?php else : ?>
		<div id="j-main-container">
			<?php endif; ?>

			<?php echo LayoutHelper::render('joomla.searchtools.default', ['view' => $this]); ?>

			<div class="clearfix"></div>
			<table class="table table-striped" id="outbox">
				<thead>
				<tr>
					<th width="1%">
						<input type="checkbox" name="checkall-toggle" value="" title="<?php echo Text::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this)" />
					</th>
					<th class="left">
						<?php echo JHtml::_('searchtools.sort', 'COM_VPEM_MAIL_TO', 'a.mail_to', $listDirn, $listOrder); ?>
					</th>
					<th class="left">
						<?php echo JHtml::_('searchtools.sort', 'COM_VPEM_MAIL_SUBJECT', 'a.subject', $listDirn, $listOrder); ?>
					</th>
					<th class="left hidden-phone">
						<?php echo JHtml::_('searchtools.sort', 'COM_VPEM_TEMPLATES_TYPE', 'a.type', $listDirn, $listOrder); ?>
					</th>
					<th class="left hidden-phone">
						<?php echo JHtml::_('searchtools.sort', 'COM_VPEM_MAIL_SENT_ON', 'a.sent_on', $listDirn, $listOrder); ?>
					</th>
					<th class="center hidden-phone">
						<?php echo JHtml::_('searchtools.sort', 'COM_VPEM_MAIL_STATUS', 'a.status', $listDirn, $listOrder); ?>
					</th>
				</tr>
				</thead>
				<tfoot>
				<tr>
					<td colspan="6">
						<?php echo $this->pagination->getListFooter(); ?>
					</td>
				</tr>
				</tfoot>
				<tbody>
				<?php foreach ($this->items as $i => $item) : ?>
					<?php $row_class = $item->status ? '' : ' error'; ?>
					<tr class="row<?php echo $i % 2; ?><?php echo $row_class; ?>">
						<td>
							<?php echo HTMLHelper::_('grid.id', $i, $item->id); ?>
						</td>
						<td>
							<?php echo $this->escape(str_replace(',', '; ', $item->mail_to)); ?>
						</td>
						<td>
							<a href="<?php echo JRoute::_('index.php?option=com_vpem&view=mail&id=' .(int) $item->id); ?>">
								<?php echo $this->escape($item->subject); ?>
							</a>
							<?php if (!empty($item->attachments)) : ?>
								<svg enable-background="new 0 0 172.288 172.288" height="15" viewBox="0 0 172.288 172.288" width="15" style="vertical-align: middle;"><g><g><path clip-rule="evenodd" d="m162.184 32.145c.502 8.537-2.564 17.377-10.174 24.987l-80.408 80.408c-4.26 4.26-9.331 6.069-14.308 5.777-4.86-.285-9.268-2.55-12.495-5.777s-5.491-7.634-5.777-12.494c-.293-4.977 1.517-10.049 5.777-14.308l70.357-70.357c1.85-1.85 4.85-1.85 6.701 0 1.85 1.85 1.85 4.85 0 6.701l-70.357 70.356c-2.441 2.441-3.144 4.908-3.018 7.05.133 2.259 1.219 4.552 3.018 6.351s4.092 2.885 6.351 3.018c2.143.126 4.61-.577 7.051-3.018l80.409-80.408c5.791-5.792 7.751-12.028 7.415-17.73-.343-5.819-3.103-11.462-7.415-15.773-4.312-4.312-9.955-7.073-15.774-7.415-5.702-.335-11.938 1.624-17.73 7.415l-80.409 80.408c-9.142 9.142-12.357 19.147-11.812 28.409.552 9.379 4.988 18.372 11.812 25.196 6.824 6.825 15.818 11.261 25.196 11.812 9.262.545 19.267-2.67 28.409-11.812l36.854-36.854c1.851-1.85 4.85-1.85 6.701 0 1.851 1.851 1.851 4.851 0 6.701l-36.854 36.854c-10.96 10.96-23.57 15.283-35.666 14.572-11.98-.704-23.088-6.32-31.341-14.572-8.252-8.252-13.867-19.361-14.572-31.341-.712-12.096 3.611-24.705 14.571-35.666l80.408-80.408c7.61-7.61 16.451-10.676 24.987-10.174 8.421.495 16.179 4.435 21.918 10.174 5.74 5.739 9.681 13.497 10.175 21.918z" fill-rule="evenodd"/></g></g></svg>
							<?php endif; ?>
						</td>
						<td class="small hidden-phone">
							<?php echo $item->type; ?>
						</td>
						<td class="nowrap small hidden-phone">
							<?php echo $item->sent_on > 0 ? JHtml::_('date', $item->sent_on, JText::_('DATE_FORMAT_LC5')) : '-'; ?>
						</td>
						<td class="hidden-phone center">
							<?php if ($item->status) : ?>
								<span class="hasTooltip" title="<?php echo Text::_('COM_VPEM_STATUS_SUCCESSFUL'); ?>">
									<svg version="1.1" x="0px" y="0px" viewBox="0 0 468.293 468.293" style="enable-background:new 0 0 468.293 468.293;" xml:space="preserve" width="15px" height="15px">
										<circle style="fill:#44C4A1;" cx="234.146" cy="234.146" r="234.146"/>
										<polygon style="fill:#EBF0F3;" points="357.52,110.145 191.995,275.67 110.773,194.451 69.534,235.684 191.995,358.148 398.759,151.378 "/>
									</svg>
								</span>
							<?php else : ?>
								<span class="hasTooltip" title="<?php echo Text::_('COM_VPEM_STATUS_FAILED'); ?>">
									<svg version="1.1" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve" width="15px" height="15px">
										<ellipse style="fill:#E04F5F;" cx="256" cy="256" rx="256" ry="255.832"/>
										<g transform="matrix(-0.7071 0.7071 -0.7071 -0.7071 77.26 32)">
											<rect x="3.98" y="-427.615" style="fill:#FFFFFF;" width="55.992" height="285.672"/>
											<rect x="-110.828" y="-312.815" style="fill:#FFFFFF;" width="285.672" height="55.992"/>
										</g>
									</svg>
								</span>
							<?php endif; ?>
						</td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>

			<input type="hidden" name="task" value=""/>
			<input type="hidden" name="boxchecked" value="0"/>
			<input type="hidden" name="list[fullorder]" value="<?php echo $listOrder; ?> <?php echo $listDirn; ?>"/>
			<?php echo HTMLHelper::_('form.token'); ?>
		</div>
</form>
<script>
    window.toggleField = function (id, task, field) {

        var f = document.adminForm, i = 0, cbx, cb = f[ id ];

        if (!cb) return false;

        while (true) {
            cbx = f[ 'cb' + i ];

            if (!cbx) break;

            cbx.checked = false;
            i++;
        }

        var inputField   = document.createElement('input');

        inputField.type  = 'hidden';
        inputField.name  = 'field';
        inputField.value = field;
        f.appendChild(inputField);

        cb.checked = true;
        f.boxchecked.value = 1;
        window.submitform(task);

        return false;
    };
</script>