<?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>Phone Bazaar Management System||Home Page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
   
    <!-- 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">

    <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>
  <?php include_once('includes/header.php');?>

    <!-- ::::::  Start  Main Container Section  ::::::  -->
    <div class="hero hero-slider hero---2">
                        <div class="swiper-wrapper">
                            <?php
                            // Fetch active sliders from database
                            $slider_query = mysqli_query($con, "SELECT * FROM tblslider WHERE Status='1' ORDER BY SliderOrder ASC, CreationDate DESC");
                            $slider_count = mysqli_num_rows($slider_query);
                            
                            if($slider_count > 0) {
                                while($slider = mysqli_fetch_array($slider_query)) {
                                    // Determine image path
                                    $image_path = !empty($slider['SliderImage']) ? 'admin/images/slider/' . $slider['SliderImage'] : 'assets/img/hero/hero-home-2-img-1.jpg';
                                    
                                    // Determine link
                                    $slider_link = !empty($slider['SliderLink']) ? $slider['SliderLink'] : 'shop-mobile.php';
                            ?>
                            <!-- Start Hero Image -->
                            <div class="hero-img hero-img--2 swiper-slide" style="background-image: url(<?php echo $image_path; ?>);">
                                <div class="hero__content">
                                    <div class="row">
                                        <div class="col-9 offset-1">
                                            <div class="title title--normal title--thin"><?php echo htmlspecialchars($slider['SliderTitle']); ?></div>
                                            <div class="title title--normal title--regular"><?php echo htmlspecialchars($slider['SliderDescription']); ?></div>
                                            <a href="<?php echo htmlspecialchars($slider_link); ?>" class="btn btn--box btn--large btn--blue btn--uppercase btn--weight">Shop Now</a>
                                        </div>
                                    </div>
                                </div>
                            </div> <!-- End Hero Image -->
                            <?php 
                                }
                            } else {
                                // Fallback to default slider if no database sliders exist
                            ?>
                            <!-- Start Hero Image -->
                            <div class="hero-img hero-img--2 swiper-slide" style="background-image: url(assets/img/hero/hero-home-2-img-1.jpg);">
                                <div class="hero__content">
                                    <div class="row">
                                        <div class="col-9 offset-1">
                                            <div class="title title--normal title--thin">New Arrivals</div>
                                            <div class="title title--normal title--regular">Get Amazing Mobile Now</div>
                                            <div class="title title--description">Sale Offer <span>-20% Off</span> This Week</div>
                                            <a href="shop-mobile.php" class="btn btn--box btn--large btn--blue btn--uppercase btn--weight">Shopping Now</a>
                                        </div>
                                    </div>
                                </div>
                            </div> <!-- End Hero Image -->
                            <?php } ?>

                            <!-- Add Pagination -->
                            <div class="swiper-pagination hero__dots hero__dots--2"></div>
                            <!-- Add Arrows -->
                            <div class="swiper-buttons">
                                <!-- Add Arrows -->
                                <div class="swiper-button-next hero__nav hero__nav--next"><i class="far fa-chevron-right"></i></div>
                                <div class="swiper-button-prev hero__nav hero__nav--prev"><i class="far fa-chevron-left"></i></div>
                            </div>
                        </div> <!-- ::::::  End Hero Section  ::::::  -->
                    </div> 
