<div class="business-products-cart">
	<div class="row">
    	<div class="col-sm-12">
        	<h3 class="cart-heading">My Cart</h3> 
        </div>
    </div>
    
    <div class="row cart-heading-bottom-line"></div>
    
    @if(count($products) > 0)
    	<?php 
			$currencyCode=$business->currency;
			if($currencyCode=="INR"){  
				$currencyIcon="<i class='fa fa-inr'></i>"; 
			}
			else{
				$currencyIcon="<i class='fa fa-dollar'></i>"; 
			}
			
			$business_currency="USD";
			if($currencyCode){
				$business_currency=$business->currency;
			}
						
			$totalQuantity=0;
			$totalAmount=0;
			$totalAmountWithoutShipping=0;
			// Static per-product shipping only (qty 1–3).
			$requiresCarrierShippingCalculator = false;
			$hasPhysicalItems = false;
		?>
        
        <input type="hidden" id="currency_code" value="{{$business_currency}}" />
        @foreach($products as $cart)
        	<?php $product=$cart->product;?>
            
            @if($product)
				<?php
					// Track whether cart has physical items to show Delivery/Pickup options.
					// Carrier shipping calculator is disabled (static shipping only).
					$isPhysicalProduct = isset($product->is_product_physical) ? ((int) $product->is_product_physical === 1) : true;
					$isFreeShipping = isset($product->is_free_shipping) ? (int) $product->is_free_shipping : 0;
					if ($isPhysicalProduct) {
						$hasPhysicalItems = true;
					}
				?>
            
			<?php
				$pricefor="";
				$selectedColor="";
				$actualPrice=0;
                if($product->product_category==1){
					
                    $priceRecord=app('App\\Repositories\\BusinessProductsCategoryValuesRepository')->getProductValueById($cart->productsizeid,$product->id,'liquid-size');
                    
                    if($priceRecord){
                        $actualPrice=$priceRecord->price;
						$pricefor="($priceRecord->value)";
                    }
                }elseif($product->product_category==2){
					
					$sizeRecord=app('App\\Repositories\\BusinessProductsCategoryValuesRepository')->getProductValueById($cart->productsizeid,$product->id,'wearable-size');					
					//$colorRecord=app('App\\Repositories\\BusinessProductsCategoryValuesRepository')->getProductValueById($cart->product_color_id,$product->id,'color');
					
					if($sizeRecord){
						$pricefor="($sizeRecord->value)";
						$selectedColor=$sizeRecord->color;
						$actualPrice=$sizeRecord->price;
					}
				}else{
                    $actualPrice=$product->price;
                }
            ?>
            @if($actualPrice)
            
            	<?php
					$price=$actualPrice;
					
					$savedPriceSave=0;
					if($product->discount){
						$savedPrice=($product->discount/100)*$actualPrice;
						$price=$actualPrice-$savedPrice;
						$savedPriceSave=($product->discount/100)*($actualPrice*$cart->quantity);
					}
					
					$subTotal=$price*$cart->quantity;
					
					$subTotalAmt=$price*$cart->quantity;
					
					$shippingCost=0; 
					
					if($product->is_free_shipping==1){
						$shippingCost=0;
					}else{
						if($product->shipping_cost_free=='no'){
							$shippingCost=$product->shipping_cost;
						}
						if($shippingCost==""){
							$shippingCost=0;
						}
										
						if($cart->quantity==2){
							$shippingCost=0;
							if($product->shipping_cost_2!="" and $product->shipping_cost_2_free=='no'){
								$shippingCost=$product->shipping_cost_2;
							}
						}elseif($cart->quantity==3){
							$shippingCost=0; 
							if($product->shipping_cost_3!="" and $product->shipping_cost_3_free=='no'){
								$shippingCost=$product->shipping_cost_3;
							}
						}elseif($cart->quantity==4){
							$shippingCost=0;
							if($product->shipping_cost_4!=""){
								$shippingCost=$product->shipping_cost_4;
							}
						}elseif($cart->quantity==5){
							$shippingCost=0; 
							if($product->shipping_cost_5!=""){
								$shippingCost=$product->shipping_cost_5;
							}
						}
					}
					
					$subTotal=$subTotal+$shippingCost;
					
					$totalQuantity=$totalQuantity+$cart->quantity;
					$totalAmount=$totalAmount+$subTotal;
					
					$totalAmountWithoutShipping=$totalAmountWithoutShipping+$subTotalAmt;
				?>
                    
                <div class="row product-cart-row">
                                       
                    <div class="col-sm-6 product-cart-name-details">
                    	<p class="cart-product-name">{{$product->product_name}} {{$pricefor}} @if($selectedColor) <span class="selected-color">{{$selectedColor}}</span> @endif</p>
                    	
                        @if($savedPriceSave!="")
                         <p class="cart-product-price-saved">You Save: {!! $currencyIcon !!}{{number_format($savedPriceSave, 2, '.', ',')}} ({{$product->discount}}%)</p>
                    	@endif
                        
                        <!--<p class="cart-product-quantity">Quantity: {{$cart->quantity}}</p>-->
                        
                        <p class="cart-product-quantity">Quantity: <?php /*?>{{$cart->quantity}}<?php */?>
                        	<select class="change-product-qty" data-id="{{$cart->id}}" data-value="{{$pageId}}" data-community-id="{{$business->community_id}}">
                               <?php 
							   		//for($i=1; $i<21; $i++)
									for($i=1; $i<4; $i++)
									{
											$qty=$i;
											
											$canShow="yes";
											$quantityPrice=0;
											if($product->is_free_shipping==0){
												if($i==1){
													$quantityPrice=$product->shipping_cost;		
													if($quantityPrice==""){
														$quantityPrice=0;
													}
													
												}else{
													$colName="shipping_cost_".$i;
													$quantityPrice=$product->$colName;	
													if(!$quantityPrice || $quantityPrice==0 || $quantityPrice=="0" || $quantityPrice==""){
														//$canShow="no";
													}	
												}
											}
							   ?>
										@if($canShow=="yes")								   
											<option value="{{$qty}}" data-price="{{$quantityPrice}}" @if($cart->quantity==$qty) selected="selected" @endif>{{$qty}}</option>
										@endif	
							  <?php } ?>
                            </select>
                        </p>
                        
                        <?php /*?>@if($product->shipping_cost)<?php */?>
                        
                        @if($product->is_free_shipping==0)
                        	
                             @if($shippingCost!=0)   	
                                 <p class="cart-product-quantity shipping-cost-value-box">Shipping Cost <span title="Final shipping cost will updated at the time of shipping from carrier." class="red-star">*</span>: 
                                                                        
                                        {!! $currencyIcon !!} <span class="shipping-value-{{$cart->id}}">{{number_format($shippingCost, 2, '.', ',')}}</span>
                                 </p>
                              @endif   
                        @endif
                        
                    </div>
                    
                    <div class="col-sm-6 product-cart-price-details">
                    	<p class="cart-product-price">Price: {!! $currencyIcon !!} {{number_format($price, 2, '.', ',')}}</p>
                        
                        <p class="cart-product-price values-with-shipping">Sub Total: {!! $currencyIcon !!} {{number_format($subTotal, 2, '.', ',')}}</p>
                        
                        <p class="cart-product-price values-without-shipping">Sub Total: {!! $currencyIcon !!} {{number_format($subTotalAmt, 2, '.', ',')}}</p>
                        
                        <p class="cart-product-delete"><span class="btn btn-xs red-color-button delete-product-from-cart" data-id="{{$cart->id}}" data-value="{{$pageId}}" data-community-id="{{$business->community_id}}">Delete</span></p>
                    </div>
                 </div>
               @else
               	  <?php
				  	$priceRecord=app('App\\Repositories\\BusinessProductsCartRepository')->deleteCartItemById($cart->id);
				  ?>	
               @endif  
             @endif  
             
             <div class="cart-devider-line"></div>  
         @endforeach   
         
         <div class="row">
         	<div class="col-sm-6 col-xs-6 cart-subtotal-row cart-items-counts-align">	
         		<span class="total-no-of-items">Total no. of items: {{$totalQuantity}} </span></span>
            </div>
            
            <div class="col-sm-6 col-xs-6 cart-subtotal-row">	
         		<span class="total-no-of-items values-with-shipping">Sub Total : <span class="cart-product-price total-no-of-items">{!! $currencyIcon !!} {{number_format($totalAmount, 2, '.', ',')}}</span></span>
                
                <span class="total-no-of-items values-without-shipping">Sub Total : <span class="cart-product-price total-no-of-items">{!! $currencyIcon !!} {{number_format($totalAmountWithoutShipping, 2, '.', ',')}}</span></span>
            </div>
            
         </div>
         
         <div class="cart-devider-line-net-total"></div> 
         
         @if($page->present()->isKycCompleted()) 
                  
             <input type="hidden" value="0" class="askPickupSchedule" />
           
             <div class="row">
                <div class="col-sm-12 cart-nettotal-row">
                        <?php
                        // Taxes apply on product subtotal only (excluding shipping)
                        $taxableAmountWithShipping = $totalAmountWithoutShipping;
                        $taxableAmountWithoutShipping = $totalAmountWithoutShipping;
    
                        $taxLines = [];
                        $taxTitle1 = trim((string) $business->product_tax_name);
                        $taxPerc1 = (float) preg_replace('/[^0-9.\-]/', '', (string) $business->product_tax);
                        if ($taxPerc1 > 0) {
                            $taxLines[] = ['name' => ($taxTitle1 !== '' ? $taxTitle1 : 'Tax'), 'percentage' => $taxPerc1];
                        }
                        $taxTitle2 = trim((string) $business->product_tax_name_1);
                        $taxPerc2 = (float) preg_replace('/[^0-9.\-]/', '', (string) $business->product_tax_1);
                        if ($taxPerc2 > 0) {
                            $taxLines[] = ['name' => ($taxTitle2 !== '' ? $taxTitle2 : 'Tax'), 'percentage' => $taxPerc2];
                        }
                        $taxTitle3 = trim((string) $business->product_tax_name_2);
                        $taxPerc3 = (float) preg_replace('/[^0-9.\-]/', '', (string) $business->product_tax_2);
                        if ($taxPerc3 > 0) {
                            $taxLines[] = ['name' => ($taxTitle3 !== '' ? $taxTitle3 : 'Tax'), 'percentage' => $taxPerc3];
                        }
    
                        $productTax = 0;
                        $productTax1 = 0;
                        $taxBreakdownWithShipping = [];
                        $taxBreakdownWithoutShipping = [];
                        foreach ($taxLines as $taxLine) {
                            $taxAmountWithShipping = ($taxableAmountWithShipping / 100) * $taxLine['percentage'];
                            $taxAmountWithoutShipping = ($taxableAmountWithoutShipping / 100) * $taxLine['percentage'];
                            $productTax += $taxAmountWithShipping;
                            $productTax1 += $taxAmountWithoutShipping;
                            $taxBreakdownWithShipping[] = ['name' => $taxLine['name'], 'percentage' => $taxLine['percentage'], 'amount' => $taxAmountWithShipping];
                            $taxBreakdownWithoutShipping[] = ['name' => $taxLine['name'], 'percentage' => $taxLine['percentage'], 'amount' => $taxAmountWithoutShipping];
                        }
                    ?>
    
                        @if(count($taxBreakdownWithShipping) > 0)
                            @foreach($taxBreakdownWithShipping as $taxItem)
                                <p class="values-with-shipping">{{$taxItem['name']}} ({{number_format($taxItem['percentage'], 2, '.', ',')}}%): {!! $currencyIcon !!}{{number_format($taxItem['amount'], 2, '.', ',')}} </p>
                            @endforeach
                        @else
                            <!--<span class="values-with-shipping">Tax (0.00%): {!! $currencyIcon !!}0.00 </span>
                            <br />-->
                        @endif
    
                        @if(count($taxBreakdownWithoutShipping) > 0)
                            @foreach($taxBreakdownWithoutShipping as $taxItem)
                                <p class="values-without-shipping">{{$taxItem['name']}} ({{number_format($taxItem['percentage'], 2, '.', ',')}}%): {!! $currencyIcon !!}{{number_format($taxItem['amount'], 2, '.', ',')}} </p>
                            @endforeach
                        @else
                            <!--<span class="values-without-shipping">Tax (0.00%): {!! $currencyIcon !!}0.00 </span>
                            <br />-->
                        @endif
    
                    <?php
                        $shippingChargesTotal = $totalAmount - $totalAmountWithoutShipping;
                        if ($shippingChargesTotal < 0) {
                            $shippingChargesTotal = 0;
                        }
                    ?>
                    <p class="values-with-shipping shipping-cost-value-box">Shipping Charges:
                        <span class="brand-colored-texts price-val">{!! $currencyIcon !!}{{number_format($shippingChargesTotal, 2, '.', ',')}}</span>
                    </p>
                    
                        <span class="values-with-shipping">Net Total: <span class="brand-colored-texts price-val">{!! $currencyIcon !!}{{number_format($totalAmount+$productTax, 2, '.', ',')}} </span></span>
                        
                        <span class="values-without-shipping">Net Total: <span class="brand-colored-texts price-val">{!! $currencyIcon !!}{{number_format($totalAmountWithoutShipping+$productTax1, 2, '.', ',')}} </span></span>
                </div>
                
             </div>
    
            <div id="store-checkout-form-section" @if(!Auth::check()) style="display:none;" @endif>
                @if(!Auth::check())
                    <div class="form-group" style="margin-top:10px;">
                        <label>Email address <span class="text-danger">*</span></label>
                        <input type="email" class="form-control" id="guest-checkout-email-inline" placeholder="Email address" autocomplete="email" />
                    </div>
                @endif
    
                @if($hasPhysicalItems)
                    {!! Theme::section('page.business.product-delivery-options') !!}
                @endif
            
                @if(\Config::get('storefront-payment-gateway')=='stripe')
                    <style type="text/css">
                        .invoice-payment-form{
                            background:none !important;
                        }
                    </style>
                    
                    <div id="storefront-stripe-form" style="padding: 13px;  margin-top: -35px;">
                        {!! Theme::section('page.business.themes.stripe-form') !!}
                    </div>
                @endif
            </div>
             
             <div class="row">
                    <div class="col-sm-12">
                        <style type="text/css">
                            .idhubs-store-checkout-fee-summary .idhubs-store-checkout-label,
                            .idhubs-store-checkout-fee-summary .idhubs-store-checkout-val {
                                font-weight: normal;
                                font-size: 17px;
                            }
                            .idhubs-store-checkout-fee-summary .idhubs-store-checkout-final-val {
                                font-weight: bold;
                                font-size: 35px;
                                line-height: 1.1;
                                color: #000;
                                margin-top: -10px;
                            }
                            .idhubs-store-checkout-fee-summary .idhubs-store-checkout-final-val i {
                                font-size: 22px;
                                color: #000;
                                font-weight: bold;
                            }
                            .idhubs-store-checkout-fee-summary .idhubs-store-checkout-net-total-amount {
                                color: #000 !important;
                                font-size: 27px;
                            }
                        </style>
    
                        <div class="idhubs-store-checkout-fee-summary" style="display:none; margin-bottom:10px;">
                            <div class="idhubs-store-checkout-total-amount" style="margin-bottom:15px;">
                                <span class="idhubs-store-checkout-label">Total Amount:</span>
                                <span class="brand-colored-texts idhubs-store-checkout-val" style="float:right;">{!! $currencyIcon !!}<span class="idhubs-store-checkout-total-amount-val">0.00</span></span>
                                <div style="clear:both;"></div>
                            </div>
                            <div class="idhubs-store-checkout-fee-line" style="display:none; margin-bottom:4px;">
                                International card processing fees (<span class="idhubs-store-checkout-fee-percent">0</span>%):
                                <span class="brand-colored-texts" style="float:right;">{!! $currencyIcon !!}<span class="idhubs-store-checkout-fee-amount">0.00</span></span>
                                <div style="clear:both;"></div>
                            </div>
                            <div class="idhubs-store-checkout-net-total">
                                <span class="idhubs-store-checkout-label"><span class="red-star">*</span>Net Total Payable Amount:</span>
                                <span class="brand-colored-texts idhubs-store-checkout-final-val" style="float:right;">{!! $currencyIcon !!}<span class="idhubs-store-checkout-net-total-amount">0.00</span></span>
                                <div style="clear:both;"></div>
                            </div>
                        </div>
    
                         @if($business->theme==1)
                            <button class="btn red-color-button cart-product-continue-btn" data-id="{{$pageId}}">Continue Shopping</button>
                         @else
                            <a href="{{$business->present()->businessUrl()}}" class="btn red-color-button cart-product-continue-btn" data-id="{{$pageId}}">Continue Shopping</a>
                         @endif
                            
                         <button class="btn black-color-button cart-product-proceed-btn" data-id="{{$pageId}}" data-community-id="{{$business->community_id}}">Proceed to Checkout</button>
                    </div>
             </div> 
             
            <!-- <div class="row red-star-description">
                  <div class="col-sm-12">
                        <span class="red-star">*</span> Final shipping cost will updated at the time of shipping from carrier.
                  </div>
             </div> --> 
             
             <input type="hidden" id="current_currency" value="{{$currencyCode}}" />	
             @if($business->currency!="INR")
                    @if(\Config::get('storefront-payment-gateway')=='paypal')
                        <input name="payment_method" id="payment_method" value="paypal" type="hidden" />
                        <div id="paypal-button-container" style="padding:10px;"></div>
                    @elseif(\Config::get('storefront-payment-gateway')=='stripe')
                        <input name="payment_method" id="payment_method" value="stripe" type="hidden" />
                    @endif
              @else
                    <input name="payment_method" id="payment_method" value="payu" type="hidden" />
                    
                    <div id="payubuyproductsdiv" style="display:none; padding:10px;">
                    
                        <form action="{{\Config::get('payu.payment_url')}}" method="post" name="payubuyproducts" id="payubuyproducts">
                            {{ csrf_field() }}
                            <input type="hidden" name="key" value="{{\Config::get('payu.payu_merchant_key')}}"/>
                            <input type="hidden" class="hash" id="hash" name="hash" value=""/>
                            <input type="hidden" class="payu_txnid" name="txnid" value="{{substr(hash('sha256', mt_rand() . microtime()), 0, 20)}}"/>
                            <input type="hidden" name="amount" value="" id="amount"/>
                            <input type="hidden" name="firstname" id="firstname" value="{{\Auth::user()->fullname}}"/>
                            <input type="hidden" name="email" id="email" value="{{\Auth::user()->email_address}}"/>
                            <input type="hidden" name="phone" id="phone" value="9998989897"/>
                            <input type="hidden" name="productinfo" id="productinfo" value="idhubs products"/>
                            <input type="hidden" name="surl" value="{{URL::route('purchasebypayu')}}"/>
                            <input type="hidden" name="furl" value="{{URL::route('purchasebypayu')}}"/>
                            <input type="hidden" name="curl" value="{{URL::route('purchasedonebypayu',array('id' => $pageId))}}">
                            <input type="hidden" name="service_provider" value="payu_paisa"/><!--payu_paisa-->
                            <input type="hidden" name="udf1" class="invoice_pay" value="" id="invoice_pay"/>
                            <input type="hidden" name="udf2" class="" value="Products"/>
                            <input type="hidden" name="udf3" class="" value="Business"/>
                            <input type="hidden" name="udf4" class="" value="{{$pageId}}"/>
                            <input type="hidden" name="ownerid" id="ownerid" value="{{$business->user_id}}">
                            <button type="button" class="btn btn-danger product-buy-payu-btn" onclick="buyProductsByPayu(this)">Pay By Payu</button>                                                
                        
                            <input type="hidden" id="tc" />
                        </form>	
                    </div> 
              @endif  
        @else
            <div style="">
                You can not buy the products from this store.<br />
                No kyc done for this store.<br />
                
                @if(\Auth::check() and \Auth::user()->id==$page->user_id)
                    <a href="{{$page->present()->url('taxdetails')}}" target="_blank" style="color:#FF0000; text-decoration:none;">Click here</a> for your store kyc.
                @endif
            </div>
        @endif         
    @else
    	<div class="row">
            <div class="col-sm-12">
            	Your cart is empty.	
            </div>
        </div>
    @endif  
   
</div>

	@if(!Auth::check())

		<?php
			$community_url = (!empty($business->community)) ? $business->community->present()->url('login') : URL::to('/login');
			$currentUrl = $business->present()->businessUrl();
		?>					  
		<div class="modal fade askLoginModalStoreCart" tabindex="-1" role="dialog">
			<div class="modal-dialog" role="document" style="padding-top:15%;">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						<h4 class="modal-title" style="text-align:center;">Please select one of the following options. Upon completion of this step, you will be returned to this page to proceed.</h4>
					</div>
					<div class="modal-body" style="text-align:center">
						<div style="max-width:420px; margin:0 auto;">
							<button type="button" class="btn btn-primary store-cart-guest-checkout-btn">Guest Checkout</button>
							<a href="{{$community_url}}?urltype=returnurl&returnUrl={{$currentUrl}}"><button type="button" class="btn btn-success">Log In</button></a>
						</div>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">Not Now</button>
					</div>
				</div>
			</div>
		</div>
	@endif



