<?php
/**
 * @version     1.3.3
 * @package     sellacious
 *
 * @copyright   Copyright (C) 2012-2017 Bhartiy Web Technologies. All rights reserved.
 * @license     SPL Sellacious Private License; see http://www.sellacious.com/spl.html
 * @author      Izhar Aazmi <info@bhartiy.com> - http://www.bhartiy.com
 */
// no direct access
defined('_JEXEC') or die;

$html = array(
	'toolbar'     => JLayoutHelper::render('joomla.searchtools.default', array('view' => $this)),
	'head'        => $this->loadTemplate('head'),
	'body'        => $this->loadTemplate('formbody'),
	'rowTemplate' => $this->loadTemplate('addrow'),
);

$data = $this->getProperties();

$data['name']      = $this->getName();
$data['view']      = &$this;
$data['html']      = &$html;
$data['view_item'] = 'shortcode';

$data = (object) $data;
$that = &$data->view;

// Load the behaviors.
JHtml::_('jquery.framework');

JHtml::_('behavior.multiselect');
JHtml::_('bootstrap.tooltip');

JHtml::_('script', 'media/com_sellacious/js/plugin/select2/select2.min.js', false, false);
JHtml::_('script', 'media/com_sellacious/js/util.txtareaautosize.min.js', false, false);
JHtml::_('script', 'media/com_sellacious/js/view.shortcodes.min.js', false, false);

$listOrder      = $that->escape($data->state->get('list.ordering'));
$listDirn       = $that->escape($data->state->get('list.direction'));
$originalOrders = array();

// @todo: minimize following JS code
?>
<script type="text/javascript">
	(function ($) {
		$(document).ready(function () {
			$('select').select2();
			var o = new TxtAreaAutosize;
			o.attach({
				selector: 'textarea[id^="jform_query_"],textarea[id^="jform_description_"]'
			});

			var oo = new SellaciousViewShortcodes;
			oo.setup({
				id: '<?php echo $data->view_item ?>List',
				rowTemplate: {
					html: '<?php echo json_encode($data->html['rowTemplate']) ?>',
					replacement: '##INDEX##'
				},
				rowIndex: <?php echo count($data->items) ?>
			});
		});
	})(jQuery);

	Joomla.submitbutton = function (task) {
		var f = document.adminForm;

		if (task == '<?php echo $data->name ?>.delete') {
			var cb = '';
			<?php foreach ($data->items as $i=>$item):?>
			cb = f['cb' +<?php echo $i;?>];
			if (cb && cb.checked) {
				if (confirm("<?php echo JText::_('COM_SELLACIOUS_' . strtoupper($data->name) . '_CONFIRM_DELETE') ?>")) {
					Joomla.submitform(task);
				}
				return;
			}
			<?php endforeach;?>
		}
		else if (task == '<?php echo $data->name ?>.save') {
			if (f.boxchecked.value == 0) {
				alert("<?php echo JText::_('COM_SELLACIOUS_' . strtoupper($data->name) . '_NO_ITEM_SELECTED_TO_SAVE') ?>");
				return;
			}
		}

		Joomla.submitform(task);
	}
</script>

<form action="<?php echo JRoute::_('index.php?option=com_sellacious&view=' . $data->name); ?>" method="post"
	  name="adminForm" id="adminForm" class="form-horizontal">
	<?php
	// Search tools bar
	echo $data->html['toolbar']; ?>

	<div class="clearfix"></div>
	<br/>
	<table id="<?php echo $data->view_item ?>List"
		   class="table table-striped table-hover table-form table-nopadding table-outerborder">
		<thead>
		<?php echo $data->html['head']; ?>
		</thead>
		<tbody>
		<?php echo $data->html['body']; ?>
		<tr class="svshortcodes-blankrow">
			<td colspan="7"></td>
			<td>
				<button type="button" id="shortcode_add"
						class="btn btn-sm bg-color-green txt-color-white glyphicon glyphicon-plus svshortcodesrow-add pull-right"></button>
			</td>
		</tr>
		</tbody>
		<tfoot>
		<tr>
			<td colspan="100" class="center">
				<?php echo $data->pagination->getListFooter(); ?><br/>
				<?php echo $data->pagination->getResultsCounter(); ?>
			</td>
		</tr>
		</tfoot>
	</table>

	<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; ?>"/>
	<input type="hidden" name="original_order_values" value="<?php echo implode($originalOrders, ','); ?>"/>
	<?php echo JHtml::_('form.token'); ?>
</form>
