<?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;
    background: #fff;
}
.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']);
            }
        }
        
    }   
}
?>
	<!-- start main -->
	<section class="single-banner d-none d-sm-none d-md-block d-lg-block">
		<div class="container">
			<h2><?php echo LANG_VALUE_51; ?> <?php echo $title; ?></h2>
			<ol class="breadcrumb">
				<li class="breadcrumb-item"><a href="<?=base_url();?>">Home</a></li>
				<li class="breadcrumb-item active" aria-current="page">
					<?php echo LANG_VALUE_51; ?> <?php echo $title; ?>
				</li>
			</ol>
		</div>
	</section>
    <section class="shop-part mb-5">
			<div class="container">
			<div class="row">
        <div class="scroll d-md-none d-lg-none ">
             <?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-6 col-12">
                    <div class="float-left w-100 my-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 w-100" placeholder="search by keyword">
                        </form>
                    </div>
                </div>
                <div class="col-sm-6 col-12">
                    <div class="w-100">
                      <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'];?>">
                            <div class="input-group my-2">
                              <span class="input-group-text">Short by :</span>
                              <select onchange="this.form.submit();" class="form-select" 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="FEATURED" <?php if(isset($_GET['order'])){if($_GET['order']=='FEATURED'){ echo 'selected';}}?>>Featured</option>
                                <option value="TODAY_DEAL" <?php if(isset($_GET['order'])){if($_GET['order']=='TODAY_DEAL'){ echo 'selected';}}?>>Today's Deal</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>
                        </div>
                        </form>
                </div>
                </div>
            </div>
        <div class="row">
      <div class="col-sm-12">
        <div class="row row-cols-2 row-cols-md-3 row-cols-lg-3 row-cols-xl-4" 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']=='FEATURED'){
                                        $statement = $this->db->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? AND p_is_featured=? order by p_id desc ",array($final_ecat_ids[$ii],1,1));
                                    }
                                    if($_GET['order']=='TODAY_DEAL'){
                                        $statement = $this->db->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=?  AND todays_best_deal=? order by p_id desc ",array($final_ecat_ids[$ii],1,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 mt-5  mb-10 item" loading="lazy" id="pid_2_<?=$row['p_id']; ?>">
            							<div class="product-card" id="out_of_stock_<?=$row['p_id']; ?>">
            								<div class="product-media">
            									<div class="product-label">
            										<label class="label-text new">feature</label>
            									</div>
            									<div class="product-label">
            										<label class="label-text new"><span class="badge badge-success" <?='id="offer_' .$row[ 'p_id']. '"'; ?> >0 % OFF</span></label>
            									</div>
            									<button class="product-wish wish"><i class="fas fa-heart"></i></button> <a class="product-image" href="<?=base_url('product_view');?>?id=<?=$row['p_id']; ?>">
            							    <img 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']; ?>" />
            							</a>
            									<!--<div class="product-widget"><a title="Product Compare" href="compare.php" class="fas fa-random"></a><a title="Product Video" href="https://youtu.be/9xzcVxSBbG8" class="venobox fas fa-play" data-autoplay="true" data-vbtype="video"></a><a title="Product View" href="#" class="fas fa-eye" data-bs-toggle="modal" data-bs-target="#product-view"></a></div>--></div>
            								<div class="product-content">
            									<!--<div class="product-rating"><i class="active icofont-star"></i><i class="active icofont-star"></i><i class="active icofont-star"></i><i class="active icofont-star"></i><i class="icofont-star"></i><a href="product-video.php">(3)</a></div>-->
            									<h6 class="product-name"><a href="<?=base_url('product_view');?>?id=<?=$row['p_id']; ?>"><?=$row['p_name']; ?></a></h6>
            									<?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'];
                                                    $color_id = $row2['color_id'];
                                                    $p_old_price = $row2['p_old_price'];
                                                    $p_current_price = $row2['p_current_price'];
                                                    $p_qty = $row2['p_qty'];
                                                    
                                                    $query4 = $this->db->query("select * from tbl_color where color_id = $color_id ");
                                                    if($query4->num_rows() > 0){
                                                        foreach ($query4->result_array() as $row4) {
                                                            $color_name = $row4['color_name'];
                                                        }
                                                    }
                                                            
                                                    $query3 = $this->db->query("select * from tbl_size where size_id = $size_id ");
                                                    if($query3->num_rows() > 0){
                                                        foreach ($query3->result_array() as $row3) {
                                                                $size_name = $row3['size_name'];
                                                        }
                                                    }
                                                    if($i==1){
                                                        ?>
                                                    <input type="hidden" id="p_current_price_<?=$row['p_id']; ?>" value="<?=$p_current_price; ?>">
                                                    <input type="hidden" id="p_old_price_<?=$row['p_id']; ?>" value="<?=$p_old_price; ?>">
                                                    <input type="hidden" id="p_name_<?=$row['p_id']; ?>" value="<?=$row['p_name']; ?>">
                                                    <input type="hidden" id="size_id_<?=$row['p_id']; ?>" value="<?=$size_id; ?>">
                                                    <input type="hidden" id="color_id_<?=$row['p_id']; ?>" value="<?=$color_id; ?>">
                                                    <input type="hidden" id="color_name_<?=$row['p_id']; ?>" value="<?=$color_name; ?>">
                                                    <input type="hidden" id="size_name_<?=$row['p_id']; ?>" value="<?=$size_name; ?>">
                                                    <input type="hidden" id="p_featured_photo_<?=$row['p_id']; ?>" value="<?= $row['p_featured_photo']; ?>">
            										<div class="input-group">
            											<input id="size2_<?=$row['p_id']; ?>" id2="<?=$row['p_id']; ?>"
            											onclick="check_size(<?=$row['p_id']; ?>);" class="form-control px-2 input-check-size" 
            											value="<?=$row3['size_name'];?> (<?=$color_name;?>)" readonly>
            											<div class="input-group-addon p-2 check-size-btn"> <i class="fa fa-bars"></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']; ?>,<?=$color_id; ?>,<?=$color_name; ?>" id2="<?=$row['p_id']; ?>" />
            										<?php
            										
                                                        }
                                                        $i++;
                                                    }
                                                    ?>
            							<h6 class="product-price">
            								<del><?php if($p_old_price!='' && $p_old_price!=$p_current_price){?><span class="regular-price"><?=$data['currency'];?><b id="price_o_<?=$row['p_id']; ?>"><?=$p_old_price; ?></b></span><?php } ?></del>
            								<span><b id="price_c_<?=$row['p_id']; ?>"><?=$data['currency'];?><?php  echo $p_current_price; ?></b></span>
            							</h6>
            							<!--Product Size start-->
                                            <?php
                                                        $count=0; 
                                                        $query21 = $this->db->query("select * from tbl_product_size where p_id = $pid");
                                                        foreach ($query21->result_array() as $row2) {
                                                            $primary_id=0; $count++; $primary_id = $row['p_id'].$row2['size_id'].$row2['color_id'];
                                                            
                                                            if($row2['p_qty'] != 0){
                                                        ?>
                                                        
                                                            <div class="product-action" id2="yes1" id3="<?=$count;?>" id="hide_A<?=$primary_id; ?>" style="display:none">
                                                                <button class="action-minus" title="Quantity Minus" onclick="minus(<?=$row['p_id'].','.$primary_id; ?>)"><i class="icofont-minus"></i></button>
                                                                <input class="action-input" title="Quantity Number" readonly name="quantity" qty_id="<?=$l;?>" id="qty_<?=$primary_id; ?>" qty="<?=$row2['p_qty']; ?>" value="1">
                                                                <button class="action-plus" title="Quantity Plus" onclick="plus(<?=$row['p_id'].','.$primary_id; ?>)"><i class="icofont-plus"></i></button>
                                                            </div>
                                                            <input id="qty2_<?=$primary_id; ?>" qty="<?=$row2['p_qty']; ?>" type="hidden" value="1" />
                                                            <button class="product-add" title="Add to Cart" id2="no1" id3="<?=$count;?>" id="hide_B<?=$primary_id; ?>" onclick="add(<?=$row['p_id'].','.$primary_id; ?>)"><i class="fas fa-shopping-basket"></i><span>add to cart</span></button>
            										<?php
                                                            }
                                                            else if($row2['p_qty'] == 0 || $row2['p_qty'] < 0)
                                                            {
                                                    ?>
                                                            <button class="product-add bg-warning" title="Out Of Stock" id2="no1" id3="<?=$count;?>" id="hide_B<?=$primary_id; ?>" onclick="sweetalert('warning','Out Of Stock')"><i class="fas fa-shopping-basket"></i><span>Out Of Stock</span></button>
            								            <?php
                                                            }
                                                        }
                                                        ?>
            									<!--Product Size end-->
            								</div>
            							</div>
            						</div>
            						<?php } 
                                    }
                        }
                         }
                         ?>
    
    
        </div>
        
      </div>
    </div>
    </div>
</section>
<?php include('footer.php');?>