<?php
include('header.php');
?>
<?php
if (!isset($_GET['id'])) {
    ?>
        <div class="col-md-12 text-center">
            <img src="<?=base_url('uploads/no_product_found.png');?>" class="img-fluid" />
        </div>
    <?php
    include('footer.php');
    
    exit;
} else {
    // Check the id is valid or not
    $statement = $this->db->query("SELECT * FROM tbl_product WHERE p_id=?", array(
        $_GET['id']
    ));
    $total     = count($statement->result_array());
    $result    = $statement->result_array();
    if ($total == 0) {
    ?>
        <div class="col-md-12 text-center">
            <img src="<?=base_url('uploads/no_product_found.png');?>" class="img-fluid" />
        </div>
    <?php
    include('footer.php');
    
    exit;
}
}
foreach ($result as $row) {
    $p_name              = $row['p_name'];
    // $p_old_price = $row['p_old_price'];
    // $p_current_price = $row['p_current_price'];
    // $p_qty = $row['p_qty'];
    $p_featured_photo    = $row['p_featured_photo'];
    $p_id                = $row['p_id'];
    $p_description       = $row['p_description'];
    $p_short_description = $row['p_short_description'];
    $p_feature           = $row['p_feature'];
    $p_condition         = $row['p_condition'];
    $p_return_policy     = $row['p_return_policy'];
    $p_total_view        = $row['p_total_view'];
    $p_is_featured       = $row['p_is_featured'];
    $p_is_active         = $row['p_is_active'];
    $ecat_id             = $row['ecat_id'];
}
// Getting all categories name for breadcrumb
$statement = $this->db->query("SELECT
t1.ecat_id,
t1.ecat_name,
t1.mcat_id,
t2.mcat_id,
t2.mcat_name,
t2.tcat_id,
t3.tcat_id,
t3.tcat_name
FROM tbl_end_category t1
JOIN tbl_mid_category t2
ON t1.mcat_id = t2.mcat_id
JOIN tbl_top_category t3
ON t2.tcat_id = t3.tcat_id
WHERE t1.ecat_id=?", array(
    $ecat_id
));
$total     = count($statement->result_array());
$result    = $statement->result_array();
foreach ($result as $row) {
    $ecat_name = $row['ecat_name'];
    $mcat_id   = $row['mcat_id'];
    $mcat_name = $row['mcat_name'];
    $tcat_id   = $row['tcat_id'];
    $tcat_name = $row['tcat_name'];
}
$p_total_view = $p_total_view + 1;
$statement    = $this->db->query("UPDATE tbl_product SET p_total_view=? WHERE p_id=?", array(
    $p_total_view,
    $_GET['id']
));
$statement    = $this->db->query("SELECT * FROM tbl_product_size WHERE p_id=?", array(
    $_GET['id']
));
$result       = $statement->result_array();
foreach ($result as $row) {
    $size[] = $row['size_id'];
}
$statement = $this->db->query("SELECT * FROM tbl_product_color WHERE p_id=?", array(
    $_GET['id']
));
$result    = $statement->result_array();
foreach ($result as $row) {
    $color[] = $row['color_id'];
}
if (isset($_POST['form_review'])) {
    $statement = $this->db->query("SELECT * FROM tbl_rating WHERE p_id=? AND cust_id=?", array(
        $_GET['id'],
        $_SESSION['customer']['cust_id']
    ));
    $total     = $statement->rowCount();
    if ($total) {
        $error_message = LANG_VALUE_68;
    } else {
        $statement       = $this->db->query("INSERT INTO tbl_rating (p_id,cust_id,comment,rating) VALUES (?,?,?,?)", array(
            $_GET['id'],
            $_SESSION['customer']['cust_id'],
            $_POST['comment'],
            $_POST['rating']
        ));
        $success_message = LANG_VALUE_163;
    }
}
// Getting the average rating for this product
$t_rating   = 0;
$statement  = $this->db->query("SELECT * FROM tbl_rating WHERE p_id=?", array(
    $_GET['id']
));
$tot_rating = count($statement->result_array());
if ($tot_rating == 0) {
    $avg_rating = 0;
} else {
    $result = $statement->result_array();
    foreach ($result as $row) {
        $t_rating = $t_rating + $row['rating'];
    }
    $avg_rating = $t_rating / $tot_rating;
}
?>


  
<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="<?= base_url(); ?>"><strong><span class="mdi mdi-home"></span> Home</strong></a>
            <span class="mdi mdi-chevron-right"></span>
            <a href="<?= base_url() . 'category.php?id=' . $tcat_id . '&type=top-category' ?>"><?= $tcat_name; ?></a>
            <span class="mdi mdi-chevron-right"></span>
            <a href="<?= base_url() . 'category.php?id=' . $mcat_id . '&type=mid-category' ?>"><?= $mcat_name; ?></a>
            <span class="mdi mdi-chevron-right"></span>
            <a href="<?= base_url() . 'category.php?id=' . $ecat_id . '&type=end-category' ?>"><?= $ecat_name; ?></a>
         </div>
      </div>
   </div>
