<?php
defined('_JEXEC') or die('Restricted access');
if(!empty($products)) :
?>
<div class="hikashop_tglproducts_module<?php echo $moduleclass_sfx; ?>" id="hikashop_tglproducts_module">
    <div class="row">
        <?php foreach ($products as $product): ?>
        <div class="product-item col bg-white mx-1 p-2" id="product-<?php echo $product['product_id']; ?>">
            <div class="product-img">
                <img class="img-fluid" src="<?php echo $product['image']; ?>" alt="<?php echo $product['product_name']; ?>" />
            </div>
            <form class="form pt-2">
                <div class="form-group row mb-2">
                    <label for="" class="col-sm-6 col-form-label"><?php echo $product['product_name']; ?></label>
                    <div class="col-sm-6">
                        <select class="form-control custom-select product-charactgleristic">
                            <?php $first = true; foreach ($product['characteristic'] as $ccItem): ?>
                            <option value="<?php echo $ccItem['id']; ?>" data-pid="<?php echo $ccItem['pid']; ?>" data-amount="<?php echo $ccItem['amount']; ?>" <?php if($first) {$first = false; echo 'selected="selected"'; } ?>>
                                <?php echo $ccItem['alias']; ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <div class="lead text-center pb-2 product-price">
                    <?php echo $currency->format((isset($product['characteristic'][0]['amount']) ? $product['characteristic'][0]['amount'] : 0), hikashop_getCurrency()); ?>
                </div>
                <div class="form-group row">
                    <div class="input-group col-sm-12">
                        <div class="input-group-append product-qinc">
                            <span class="input-group-text">+</span>
                        </div>
                        <input type="text" class="form-control text-center product-quantity" placeholder="<?php echo JText::_('MOD_HIKASHOP_TGLPRODUCTS_COUNT'); ?>" value="1">
                        <div class="input-group-prepend product-qdec">
                            <span class="input-group-text">-</span>
                        </div>
                    </div>
                </div>
  <a class="btn btn-block hikabtn hikacart tglproduct_addtocart" href="index.php/component/hikashop/product/updatecart/add-1/cid-<?php echo (isset($product['characteristic'][0]['pid']) ? $product['characteristic'][0]['pid'] : $product['product_id']); ?>/quantity-1"><?php echo JText::_('MOD_HIKASHOP_TGLPRODUCTS_ADD_TO_CART'); ?></a>
            </form>
        </div>
        <?php endforeach; ?>
    </div>
</div>
<script>	
    jQuery(document).ready(function () {
        // Get currency
        var priceFormat = '<?php echo $currency->format(0, hikashop_getCurrency()); ?>';
        var priceFormatParts = priceFormat.trim().split(' ');
        var currency = priceFormatParts.pop();
        var baseAddLink = 'index.php/component/hikashop/product/updatecart/add-1/';
        jQuery('.product-qinc').click(function () {
            var productQuantityField = jQuery(this).parent().children('input.product-quantity');
            productQuantityField.val(parseInt(productQuantityField.val()) + 1);
            createAddLink(this);
        });
        jQuery('.product-qdec').click(function () {
            var productQuantityField = jQuery(this).parent().children('input.product-quantity');
            productQuantityField.val(parseInt(productQuantityField.val()) - 1);
            createAddLink(this);
        });
        jQuery('.product-charactgleristic').change(function () {
            var amount = jQuery("option[value=" + jQuery(this).val() + "]", this).attr('data-amount');
            // change amount
            jQuery(this).parents('form').children('.product-price').html(amount + ' ' + currency);
            createAddLink(this);
        });
        function createAddLink(currentPosition) {
            var productParent = jQuery(currentPosition).parents("div[id^='product-']");
            var pid = productParent.find('select.product-charactgleristic').find("option:selected").attr('data-pid');
            var quantity = productParent.find('input.product-quantity').val();
            var propString = 'cid-' + pid + '/quantity-' + quantity;
            productParent.find('a.tglproduct_addtocart').attr('href', baseAddLink + propString);
        }
    });
</script>
<?php endif;