<?php 
/** 
 * @package JMAP::AIGENERATOR::administrator::components::com_jmap
 * @subpackage views
 * @subpackage aigenerator
 * @subpackage tmpl
 * @author Joomla! Extensions Store
 * @copyright (C) 2021 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html  
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
?>
<form action="index.php" method="post" name="adminForm" id="adminForm">
	<table class="full headerlist">
		<tr>
			<td align="left" width="60%">
				<span class="input-group">
				  <span class="input-group-addon"><span class="glyphicon glyphicon-filter"></span> <?php echo JText::_('COM_JMAP_FILTER' ); ?>:</span>
				  <input type="text" name="search" id="search" value="<?php echo htmlspecialchars($this->searchword, ENT_COMPAT, 'UTF-8');?>" class="text_area"/>
				</span>

				<button class="btn btn-primary btn-xs" onclick="this.form.submit();"><?php echo JText::_('COM_JMAP_GO' ); ?></button>
				<button class="btn btn-primary btn-xs" onclick="document.getElementById('search').value='';this.form.submit();"><?php echo JText::_('COM_JMAP_RESET' ); ?></button>
			</td>
			<td nowrap="nowrap">
				<?php if($this->languagePluginEnabled):?>
					<?php echo $this->lists['languages'];?>
				<?php endif;?>
				
				<?php echo $this->lists['contentsapi'];?>
				
				<?php echo $this->pagination->getLimitBox(); ?>
			</td>
		</tr>
	</table>

	<table class="adminlist table table-striped table-hover">
	<thead>
		<tr>
			<th width="1%">
				<?php echo JText::_('COM_JMAP_NUM' ); ?>
			</th>
			<th width="1%">
				<input type="checkbox" name="checkall-toggle" value=""  onclick="Joomla.checkAll(this)" />
			</th>
			<th width="15%" class="title">
				<?php echo JHtml::_('grid.sort',  'COM_JMAP_AIGENERATOR_KEYWORDS_PHRASE', 's.keywords_phrase', @$this->orders['order_Dir'], @$this->orders['order'], 'aigenerator.display' ); ?>
			</th>
			<th class="title hidden-phone">
				<?php echo JHtml::_('grid.sort',  'COM_JMAP_AIGENERATOR_CONTENTS_EXCERPT', 's.contents', @$this->orders['order_Dir'], @$this->orders['order'], 'aigenerator.display' ); ?>
			</th>
			<th class="title hidden-phone">
				<?php echo JText::_('COM_JMAP_AIGENERATOR_NUM_CONTENTS'); ?>
			</th>
			<th class="title hidden-phone">
				<?php echo JHtml::_('grid.sort',  'COM_JMAP_AIGENERATOR_API', 's.api', @$this->orders['order_Dir'], @$this->orders['order'], 'aigenerator.display' ); ?>
			</th>
			
			<?php if($this->languagePluginEnabled):?>
				<th width="5%">
					<?php echo JHtml::_('grid.sort',  'COM_JMAP_DATASOURCE_LANGUAGE', 's.language', @$this->orders['order_Dir'], @$this->orders['order'], 'aigenerator.display' ); ?>
				</th>
			<?php endif;?>
			<th width="1%">
				<?php echo JHtml::_('grid.sort',   'COM_JMAP_ID', 's.id', @$this->orders['order_Dir'], @$this->orders['order'], 'aigenerator.display' ); ?>
			</th>
		</tr>
	</thead>
	<?php
	$k = 0;
	$canCheckin = $this->user->authorise('core.manage', 'com_checkin');
	for ($i=0, $n=count( $this->items ); $i < $n; $i++) {
		$row = $this->items[$i];
		$link =  'index.php?option=com_jmap&task=aigenerator.editEntity&cid[]='. $row->id ;
		
		$checked = null;
		if($this->user->authorise('core.edit', 'com_jmap')) {
			$checked = $row->checked_out && $row->checked_out != $this->user->id ?
						JHtml::_('jgrid.checkedout', $i, JFactory::getUser($row->checked_out)->name, $row->checked_out_time, 'aigenerator.', $canCheckin) . '<input type="checkbox" style="display:none" id="cb' . $i . '" name="cid[]" value="' . $row->id . '"/>' :
						JHtml::_('grid.id', $i, $row->id);
		} else {
			$checked = null;
		}
		?>
		<tr>
			<td align="center">
				<?php echo $this->pagination->getRowOffset($i); ?>
			</td>
			<td align="center">
				<?php echo $checked; ?>
			</td>
			<td>
				<?php
				if ( ($row->checked_out && ( $row->checked_out != $this->user->get ('id'))) || !$this->user->authorise('core.edit', 'com_jmap') ) {
					echo $row->keywords_phrase;
				} else {
					?>
					<a href="<?php echo $link; ?>" title="<?php echo JText::_('COM_JMAP_EDIT_LINK' ); ?>">
						<span class="icon-pencil"></span>
						<?php echo $row->keywords_phrase; ?>
					</a>
					<?php
				}
				?>
			</td>
			<td class="hidden-phone">
				<?php if($row->api == 'openai'):?>
					<?php 
					$openAiListContent = preg_replace('/{title}.*{\/title}/iU', '', $row->contents);
					echo JHtml::_('string.truncate', JString::str_ireplace(['{content}', '{/content}', '{contentdivider}'], '', strip_tags($openAiListContent)), 300); ?>
				<?php else:?>
					<?php echo $row->contents ? JHtml::_('string.truncate', JString::str_ireplace(['{title}','{/title}', '{content}', '{/content}', '{contentdivider}'], '', strip_tags($row->contents)), 300) : '-'; ?>
				<?php endif;?>
				
			</td>
			<td class="hidden-phone">
				<span class="badge badge-primary"><?php echo $row->contents ? substr_count($row->contents, '{contentdivider}') : '-';?></span>
			</td>
			<td class="hidden-phone">
				<span class="badge badge-warning"><?php echo JString::ucfirst($row->api);?></span>
			</td>
			<?php if($this->languagePluginEnabled):?>
				<td>
					<img style="margin-left:10px" src="<?php echo JUri::root(false) . 'media/mod_languages/images/' .  JString::str_ireplace('-', '_', $row->language) . '.gif';?>" alt="language_flag" />
				</td>
			<?php endif;?>
			<td align="center">
				<?php echo $row->id; ?>
			</td>
		</tr>
		<?php
	}
	?>
	<tfoot>
		<td colspan="13">
			<?php echo $this->pagination->getListFooter(); ?>
		</td>
	</tfoot>
	</table>

	<input type="hidden" name="option" value="<?php echo $this->option;?>" />
	<input type="hidden" name="task" value="aigenerator.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'];?>" />
</form>