<?php require_once('header.php'); ?>
<style>
    .scroll {
    overflow: scroll;
    width: 1000px;
    display: flex;
}
.btn-radius {
    margin: 5px;
}
::-webkit-scrollbar {
    -webkit-appearance: none;
}

::-webkit-scrollbar:vertical {
    width: 0px;
}

::-webkit-scrollbar:horizontal {
    height: 0px;
}

::-webkit-scrollbar-thumb {
    background-color: rgba(0, 0, 0, .5);
    border-radius: 10px;
    border: 0px solid #ffffff;
}

::-webkit-scrollbar-track {
    border-radius: 10px;  
    background-color: #ffffff; 
}
</style>
<?php
$statement = $pdo->prepare("SELECT * FROM tbl_settings WHERE id=1");
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);                            
foreach ($result as $row) {
    $banner_product_category = $row['banner_product_category'];
}
?>

<?php
if( !isset($_REQUEST['id']) || !isset($_REQUEST['type']) ) {
    header('location: index.php');
    exit;
} else {

    if( ($_REQUEST['type'] != 'top-category') && ($_REQUEST['type'] != 'mid-category') && ($_REQUEST['type'] != 'end-category') ) {
        header('location: index.php');
        exit;
    } else {

        $statement = $pdo->prepare("SELECT * FROM tbl_top_category");
        $statement->execute();
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);                            
        foreach ($result as $row) {
            $top[] = $row['tcat_id'];
            $top1[] = $row['tcat_name'];
        }

        $statement = $pdo->prepare("SELECT * FROM tbl_mid_category");
        $statement->execute();
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);                            
        foreach ($result as $row) {
            $mid[] = $row['mcat_id'];
            $mid1[] = $row['mcat_name'];
            $mid2[] = $row['tcat_id'];
        }

        $statement = $pdo->prepare("SELECT * FROM tbl_end_category");
        $statement->execute();
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);                            
        foreach ($result as $row) {
            $end[] = $row['ecat_id'];
            $end1[] = $row['ecat_name'];
            $end2[] = $row['mcat_id'];
        }

        if($_REQUEST['type'] == 'top-category') {
            if(!in_array($_REQUEST['id'],$top)) {
                header('location: index.php');
                exit;
            } else {

                // Getting Title
                for ($i=0; $i < count($top); $i++) { 
                    if($top[$i] == $_REQUEST['id']) {
                        $title = $top1[$i];
                        break;
                    }
                }
                $arr1 = array();
                $arr2 = array();
                // Find out all ecat ids under this
                for ($i=0; $i < count($mid); $i++) { 
                    if($mid2[$i] == $_REQUEST['id']) {
                        $arr1[] = $mid[$i];
                    }
                }
                for ($j=0; $j < count($arr1); $j++) {
                    for ($i=0; $i < count($end); $i++) { 
                        if($end2[$i] == $arr1[$j]) {
                            $arr2[] = $end[$i];
                        }
                    }   
                }
                $final_ecat_ids = $arr2;
            }   
        }

        if($_REQUEST['type'] == 'mid-category') {
            if(!in_array($_REQUEST['id'],$mid)) {
                header('location: index.php');
                exit;
            } else {
                // Getting Title
                for ($i=0; $i < count($mid); $i++) { 
                    if($mid[$i] == $_REQUEST['id']) {
                        $title = $mid1[$i];
                        break;
                    }
                }
                $arr2 = array();        
                // Find out all ecat ids under this
                for ($i=0; $i < count($end); $i++) { 
                    if($end2[$i] == $_REQUEST['id']) {
                        $arr2[] = $end[$i];
                    }
                }
                $final_ecat_ids = $arr2;
            }
        }

        if($_REQUEST['type'] == 'end-category') {
            if(!in_array($_REQUEST['id'],$end)) {
                header('location: index.php');
                exit;
            } else {
                // Getting Title
                for ($i=0; $i < count($end); $i++) { 
                    if($end[$i] == $_REQUEST['id']) {
                        $title = $end1[$i];
                        break;
                    }
                }
                $final_ecat_ids = array($_REQUEST['id']);
            }
        }
        
    }   
}
?>






