<?php  include('header.php');

if(isset($arr_cart_p_id2)){$arr_cart_p_id2=$arr_cart_p_id2;}else{$arr_cart_p_id2=array();}
if(isset($arr_cart_size_id2)){$arr_cart_size_id2=$arr_cart_size_id2;}else{$arr_cart_size_id2=array();}
$f=0;
?>
<style>
    .scroll {
    overflow: scroll;
    width: 100%;
    display: flex;
}
.btn-radius {
    margin: 5px;
}
::-webkit-scrollbar {
    -webkit-appearance: none;
}

.bg-light ::-webkit-scrollbar:vertical {
    width: 0px;
}

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

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

::-webkit-scrollbar-track {
    background-color: #ffffff; 
}
</style>

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

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

        $statement = $this->db->query("SELECT * FROM tbl_top_category");
        $result = $statement->result_array();                            
        foreach ($result as $row) {
            $top[] = $row['tcat_id'];
            $top1[] = $row['tcat_name'];
        }

        $statement = $this->db->query("SELECT * FROM tbl_mid_category");
        $result = $statement->result_array();                            
        foreach ($result as $row) {
            $mid[] = $row['mcat_id'];
            $mid1[] = $row['mcat_name'];
            $mid2[] = $row['tcat_id'];
        }

        $statement = $this->db->query("SELECT * FROM tbl_end_category");
        $result = $statement->result_array();                            
        foreach ($result as $row) {
            $end[] = $row['ecat_id'];
            $end1[] = $row['ecat_name'];
            $end2[] = $row['mcat_id'];
        }

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

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

        if($_GET['type'] == 'end-category') {
            if(!in_array($_GET['id'],$end)) {
                header('location: index.php');
                exit;
            } else {
                // Getting Title
                for ($i=0; $i < count($end); $i++) { 
                    if($end[$i] == $_GET['id']) {
                        $title = $end1[$i];
                        break;
                    }
                }
                $final_ecat_ids = array($_GET['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['mid'])){
                 $statement2 = $this->db->query("SELECT * FROM tbl_end_category WHERE mcat_id=?",array($_GET['mid']));
                                                            $result2 = $statement2->result_array();
                                                            foreach ($result2 as $row2) {
                                                    ?>
                                                    <?php if($row2['ecat_id']===$_GET['id']){ ?>
                <a href="<?=base_url('product_special');?>?id=<?php echo $row2['ecat_id']; ?>&type=end-category<?php if(isset($_GET['order'])){ echo '&order='.$_GET['order'];}?>&mid=<?php echo (isset($_GET['mid'])) ? $_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="<?=base_url('product_special');?>?id=<?php echo $row2['ecat_id']; ?>&type=end-category<?php if(isset($_GET['order'])){ echo '&order='.$_GET['order'];}?>&mid=<?php echo (isset($_GET['mid'])) ? $_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-left mt-2">
                <form method="GET" action="<?=base_url('product_special');?>">
                    <input type="hidden" id="page" value="product_special">
                    <input type="hidden" id="id" value="<?php echo $_GET['id'];?>">
                    <input type="hidden" id="mid" value="<?php echo (isset($_GET['mid'])) ? $_GET['mid']  : '';?>">
                    <input type="hidden" id="type" value="<?php echo $_GET['type'];?>">
                    <input type="hidden" id="order" value="<?php if(isset($_GET['order'])){ echo $_GET['order'];}?>">
                    <input type="text" id="search_item" class="form-control" placeholder="search">
                </form>
            </div>
    							
    							    <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="<?=base_url('product_special');?>">
                      <input type="hidden" name="id" value="<?php echo $_GET['id'];?>">
                      <input type="hidden" name="mid" value="<?php echo (isset($_GET['mid'])) ? $_GET['mid']  : '';?>">
                      <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</option>
                  <option value="DESC" <?php if(isset($_GET['order'])){if($_GET['order']=='DESC'){ echo 'selected';}}?>>Newest</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" id="search_result">
            
    
     <?php
                            // Checking if any product is available or not
                            $prod_count = 0;
                            $prod_table_ecat_ids[] = 0;
                            $statement = $this->db->query("SELECT * FROM tbl_product");
                            $result = $statement->result_array();
                            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) {
                            ?>
                            <div class="col-md-12 text-center">
                                <img src="<?=base_url('uploads/no_product_found.png');?>" class="img-fluid" />
                            </div>
                            <?php
                        } else {
                                for($ii=0;$ii<count($final_ecat_ids);$ii++) {
                                    if(isset($_GET['order'])){
                                    if($_GET['order']=='default'){
                                        $statement = $this->db->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? ",array($final_ecat_ids[$ii],1));
                                    }
                                    if($_GET['order']=='ASC_a_to_z'){
                                        $statement = $this->db->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? order by p_name asc ",array($final_ecat_ids[$ii],1));
                                    }
                                    if($_GET['order']=='DESC'){
                                        $statement = $this->db->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? order by p_id desc ",array($final_ecat_ids[$ii],1));
                                    }
                                    if($_GET['order']=='ASC_z_to_a'){
                                        $statement = $this->db->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? order by p_name desc ",array($final_ecat_ids[$ii],1));
                                    }
                                    if($_GET['order']=='ASC_price_low'){
                                        $statement = $this->db->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? order by p_current_price asc ",array($final_ecat_ids[$ii],1));
                                    }
                                    if($_GET['order']=='ASC_price_high'){
                                        $statement = $this->db->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? order by p_current_price desc",array($final_ecat_ids[$ii],1));
                                    }
                                    }
                                    if(!isset($_GET['order'])){
                                        $statement = $this->db->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? ",array($final_ecat_ids[$ii],1));
                                    }
                                    
                                    $result = $statement->result_array();
                                    if(count($result)){
                                        foreach ($result as $row) {
                    ?>
                        <div class="col-6 col-sm-6 col-md-4 col-lg-3 col-xl-2 mt-5  mb-10 item" loading="lazy" id="pid_2_<?=$row['p_id']; ?>">
                          <div class="product">
                              <a href="<?=base_url('product_view');?>?id=<?=$row['p_id']; ?>">
                                <div class="product-header">
                                    <span class="badge badge-success" <?='id="offer_'.$row['p_id'].'"'; ?> >0 % OFF</span>
                                   
                                   <img class="img-fluid" id="pic_<?=$row['p_id']; ?>" src="<?=base_url('uploads/shop/product/'.$row['p_featured_photo']); ?>" alt="<?=$row['p_name']; ?>">
                                   <input type="hidden" id="pic2_<?=$row['p_id']; ?>" value="<?= $row['p_featured_photo']; ?>" />
                                   <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"><?=$row['p_name']; ?></h5>
                                   <h6><strong><span class="mdi mdi-approval"></span> Available in</strong> - </h6>
                                </div>
                                </a>
                                <div class="product-footer">
                                    <p>
                                    <?php
                                        $i=1;
                                        $pid=$row['p_id'];
                                        $query_product_size = $this->db->query("select * from tbl_product_size where p_id = $pid");
                                        foreach ($query_product_size->result_array() as $row2) {
                                            $size_id = $row2['size_id'];
                                            $query3 = $this->db->query("select * from tbl_size where size_id = $size_id ");
                                            if($query3->num_rows() > 0){
                                            foreach ($query3->result_array() 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'];
                                                ?>
                                                    <div class="input-group">
                                                      <input id="size2_<?=$row['p_id']; ?>"  id2="<?=$row['p_id']; ?>" onclick="check_size(<?=$row['p_id']; ?>);" class="form-control input-sm" value='<?=$row3['size_name'];?>' readonly>
                                                      <div class="input-group-addon" style="background: #ddd;padding: 5px;">
                                                        <i class="mdi mdi-view-module"></i>
                                                      </div>
                                                    </div>
                                                    
                                                    <input type="hidden" 
                                                    id="size_<?=$row['p_id']; ?>"
                                                    class="form_value" 
                                                    value="<?=$size_id; ?>,<?=$p_old_price;?>,<?=$p_current_price;?>,<?=$p_qty;?>,<?=$size_name;?>,<?=$row['p_id']; ?>"
                                                    id2="<?=$row['p_id']; ?>" />
                                           <?php
                                                }
                                                
                                                $i++;
                                            }
                                            }
                                            else{
                                                ?>
                                                <script>
                                                    $('#pid_2_<?=$row['p_id']; ?>').remove();
                                                    check_results_2();
                                                </script>
                                                <?php
                                            }
                                        }
                                    ?>
                                    </p>
                                   <p class="offer-price mb-0">
                                       &#8377;<b id="price_c_<?=$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_<?=$row['p_id']; ?>"><?=$p_old_price; ?></b></span><?php } ?>
                                   
                                   </p>
                                        <!--Product Size start-->
                                    <?php 
                                        if($p_qty >= 1){
                                    ?>
                                        <input type="hidden" name="p_current_price" id="p_current_price_<?=$row['p_id']; ?>" value="<?=$p_current_price; ?>">
                                        <input type="hidden" name="p_old_price" id="p_old_price_<?=$row['p_id']; ?>" value="<?=$p_old_price; ?>">
                                        <input type="hidden" name="p_name" id="p_name_<?=$row['p_id']; ?>" value="<?=$row['p_name']; ?>">
                                        <input type="hidden" name="size_id" id="size_id_<?=$row['p_id']; ?>" value="<?=$size_id; ?>">
                                        <input type="hidden" name="size_name" id="size_name_<?=$row['p_id']; ?>" value="<?=$size_name; ?>">
                                        <input type="hidden" name="p_featured_photo" id="p_featured_photo_<?=$row['p_id']; ?>" value="<?= $row['p_featured_photo']; ?>">
                                        
                                        <?php  
                                            $count=0; 
                                            $query21 = $this->db->query("select * from tbl_product_size where p_id = $pid");
                                            foreach ($query21->result_array() as $row2) {
                                                $pid_search=0;
                                                $size_id_search=0;
                                                $primary_id=0;
                                                $ok=0;
                                                $count++;
                                                
                                                for($l=1;$l<=count($arr_cart_p_id2);$l++){
                                                    $size_id_search = array_search($row2['size_id'],$arr_cart_size_id2);
                                                    if($row['p_id'] == $arr_cart_p_id2[$l] && $row2['size_id'] == $arr_cart_size_id2[$l]){
                                                        $size_id_search=$l;
                                                        $ok=1;
                                                        break;
                                                    }
                                                }
                                                $primary_id = $row['p_id'].$row2['size_id'];
                                                if($ok){
                                            ?>
                                                <div class="col-xs-12 col-sm-12 input-group" id2="yes" id3="<?=$count;?>" id="hide_A<?=$primary_id; ?>">
                                                    <button class="btn btn-md input-group-btn bg-theme-color" onclick="minus(<?=$row['p_id'].','.$primary_id; ?>)">-</button>
                                                    <input type="text" name="quantity" class="form-control input-type text-center" qty_id="<?=$l;?>" id="qty_<?=$primary_id; ?>"
                                                    qty="<?=$row2['p_qty']; ?>" readonly value="<?=$arr_cart_p_qty2[$l];?>" >
                                                    <button class="btn btn-md input-group-btn bg-theme-color" onclick="plus(<?=$row['p_id'].','.$primary_id; ?>)">+</button>
                                                </div>
                                                
                                                <input id="qty2_<?=$primary_id; ?>"  qty="<?=$row2['p_qty']; ?>" type="hidden" value="1"/>
                                                <button type="button" id2="no" id3="<?=$count;?>" id="hide_B<?=$primary_id; ?>" style="display:none" class="btn  btn-secondary btn-sm" onclick="add(<?=$row['p_id'].','.$primary_id; ?>)"><i class="mdi mdi-cart-outline"></i> Add To Cart</button>
                                            <?php
                                                } else {
                                            ?>
                                                <div class="col-xs-12 col-sm-12 input-group" id2="yes1" id3="<?=$count;?>" id="hide_A<?=$primary_id; ?>" style="display:none">
                                                <button class="btn btn-md input-group-btn bg-theme-color" onclick="minus(<?=$row['p_id'].','.$primary_id; ?>)">-</button>
                                                <input type="text" readonly name="quantity" class="form-control input-type text-center " qty_id="<?=$l;?>" id="qty_<?=$primary_id; ?>"
                                                qty="<?=$row2['p_qty']; ?>"  value="1" >
                                                <button class="btn btn-md input-group-btn bg-theme-color " onclick="plus(<?=$row['p_id'].','.$primary_id; ?>)">+</button>
                                                </div>
                                                <input id="qty2_<?=$primary_id; ?>"  qty="<?=$row2['p_qty']; ?>" type="hidden" value="1" />
                                                <button type="button" id2="no1" id3="<?=$count;?>" id="hide_B<?=$primary_id; ?>" class="btn btn-secondary btn-sm" onclick="add(<?=$row['p_id'].','.$primary_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 } ?>
                                        <!--Product Size end-->
                                </div>
                          </div>
                       </div>
                        <?php } 
                                    } else {
                                        ?>
                                        <div class="col-md-12 text-center">
                                            <img src="<?=base_url('uploads/no_product_found.png');?>" class="img-fluid" />
                                        </div>
                                        <?php
                                    }
                        }
                         }
                         ?>
    
    
        </div>
        
      </div>
    </div>
    </div>
</section>
<?php include('footer.php');?>