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

<?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;

if(!isset($_GET['search_text'])) {
    header('location: index.php');
    exit;
} else {
	if($_GET['search_text']=='') {
		header('location: index.php');
    	exit;
	}
}
?>

            <?php 
                $search_text = strip_tags($_GET['search_text']); 
                $search_text1=  $search_text; 
            ?>            
   


                        <?php
                            $search_text = '%'.$search_text.'%';
                        ?>
	<!-- start main -->
	<section class="inner-section single-banner">
		<div class="container">
			<h2><?php echo LANG_VALUE_51; ?> "<?php echo $search_text1; ?>"</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 $search_text1; ?>"
				</li>
			</ol>
		</div>
	</section>
    <section class="inner-section shop-part">
        <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('search_result');?>">
                            <input type="hidden" id="page" value="search_result">
                            <input type="hidden" id="id" value="<?php echo (isset($_GET['id'])) ? $_GET['id']  : '';?>">
                            <input type="hidden" id="mid" value="<?php echo (isset($_GET['mid'])) ? $_GET['mid']  : '';?>">
                            <input type="hidden" id="type" value="<?php echo (isset($_GET['type'])) ? $_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('search_result');?>">
                            <input type="hidden" name="search_text" value="<?php echo $search_text1;?>">
                            <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 row-cols-2 row-cols-md-3 row-cols-lg-3 row-cols-xl-4" id="search_result">
            			    <?php
            			    if(isset($_GET['order'])){
                                            if($_GET['order']=='default'){
                                                $statement = $this->db->query("SELECT * FROM tbl_product WHERE SOUNDEX(p_name) LIKE CONCAT(TRIM(TRAILING '0' FROM SOUNDEX('$search_text')), '%') AND p_is_active=1 limit 12");
                                            }
                                            if($_GET['order']=='ASC_a_to_z'){
                                                $statement = $this->db->query("SELECT * FROM tbl_product WHERE `p_name` LIKE ? AND p_is_active=? order by p_name asc ",array($search_text,1));
                                            }
                                            if($_GET['order']=='DESC'){
                                                $statement = $this->db->query("SELECT * FROM tbl_product WHERE `p_name` LIKE ? AND p_is_active=? order by p_id desc ",array($search_text,1));
                                            }
                                            if($_GET['order']=='ASC_z_to_a'){
                                                $statement = $this->db->query("SELECT * FROM tbl_product WHERE `p_name` LIKE ? AND p_is_active=? order by p_name desc ",array($search_text,1));
                                            }
                                            if($_GET['order']=='FEATURED'){
                                                $statement = $this->db->query("SELECT * FROM tbl_product WHERE `p_name` LIKE ? AND p_is_active=? AND p_is_featured=? order by p_name desc ",array($search_text,1,1));
                                            }
                                            if($_GET['order']=='TODAY_DEAL'){
                                                $statement = $this->db->query("SELECT * FROM tbl_product WHERE `p_name` LIKE ? AND p_is_active=? AND todays_best_deal=? order by p_name desc ",array($search_text,1,1));
                                            }
                                            if($_GET['order']=='ASC_price_low'){
                                                $statement = $this->db->query("SELECT * FROM tbl_product WHERE `p_name` LIKE ? AND p_is_active=? order by p_current_price asc ",array($search_text,1));
                                            }
                                            if($_GET['order']=='ASC_price_high'){
                                                $statement = $this->db->query("SELECT * FROM tbl_product WHERE `p_name` LIKE ? AND p_is_active=? order by p_current_price desc",array($search_text,1));
                                            }
                                            }
                                            if(!isset($_GET['order'])){
                                                $statement = $this->db->query("SELECT * FROM tbl_product WHERE `p_name` LIKE ? AND p_is_active=? || `tags` LIKE ? AND p_is_active=? ",array($search_text,1,$search_text,1));
                                            }
                                            
                                          
                                            
                                            $result = $statement->result_array();
                                            if(empty($statement->num_rows())):
                                        
                                                ?>
                                                <div class="col-md-12 text-center">
                                                    <img src="<?=base_url('uploads/no_product_found.png');?>" class="img-fluid" />
                                                </div>
                                                <?php
                                            
                                    else:
                                    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"><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) {
                                            
                                            $p_pv = $row2['p_pv'];
                                            $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'];
                                            
                                            if(empty($color_id)){
                                                $color_id='';
                                                $color_name = '';
                                            }
                                            else
                                            {
                                                $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="p_pv_<?=$row['p_id']; ?>" value="<?=$p_pv; ?>">
                                            <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'];?> <?php if(empty($color_name)){}else{?>(<?=$color_name;?>)<?php } ?>" 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; ?>,<?=$p_pv;?>" 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'];?><?=$p_current_price; ?></b> <b class="pv">(<b id="p_pv2_<?=$row['p_id']; ?>"><?=$p_pv;?></b> PV)</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'];
                                                    ?>
                                                    <button class="product-wish wish wish2" id="wish_hide_A<?=$primary_id; ?>" onclick="add_to_wishlist(<?=$row['p_id'].','.$primary_id; ?>)"><i class="fas fa-heart"></i></button>
                                                <?php
                                                    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 } ?>
                                    <?php
                                    endif;
                                ?>
              </div>
        </div>
    </section>
          

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