<?php
session_start();
error_reporting(0);
include('includes/dbconnection.php');
if(isset($_POST['submit']))
{
$pid=$_POST['pid'];
$userid= $_SESSION['msmsuid'];
$query=mysqli_query($con,"insert into tblorders(UserId,PId) values('$userid','$pid') ");
if($query)
{
 echo "<script>alert('Mobile has been added in to the cart');</script>";
 echo "<script type='text/javascript'> document.location ='cart.php'; </script>";   
} else {
 echo "<script>alert('Something went wrong.');</script>";      
}
}

if(isset($_POST['wsubmit']))
{
$wpid=$_POST['wpid'];
$userid= $_SESSION['msmsuid'];
$query=mysqli_query($con,"insert into tblwish(UserId,ProductId) values('$userid','$wpid') ");
if($query)
{
 echo "<script>alert('Mobile has been added to the wishlist');</script>";
 echo "<script type='text/javascript'> document.location ='wishlist.php'; </script>";   
} else {
 echo "<script>alert('Something went wrong.');</script>";      
}
}
  ?>
<!DOCTYPE html>
<html lang="zxx">

<head>
    <title>Mobile Store Management System||Shop Mobile</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
   
    <!-- ::::::::::::::All CSS Files here :::::::::::::: -->

    <!-- Vendor CSS Files -->
    <link rel="stylesheet" href="assets/css/vendor/jquery-ui.min.css">
    <link rel="stylesheet" href="assets/css/vendor/fontawesome.css">
    <link rel="stylesheet" href="assets/css/vendor/plaza-icon.css">
    <link rel="stylesheet" href="assets/css/vendor/bootstrap.min.css">

    <!-- Plugin CSS Files -->
    <link rel="stylesheet" href="assets/css/plugin/swiper.min.css">
    <link rel="stylesheet" href="assets/css/plugin/material-scrolltop.css">
    <link rel="stylesheet" href="assets/css/plugin/price_range_style.css">
    <link rel="stylesheet" href="assets/css/plugin/in-number.css">
    <link rel="stylesheet" href="assets/css/plugin/venobox.min.css">


    <!-- Main Style CSS File -->
    <link rel="stylesheet" href="assets/css/main.css">
    <link rel="stylesheet" href="assets/css/mobile-grid-custom.css">
    <link rel="stylesheet" href="assets/css/festive-flash-sale.css">
    <link rel="stylesheet" href="assets/css/responsive-custom.css">
</head>

<body>

    <!-- ::::::  Start  Header Section  ::::::  -->
   <?php include_once('includes/header.php');?>
    
    <!-- ::::::  Start  Breadcrumb Section  ::::::  -->
    <div class="page-breadcrumb">
        <div class="container">
            <div class="row">
                <div class="col-12">
                    <ul class="page-breadcrumb__menu">
                        <li class="page-breadcrumb__nav"><a href="index.php">Home</a></li>
                        <li class="page-breadcrumb__nav active">Mobile Shop </li>
                    </ul>
                </div>
            </div>
        </div>
    </div> <!-- ::::::  End  Breadcrumb Section  ::::::  -->

    <!-- ::::::  Start  Main Container Section  ::::::  -->
    <main id="main-container" class="main-container">
        <div class="container">
            <div class="row">
                 <!-- Start Rightside - Content -->
                <div class="col-12">
                    <!-- ::::::  Start banner Section  ::::::  -->
                    <div class="banner">
                        <div class="row">
                            <div class="col-12">
                                <div class="banner__box">
                                    <a href="shop-mobile.php" class="banner__link">
                                        <img src="assets/img/banner/banner-shop-1-img-1.jpg"  alt="" class="banner__img">
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div> <!-- ::::::  End banner Section  ::::::  -->

                    <!-- ::::::  Start  Product Style - Default Section [2column]  ::::::  -->
                    <div class="mobile-products-section">
                        <div class="row">
                            <div class="col-12">
                                <div class="mobile-section-header">
                                    <h5 class="mobile-section-title">Mobile Shop</h5>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-12">
                                <div class="mobile-product-grid">
                                    <?php
//Getting default page number
        if (isset($_GET['pageno'])) {
            $pageno = $_GET['pageno'];
        } else {
            $pageno = 1;
        }
// Formula for pagination
        $no_of_records_per_page = 12;
        $offset = ($pageno-1) * $no_of_records_per_page;
// Getting total number of pages
        $total_pages_sql = "SELECT COUNT(*) FROM tblproducts where Status='1'";
        $result = mysqli_query($con,$total_pages_sql);
        $total_rows = mysqli_fetch_array($result)[0];
        $total_pages = ceil($total_rows / $no_of_records_per_page);
                    
