<?php 
/** 
 * @package JCHAT::LAMESSAGES::administrator::components::com_jchat
 * @subpackage views
 * @subpackage lamessages
 * @subpackage tmpl
 * @author Joomla! Extensions Store
 * @Copyright (C) 2015 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html 
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
?>
<form action="<?php echo Route::_('index.php?option=com_jchat&view=lamessages'); ?>" method="post" class="jes jesform" name="adminForm" id="adminForm"> 
	<div class="btn-toolbar well" id="toolbar">
		<div class="btn-wrapper pull-left" id="toolbar-buttons">
			<button type="button" onclick="jQuery.submitbutton('lamessages.editEntity')" class="btn btn-primary btn-sm">
				<span class="glyphicon glyphicon-edit"></span> <?php echo Text::_('COM_JCHAT_TICKET_DETAILS');?>
			</button>
			
			<button type="button" onclick="jQuery.submitbutton('lamessages.deleteEntity')" class="btn btn-primary btn-sm">
				<span class="glyphicon glyphicon-remove"></span> <?php echo Text::_('COM_JCHAT_DELETE');?>
			</button>
			
			<button type="button" onclick="jQuery.submitbutton('lamessages.exportMessages')" class="btn btn-primary btn-sm">
				<span class="glyphicon glyphicon-download-alt"></span> <?php echo Text::_('COM_JCHAT_EXPORT_TICKETS');?>
			</button>
		</div>
	</div>
	
	<div class="headerlist well">

			<div class="input-prepend active blockfield">
				<span class="add-on"><span class="icon-filter" aria-hidden="true"></span> <?php echo Text::_('COM_JCHAT_FILTER' ); ?>:</span>
				<input type="text" name="search" id="search" value="<?php echo htmlspecialchars($this->searchword, ENT_COMPAT, 'UTF-8');?>" class="text_area"/>
			</div>
			<button class="btn btn-primary btn-xs" onclick="this.form.submit();"><?php echo Text::_('COM_JCHAT_GO' ); ?></button>
			<button class="btn btn-primary btn-xs" onclick="document.getElementById('search').value='';this.form.submit();"><?php echo Text::_('COM_JCHAT_RESET' ); ?></button>
			
			<div class="clr vspacer"></div>
			<div class="input-prepend active blockfield">
				<span class="add-on"><span class="icon-calendar" aria-hidden="true"></span> <?php echo Text::_('COM_JCHAT_FILTER_BY_DATE_FROM' ); ?>:</span>
				<input type="text" name="fromperiod" id="fromPeriod" data-role="calendar" autocomplete="off" value="<?php echo $this->dates['start'];?>" class="text_area"/>
			</div>
			
			<div class="input-prepend active blockfield">
				<span class="add-on"><span class="icon-calendar" aria-hidden="true"></span> <?php echo Text::_('COM_JCHAT_FILTER_BY_DATE_TO' ); ?>:</span>
				<input type="text" name="toperiod" id="toPeriod" data-role="calendar" autocomplete="off" value="<?php echo $this->dates['to'];?>" class="text_area"/>
			</div>
			<button class="btn btn-primary btn-xs" onclick="document.adminForm.task.value='lamessages.display';this.form.submit();"><?php echo Text::_('COM_JCHAT_GO' ); ?></button>
			<button class="btn btn-primary btn-xs" onclick="document.getElementById('fromPeriod').value='';document.getElementById('toPeriod').value='';this.form.submit();"><?php echo Text::_('COM_JCHAT_RESET' ); ?></button>

			<div class="clr vspacer"></div>
			<div class="input-prepend active blockfield hidden-xs">
				<span class="add-on"><span class="icon-filter" aria-hidden="true"></span> <?php echo Text::_('COM_JCHAT_STATE' ); ?></span>
				<?php
					echo $this->lists['answered'];
					echo $this->lists['closed'];
					echo $this->pagination->getLimitBox();
				?>
			</div>
			
	</div>

	<table class="sitelist table table-striped" cellpadding="1">
		<thead>
			<tr>
				<th width="1%" class="title">
					<?php echo Text::_( 'COM_JCHAT_NUM' ); ?>
				</th>
				<th width="1%" class="title">
					<input type="checkbox" name="checkall-toggle" value="" onclick="Joomla.checkAll(this);" />
				</th>
				<th width="8%"class="title">
					<?php echo HTMLHelper::_('grid.sort', 'COM_JCHAT_LAMESSAGE_NAME', 'a.name', @$this->orders['order_Dir'], @$this->orders['order'], 'lamessages.display' ); ?>
				</th>
				<th width="8%" class="title hidden-xs" >
					<?php echo HTMLHelper::_('grid.sort', 'COM_JCHAT_LAMESSAGE_EMAIL', 'a.email', @$this->orders['order_Dir'], @$this->orders['order'], 'lamessages.display' ); ?>
				</th>
				<?php if($this->phoneNumberEnabled):?>
				<th width="4%" class="title hidden-sm hidden-xs">
					<?php echo Text::_('COM_JCHAT_LAMESSAGE_PHONENUMBER'); ?>
				</th>
				<?php endif;?>
				<th width="25%" class="title hidden-xs">
					<?php echo Text::_('COM_JCHAT_MESSAGE'); ?>
				</th>
				<th width="5%" class="title">
					<?php echo HTMLHelper::_('grid.sort', 'COM_JCHAT_SENT', 'a.sentdate', @$this->orders['order_Dir'], @$this->orders['order'], 'lamessages.display' ); ?>
				</th>
				<th width="5%" class="title hidden-xs">
					<?php echo Text::_('COM_JCHAT_NUM_REPLIES');?>
				</th>
				<th width="5%" class="title hidden-xs">
					<?php echo Text::_('COM_JCHAT_LAST_REPLY');?>
				</th>
				<th width="5%" class="title hidden-xs">
					<?php echo HTMLHelper::_('grid.sort', 'COM_JCHAT_USERID', 'a.userid', @$this->orders['order_Dir'], @$this->orders['order'], 'lamessages.display' ); ?>
				</th>
				<th width="5%" class="title">
					<?php echo HTMLHelper::_('grid.sort', 'COM_JCHAT_WORKED', 'a.worked', @$this->orders['order_Dir'], @$this->orders['order'], 'lamessages.display' ); ?>
				</th> 
				<th width="5%" class="title">
					<?php echo HTMLHelper::_('grid.sort', 'COM_JCHAT_CLOSED_TICKET', 'a.closed_ticket', @$this->orders['order_Dir'], @$this->orders['order'], 'lamessages.display' ); ?>
				</th> 
				<th width="1%" class="title hidden-sm hidden-xs">
					<?php echo HTMLHelper::_('grid.sort', 'ID', 'a.id', @$this->orders['order_Dir'], @$this->orders['order'], 'lamessages.display' ); ?>
				</th> 
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="100%">
					<div class="w-100">
						<p class="counter float-right float-end pt-3 pr-2">
							<?php echo $this->pagination->getPagesCounter(); ?>
						</p>
						<?php echo $this->pagination->getPagesLinks(); ?>
					</div>
				</td>
			</tr>
		</tfoot>
		<tbody>
		<?php
			$k = 0;
			for ($i=0, $n=count( $this->items ); $i < $n; $i++) {
				$row = & $this->items[$i]; 
				
				// Try to find num responses and last response info
				$numResponses = 0;
				$lastResponse = array('date'=>null, 'user'=>null);
				if(!empty($row->responses)) {
					$responses = unserialize($row->responses);
					$numResponses = count($responses);
					foreach ($responses as $response) {
						if($lastResponse['date'] < $response[0]) {
							$lastResponse = array('date'=>$response[0], 'user'=>$response[2]);
						}
					}
				}
				
				// Read status
				$imgRead 	= $row->worked ? 'icon-16-tick.png' : 'icon-16-publish_x.png'; 
				$altRead 	= $row->worked ? Text::_( 'COM_JCHAT_READ' ) : Text::_( 'COM_JCHAT_UNREAD' );
				$taskState 	= $row->worked ? 'lamessages.workedFlagOff()' : 'lamessages.workedFlagOn()';
				
				// Closed status
				$imgClosed 	= $row->closed_ticket ? 'icon-16-tick.png' : 'icon-16-publish_x.png';
				$altClosed 	= $row->closed_ticket ? Text::_( 'COM_JCHAT_CLOSED_TICKET' ) : Text::_( 'COM_JCHAT_NOTCLOSED_TICKET' );
				$taskClosed = $row->closed_ticket ? 'lamessages.closedFlagOff()' : 'lamessages.closedFlagOn()';
				
				// Accorciamento message text
				$row->message = strlen($row->message) > 80 ? substr($row->message, 0, 80) . '...' : $row->message;
			?>
			<tr class="<?php echo "row$k"; ?>">
				<td align="center">
					<a class="clearopen badge badge-info" href="javascript:void(0);" onclick="return Joomla.listItemTask('cb<?php echo $i;?>','lamessages.editEntity')">
						<?php echo $i+1+$this->pagination->limitstart;?>
					</a>
				</td>
				<td align="center">
					<?php 
					$canCheckin = $this->user->authorise('core.manage', 'com_checkin');
					$checked = 	$row->checked_out && $row->checked_out != $this->user->id ?
								JHtml::_('jgrid.checkedout', $i, Factory::getContainer()->get(\Joomla\CMS\User\UserFactoryInterface::class)->loadUserById($row->checked_out)->name, $row->checked_out_time, 'lamessages.', $canCheckin) . '<input type="checkbox" style="display:none" data-enabled="false" id="cb' . $i . '" name="cid[]" value="' . $row->id . '"/>' :
								JHtml::_('grid.id', $i, $row->id);
					echo $checked; ?>
				</td>
				<td class="word-break"> 
					<?php echo $row->name; ?> 
				</td>
				<td class="word-break hidden-xs">
					<?php echo $row->email; ?>
				</td>
				<?php if($this->phoneNumberEnabled):?>
				<td class="word-break word-break-zero hidden-sm hidden-xs">
					<?php echo $row->phonenumber; ?>
				</td>
				<?php endif;?>
				<td class="hidden-sm hidden-xs">
					<?php echo $row->message; ?>
				</td>    
				<td>
					<?php echo $row->sentdate; ?>
				</td>
				<td class="hidden-xs">
					<?php echo $numResponses; ?>
				</td>
				<td class="hidden-sm hidden-xs">
					<?php echo $lastResponse['date'] . ' / ' . $lastResponse['user']; ?>
				</td>
				<td class="hidden-xs">
					<?php echo isset($row->username_logged) ? $row->username_logged : Text::_('COM_JCHAT_ANONYMOUS'); ?>
				</td>
				<td align="center"> 
					<a class="jesflag" href="javascript:void(0);" onclick="return Joomla.listItemTask('cb<?php echo $i;?>','<?php echo $taskState;?>')">
						<img src="administrator/components/com_jchat/images/<?php echo $imgRead;?>" width="16" height="16" border="0" alt="<?php echo $altRead; ?>" />   
					</a>
				</td> 
				<td align="center"> 
					<a class="jesflag" href="javascript:void(0);" onclick="return Joomla.listItemTask('cb<?php echo $i;?>','<?php echo $taskClosed;?>')">
						<img src="administrator/components/com_jchat/images/<?php echo $imgClosed;?>" width="16" height="16" border="0" title="<?php echo $altClosed; ?>" alt="<?php echo $altClosed; ?>" />   
					</a>
				</td> 
				<td class="hidden-sm hidden-xs">
					<?php echo $row->id; ?>
				</td>
			</tr>
			<?php
				$k = 1 - $k;
				}
			?>
		</tbody>
	</table>

	<input type="hidden" name="option" value="<?php echo $this->option;?>" />
	<input type="hidden" name="task" data-originalvalue="lamessages.display" value="lamessages.display" /> 
	<input type="hidden" name="boxchecked" value="0" /> 
	<input type="hidden" name="filter_order" value="<?php echo $this->orders['order']; ?>" />
	<input type="hidden" name="filter_order_Dir" value="<?php echo $this->orders['order_Dir']; ?>" />
	<input type="hidden" name="limitstart" value="<?php echo $this->limitstart; ?>">
</form>