<main id="main-container" class="main-container m-t-30">
        <div class="container">
            <div class="row">
                <div class="col-12">
                    <div class="section-content section-content--border d-md-flex align-items-center justify-content-between">
                        <h5 class="section-content__title">All Brands</h5>
                    </div>
                </div>
            </div>
                        <div class="row">
                <?php
                    // Test database connection and check if BrandIcon column exists
                    $test_query = mysqli_query($con, "SELECT BrandName, BrandIcon, Status FROM tblbrand LIMIT 5");
                    if ($test_query) {
                        echo "<!-- DEBUG: Database query successful -->";
                        while ($test_row = mysqli_fetch_array($test_query)) {
                            echo "<!-- DEBUG TEST: Brand: {$test_row['BrandName']}, Icon: '{$test_row['BrandIcon']}', Status: {$test_row['Status']} -->";
                        }
                    } else {
                        echo "<!-- DEBUG: Database query failed: " . mysqli_error($con) . " -->";
                    }
                    
                    $ret=mysqli_query($con,"select * from tblbrand where Status='1'");
                    $brandCount = mysqli_num_rows($ret);
                    echo "<!-- DEBUG: Total active brands: $brandCount -->";
                    
                    // Reset pointer
                    mysqli_data_seek($ret, 0);
                    
                    while ($row=mysqli_fetch_array($ret)) {
                        $brandIcon = isset($row['BrandIcon']) ? trim($row['BrandIcon']) : '';
                        $brandNameUpper = strtoupper(trim($row['BrandName']));
                        $appleIcon = 'assets/img/brand-icons/apple.svg';
                        $genericIcon = 'assets/img/brand-icons/generic.svg';
                        $isEmoji = !empty($brandIcon) && preg_match('/[\x{1F600}-\x{1F64F}]|[\x{1F300}-\x{1F5FF}]|[\x{1F680}-\x{1F6FF}]|[\x{1F1E0}-\x{1F1FF}]|[\x{2600}-\x{26FF}]|[\x{2700}-\x{27BF}]/u', $brandIcon);
                        
                        // Debug output - remove after testing
                        echo "<!-- DEBUG: Brand: {$row['BrandName']}, Icon: '$brandIcon', IsEmoji: " . ($isEmoji ? 'true' : 'false') . " -->";
                        
                        // Decide which icon to show: emoji first, then uploaded image, then default icons
                        $showEmoji = false;
                        $iconSrc = $genericIcon;
                        
                        if (!empty($brandIcon)) {
                            if ($isEmoji) {
                                $showEmoji = true; // Show emoji
                            } else {
                                $iconSrc = $brandIcon; // Show uploaded image
                                // Fix path for uploaded images - they're stored in admin/images/brand_icons/
                                if (strpos($brandIcon, 'images/brand_icons/') === 0) {
                                    $iconSrc = 'admin/' . $brandIcon;
                                }
                            }
                        } else if (strpos($brandNameUpper, 'APPLE') !== false) {
                            $iconSrc = $appleIcon;
                        } else {
                            $iconSrc = $genericIcon;
                        }
                        
                        // Debug output - remove after testing
                        echo "<!-- DEBUG: ShowEmoji: " . ($showEmoji ? 'true' : 'false') . ", IconSrc: '$iconSrc' -->";
                ?>
                <div class="col-md-2 col-sm-4 col-6">
                    <div class="brand-item text-center">
                        <a href="brand-details.php?bname=<?php echo $row['BrandName'];?>" class="brand-link">
                            <div class="brand-icon">
                                <?php if ($showEmoji): ?>
                                    <span style="font-size: 48px; line-height: 1;"><?php echo $brandIcon; ?></span>
                                <?php else: ?>
                                    <img src="<?php echo $iconSrc; ?>" alt="<?php echo htmlspecialchars($row['BrandName']); ?>" style="max-width: 48px; max-height: 48px;" />
                                <?php endif; ?>
                            </div>
                            <div class="brand-name"><?php echo $row['BrandName']; ?></div>
                        </a>
                    </div>
                </div>
                <?php } ?>
            </div>
            <hr>
            <div class="row">
                <div class="col-12">
                    <!-- ::::::  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">Mobiles Show</h5>
                                    <a href="shop-mobile.php" class="mobile-section-link">Show All Products <i class="icon-chevron-right"></i></a>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-12">
                                <div class="mobile-product-grid">
                                    <?php
                                    $ret=mysqli_query($con,"select * from tblproducts where Status='1'");
                                    $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]  ::::::  -->

                    <!-- ::::::  Start banner Section  ::::::  -->
                    <div class="banner banner--1">
                        <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-home-4-img-2 (3).jpg" height="175" width="870" alt="" class="banner__img">
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div> <!-- ::::::  End banner Section  ::::::  -->

                    <!-- ::::::  Start Festive Season Banner Section  ::::::  -->
                    <div class="festive-banner">
                        <div class="festive-banner-content">
                            <h2>Festive Season Sale!</h2>
                            <p>Up to 40% off on all accessories</p>
                            <a href="shop-mobile.php" class="btn-shop">Shop Now</a>
                        </div>
                    </div> <!-- ::::::  End Festive Season Banner Section  ::::::  -->

                    <!-- ::::::  Start Flash Sale Section  ::::::  -->
                    <div class="flash-sale-section">
                        <div class="flash-sale-header">
                            <div class="flash-sale-title">
                                <h3>Flash Sale</h3>
                                <span class="flash-sale-badge">Ends in 04:50</span>
                            </div>
                            <div class="flash-sale-timer">
                                <span class="timer-text">Ends in</span>
                                <div class="countdown">
                                    <div class="countdown-item">
                                        <span class="countdown-number" id="hours">04</span>
                                        <span class="countdown-label">hrs</span>
                                    </div>
                                    <div class="countdown-item">
                                        <span class="countdown-number" id="minutes">50</span>
                                        <span class="countdown-label">min</span>
                                    </div>
                                    <div class="countdown-item">
                                        <span class="countdown-number" id="seconds">30</span>
                                        <span class="countdown-label">sec</span>
                                    </div>
                                </div>
                            </div>
                            <a href="shop-mobile.php" class="view-all-flash">View All</a>
                        </div>
                        
                        <div class="flash-sale-grid">
                            <?php
                            // Get flash sale products (limit to 5 for display)
                            $flashRet=mysqli_query($con,"select * from tblproducts where Status='1' order by rand() limit 5");
                            while ($flashRow=mysqli_fetch_array($flashRet)) {
                                // Higher discounts for flash sale (20-70%)
                                $flashDiscount = rand(20, 70);
                                $flashOriginalPrice = $flashRow['Price'] * (1 + $flashDiscount/100);
                                // Generate random rating (4.0-5.0 for flash sale items)
                                $flashRating = rand(40, 50) / 10;
                                $flashRatingCount = rand(100, 1000);
                                
                                // Random specs for demo
                                $ramOptions = [3, 4, 6, 8, 12];
                                $storageOptions = [32, 64, 128, 256];
                                $ram = $ramOptions[array_rand($ramOptions)];
                                $storage = $storageOptions[array_rand($storageOptions)];
                            ?>
                            <div class="flash-product-card">
                                <div class="flash-product-image">
                                    <div class="flash-discount-badge"><?php echo $flashDiscount; ?>% OFF</div>
                                    <a href="single.php?pid=<?php echo $flashRow['ID']; ?>">
                                        <img src="admin/images/<?php echo $flashRow['Image1']; ?>" alt="<?php echo $flashRow['ProductName']; ?>">
                                    </a>
                                </div>
                                
                                <div class="flash-product-info">
                                    <div class="flash-product-brand"><?php echo $flashRow['CompanyName']; ?></div>
                                    <div class="flash-product-name">
                                        <a href="single.php?pid=<?php echo $flashRow['ID']; ?>" style="text-decoration: none; color: inherit;">
                                            <?php echo $flashRow['ProductName']; ?>
                                        </a>
                                    </div>
                                    
                                    <div class="flash-product-price">
                                        <span class="flash-current-price">₹<?php echo number_format($flashRow['Price']); ?></span>
                                        <span class="flash-original-price">₹<?php echo number_format($flashOriginalPrice); ?></span>
                                    </div>
                                    
                                    <div class="flash-product-rating">
                                        <div class="flash-stars">
                                            <?php 
                                            $fullStars = floor($flashRating);
                                            $halfStar = ($flashRating - $fullStars) >= 0.5;
                                            for($i = 1; $i <= 5; $i++) {
                                                if($i <= $fullStars) {
                                                    echo '<i class="fas fa-star flash-star"></i>';
                                                } elseif($i == $fullStars + 1 && $halfStar) {
                                                    echo '<i class="fas fa-star-half-alt flash-star"></i>';
                                                } else {
                                                    echo '<i class="far fa-star flash-star"></i>';
                                                }
                                            }
                                            ?>
                                        </div>
                                        <span class="flash-rating-text">(<?php echo $flashRatingCount; ?>)</span>
                                    </div>
                                    
                                    <div class="flash-product-specs">
                                        <span class="flash-spec-item"><?php echo $ram; ?>GB RAM</span>
                                        <span class="flash-spec-item"><?php echo $storage; ?>GB</span>
                                    </div>
                                    
                                    <div class="flash-product-actions">
                                        <?php if($_SESSION['msmsuid']==""){?>
                                            <a href="login.php" class="flash-btn flash-btn-primary">Add to Cart</a>
                                        <?php } else {?>
                                            <form method="post" style="flex: 1;"> 
                                                <input type="hidden" name="pid" value="<?php echo $flashRow['ID'];?>">   
                                                <button type="submit" name="submit" class="flash-btn flash-btn-primary">Add to Cart</button>
                                            </form> 
                                        <?php }?>
                                        
                                        <?php if($_SESSION['msmsuid']==""){?>
                                            <a href="login.php" class="flash-btn flash-btn-secondary"><i class="icon-heart"></i></a>
                                        <?php } else {?>
                                            <form method="post"> 
                                                <input type="hidden" name="wpid" value="<?php echo $flashRow['ID'];?>">   
                                                <button type="submit" name="wsubmit" class="flash-btn flash-btn-secondary"><i class="icon-heart"></i></button>
                                            </form> 
                                        <?php }?>
                                    </div>
                                </div>
                            </div>
                            <?php } ?>
                        </div>
                    </div> <!-- ::::::  End Flash Sale Section  ::::::  -->
                </div>
            </div>

            <div class="row">
                <div class="col-12">
                    <!-- ::::::  Start CMS Section  ::::::  -->
                    <div class="cms cms--1">
                        <div class="row">
                            <div class="col-md-3 col-sm-6 col-12">
                                <!-- Start Single CMS box -->
                                <div class="cms__content">
                                    <div class="cms__icon">
                                        <img class="cms__icon-img" src="assets/img/icon/cms/icon1.png" alt="">
                                    </div>
                                    <div class="cms__text">
                                        <h6 class="cms__title">Free Delivery</h6>
                                        <span class="cms__des">For all orders over ₹99</span>
                                    </div>
                                </div>
                            </div> <!-- End Single CMS box -->
                            <!-- Start Single CMS box -->
                            <div class="col-md-3 col-sm-6 col-12">
                                <div class="cms__content">
                                    <div class="cms__icon">
                                        <img class="cms__icon-img" src="assets/img/icon/cms/icon2.png" alt="">
                                    </div>
                                    <div class="cms__text">
                                        <h6 class="cms__title">Safe Payment</h6>
                                        <span class="cms__des">100% secure payment</span>
                                    </div>
                                </div>
                            </div> <!-- End Single CMS box -->
                            <!-- Start Single CMS box -->
                            <div class="col-md-3 col-sm-6 col-12">
                                <div class="cms__content">
                                    <div class="cms__icon">
                                        <img class="cms__icon-img" src="assets/img/icon/cms/icon3.png" alt="">
                                    </div>
                                    <div class="cms__text">
                                        <h6 class="cms__title">Shop With Confidence</h6>
                                        <span class="cms__des">If goods have problems</span>
                                    </div>
                                </div>
                            </div> <!-- End Single CMS box -->
                            <!-- Start Single CMS box -->
                            <div class="col-md-3 col-sm-6 col-12">
                                <div class="cms__content">
                                    <div class="cms__icon">
                                        <img class="cms__icon-img" src="assets/img/icon/cms/icon4.png" alt="">
                                    </div>
                                    <div class="cms__text">
                                        <h6 class="cms__title">24/7 Help Center</h6>
                                        <span class="cms__des">Dedicated 24/7 support</span>
                                    </div>
                                </div>
                            </div> <!-- End Single CMS box -->
                        </div>
                    </div> <!-- ::::::  End CMS Section  ::::::  -->
                </div>
            </div>
        </div>
    </main> <!-- ::::::  End  Main Container Section  ::::::  -->
 
    <!-- ::::::  Start  Footer Section  ::::::  -->
    <?php include_once('includes/footer.php');?>


    <!-- ::::::::::::::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>

    <script src="assets/js/main.js"></script>
    <script src="assets/js/flash-sale-timer.js"></script>
</body>

</html>
