<?php
/**
 * @package DJ-Catalog2
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 *
 */
// no direct access
defined('_JEXEC') or die;

//Joomla\CMS\HTML\HTMLHelper::_('bootstrap.tooltip');

$user		= JFactory::getUser();
$userId		= $user->get('id');
$listOrder	= $this->state->get('list.ordering');
$listDirn	= $this->state->get('list.direction');
$saveOrder	= ($listOrder == 'a.ordering' && !empty($this->items));

$db = JFactory::getDbo();

$params = JComponentHelper::getParams('com_djcatalog2');

$net_prices = (bool)((int)$params->get('price_including_tax', 1) == 0);

$has_group = (bool)$this->state->get('filter.customergroup', false);

$onstock = array();
$onstock[] = Joomla\CMS\HTML\HTMLHelper::_('select.option', 0, JText::_('JNO'));
$onstock[] = Joomla\CMS\HTML\HTMLHelper::_('select.option', 1, JText::_('JYES'));
$onstock[] = Joomla\CMS\HTML\HTMLHelper::_('select.option', 2, JText::_('COM_DJCATALOG2_ALWAYS'));
$onstock[] = Joomla\CMS\HTML\HTMLHelper::_('select.option', 3, JText::_('COM_DJCATALOG2_ALWAYS_INC_COMBINATIONS'));


$available = array();
if ($has_group) {
	$available[] = Joomla\CMS\HTML\HTMLHelper::_('select.option', -1, JText::_('JLIB_RULES_INHERITED'));
}
$available[] = Joomla\CMS\HTML\HTMLHelper::_('select.option', 0, JText::_('JNO'));
$available[] = Joomla\CMS\HTML\HTMLHelper::_('select.option', 1, JText::_('JYES'));