$ret=mysqli_query($con,"select * from tblproducts where Status='1' LIMIT $offset, $no_of_records_per_page");
$cnt=1;
while ($row=mysqli_fetch_array($ret)) {
    // Calculate random discount for demo (5-20%)
    $discount = rand(5, 20);
    $originalPrice = $row['Price'] * (1 + $discount/100);
    // Generate random rating (3.5-5.0)
    $rating = rand(35, 50) / 10;
    $ratingCount = rand(50, 500);
?>
                                    <div class="mobile-product-card">
                                        <div class="discount-badge"><?php echo $discount; ?>% OFF</div>
                                        
                                        <a href="single.php?pid=<?php echo $row['ID']; ?>">
                                            <img class="product-image" src="admin/images/<?php echo $row['Image1']; ?>" alt="<?php echo $row['ProductName']; ?>">
                                        </a>
                                        
                                        <div class="product-name">
                                            <a href="single.php?pid=<?php echo $row['ID']; ?>" style="text-decoration: none; color: inherit;">
                                                <?php echo $row['ProductName']; ?>
                                            </a>
                                        </div>
                                        
                                        <div class="product-price">
                                            ₹<?php echo $row['Price']; ?>
                                            <span class="product-original-price">₹<?php echo number_format($originalPrice, 2); ?></span>
                                        </div>
                                        
                                        <div class="product-rating">
                                            <div class="stars">
                                                <?php 
                                                $fullStars = floor($rating);
                                                $halfStar = ($rating - $fullStars) >= 0.5;
                                                for($i = 1; $i <= 5; $i++) {
                                                    if($i <= $fullStars) {
                                                        echo '<i class="fas fa-star"></i>';
                                                    } elseif($i == $fullStars + 1 && $halfStar) {
                                                        echo '<i class="fas fa-star-half-alt"></i>';
                                                    } else {
                                                        echo '<i class="far fa-star"></i>';
                                                    }
                                                }
                                                ?>
                                            </div>
                                            <span class="rating-count">(<?php echo $ratingCount; ?>)</span>
                                        </div>
                                        
                                        <div class="product-specs">
                                            ram <?php echo rand(3,8); ?>GB, storage <?php echo rand(32,128); ?>GB
                                        </div>
                                        
                                        <div class="product-actions">
                                            <?php if($_SESSION['msmsuid']==""){?>
                                                <a href="login.php" class="btn-add-cart">Add to Cart</a>
                                            <?php } else {?>
                                                <form method="post" style="flex: 1;"> 
                                                    <input type="hidden" name="pid" value="<?php echo $row['ID'];?>">   
                                                    <button type="submit" name="submit" class="btn-add-cart">Add to Cart</button>
                                                </form> 
                                            <?php }?>
                                            
                                            <?php if($_SESSION['msmsuid']==""){?>
                                                <a href="login.php" class="btn-wishlist"><i class="icon-heart"></i></a>
                                            <?php } else {?>
                                                <form method="post"> 
                                                    <input type="hidden" name="wpid" value="<?php echo $row['ID'];?>">   
                                                    <button type="submit" name="wsubmit" class="btn-wishlist"><i class="icon-heart"></i></button>
                                                </form> 
                                            <?php }?>
                                        </div>
                                    </div>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                    </div> <!-- ::::::  End  Product Style - Default Section [2column]  ::::::  -->

                      <div class="page-pagination" align="center">


    <ul class="page-pagination__list" >
        <li class="page-pagination__item"><a href="?pageno=1" class="page-pagination__link btn btn--gray" >First</a></li>
        <li class="<?php if($pageno <= 1){ echo 'disabled'; } ?> page-pagination__item">
            <a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>" class="page-pagination__link btn btn--gray" >Prev</a>
        </li>
        <li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?> page-pagination__item">
            <a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>" class="page-pagination__link btn btn--gray" >Next</a>
        </li>
        <li class="page-pagination__item"><a href="?pageno=<?php echo $total_pages; ?> " class="page-pagination__link btn btn--gray" >Last</a></li>
    </ul>
</div>


                     
            </div>
        </div>
    </main> <!-- ::::::  End  Main Container Section  ::::::  -->

    <!-- ::::::  Start  Footer Section  ::::::  -->
  <?php include_once('includes/footer.php');?>

    <!-- material-scrolltop button -->
    <button class="material-scrolltop" type="button"></button>

  


    <!-- ::::::::::::::All Javascripts Files here ::::::::::::::-->

    <!-- Vendor JS Files -->
    <script src="assets/js/vendor/jquery-3.5.1.min.js"></script>
    <script src="assets/js/vendor/modernizr-3.7.1.min.js"></script>
    <script src="assets/js/vendor/jquery-ui.min.js"></script>
    <script src="assets/js/vendor/bootstrap.bundle.js"></script>

    <!-- Plugins JS Files -->
    <script src="assets/js/plugin/swiper.min.js"></script>
    <script src="assets/js/plugin/jquery.countdown.min.js"></script>
    <script src="assets/js/plugin/material-scrolltop.js"></script>
    <script src="assets/js/plugin/price_range_script.js"></script>
    <script src="assets/js/plugin/in-number.js"></script>
    <script src="assets/js/plugin/jquery.elevateZoom-3.0.8.min.js"></script>
    <script src="assets/js/plugin/venobox.min.js"></script>

    <!-- Use the minified version files listed below for better performance and remove the files listed above -->
    <!-- <script src="assets/js/vendor/vendor.min.js"></script>
    <script src="assets/js/plugin/plugins.min.js"></script> -->

    <!-- Main js file that contents all jQuery plugins activation. -->
    <script src="assets/js/main.js"></script>
</body>

</html>
