<?php
/**
* @package com_spmedical
* @author JoomShaper http://www.joomshaper.com
* @copyright Copyright (c) 2010 - 2017 JoomShaper
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
*/

// No direct access to this file
defined('_JEXEC') or die('Restricted Access');

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

if (JVERSION < 4)
{
	HTMLHelper::_('formbehavior.chosen', 'select');
}

$user		= Factory::getUser();
$userId		= $user->get('id');

$listOrder = $this->escape($this->filter_order);
$listDirn = $this->escape($this->filter_order_Dir);
$saveOrder = $listOrder == 'a.ordering';

if ($saveOrder && !empty($this->items))
{
	if (JVERSION < 4)
	{
		$saveOrderingUrl = 'index.php?option=com_spmedical&task=appointments.saveOrderAjax&tmpl=component';
		HTMLHelper::_('sortablelist.sortable', 'appointmentList', 'adminForm', strtolower($listDirn), $saveOrderingUrl, false, true);
	}
	else
	{
		$saveOrderingUrl = 'index.php?option=com_spmedical&task=appointments.saveOrderAjax&tmpl=component&' . Session::getFormToken() . '=1';
		HTMLHelper::_('draggablelist.draggable');
	}
}
?>

<script type="text/javascript">
window.addEventListener("DOMContentLoaded", e => {
	Joomla.orderTable = function() {
		table = document.getElementById("sortTable");
		direction = document.getElementById("directionTable");
		order = table.options[table.selectedIndex].value;
		if (order != '<?php echo $listOrder; ?>') {
			dirn = 'asc';
		} else {
			dirn = direction.options[direction.selectedIndex].value;
		}
		Joomla.tableOrdering(order, dirn, '');
	}
})
</script>