$dimension_units = $this->dimension_units;
$weight_units = $this->weight_units;
?>
<form action="<?php echo JRoute::_('index.php?option=com_djcatalog2&view=prices');?>" method="post" name="adminForm" id="adminForm">
<div class="row">
	<div id="j-sidebar-container" class="col-md-2">
		<?php echo $this->sidebar; ?>
	</div>
	<div id="j-main-container" class="col-md-10">
	
	<?php echo JLayoutHelper::render('joomla.searchtools.default', array('view' => $this)); ?>

	<table class="table table-striped">
		<thead>
			<tr>
				<th class="title" width="20%">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', 'COM_DJCATALOG2_NAME', 'a.name', $listDirn, $listOrder); ?>
				</th>
				<th class="center">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort',  'COM_DJCATALOG2_SKU', 'a.sku', $listDirn, $listOrder ); ?>
				</th>
				<th class="center">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort',  'COM_DJCATALOG2_CATEGORY', 'category_name', $listDirn, $listOrder ); ?>
				</th>
				<?php if ($has_group) { ?>
					<th width="15%" class="center">
						<?php echo JText::_('COM_DJCATALOG2_BASE_PRODUCT_PRICE'); ?>
						<p class="smallsub"><small>[<?php echo JText::_(($net_prices) ? 'COM_DJCATALOG2_PRICE_EXCL_TAX' : 'COM_DJCATALOG2_PRICE_INCL_TAX'); ?>]</small></p>
					</th>
				<?php } ?>
				
				<th width="10%" class="center">
					<?php echo JText::_('COM_DJCATALOG2_PRICE_EXCL_TAX'); ?>
				</th>
				<th width="10%" class="center">
					<?php echo JText::_('COM_DJCATALOG2_PRICE_INCL_TAX'); ?>
				</th>
				<th width="10%" class="center">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort',  'COM_DJCATALOG2_AVAILABLE', 'a.available', $listDirn, $listOrder ); ?>
				</th>
				<th width="10%" class="center">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort',  'COM_DJCATALOG2_STOCK', 'a.stock', $listDirn, $listOrder ); ?>
				</th>
				<th width="20%" class="center">
					<?php echo JText::_('COM_DJCATALOG2_DIMENSIONS_LBL'); ?>
				</th>
				<th width="10%" class="center">
					<?php echo JText::_('COM_DJCATALOG2_WEIGHT'); ?>
				</th>
				<th width="1%" class="nowrap">
					<?php echo Joomla\CMS\HTML\HTMLHelper::_('searchtools.sort', 'JGRID_HEADING_ID', 'a.id', $listDirn, $listOrder); ?>
				</th>
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="<?php echo ($has_group) ? '11' : '10'; ?>">
					<?php echo $this->pagination->getListFooter(); ?>
				</td>
			</tr>
		</tfoot>
		<tbody>
		<?php foreach ($this->items as $i => $item) :
				$product_price = null;
				if ($has_group && $item->group_price > 0) {
					$product_price = $item->group_price;
				} else if (!$has_group) {
					$product_price = $item->price ? $item->price : 0.00;
				}
				?>
			<tr class="row<?php echo $i % 2; ?>">
				<td>
					<a target="_blank" href="<?php echo JRoute::_('index.php?option=com_djcatalog2&task=item.edit&id='.$item->id);?>"><?php echo $this->escape($item->name); ?></a>
					<p class="smallsub">
						<?php echo JText::sprintf('JGLOBAL_LIST_ALIAS', $this->escape($item->alias));?><br />
					</p>
					<?php if ($item->combo_count > 0) {?>
						<div class="btn-group">
							<a class="btn btn-primary btn-mini" target="_blank" href="<?php echo JRoute::_('index.php?option=com_djcatalog2&task=item.edit&id='.$item->id.'#combinations');?>"><?php echo JText::_('COM_DJCATALOG2_ITEM_COMBINATIONS').': '.$item->combo_count; ?></a>
							<a class="btn btn-primary btn-mini" href="<?php echo JRoute::_('index.php?option=com_djcatalog2&view=prices&layout=combinations&filter_item='.$item->id);?>"><?php echo JText::_('COM_DJCATALOG2_EDIT_COMBINATION_PRICES'); ?></a>
						</div>
					<?php } ?>
				</td>
				<td class="center">
					<?php echo $this->escape($item->sku); ?>
				</td>
				<td class="center">
					<?php echo $this->escape($item->category_name); ?>
				</td>
				<?php if ($has_group) { ?>
					<td class="center">
						<input class="input input-mini input form-control readonly" type="text" readonly="readonly" value="<?php echo $item->price > 0.0 ? $item->price : 0.00; ?>" />
					</td>
				<?php } ?>
				
				<?php if ($net_prices) { ?>
					<td class="center">
						<input data-taxrate="<?php echo floatval($item->tax_rate); ?>" data-type="net" data-target="djc_tax_price_<?php echo $item->id; ?>" class="input input-mini input form-control djc_price" type="text" value="<?php echo $product_price; ?>" name="djc_prices[<?php echo $item->id ?>][new]" />
						<input type="hidden" name="djc_prices[<?php echo $item->id ?>][old]" value="<?php echo $product_price; ?>" />
					</td>
					<td class="center">
						<input id="djc_tax_price_<?php echo $item->id; ?>" class="input input-mini input form-control djc_tax_price readonly" type="text" readonly="readonly" />
					</td>
				<?php } else { ?>
					<td class="center">
						<input id="djc_tax_price_<?php echo $item->id; ?>" class="input input-mini input form-control djc_tax_price readonly" type="text" readonly="readonly" />
					</td>
					<td class="center">
						<input data-taxrate="<?php echo floatval($item->tax_rate); ?>" data-type="gross" data-target="djc_tax_price_<?php echo $item->id; ?>" class="input input-mini input form-control djc_price" type="text" value="<?php echo $product_price; ?>" name="djc_prices[<?php echo $item->id ?>][new]" />
						<input type="hidden" name="djc_prices[<?php echo $item->id ?>][old]" value="<?php echo $product_price; ?>" />
					</td>
				<?php } ?>
				<td class="center">
					<div class="input-append">
						<?php if ($has_group) {
							$available[0]->text = JText::_('JLIB_RULES_INHERITED').' ('.(JText::_(($item->available ? 'JYES' : 'JNO'))).')';
						}?>
						<select name="djc_prices[<?php echo $item->id?>][available]" class="input input-small">
							<?php 
							echo Joomla\CMS\HTML\HTMLHelper::_('select.options', $available, 'value', 'text', ($has_group ? $item->group_available : $item->available), true);?>
						</select>
					</div>
				</td>
				<td class="center">
					<div class="input-append">
						<input class="input input-mini input form-control djc_stock" type="text" value="<?php echo $item->stock; ?>" name="djc_prices[<?php echo $item->id ?>][stock]"  <?php if ($has_group) echo 'disabled="disabled"'; ?> />
							<select name="djc_prices[<?php echo $item->id?>][onstock]" class="input input-small " <?php if ($has_group) echo 'disabled="disabled"'; ?> >
								<?php 
								echo Joomla\CMS\HTML\HTMLHelper::_('select.options', $onstock, 'value', 'text', $item->onstock, true);?>
							</select>
					</div>
				</td>
				<td class="center">
					<div class="input-append">
						<input class="input input-mini input form-control djc_stock" type="text" value="<?php echo $item->length; ?>" name="djc_prices[<?php echo $item->id ?>][length]" <?php if ($has_group) echo 'disabled="disabled"'; ?> />
						<br /><input class="input input-mini input form-control djc_stock" type="text" value="<?php echo $item->width; ?>" name="djc_prices[<?php echo $item->id ?>][width]" <?php if ($has_group) echo 'disabled="disabled"'; ?>  />
						<br /><input class="input input-mini input form-control djc_stock" type="text" value="<?php echo $item->height; ?>" name="djc_prices[<?php echo $item->id ?>][height]" <?php if ($has_group) echo 'disabled="disabled"'; ?>  />
						<br /><select name="djc_prices[<?php echo $item->id?>][dimensions_unit]" class="input input-small " <?php if ($has_group) echo 'disabled="disabled"'; ?> >
							<?php 
							echo Joomla\CMS\HTML\HTMLHelper::_('select.options', $dimension_units, 'value', 'text', $item->dimensions_unit, true); ?>
						</select>
					</div>
				</td>
				<td class="center">
					<div class="input-append">
						<input class="input input-mini input form-control djc_stock" type="text" value="<?php echo $item->weight; ?>" name="djc_prices[<?php echo $item->id ?>][weight]" <?php if ($has_group) echo 'disabled="disabled"'; ?>  />
						<br /><select name="djc_prices[<?php echo $item->id?>][weight_unit]" class="input input-small " <?php if ($has_group) echo 'disabled="disabled"'; ?> >
								<?php 
								echo Joomla\CMS\HTML\HTMLHelper::_('select.options', $weight_units, 'value', 'text', $item->weight_unit, true);?>
							</select>
					</div>
				</td>
				<td class="center">
					<?php echo (int) $item->id; ?>
				</td>
			</tr>
			<?php endforeach; ?>
		</tbody>
	</table>

	<div>
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<?php echo Joomla\CMS\HTML\HTMLHelper::_('form.token'); ?>
	</div>
