<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Get extends CI_Controller
{
    // Session Authoriesed
    public function __construct()
    {
        parent::__construct();

    }

    public function product_stock() {
        if (isset($_POST['p_id']) && isset($_POST['size_id']) && isset($_POST['color_id']))
        {
            $p_id = $_POST['p_id'];
            $size_id = $_POST['size_id'];
            $color_id = $_POST['color_id'];

            // loading common model
            $this->load->model('Common_model');
            $where['p_id'] = $p_id;
            $where['size_id'] = $size_id;
            $where['color_id'] = $color_id;
            $table = 'tbl_product_size';

            // we are getting results from common modal
            $result = $this->Common_model->Result($where, $table);
            
            if(!empty($result)){
                $num_rows = 1;
                $stock = $result['p_qty'];
            }
            else{
                $num_rows = 0;
                $stock = null;
            }
            
            // response for user
            $myObj = new \stdClass();
            $myObj->log = $num_rows;
            $myObj->val = $stock;
            $myJSON = json_encode($myObj);
            echo $myJSON;
        }
    }
    
    
    public function district() {
        if (isset($_POST['dt']))
        {
            $state = $_POST['dt'];

            // loading common model
            $this
                ->load
                ->model('Common_model');
            $column['state'] = $state;
            $table = 'tbl_state';

            // we are getting results from common modal
            $result = $this
                ->Common_model
                ->Result($column, $table);
            $column['state'] = $result['id'];
            $table = 'tbl_district';

            // we are getting results from common modal
            $result4 = $this
                ->Common_model
                ->ResultsWhere2($column, $table);

            echo '<option value="" selected> Select District </option>';
            foreach ($result4 as $row)
            {
                echo '<option value="' . $row['district'] . '">' . $row['district'] . '</option>';
            }
        }
    }

    public function pin_code()
    {
        if (isset($_POST['pin_code']))
        {
            $pin_code = $_POST['pin_code'];

            $statement9 = $this
                ->db
                ->query("SELECT pin FROM `tbl_pin` WHERE `pin` = ? AND `status` = ? ", array(
                $pin_code,
                1
            ));
            $num_rows = count($statement9->result_array());
            foreach ($statement9->row_array() as $stmt_row2)
            {
                $stmt_row = $stmt_row2;
            }

            if ($num_rows === 1)
            {
                $_SESSION['pin_code'] = $stmt_row;
                $_SESSION['add_sess']=1;
            }
            // response for user
            $myObj = new \stdClass();

            $myObj->pin_count = $num_rows;
            $myObj->pin_code = $stmt_row;
            $myObj->bak = $statement9->result_array();
            $myJSON = json_encode($myObj);

            echo $myJSON;

        }
    }

    public function keyword()
    {
        if (isset($_POST['keyword']))
        {
            $KEYWORD = $_POST['keyword'];
            
        
            $statement1 = $this->db->query("SELECT * FROM tbl_product WHERE p_name LIKE '%$KEYWORD%' AND p_is_active=1 OR tags LIKE '%$KEYWORD%' AND p_is_active=1");
            $num_rows1 = $statement1->num_rows();
            $result1 = $statement1->result_array();
            
            $statement9 = $this->db->query("SELECT * FROM tbl_product WHERE SOUNDEX(p_name) LIKE CONCAT(TRIM(TRAILING '0' FROM SOUNDEX('$KEYWORD')), '%') AND p_is_active=1 limit 12");
            $num_rows = $statement9->num_rows();
            $result9 = $statement9->result_array();
            
            
?>
                <ul id="search-list">
            <?php
             foreach ($result1 as $product)
                {
?>
            <li onClick="selectItem('<?php echo $product["p_name"]; ?>');"><a href="<?=base_url('search_result') ?>?search_text=<?php echo $product["p_name"]; ?>"><?php echo $product["p_name"]; ?></a></li>
            <?php
                }
                
             foreach ($result9 as $product)
                {/*
?>
            <li onClick="selectItem('<?php echo $product["p_name"]; ?>');"><a href="<?=base_url('search_result') ?>?search_text=<?php echo $product["p_name"]; ?>"><?php echo $product["p_name"]; ?></a></li>
            <?php
                */}
                
               
                
                
           

?>
            </ul>
            <?php
        }
    }

    public function new ()
    {
        // loading common model
        $this
            ->load
            ->model('Common_model');

        $column['id'] = '1';
        $table = 'tbl_settings';

        // we are getting results from common modal
        $result = $this
            ->Common_model
            ->Result($column, $table);

        $i = 1;
        $result23456789 = $this
            ->Common_model
            ->Results('tbl_language');
        foreach ($result23456789 as $row)
        {
            define('LANG_VALUE_' . $i, $row['lang_value']);
            $i++;
        }

        $column1['status'] = '1';
        $table1 = 'tbl_slider';
        // we are getting results from common modal
        $result1 = $this
            ->Common_model
            ->ResultsWhere2($column1, $table1);

        $column2['status'] = '1';
        $table2 = 'tbl_photo';
        // we are getting results from common modal
        $result2 = $this
            ->Common_model
            ->ResultsWhere2($column2, $table2);

        $columntop_cities['status'] = '1';
        $tabletop_cities = 'tbl_top_cities';
        // we are getting results from common modal
        $resulttop_cities = $this
            ->Common_model
            ->ResultsWhere2($columntop_cities, $tabletop_cities);

        $column3['status'] = '1';
        $column3['status_home'] = '1';
        $table3 = 'tbl_top_category';
        // we are getting results from common modal
        $top_category = $this
            ->Common_model
            ->ResultsWhere2($column3, $table3);

        $column3['status'] = '1';
        $column3['status_home'] = '1';
        $table3 = 'tbl_mid_category';
        // we are getting results from common modal
        $mid_category = $this
            ->Common_model
            ->ResultsWhere2($column3, $table3);

        $column3['status'] = '1';
        $column3['status_home'] = '1';
        $table3 = 'tbl_end_category';
        // we are getting results from common modal
        $end_category = $this
            ->Common_model
            ->ResultsWhere2($column3, $table3);

        $column3['status'] = '1';
        $column3['status_home'] = '1';
        $table3 = 'tbl_top_category';
        // we are getting results from common modal
        $top_category = $this
            ->Common_model
            ->ResultsWhere2($column3, $table3);

        $column32['status'] = '1';
        $table32 = 'tbl_mid_category';
        // we are getting results from common modal
        $data['mid_category2'] = $this
            ->Common_model
            ->ResultsWhere2($column32, $table32);

        $column33['status'] = '1';
        $table33 = 'tbl_end_category';
        // we are getting results from common modal
        $data['end_category2'] = $this
            ->Common_model
            ->ResultsWhere2($column33, $table33);

        $column31['status'] = 1;
        $table31 = 'tbl_top_cities';
        // we are getting results from common modal
        $top_cities = $this
            ->Common_model
            ->ResultsWhere2($column31, $table31);

        $column31['status'] = 1;
        $table31 = 'tbl_social';
        // we are getting results from common modal
        $social = $this
            ->Common_model
            ->ResultsWhere2($column31, $table31);

        // we are setting result in data array as $data
        $data['data'] = $result;
        $data['slider'] = $result1;
        $data['photo'] = $result2;
        $data['top_cities'] = $top_cities;
        $data['top_category'] = $top_category;
        $data['mid_category'] = $mid_category;
        $data['end_category'] = $end_category;
        $data['social'] = $social;
        $data['page'] = 'Speacial Products';

        // we are loading admin dashboard
        $this
            ->load
            ->view('website/new', $data);
    }

    public function product()
    {
        if (isset($_POST['keyword']))
        {
            if($_POST['page']=='product_special'){
                
                if (($_POST['type'] != 'top-category') && ($_POST['type'] != 'mid-category') && ($_POST['type'] != 'end-category'))
                {
                    
                }
                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 ($_POST['type'] == 'top-category')
                    {
                        if (!in_array($_POST['id'], $top))
                        {
                            header('location: index.php');
                            exit;
                        }
                        else
                        {
    
                            // Getting Title
                            for ($i = 0;$i < count($top);$i++)
                            {
                                if ($top[$i] == $_POST['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] == $_POST['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 ($_POST['type'] == 'mid-category')
                    {
                        if (!in_array($_POST['id'], $mid))
                        {
                            header('location: index.php');
                            exit;
                        }
                        else
                        {
                            // Getting Title
                            for ($i = 0;$i < count($mid);$i++)
                            {
                                if ($mid[$i] == $_POST['id'])
                                {
                                    $title = $mid1[$i];
                                    break;
                                }
                            }
                            $arr2 = array();
                            // Find out all ecat ids under this
                            for ($i = 0;$i < count($end);$i++)
                            {
                                if ($end2[$i] == $_POST['id'])
                                {
                                    $arr2[] = $end[$i];
                                }
                            }
                            $final_ecat_ids = $arr2;
                        }
                    }
    
                    if ($_POST['type'] == 'end-category')
                    {
                        if (!in_array($_POST['id'], $end))
                        {
                            header('location: index.php');
                            exit;
                        }
                        else
                        {
                            // Getting Title
                            for ($i = 0;$i < count($end);$i++)
                            {
                                if ($end[$i] == $_POST['id'])
                                {
                                    $title = $end1[$i];
                                    break;
                                }
                            }
                            $final_ecat_ids = array(
                                $_POST['id']
                            );
                        }
                    }
    
                }
    
                // 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;
    
                $KEYWORD = $_POST['keyword'];
    
                if ($prod_count != 0)
                {
    
                    for ($ii = 0;$ii < count($final_ecat_ids);$ii++)
                    {
                        if (isset($_POST['order']) && !empty($_POST['order']))
                        {
                            if ($_POST['order'] == 'default')
                            {
                                $statement = $this
                                    ->db
                                    ->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? AND  `p_name` LIKE ? ", array(
                                    $final_ecat_ids[$ii],
                                    1,
                                    "%$KEYWORD%"
                                ));
                            }
                            if ($_POST['order'] == 'ASC_a_to_z')
                            {
                                $statement = $this
                                    ->db
                                    ->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? AND  `p_name` LIKE ? order by p_name asc ", array(
                                    $final_ecat_ids[$ii],
                                    1,
                                    "%$KEYWORD%"
                                ));
                            }
                            if ($_POST['order'] == 'DESC')
                            {
                                $statement = $this
                                    ->db
                                    ->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? AND  `p_name` LIKE ? order by p_id desc ", array(
                                    $final_ecat_ids[$ii],
                                    1,
                                    "%$KEYWORD%"
                                ));
                            }
                            if ($_POST['order'] == 'ASC_z_to_a')
                            {
                                $statement = $this
                                    ->db
                                    ->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? AND  `p_name` LIKE ? order by p_name desc ", array(
                                    $final_ecat_ids[$ii],
                                    1,
                                    "%$KEYWORD%"
                                ));
                            }
                            if ($_POST['order'] == 'ASC_price_low')
                            {
                                $statement = $this
                                    ->db
                                    ->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? AND  `p_name` LIKE ? order by p_current_price asc ", array(
                                    $final_ecat_ids[$ii],
                                    1,
                                    "%$KEYWORD%"
                                ));
                            }
                            if ($_POST['order'] == 'ASC_price_high')
                            {
                                $statement = $this
                                    ->db
                                    ->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? AND  `p_name` LIKE ?  order by p_current_price desc", array(
                                    $final_ecat_ids[$ii],
                                    1,
                                    "%$KEYWORD%"
                                ));
                            }
                        }
                        if (!isset($_POST['order']) || empty($_POST['order']))
                        {
                            $statement = $this
                                ->db
                                ->query("SELECT * FROM tbl_product WHERE ecat_id=? AND p_is_active=? AND  `p_name` LIKE ?  ", array(
                                $final_ecat_ids[$ii],
                                1,
                                "%$KEYWORD%"
                            ));
                        }
    
                        $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"><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 }
                        }
                        else{
                            ?>
                            <div class="col-md-12 mt-5 mx-auto text-center">
                                <img src="<?=base_url('uploads/no_product_found.png');?>" class="img-fluid">
                            </div>
                            <?php
                        }
                    }
    
                }
                
            }
            
            
            if($_POST['page']=='search_result'){
                
            $KEYWORD = $_POST['keyword'];
            if(isset($_POST['order']) && !empty($_POST['order'])){
                                    if($_POST['order']=='default'){
                                        $statement = $this->db->query("SELECT * FROM tbl_product WHERE `p_name` LIKE ? AND p_is_active=? ",array("%$KEYWORD%",1));
                                    }
                                    if($_POST['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("%$KEYWORD%",1));
                                    }
                                    if($_POST['order']=='DESC'){
                                        $statement = $this->db->query("SELECT * FROM tbl_product WHERE `p_name` LIKE ? AND p_is_active=? order by p_id desc ",array("%$KEYWORD%",1));
                                    }
                                    
                                    if($_POST['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("%$KEYWORD%",1,1));
                                    }
                                    if($_POST['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("%$KEYWORD%",1,1));
                                    }
                                    
                                    if($_POST['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("%$KEYWORD%",1));
                                    }
                                    if($_POST['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("%$KEYWORD%",1));
                                    }
                                    if($_POST['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("%$KEYWORD%",1));
                                    }
                                    }
                                    if(!isset($_POST['order']) || empty($_POST['order'])){
                                        
                                        $statement = $this->db->query("SELECT * FROM tbl_product WHERE `p_name` LIKE ? AND p_is_active=? ",array("%$KEYWORD%",1));
                                    }
                                    
                                    $result = $statement->result_array();
                                    if(!empty($statement->num_rows())){
                ?>
            <?php
            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 }
            }
                                    else{
                                        ?>
                                        <div class="col-md-12 mt-5 mx-auto text-center">
                                            <img src="<?=base_url('uploads/no_product_found.png');?>" class="img-fluid">
                                        </div>
                                        <?php
                                    }
            }
        }
    }

}