<form action="<?php echo Route::_('index.php?option=com_spmedical&view=appointments'); ?>" method="post" id="adminForm" name="adminForm">
		<?php if (JVERSION < 4 && !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', array('view' => $this)); ?>

		<?php if (empty($this->items)) : ?>
			<div class="alert alert-no-items alert-info">
				<?php echo Text::_('JGLOBAL_NO_MATCHING_RESULTS'); ?>
			</div>
		<?php else : ?>
			<table class="table table-striped" id="appointmentList">
				<thead>
				<tr>
					<th width="2%" class="nowrap center hidden-phone">
						<?php echo HTMLHelper::_('grid.sort', '<i class="icon-menu-2"></i>', 'a.ordering', $listDirn, $listOrder); ?>
					</th>
					<th width="2%" class="hidden-phone">
						<?php echo HTMLHelper::_('grid.checkall'); ?>
					</th>
					<th width="1%" class="nowrap center">
						<?php echo HTMLHelper::_('grid.sort', 'JSTATUS', 'a.published', $listDirn, $listOrder); ?>
					</th>
					<th>
						<?php echo HTMLHelper::_('grid.sort', 'COM_SPMEDICAL_FIELD_PATIENT_NAME', 'a.patient_name', $listDirn, $listOrder); ?>
					</th>
					<th>
						<?php echo HTMLHelper::_('grid.sort', 'COM_SPMEDICAL_FIELD_PATIENT_EMAIL', 'a.patient_email', $listDirn, $listOrder); ?>
					</th>
					<th>
						<?php echo HTMLHelper::_('grid.sort', 'COM_SPMEDICAL_FIELD_PATIENT_APPOINTMENT_DATE', 'a.appintment_date', $listDirn, $listOrder); ?>
					</th>
					<th>
						<?php echo HTMLHelper::_('grid.sort', 'COM_SPMEDICAL_FIELD_PATIENT_APPOINTMENT_TIME', 'a.appintment_time', $listDirn, $listOrder); ?>
					</th>
					<th>
						<?php echo HTMLHelper::_('grid.sort', 'COM_SPMEDICAL_FIELD_PATIENT_DOCTOR_NAME', 'a.specialist_id', $listDirn, $listOrder); ?>
					</th>
					<th>
						<?php echo HTMLHelper::_('grid.sort', 'COM_SPMEDICAL_FIELD_DEPARTMENT', 'a.department_id', $listDirn, $listOrder); ?>
					</th>
					<th width="1%" class="nowrap hidden-phone">
						<?php echo HTMLHelper::_('grid.sort', 'JGRID_HEADING_ID', 'a.id', $listDirn, $listOrder); ?>
					</th>
				</tr>
				</thead>

				<tfoot>
					<tr>
						<td colspan="12">
							<?php echo $this->pagination->getListFooter(); ?>
						</td>
					</tr>
				</tfoot>

				<?php if (JVERSION < 4 ) : ?>
				<tbody>
				<?php else :?>
				<tbody <?php if ($saveOrder) :?> class="js-draggable" data-url="<?php echo $saveOrderingUrl; ?>" data-direction="<?php echo strtolower($listDirn); ?>" data-nested="false"<?php endif; ?>>
				<?php endif; ?>
					<?php if (!empty($this->items)) : ?>
						<?php foreach ($this->items as $i => $item) :
							$item->max_ordering = 0;
							$ordering   = ($listOrder == 'a.ordering');
							$canEdit    = $user->authorise('core.edit', 'com_spmedical.appointment.' . $item->id) || ($user->authorise('core.edit.own',   'com_spmedical.appointment.' . $item->id) && $item->created_by == $userId);
							$canCheckin = $user->authorise('core.manage', 'com_checkin') || $item->checked_out == $userId || $item->checked_out == 0;
							$canChange  = $user->authorise('core.edit.state', 'com_spmedical.appointment.' . $item->id) && $canCheckin;
							$link = Route::_('index.php?option=com_spmedical&task=appointment.edit&id=' . $item->id);
						?>
							<?php if(JVERSION < 4) :?>
							<tr>
							<?php else: ?>
							<tr class="row<?php echo $i % 2; ?>" data-draggable-group="1">
							<?php endif; ?>
								<td class="order nowrap center hidden-phone">
									<?php
									$iconClass = '';
									if (!$canChange) {
										$iconClass = ' inactive';
									} elseif (!$saveOrder) {
										$iconClass = ' inactive tip-top hasTooltip" title="' . HTMLHelper::tooltipText('JORDERINGDISABLED');
									} ?>
									<span class="sortable-handler<?php echo $iconClass ?>">
										<span class="icon-menu"></span>
									</span>
									<?php if ($canChange && $saveOrder) : ?>
										<input type="text" style="display:none" name="order[]" size="5" value="<?php echo $item->ordering; ?>" class="width-20 text-area-order " />
									<?php endif; ?>
								</td>
								<td class="hidden-phone">
									<?php echo HTMLHelper::_('grid.id', $i, $item->id); ?>
								</td>

								<td class="center">
									<?php echo HTMLHelper::_('jgrid.published', $item->published, $i, 'appointments.', $canChange);?>
								</td>

								<td>
									<?php if ($item->checked_out) : ?>
										<?php echo HTMLHelper::_('jgrid.checkedout', $i, $item->editor, $item->checked_out_time, 'appointments.', $canCheckin); ?>
									<?php endif; ?>

									<?php if ($canEdit) : ?>
										<a href="<?php echo Route::_('index.php?option=com_spmedical&task=appointment.edit&id='.$item->id);?>">
											<?php echo $this->escape($item->patient_name); ?>
										</a>
									<?php else : ?>
										<?php echo $this->escape($item->patient_name); ?>
									<?php endif; ?>
									<?php if($item->patient_phone) { ?>
										<span class="small break-word">
											( <?php echo Text::_('COM_SPMEDICAL_FIELD_PATIENT_PHONE') . ': ' .  $item->patient_phone; ?> )
										</span>
									<?php } ?>
								</td>

								<td class="hidden-phone">
									<?php echo $item->patient_email; ?>
								</td>

								<td class="hidden-phone">
									<?php echo HTMLHelper::date($item->appintment_date , Text::_('DATE_FORMAT_LC')); ?>
								</td>

								<td class="hidden-phone">
									<?php echo !empty($item->appintment_time) ? date('g:i a', strtotime($item->appintment_time)) : '' ; ?>
								</td>

								<td class="hidden-phone">
									<?php echo $item->specialist_name; ?>
								</td>

								<td class="hidden-phone">
									<?php echo $item->department_name; ?>
								</td>

								<td align="center" class="hidden-phone">
									<?php echo $item->id; ?>
								</td>
							</tr>
						<?php endforeach; ?>
					<?php endif; ?>
				</tbody>
			</table>
		<?php endif; ?>
	</div>

	<input type="hidden" name="task" value="" />
	<input type="hidden" name="boxchecked" value="0" />
	<input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>" />
	<input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>" />
	<?php echo HTMLHelper::_('form.token'); ?>
</form>
