<?php $__env->startSection("content"); ?>
<div class="col-md-12" style="background-color: rgb(249, 247, 241);padding:18px;margin-bottom:350px;">
	<div class="row" style="background-color:#fff;padding:30px;">
		<div class="col-md-12 page_title" style="border:1px solid #555;padding:0px 60px;">
			<h2 class=" text-center">SHOPPING BAG</h2>

			<div class="col-xs-12 shopping_bag">
                <?php echo Form::open(array(
                    'action'=> ('FrontController@getCheckout'),
                    'class'=>'form-inline',
                    'role' => 'form',
                    'style'=> 'z-index:999;margin-top:50px;',
                    'method'=>'post'
                )); ?>

				<?php for($i = 0 ; $i < count($products) ; $i++): ?>
				<div class="col-xs-12 bag_product">
					<div class="form-group col-xs-4">
                        <div class="input-group">
                            <?php if($productimgs[$i] != null): ?> 
                            <?php echo HTML::image($productimgs[$i]->image, $products[$i]->title, array('width'=>"75","style"=>"margin-top:-9px;","class"=>"img-circle")); ?>

                            <?php endif; ?>
                            <span class="bag_product_name"><?php echo $products[$i]->title; ?></span>
                            <input type="hidden" name="ids[]" value="<?php echo $products[$i]->id; ?>"/>
						</div>
					</div>
                    <div class="form-group col-xs-3">
        				<?php echo Form::select('format[]', $formats[$i], $formats[$i], array('class' => 'form-control times text-center input-lg', 'style'=>'width:100%') ); ?>

					</div>

					<div class="form-group col-xs-2">
						<select name="number[]" class="form-control times text-center input-lg number" style="width:100%;">
							<option>1</option>
							<option>2</option>
							<option>3</option>
							<option>4</option>
							<option>5</option>
							<option>6</option>
							<option>7</option>
						</select>
					</div>

					<div class="form-group col-xs-2">
						<span class="product_price times" data-price="<?php echo $products[$i]->selling_price; ?>">NT $<?php echo $products[$i]->selling_price; ?></span>
					</div>

					<div class="form-group col-xs-1">
						<a href="<?php echo URL::to('front/cleancookie/'.$products[$i]->id.'/'.$productposition[$i]); ?>">
							<img style="margin-top: 20px;" data-id="<?php echo $products[$i]->id; ?>" class="cancel pull-right" src="<?php echo asset('images/icons/icon_cancel.png'); ?>">
                        </a>
					</div>
				</div>
				<?php endfor; ?>

				<div class="col-xs-2 col-xs-offset-9 text-right">
					<h2 class="times total" style="margin-top:28px;">NT$1740</h2>
				</div>

				<div class="col-xs-12" style="clear:both;margin-top:100px;padding-bottom:50px;">
					<div class="col-xs-2 col-xs-offset-7" >
						<a class="btn-lg btn btn-default " href="<?php echo URL::to('front'); ?>" style="width:100%;">繼續購物</a>
					</div>

					<div class="col-xs-3">
						<input type="submit" class="btn-lg btn btn-default" style="width:100%;" value="結賬">
					</div>
				</div>
			<?php echo Form::close(); ?>

		</div>
	</div>
</div>

</div>


<script type="text/javascript" charset="utf-8">
	$(document).ready(function(){
		var numbers = <?php echo json_encode($productnumber); ?>; 
		var count =0, price = 0, countprice = 0;
		var products = <?php echo json_encode($products); ?>;
		var getprice = [];
		
		$(".number").each(function(){
			getprice[count] = numbers[count];
			$(this).val(numbers[count++]);
		});


        $(".number").change(function(){
            var product = $(this).parents(".bag_product").find(".product_price");
            var price = product.data("price");
            product.text("NT "+ parseInt(price) * $(this).val());

            var total = 0;
            $(".product_price").each(function(){
                var number = $(this).parents(".bag_product").find(".number").val();
                total += parseInt($(this).data("price")) * number;
            });

            $(".total").text("NT " + total);
        });

		$(".product_price").each(function(){
//			console.log($(this).text().substr(4)*getprice[countprice++]);	
			$(this).text("NT "+$(this).text().substr(4)*getprice[countprice++]);
		});	
		
		for (var i = 0 ; i<products.length ; i++){
			price += parseInt(products[i].selling_price)*getprice[i];
		}
		$(".total").text("NT "+price);

		$(".buy").click(function(){
			var url = "<?php echo URL::to('front/checkout/1'); ?>";	
			window.location = url;
		});

		$(".delete").click(function(){
			console.log("aa");	
		});
        
        $(".cancel").click(function(){
            var products;
            if($.cookie("cart") == undefined) {
                products = [];
            } else {
                products = JSON.parse($.cookie("cart"));
            }

            var id = $(this).data("id");
            var removeElem = [];
            for(var i = 0; i < products.length; i++) {
                if(id != products[i]){
                    removeElem.push(products[i]);
                }
            }
            $.cookie("cart", JSON.stringify(removeElem), { expires : 1000 , path:'/'});
            location.reload();
        });
	});
</script>

<?php $__env->stopSection(); ?>





<?php echo $__env->make('layout.basic_front', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>