<?php require_once('header.php'); ?>

<?php
$statement = $pdo->prepare("SELECT * FROM tbl_settings WHERE id=1");
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);                            
foreach ($result as $row) {
    $banner_cart = $row['banner_cart'];
}
?>

<?php
$error_message = '';
if(isset($_POST['product_id'])) {

    $i = 0;
    $statement = $pdo->prepare("SELECT * FROM tbl_product");
    $statement->execute();
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $row) {
        $i++;
        $table_product_id[$i] = $row['p_id'];
        $table_quantity[$i] = $row['p_qty'];
    }

    $i=0;
    foreach($_POST['product_id'] as $val) {
        $i++;
        $arr1[$i] = $val;
    }
    $i=0;
    foreach($_POST['quantity'] as $val) {
        $i++;
        $arr2[$i] = $val;
    }
    $i=0;
    foreach($_POST['product_name'] as $val) {
        $i++;
        $arr3[$i] = $val;
    }
    
    $allow_update = 1;
    for($i=1;$i<=count($arr1);$i++) {
        for($j=1;$j<=count($table_product_id);$j++) {
            if($arr1[$i] == $table_product_id[$j]) {
                $temp_index = $j;
                break;
            }
        }
        if($table_quantity[$temp_index] < $arr2[$i]) {
        	$allow_update = 0;
            $error_message .= '"'.$arr2[$i].'" items are not available for "'.$arr3[$i].'"\n';
        } else {
            $_SESSION['cart_p_qty'][$i] = $arr2[$i];
        }
    }
    $error_message .= '\nOther items quantity are updated successfully!';
    ?>
    
    <?php if($allow_update == 0): ?>
    	<script>alert('<?php echo $error_message; ?>');</script>
	<?php else: ?>
	
	<?php endif; ?>
    <?php

}
?>

       

	  
	  
	   <section class="cart-page section-padding" id="here">
	       