</section>
<section class="shop-single section-padding pt-3">
   <div class="container">
      <div class="row">
         <div class="col-md-6">
            <div class="shop-detail-left">
               <div class="shop-detail-slider">
                  <div class="favourite-icon">
                     <a class="fav-btn" title="<?= $p_name; ?>" data-placement="bottom" data-toggle="tooltip" href="#" data-original-title="59% OFF"><i class="mdi mdi-tag-outline"></i></a>
                  </div>
                  <div id="sync1" class="owl-carousel">
                        <div class="item"><img alt="<?= $p_name; ?>" id="pic_<?= $_GET['id']; ?>" src="<?=base_url('uploads/shop/product/');?><?= $p_featured_photo; ?>" class="img-fluid img-center"></div>
                        <input type="hidden" id="pic2_<?= $_GET['id']; ?>" value="<?= $p_featured_photo; ?>" />
                              
                      <?php
$statement = $this->db->query("SELECT * FROM tbl_product_photo WHERE p_id=?", array(
    $_GET['id']
));
$result    = $statement->result_array();
foreach ($result as $row) {
?>
                                <div class="item"><img alt="<?= $p_name; ?>" src="<?=base_url('uploads/shop/product/');?>more/<?= $row['photo']; ?>" class="img-fluid img-center"></div>
                        <?php
}
?>
                 </div>
                  <div id="sync2" class="owl-carousel d-xs-none">
                        <div class="item"><img alt="<?= $p_name; ?>" src="<?=base_url('uploads/shop/product/');?><?= $p_featured_photo; ?>" class="img-fluid img-center"></div>
                     <?php
$statement = $this->db->query("SELECT * FROM tbl_product_photo WHERE p_id=?", array(
    $_GET['id']
));
$result    = $statement->result_array();
foreach ($result as $row) {
?>
                                <div class="item"><img alt="<?= $p_name; ?>" src="<?=base_url('uploads/shop/product/');?>more/<?= $row['photo']; ?>" class="img-fluid img-center"></div>
                        <?php
}
?>
                 </div>
               </div>
            </div>
         </div>
         <div class="col-md-6 no-padding2">
            <div class="shop-detail-right no-padding3">
                <span class="badge badge-success" <?= 'id="offer_' . $p_id . '"'; ?> >1 % OFF</span>
               <h2><?= $p_name; ?></h2>
               <h6><strong><span class="mdi mdi-approval"></span> Available in</strong> - </h6>
               <p><?php
$statement2 = $this->db->query("SELECT * FROM tbl_product_size WHERE p_id=?", array(
    $p_id
));
$result2    = $statement2->result_array();
$i          = 1;

foreach ($result2 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>
                                                    window.history.back();
                                                </script>
                                                <?php
                                            }
                                        }