</div>
</form>


<script type="text/javascript">
(function($){
	$(document).ready(function() {
		$('input.djc_price').on('keyup change click', function(){
			djValidatePrice($(this));
		});

		$('input.djc_price').trigger('change');
	});


	function djValidatePrice(priceInput) {
		//var r = new RegExp("\,", "i");
		//var t = new RegExp("[^0-9\,\.]+", "i");
		//priceInput.setProperty('value', priceInput.getProperty('value').replace(r, "."));
		//priceInput.setProperty('value', priceInput.getProperty('value').replace(t, ""));
	
	
		var price = priceInput.val();
		
		// valid format
		var valid_price = new RegExp(/^(\d+|\d+\.\d+)$/);
		
		// comma instead of dot
		var wrong_decimal = new RegExp(/\,/g);
		
		// non allowed characters
		var restricted = new RegExp(/[^\d+\.]/g);
		
		// replace comma with a dot
		price = price.replace(wrong_decimal, ".");
		
		if (valid_price.test(price) == false) {
			// remove illegal chars
			price = price.replace(restricted, '');
		}
		
		if (valid_price.test(price) == false) {
			// too many dots in here
			parts = price.split('.');
			if (parts.length > 2 ) {
				price = parts[0] + '.' + parts[1];
			}
		}
		priceInput.val(price);

		var inputType = priceInput.attr('data-type');
		var taxRate = priceInput.attr('data-taxrate');

		if (inputType == 'gross') {
			djPriceFromGross($('#' + priceInput.attr('data-target')), price, taxRate);
		} else if (inputType == 'net') {
			djPriceFromNet($('#' + priceInput.attr('data-target')), price, taxRate);
		}
	}

	function djPriceFromGross(element, price, taxrate) {
		price = parseFloat(price);
		taxrate = parseFloat(taxrate);
		if (!price || !(taxrate >= 0)) {
			element.val('');
			return;
		}

		var netPrice = (price * 100) / (100 + taxrate);
		element.val(netPrice.toFixed(2));
	}

	function djPriceFromNet(element, price, taxrate) {
		price = parseFloat(price);
		taxrate = parseFloat(taxrate);

		if (!price || !(taxrate >= 0)) {
			element.val('');
			return;
		}

		var grossPrice = price * ((100 + taxrate)/100) ;
		element.val(grossPrice.toFixed(2));
	}
})(jQuery);
</script>