<section class="pt-3 pb-3 page-info section-padding border-bottom bg-white">
			<div class="container">
				<div class="row">
					<div class="col-md-12">
						<a href="<?php echo BASE_URL;?>"><strong><span class="mdi mdi-home"></span> Home</strong></a>
						<span class="mdi mdi-chevron-right"></span> <a href="#"><?php echo LANG_VALUE_51; ?> <?php echo $title; ?></a>
					</div>
				</div>
			</div>
		</section>

<section class="bg-light pb-1 pt-1">
			<div class="container">
			<div class="row">
        <div class="scroll hidden-md hidden-lg ">
             <?php
             
             if(isset($_GET['type']) && $_GET['type']=='mid-category'){
                                                            $statement2 = $pdo->prepare("SELECT * FROM tbl_end_category WHERE mcat_id=?");
                                                            $statement2->execute(array($_GET['id']));
                                                            $result2 = $statement2->fetchAll(PDO::FETCH_ASSOC);
                                                            foreach ($result2 as $row2) {
                                                    ?>
                                                    <?php if($row2['ecat_id']===$row2['ecat_id']){ ?>
                <a href="product_special.php?id=<?php echo $row2['ecat_id']; ?>&type=end-category<?php if(isset($_GET['order'])){ echo '&order='.$_GET['order'];}?>&mid=<?php echo $_GET['id'];?>" class=" btn-radius <?php if($row2['ecat_id']===$row2['ecat_id']){ ?>bg-white text-theme-color<?php } ?>"><?php echo $row2['ecat_name']; ?></a>
                <?php } ?>
              
           
                  <?php }
                 
                 foreach ($result2 as $row2) {
                                                    ?>
                                                    <?php if($row2['ecat_id'] != $row2['ecat_id']){ ?>
                <a href="product_special.php?id=<?php echo $row2['ecat_id']; ?>&type=end-category<?php if(isset($_GET['order'])){ echo '&order='.$_GET['order'];}?>&mid=<?php echo $_GET['id'];?>" class=" btn-radius <?php if($row2['ecat_id']===$row2['ecat_id']){ ?>bg-white text-theme-color<?php } ?>"><?php echo $row2['ecat_name']; ?></a>
                <?php } ?>
              
           
                  <?php }
                 
             } 
             if(isset($_GET['mid'])){
                 $statement2 = $pdo->prepare("SELECT * FROM tbl_end_category WHERE mcat_id=?");
                                                            $statement2->execute(array($_GET['mid']));
                                                            $result2 = $statement2->fetchAll(PDO::FETCH_ASSOC);
                                                            foreach ($result2 as $row2) {
                                                    ?>
                                                    <?php if($row2['ecat_id']===$_GET['id']){ ?>
                <a href="product_special.php?id=<?php echo $row2['ecat_id']; ?>&type=end-category<?php if(isset($_GET['order'])){ echo '&order='.$_GET['order'];}?>&mid=<?php echo $_GET['mid'];?>" class=" btn-radius col-auto <?php if($row2['ecat_id']===$_GET['id']){ ?>bg-theme-color text-white<?php }else{ ?>bg-white text-theme-color<?php } ?>"><?php echo $row2['ecat_name']; ?></a>
              
           
                  <?php
                  }
                  }
                   foreach ($result2 as $row2) {
                                                    ?>
                                                    <?php if($row2['ecat_id'] != $_GET['id']){ ?>
                <a href="product_special.php?id=<?php echo $row2['ecat_id']; ?>&type=end-category<?php if(isset($_GET['order'])){ echo '&order='.$_GET['order'];}?>&mid=<?php echo $_GET['mid'];?>" class="col-auto btn-radius <?php if($row2['ecat_id']===$_GET['id']){ ?>bg-theme-color text-white<?php }else{ ?>bg-white text-theme-color<?php } ?>"><?php echo $row2['ecat_name']; ?></a>
              
           
                  <?php
                  }
                  }
                  }
                  ?>
                  
            
        </div>
    </div>
    </div>
		</section>