?>
              </p>
               <?php
if ($p_old_price != '' && $p_old_price != $p_current_price) {
?><p class="regular-price"><i class="mdi mdi-tag-outline"></i> MRP : <?= LANG_VALUE_1; ?><b id="price_o_<?= $p_id; ?>"><?= $p_old_price; ?></b></p><?php
}
?>
              <p class="offer-price mb-0">Discounted price : <span class="text-success"><?= LANG_VALUE_1; ?><b id="price_c_<?= $p_id; ?>"><?php
echo $p_current_price;
?></b></span></p>
               <p>
               <?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="<?= $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="<?= $p_featured_photo; ?>">
                                              
                                          
                                              
                                      
                                              
                                           <?php
    $count      = 0;
    $statement2 = $this->db->query("SELECT * FROM tbl_product_size WHERE p_id=?", array(
        $row['p_id']
    ));
    $result2    = $statement2->result_array();
    foreach ($result2 as $row2) {
        $pid_search     = 0;
        $size_id_search = 0;
        $primary_id     = 0;
        $ok             = 0;
        $count++;
        
        $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-sm input-group-btn btn-danger" onclick="minus(<?= $row['p_id'] . ',' . $primary_id; ?>)">-</button>
                                              <input type="text" name="quantity" class="form-control  text-center" qty_id="" id="qty_<?= $primary_id; ?>"
                                                 qty="<?= $row2['p_qty']; ?>" readonly value="" >
                                              <button class=" btn-sm input-group-btn btn-danger" 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-sm input-group-btn btn-danger" onclick="minus(<?= $row['p_id'] . ',' . $primary_id; ?>)">-</button>
                                                    <input type="text" readonly name="quantity" class="form-control  text-center " qty_id="" id="qty_<?= $primary_id; ?>"
                                                     qty="<?= $row2['p_qty']; ?>"  value="1" >
                                                    <button class=" btn-sm input-group-btn btn-danger " 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
}
?>
     </p>
               
            </div>
         </div>
         <div class="col-md-12">
             <div class="short-description">
                  <h5>
                     Quick Overview
                     <p class="float-right">Availability: <?php
                        if ($p_qty >= 0) {
                        ?><span class="badge badge-success">In Stock</span><?php
                        } elseif ($p_qty <= 0) {
                        ?><span class="badge badge-warning">Out Of Stock</span><?php
                        }
                        ?>
                    </p>
                  </h5>
                  <p><b><?= $p_short_description; ?></b></p>
                  <p class="mb-0"><?= $p_description; ?></p>
                  <p class="mb-0"><?= $p_feature; ?></p>
                  <p class="mb-0"><?= $p_condition; ?></p>
                  <p class="mb-0"><?= $p_return_policy; ?></p>
               </div>
         </div>
      </div>
   </div>
</section>
<section class="product-items-slider section-padding bg-white border-top">
   <div class="container">
      <div class="section-header">
         <h5 class="heading-design-h5">Best Offers <!--View--> <!--<span class="badge badge-primary">20% OFF</span>-->
            <a class="float-right text-secondary" href="<?=base_url('featured');?>">View All</a>
         </h5>
      </div>
            <?php
$query11 = $this->db->query("SELECT * FROM tbl_product WHERE p_is_active=?  AND p_is_featured=?   AND p_id !=? ORDER BY p_id DESC", array(
    1,
    1,
    $p_id
));
if (count($query11->result_array())) {
?>
               <div class="owl-carousel owl-carousel-featured">
                    <?php
    foreach ($query11->result_array() as $row) {
?>
                       <div class="item">
                          <div class="product">
                              <a href="<?= base_url('product_view/index'); ?>?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 ");
            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++;
            }
        }
?>
                                   </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++;
                
                $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="" id="qty_<?= $primary_id; ?>"
                                                    qty="<?= $row2['p_qty']; ?>" readonly value="" >
                                                    <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="" 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
    }
?>
               </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>
</section>


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