<div id="product-product" class="container product">
  <ul class="breadcrumb">
      <h1>Cart</h1>
    <li><a href="<?php echo BASE_URL; ?>"><i class="fa fa-home"></i></a></li>
    <li><a href="<?php echo BASE_URL.$cur_page ?>"><?php echo 'Cart'; ?></a></li>
  </ul>
  </div>
  
         <div class="container">
            <div class="row">
               <div class="col-md-12">
			    <form action="" method="post" id="myform">
                  <div class="card card-body cart-table">
				  
				  <?php if(!isset($_SESSION['cart_p_id'])): ?>
                    <?php echo 'Cart is empty'; ?>
                <?php else: ?>
				  
                    <?php $csrf->echoInputField(); ?>
				
                     <div class="table-responsive">
                        <table class="table table-striped  table-bordered cart_summary">
                           <thead>
                              <tr>
							   <th >SN.</th>
                                 <th class="cart_product">Product</th>
                                 <th>Description</th>
                                 <th>Unit price</th>
                                 <th width="20%">Qty</th>
                                 <th>Total</th>
                                 <th class="action"><i class="fa fa-delete-forever"></i></th>
                              </tr>
                           </thead>
                           <tbody>
						   
						           <?php
                        $table_total_price = 0;

                        $i=0;
                        foreach($_SESSION['cart_p_id'] as $key => $value) 
                        {
                            $i++;
                            $arr_cart_p_id[$i] = $value;
                        }

                        $i=0;
                        foreach($_SESSION['cart_size_id'] as $key => $value) 
                        {
                            $i++;
                            $arr_cart_size_id[$i] = $value;
                        }

                        $i=0;
                        foreach($_SESSION['cart_size_name'] as $key => $value) 
                        {
                            $i++;
                            $arr_cart_size_name[$i] = $value;
                        }

                        $i=0;
                        foreach($_SESSION['cart_color_id'] as $key => $value) 
                        {
                            $i++;
                            $arr_cart_color_id[$i] = $value;
                        }

                        $i=0;
                        foreach($_SESSION['cart_color_name'] as $key => $value) 
                        {
                            $i++;
                            $arr_cart_color_name[$i] = $value;
                        }

                        $i=0;
                        foreach($_SESSION['cart_p_qty'] as $key => $value) 
                        {
                            $i++;
                            $arr_cart_p_qty[$i] = $value;
                        }

                        $i=0;
                        foreach($_SESSION['cart_p_current_price'] as $key => $value) 
                        {
                            $i++;
                            $arr_cart_p_current_price[$i] = $value;
                        }

                        $i=0;
                        foreach($_SESSION['cart_p_name'] as $key => $value) 
                        {
                            $i++;
                            $arr_cart_p_name[$i] = $value;
                        }

                        $i=0;
                        foreach($_SESSION['cart_p_featured_photo'] as $key => $value) 
                        {
                            $i++;
                            $arr_cart_p_featured_photo[$i] = $value;
                        }
                        ?>
                        <?php for($i=1;$i<=count($arr_cart_p_id);$i++): ?>
                        
                              <tr id="cartdel_<?php echo $i;?>">
							  <td><?php echo $i; ?></td>
                                 <td class="cart_product"><a href="#"><img style="width:100px;"class="img-fluid" src="assets/uploads/<?php echo $arr_cart_p_featured_photo[$i]; ?>" alt=""></a></td>
                                 <td class="cart_description">
                                    <h5 class="product-name"><?php echo $arr_cart_p_name[$i]; ?></h5>
                                    <h6><strong><span class="fa fa-approval"></span><?php echo $arr_cart_size_name[$i]; ?>
									</strong> <?php echo $arr_cart_color_name[$i]; ?></h6>
                                 </td>
                                 <td class="price"><span><?php echo number_format($arr_cart_p_current_price[$i],2); ?></span></td>
                                 <td class="qty" width="100">
								       <input type="hidden" name="product_id[]" value="<?php echo $arr_cart_p_id[$i]; ?>">
                                <input type="hidden" name="product_name[]" value="<?php echo $arr_cart_p_name[$i]; ?>">
                              
							  
							  <?php 
							    $id  = $arr_cart_p_id[$i];
							    $statement = $pdo->prepare("SELECT * FROM tbl_product_size WHERE p_id=? AND size_id=?");
                                $statement->execute(array($id,$arr_cart_size_id[$i]));
                                $result = $statement->fetchAll(PDO::FETCH_ASSOC);
                                foreach ($result as $row) {
                                    $p_qty = $row['p_qty'];
                                }
							  ?>
							  
                                   <div class="input-group col-xs-12 col-sm-12 button-group" >
                                              <span class="input-group-btn" ><b class="btn btn-md bg-theme-color text-white" onclick="minus(<?php echo $arr_cart_p_id[$i]; ?>)">-</b></span>
                                          <input type="text" name="quantity" class="form-control text-center" qty_id="<?php echo $i;?>" id="qty_<?php echo $arr_cart_p_id[$i]; ?>"
                                             qty="<?php echo $p_qty; ?>"  value="<?php echo $arr_cart_p_qty[$i]; ?>" >
                                          <span class="input-group-btn"><b  class="btn btn-md bg-theme-color text-white" onclick="plus(<?php echo $arr_cart_p_id[$i]; ?>)">+</b></span>
                                       </div>
                                 </td>
                                 <td class="price"><span class="total_amt_cart"><?php
                                $row_total_price = $arr_cart_p_current_price[$i]*$arr_cart_p_qty[$i];
                                $table_total_price = $table_total_price + $row_total_price;
                                $finaltotal +=$table_total_price;
                                ?>
                                <?php echo LANG_VALUE_1; ?><?php echo number_format($row_total_price,2); ?></span></td>
                                 <td class="action">
								 <a  onclick="mmy(<?php echo $i;?>);" class="btn btn-sm btn-danger">X</a>
                                </td>
                              </tr>
                        <?php endfor; ?>
                                <tr>
                                    <td colspan="5">Sub Total</td>
                                    <td colspan="2"><?php echo LANG_VALUE_1.number_format($finaltotal,2); ?></td>
                                </tr>
                        
                           </tbody>
                           
                        </table>
                     </div>
					 
					 
                   
					 
					 
					
					
				 
					 
					 </div>
					 <div class="col-md-8 col-sm-2"></div>
					 <div class="col-md-2 col-sm-5 col-xs-6 text-left">
					      <a href="index.php"  class="btn bg-theme-color text-white" >
                                <i class="fa fa-cart"></i>Add More Product
            			  </a>
					 </div>
					 <div class="col-md-2 col-sm-5 col-xs-6 text-right">
					     <a href="checkout.php"  class="btn btn-success" >
        					 <span class="float-left"><i class="fa fa-cart"></i> <!--Proceed to Checkout--> Final Order  </span>
        					 <!--<span class="float-right"><strong><?php echo number_format($table_total_price,2); ?></strong></span>-->
        				</a>
					 </div>
					 	 
            
                  </div>
                  
                </form>
                <?php endif; ?>
               </div>
            </div>
         </div>
      </section>



<?php require_once('footer.php'); ?>