<section class="shop-list section-padding">
    <div class="container">
        <div class="row">
      <div class="col-sm-12">
        <div class="shop-head">
    							<div class="btn-group float-right mt-2">
    								<div class="col-4 col-sm-4 col-md-4 text-right">
                <label class="control-label" for="input-sort">Sort By:</label>
              </div>
              <div class="col-8 col-sm-8 col-md-8">
                  <form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                      <input type="hidden" name="id" value="<?php echo $_GET['id'];?>">
                      <input type="hidden" name="type" value="<?php echo $_GET['type'];?>">
                <select onchange="this.form.submit();" name="order">
                  <option value="default" <?php if(isset($_GET['order'])){if($_GET['order']=='default'){ echo 'selected';}}?>>Default</option>
                  <option value="ASC_a_to_z" <?php if(isset($_GET['order'])){if($_GET['order']=='ASC_a_to_z'){ echo 'selected';}}?>>Name (A - Z)</option>
                  <option value="ASC_z_to_a" <?php if(isset($_GET['order'])){if($_GET['order']=='ASC_z_to_a'){ echo 'selected';}}?>>Name (Z - A)</option>
                  <option value="ASC_price_low" <?php if(isset($_GET['order'])){if($_GET['order']=='ASC_price_low'){ echo 'selected';}}?>>Price (Low &gt; High)</option>
                  <option value="ASC_price_high" <?php if(isset($_GET['order'])){if($_GET['order']=='ASC_price_high'){ echo 'selected';}}?>>Price (High &gt; Low)</option>
                </select>
                </form>
              </div>
    					
                
    							</div>
    						</div>
    						</div>	
      <div class="col-sm-12">
    						
        <div class="row product-layoutrow section-product">
            
    
     <?php
                            // Checking if any product is available or not
                            $prod_count = 0;
                            $statement = $pdo->prepare("SELECT * FROM tbl_product");
                            $statement->execute();
                            $result = $statement->fetchAll(PDO::FETCH_ASSOC);
                            foreach ($result as $row) {
                                $prod_table_ecat_ids[] = $row['ecat_id'];
                            }
    
                            for($ii=0;$ii<count($final_ecat_ids);$ii++):
                                if(in_array($final_ecat_ids[$ii],$prod_table_ecat_ids)) {
                                    $prod_count++;
                                }
                            endfor;
    
                            if($prod_count==0) {
                                echo '<div class="pl_15">'.LANG_VALUE_153.'</div>';
                            } else {
                                for($ii=0;$ii<count($final_ecat_ids);$ii++) {
                                    if(isset($_GET['order'])){
                                    if($_GET['order']=='default'){
                                        $statement = $pdo->prepare("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? ");
                                    }
                                    if($_GET['order']=='ASC_a_to_z'){
                                        $statement = $pdo->prepare("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? order by p_name asc ");
                                    }
                                    if($_GET['order']=='ASC_z_to_a'){
                                        $statement = $pdo->prepare("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? order by p_name desc ");
                                    }
                                    if($_GET['order']=='ASC_price_low'){
                                        $statement = $pdo->prepare("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? order by p_current_price asc ");
                                    }
                                    if($_GET['order']=='ASC_price_high'){
                                        $statement = $pdo->prepare("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? order by p_current_price desc");
                                    }
                                    }
                                    if(!isset($_GET['order'])){
                                        $statement = $pdo->prepare("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? ");
                                    }
                                    
                                    
                                    
                                    $statement->execute(array($final_ecat_ids[$ii],0));
                                    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
                                    foreach ($result as $row) {
                            $statement2 = $pdo->prepare("SELECT * FROM tbl_product_size WHERE p_id=?");
                            $statement2->execute(array($row['p_id']));
                            $result2 = $statement2->fetchAll(PDO::FETCH_ASSOC);                            
                            foreach ($result2 as $row2) {
                                    $statement3 = $pdo->prepare("SELECT * FROM tbl_size WHERE size_id=?");
                                    $statement3->execute(array($row2['size_id']));
                                    $result3 = $statement3->fetchAll(PDO::FETCH_ASSOC);         
                                    foreach ($result3 as $row3) {
                                         $row3['size_name'];
                                    }
                            }
                                ?>
                    <div class="col-6 col-sm-6 col-md-4 col-lg-3 col-xl-2 mt-5  mb-10 item">
                      <div class="product">
                          <a href="product_view.php?id=<?php echo $row['p_id']; ?>">
                            <div class="product-header">
                                <?php if( $row['p_old_price']!='' &&  $row['p_old_price']!=$row['p_current_price']) { $c= $row['p_old_price']-$row['p_current_price']; $d=$c/$row['p_old_price']; ?>
                                    <!--<span class="badge badge-success" <?php echo 'id="offer_'.$row['p_id'].'"'; ?> >1 % OFF</span>-->
                                <?php }?>
                                <span class="badge badge-success" <?php echo 'id="offer_'.$row['p_id'].'"'; ?> >1 % OFF</span>
                               
                               <img class="img-fluid" id="pic_<?php echo $row['p_id']; ?>" src="assets/uploads/<?php echo  $row['p_featured_photo']; ?>" alt="<?php echo $row['p_name']; ?>">
                               <span class="veg text-success mdi mdi-circle"></span>
                               <!--<span class="non-veg text-danger mdi mdi-circle"></span>-->
                            </div>
                            <div class="product-body">
                               <h5 class="text-inline"><?php echo $row['p_name']; ?></h5>
                               <h6><strong><span class="mdi mdi-approval"></span> Available in</strong> - </h6>
                            </div>
                            </a>
                            <div class="product-footer">
                                <p>
                                    <select id="size_<?php echo $row['p_id']; ?>" onchange="Myvalueput(<?php echo $row['p_id']; ?>)" class="form-control form_value" id2="<?php echo $row['p_id']; ?>">
                                                  
                                    <?php
                                                    $statement2 = $pdo->prepare("SELECT * FROM tbl_product_size WHERE p_id=?");
                                                    $statement2->execute(array($row['p_id']));
                                                    $result2 = $statement2->fetchAll(PDO::FETCH_ASSOC);  
                                                    $i=1;
                                                    
                                                    foreach ($result2 as $row2) {
                                                            $statement3 = $pdo->prepare("SELECT * FROM tbl_size WHERE size_id=?");
                                                            $statement3->execute(array($row2['size_id']));
                                                            $result3 = $statement3->fetchAll(PDO::FETCH_ASSOC);         
                                                            foreach ($result3 as $row3) {
                                                                if($i==1){
                                                                    $p_old_price = $row2['p_old_price'];
                                                                    $p_current_price = $row2['p_current_price'];
                                                                    $p_qty = $row2['p_qty'];
                                                                    $size_id = $row3['size_id'];
                                                                    $size_name = $row3['size_name'];
                                                                }
                                                                $i++;
                                                                ?>
                                                                 <option 
                                                                 value='<?php echo $row2['size_id']?>,<?php echo $row2['p_old_price']?>,<?php echo $row2['p_current_price']?>,<?php echo $row2['p_qty']?>,<?php echo $row3['size_name'];?>,<?php echo $row2['p_featured_photo'];?>,<?php echo $row['p_featured_photo'];?>'
                                                                 
                                                                 <?php 
                                                                 for($i3=1;$i3<=count($arr_cart_size_id2);$i3++){
                                                                 if($arr_cart_size_id2[$i3]==$row3['size_id']){ echo 'selected';}
                                                                 }
                                                                 
                                                                 ?>
                                                                 
                                                                 >
                                                                     <?php echo $row3['size_name'];?>
                                                                </option>
                                                           <?php
                                                            }
                                                    }
                                                        
                                                    ?>
                                                    </select>
                                </p>
                               <p class="offer-price mb-0">
                                   &#8377;<b id="price_c_<?php echo $row['p_id']; ?>"><?php  echo $p_current_price; ?></b>
                                   <i class="mdi mdi-tag-outline"></i> 
                                   <?php if($p_old_price!='' && $p_old_price!=$p_current_price){?><span class="regular-price">&#8377;<b id="price_o_<?php echo $row['p_id']; ?>"><?php echo $p_old_price; ?></b></span><?php } ?>
                               
                               </p>
                                           
                                 
                                     
                                           <!--advanced feature start-->
                                           <?php 
                                           if($p_qty >= 1){
                                           ?>
                                            <input type="hidden" name="p_current_price" id="p_current_price_<?php echo $row['p_id']; ?>" value="<?php echo $p_current_price; ?>">
                                            <input type="hidden" name="p_old_price" id="p_old_price_<?php echo $row['p_id']; ?>" value="<?php echo $p_old_price; ?>">
                                              <input type="hidden" name="p_name" id="p_name_<?php echo $row['p_id']; ?>" value="<?php echo $row['p_name']; ?>">
                                              <input type="hidden" name="size_id" id="size_id_<?php echo $row['p_id']; ?>" value="<?php echo $size_id; ?>">
                                              <input type="hidden" name="size_name" id="size_name_<?php echo $row['p_id']; ?>" value="<?php echo $size_name; ?>">
                                              <input type="hidden" name="p_featured_photo" id="p_featured_photo_<?php echo $row['p_id']; ?>" value="<?php echo  $row['p_featured_photo']; ?>">
                                              
                                       
                                              
                                           <?php  
                                              for($i3=1;$i3<=count($arr_cart_p_id2);$i3++){
                                                  if($arr_cart_p_id2[$i3]==$row['p_id']){
                                                     ?>
                                                    
                                             <div class="col-xs-12 col-sm-12 input-group" id="hide_A<?php echo $row['p_id']; ?>">
                                              <button class="btn btn-md input-group-btn btn-danger" onclick="minus(<?php echo $row['p_id']; ?>)">-</button>
                                              <input type="text" name="quantity" class="form-control input-type text-center" qty_id="<?php echo $i3;?>" id="qty_<?php echo $row['p_id']; ?>"
                                                 qty="<?php echo $row['p_qty']; ?>" readonly value="<?php echo $arr_cart_p_qty2[$i3];?>" >
                                              <button class="btn btn-md input-group-btn btn-danger" onclick="plus(<?php echo $row['p_id']; ?>)">+</button>
                                           </div>
                                           
                                           <input id="qty2_<?php echo $row['p_id']; ?>"  qty="<?php echo $row['p_qty']; ?>" type="hidden" value="1"/>
                                           <button type="button" id="hide_B<?php echo $row['p_id']; ?>" style="display:none" class="btn  btn-secondary btn-sm" onclick="add(<?php echo $row['p_id']; ?>)"><i class="mdi mdi-cart-outline"></i> Add To Cart</button>
                                           <?php
                                              } else{ if(empty($f)){ $f=0; } $f=$f+1;} } 
                                              if($f < count($arr_cart_p_id2)){ $f=0; }
                                              if(count($arr_cart_p_id2) == $f){ $f=0;
                                               ?>
                                                <div class="col-xs-12 col-sm-12 input-group" id="hide_A<?php echo $row['p_id']; ?>" style="display:none">
                                                    <button class="btn btn-md input-group-btn btn-danger" onclick="minus(<?php echo $row['p_id']; ?>)">-</button>
                                                    <input type="text" readonly name="quantity" class="form-control input-type text-center " qty_id="<?php echo $i3;?>" id="qty_<?php echo $row['p_id']; ?>"
                                                     qty="<?php echo $row['p_qty']; ?>"  value="1" >
                                                    <button class="btn btn-md input-group-btn btn-danger " onclick="plus(<?php echo $row['p_id']; ?>)">+</button>
                                                </div>
                                           <input id="qty2_<?php echo $row['p_id']; ?>"  qty="<?php echo $row['p_qty']; ?>" type="hidden" value="1" />
                                           <button type="button" id="hide_B<?php echo $row['p_id']; ?>" class="btn btn-secondary btn-sm" onclick="add(<?php echo $row['p_id']; ?>)"><i class="mdi mdi-cart-outline"></i> Add To Cart</button>
                                        <?php  }  }
                                        elseif($p_qty <= 0){ ?>
                                        <button type="button" class="btn btn-secondary btn-sm"><i class="mdi mdi-cart-outline"></i> Out Of Stock</button>
                                        <?php } ?>
                            </div>
                      </div>
                   </div>
                    <?php } 
                         }
                         }
                         ?>
    
    
        </div>
        
      </div>
    </div>
    </div>
</section>
<?php include('footer.php');?>