<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
?>

<?php
include "../partials/dbconnect.php";

$blog_slug = key($_GET); // Gets the first query parameter key (the slug)

if (empty($blog_slug)) {
    die("Blog slug not found");
}
?>

<?php
$sql = "SELECT * FROM blogs INNER JOIN blog_categories ON blogs.blog_category = blog_categories.category_id WHERE blogs.blog_slug = '" . mysqli_real_escape_string($conn, $blog_slug) . "'";
$result = mysqli_query($conn, $sql);
if (!$result || mysqli_num_rows($result) == 0) {
    die("Blog not found");
}

if ($row = mysqli_fetch_assoc($result)) {

    $blog_meta_description = $row['blog_meta_description'];
    $blog_meta_keywords = $row['blog_meta_keywords'];
    $blog_page_title = $row['blog_page_title'];
    $blog_faq_schema  = $row['blog_faq_schema'];
    $blog_title = $row['blog_title'];
    $blog_image = $row['blog_image'];
    $blog_img_alt_desc = $row['blog_img_alt_desc'];
    $blog_image_credit_link = $row['blog_image_credit_link'];
    $blog_tagline = $row['blog_tagline'];
    $blog_tags = $row['blog_tags'];
    $blog_content  = $row['blog_content'];
    $blog_posting_date = date('M d, Y', strtotime($row['blog_posting_date']));
    $blog_category_name = $row['category_name'];
    $blog_category_name_button = $blog_category_name;
    $blog_category_name_button = str_replace(" ", "-", $blog_category_name_button);
    $blog_category_name_cat_link = strtolower($blog_category_name);
    $blog_category_name_cat_link = str_replace(" ", "_", $blog_category_name_cat_link);
    $blog_slug = $row['blog_slug'];
    
    
    // $share_url = "https://theunitedindian.com/news/blog?b=$bid";
    $share_url = "https://theunitedindian.com/news/" . $blog_slug;
    $share_image = "https://theunitedindian.com/images/$blog_image";

    // Store the category ID and blog ID
    $blog_category_id = $row['blog_category'];
    $current_blog_id = $row['blog_id'];

    // Store blog_id for the comment system
    $blog_id = $row['blog_id'];
    $category_id = $row['blog_category'];
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0, user-scalable=yes">
    <meta name="description" <?php echo ' content=" ' . $blog_meta_description . ' " '; ?> />
    <meta name="keywords" <?php echo ' content=" ' . $blog_meta_keywords . ' " '; ?> />
    <link rel="icon" href="../images/site-logo.png" type="image/png">
    <link rel="stylesheet" href="../styles/style.css">
    <link rel="stylesheet" href="../styles/media.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" />

    <!-- For sharing blogs start -->
    <!--<meta property="og:title" <?php echo 'content="' . $blog_title . '"'; ?> />-->
    <!--<meta property="og:image" <?php echo 'content="' . $share_image . '"'; ?> />-->
    <!--<meta property="og:url" <?php echo 'content="' . $share_url . '"'; ?> />-->
    <!--<meta property="og:description" <?php echo 'content="' . $blog_content . '"'; ?> />-->
    <!--<meta property="og:site_name" content="The United Indian" />-->
    <!-- For sharing blogs end -->

    <!-- For sharing blogs new start -->
    <?php
    $clean_url = "https://theunitedindian.com/news/" . $blog_slug;
    ?>
    <meta property="og:title" content="<?php echo htmlspecialchars($blog_title, ENT_QUOTES, 'UTF-8'); ?>" />
    <meta property="og:description" content="<?php echo htmlspecialchars($blog_meta_description, ENT_QUOTES, 'UTF-8'); ?>" />
    <meta property="og:image" content="<?php echo htmlspecialchars($share_image, ENT_QUOTES, 'UTF-8'); ?>" />
    <meta property="og:url" content="<?php echo htmlspecialchars($clean_url, ENT_QUOTES, 'UTF-8'); ?>" />
    <meta property="og:site_name" content="The United Indian" />
    
    <!--amr-ads-->
    <meta name="verify-admitad" content="b8bf14802c" />

    <!-- Twitter -->
    <meta name="twitter:card" content="summary_large_image" />
    <meta name="twitter:title" content="<?php echo htmlspecialchars($blog_title, ENT_QUOTES, 'UTF-8'); ?>" />
    <meta name="twitter:description" content="<?php echo htmlspecialchars($blog_meta_description, ENT_QUOTES, 'UTF-8'); ?>" />
    <meta name="twitter:image" content="<?php echo htmlspecialchars($share_image, ENT_QUOTES, 'UTF-8'); ?>" />
    <meta name="twitter:url" content="<?php echo htmlspecialchars($clean_url, ENT_QUOTES, 'UTF-8'); ?>" />

    <!-- For sharing blogs new end -->

    <!-- canonical tag start -->
    <?php
    // Use blog_slug from database
    $canonical_url = "https://theunitedindian.com/news/" . $blog_slug;
    ?>
    <link rel="canonical" href="<?php echo htmlspecialchars($canonical_url, ENT_QUOTES, 'UTF-8'); ?>" />
    <!-- canonical tag end -->

    <title>The United Indian | <?php echo ' ' . $blog_category_name . ' '; ?> | <?php echo ' ' . $blog_page_title . ' '; ?>
    </title>

    <!--Blog FAQ schema start-->
    <?php echo '' . $blog_faq_schema . ''; ?>
    <!--Blog FAQ schema end-->

    <!-- Google Tag Manager -->
    <script>
        (function(w, d, s, l, i) {
            w[l] = w[l] || [];
            w[l].push({
                'gtm.start': new Date().getTime(),
                event: 'gtm.js'
            });
            var f = d.getElementsByTagName(s)[0],
                j = d.createElement(s),
                dl = l != 'dataLayer' ? '&l=' + l : '';
            j.async = true;
            j.src =
                'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
            f.parentNode.insertBefore(j, f);
        })(window, document, 'script', 'dataLayer', 'GTM-TJSZ8K2G');
    </script>
    <!-- End Google Tag Manager -->
</head>



<body>
    <!-- Google Tag Manager (noscript) -->
    <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-TJSZ8K2G"
            height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <!-- End Google Tag Manager (noscript) -->

    <?php
    include "../partials/header.php";
    ?>

    <?php
    include "../partials/sidebar.php";
    ?>

    <div class="blog-main-container">
        <div class="blog-home-screen">
            <div class="title">
                <a href="https://theunitedindian.com/news/<?php echo '' . $blog_category_name_cat_link . ''; ?>"
                    class="<?php echo '' . $blog_category_name_button . ''; ?>-btn">
                    <span><?php echo '' . $blog_category_name . ''; ?></span>
                </a>
            </div>

            <div class="main-blog-details">
                <h1>
                    <span>
                        <?php echo '' . $blog_title . ''; ?>
                    </span>

                </h1>
            </div>

            <div class="img">
                <img src="https://theunitedindian.com/images/<?php echo '' . $blog_image . ''; ?>" alt="<?php echo '' . $blog_img_alt_desc . ''; ?>">
            </div>

            <div class="image-credit">
                <a href="<?php echo '' . $blog_image_credit_link . ''; ?>" target="_blank">
                    <span>image source</span>
                </a>
            </div>

        </div>

        <div class="blog-details-main-container">
            <div class="left">
                <hr class="left-top-line">

                <div class="left-heading">
                    <h4><?php echo '' . $blog_tagline . ''; ?></h4>
                </div>
                <div class="social-links">
                    <h5>Share</h5>
                    <div class="social-links-details">
                        <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode($share_url); ?>" target="_blank"><img
                                src="https://theunitedindian.com/images/fa.png" alt="Facebook logo" aria-label="Facebook"><i class="fab fa-facebook-f"></i></a>
                        <a href="https://twitter.com/intent/tweet?url=<?php echo urlencode($share_url); ?>&text=<?php echo urlencode($blog_title); ?>"
                            target="_blank"><img src="https://theunitedindian.com/images/tw.png" alt="Twitter logo" aria-label="Twitter"><i class="fa-brands fa-x-twitter"></i></a>
                        <a href="mailto:?subject=<?php echo urlencode($blog_title); ?>&body=<?php echo urlencode('Check out this article: https://theunitedindian.com/news/' . $blog_slug); ?>" target="_blank">
                            <img src="https://theunitedindian.com/images/mail.png" alt="Mail logo"><i class="fa-solid fa-envelope"></i>
                        </a>
                        <a href="https://www.linkedin.com/sharing/share-offsite/?url=<?php echo urlencode($share_url); ?>"
                            target="_blank"><img src="https://theunitedindian.com/images/ln.png" alt="Linkedln logo" aria-label="LinkedIn"><i
                                class="fab fa-linkedin-in"></i></a>
                        <a href="https://api.whatsapp.com/send?text=<?php echo urlencode($blog_title . ' ' . $share_url); ?>" target="_blank"><img
                                src="https://theunitedindian.com/images/wa.png" alt="WhatsApp logo" aria-label="Whatsapp"><i class="fab fa-whatsapp"></i></a>
                    </div>
                </div>
                <div class="posted-date">
                    <h5>Posted</h5>
                    <span class="date"><?php echo '' . $blog_posting_date . ''; ?></span>
                </div>
                <div class="blog-category">
                    <h5>Category</h5>
                    <a href="https://theunitedindian.com/news/environment" class="name"><?php echo '' . $blog_category_name . ''; ?></a>
                </div>
                <div class="blog-tag-links">
                    <h5>Tags</h5>
                    <div class="tag-links">

                        <form action="" method="GET">

                            <?php
                            $sql_tags = "SELECT * FROM blogs WHERE blogs.blog_slug = '" . mysqli_real_escape_string($conn, $blog_slug) . "'";
                            $result = mysqli_query($conn, $sql_tags);

                            if ($result) {
                                while ($row = mysqli_fetch_assoc($result)) {
                                    $tags = explode(",", $row['blog_tags']);
                                    foreach ($tags as $value) {
                                        echo '
                                            <a href="https://theunitedindian.com/partials/handle_tag_search?name=' . $value . '" class="name"><input type="hidden" name="' . $value . '"
                                    id="search" placeholder="SEARCH">' . $value . '</a>
                                            ';
                                    }
                                }
                            }
                            ?>

                        </form>
                    </div>
                </div>
                <div class="amr-ads">
                    <iframe border="0" src="https://www.trip.com/partners/ad/SB4987894?Allianceid=6897388&SID=246101010&trip_sub1=" style="width:120px;height:600px" frameborder="0" scrolling="no" style="border:none" id="SB4987894"></iframe>
                </div>
            </div>

            <div class="mid">
                <div class="blog-details-container">
                    <!-- hero section blog details start -->
                    <div class="para">
                        <p>
                            <?php echo ''.$blog_content.''; ?>
                        </p>
                    </div>
                </div>
                
                <!-- FAQs -->
                <div class="blog2_faq_container">
                    <div class="header">
                        <h1>FAQ</h1>
                        <p>Everything you need to know</p>
                    </div>

                    <div class="faq-list">
                        <?php
                        $blog2_faq = "Select * FROM `blog_faqs` WHERE `blog_id` = '$blog_id' ORDER BY `faq_order` ASC";
                        $result_faqs = mysqli_query($conn, $blog2_faq);

                        // Check if FAQs exist
                        if (mysqli_num_rows($result_faqs) > 0) {
                            echo "<div class='faq-section'>";
                            echo "<h2>Frequently Asked Questions</h2>";

                            while ($faq = mysqli_fetch_assoc($result_faqs)) {
                                echo '
                                    <div class="faq-item">
                                        <div class="faq-question">
                                            '.htmlspecialchars($faq['question']).'
                                            <div class="faq-icon"></div>
                                        </div>
                                        <div class="faq-answer">
                                            <p>'.nl2br(htmlspecialchars($faq['answer'])).'</p>
                                        </div>
                                    </div>
                                ';
                            }

                            echo "</div>";
                        } else {
                            echo "<p>No FAQs available for this blog.</p>";
                        }
                        ?>

                    </div>
                </div>

            </div>

            <div class="right" style="display:none;">
                <!--<div class="section">-->
                <!--    <img class="image" src="../images/ad1.png" alt="Advertisement image">-->
                <!--</div>-->
                <!--<div class="section">-->
                <!--    <img class="image" src="../images/ad2.png" alt="Advertisement image">-->
                <!--</div>-->
                <!--<div class="section">-->
                <!--    <img class="image" src="../images/ad3.png" alt="Advertisement image">-->
                <!--</div>-->

                <div class="advertise">
                    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-6117297715991117" crossorigin="anonymous"></script>
                    <ins class="adsbygoogle"
                        style="display:block"
                        data-ad-format="auto"
                        data-full-width-responsive="true"
                        data-ad-client="ca-pub-6117297715991117">
                    </ins>
                    <script>
                        (adsbygoogle = window.adsbygoogle || []).push({});
                    </script>
                </div>
            </div>
        </div>

        <!-- Comment -->
        <?php
        // include "comment-system/comment.php";

        // Make sure blog_id and category_id are available for comment system
        if (isset($blog_id) && isset($category_id)) {
            // Create GET parameters for comment.php
            $_GET['b'] = $blog_id;
            $_GET['c'] = $category_id;
            include "comment-system/comment.php";
        } else {
            echo '<div class="error-message">Unable to load comment system. Blog ID not found.</div>';
        }
        ?>
        
        <div class="amr-land-ads">
            <iframe border="0" src="https://www.trip.com/partners/ad/SB4987894?Allianceid=6897388&SID=246101010&trip_sub1=" style="width:728px;height:90px" frameborder="0" scrolling="no" style="border:none" id="SB4987894"></iframe>
        </div>

        <!--Subscription-->
        <?php
        // include "../partials/subscription.php";
        include "../partials/newsletter_subscription/newsletter_form.php";
        ?>

        <div class="similar-blog-main-container">
            <div class="similar-top-column">
                <div class="title">
                    <h3>Read more in <?php echo '' . $blog_category_name . ''; ?></h3>
                </div>
                <div class="explore-link">
                    <a href="https://theunitedindian.com/news/<?php echo '' . $blog_category_name_cat_link . ''; ?>"
                        class="explore">View all</a>
                </div>
            </div>
            <div class="similar-bottom-column">

                <?php
                // $read_more_cat_slug = key($_GET); // Gets the first query parameter key (the slug)

                // if (empty($read_more_cat_slug)) {
                //     die("Blog cat slug not found");
                // }

                // // $read_more_cat_id = $_GET['c'];
                // $sql_read_more = "SELECT * FROM blogs INNER JOIN blog_categories ON blogs.blog_category = blog_categories.category_id WHERE blogs.blog_slug = '" . mysqli_real_escape_string($conn, $read_more_cat_slug) . "' ORDER BY blogs.blog_category, blogs.blog_posting_date DESC LIMIT 4";
                // $result = mysqli_query($conn, $sql_read_more);
                
                
                
                
                // $read_more_cat_slug = key($_GET); // Gets the first query parameter key (the slug)

                // if (empty($read_more_cat_slug)) {
                //     die("Blog cat slug not found");
                // }

                // $read_more_cat_id = $_GET['c'];
                $sql_read_more = "SELECT * FROM blogs 
                  INNER JOIN blog_categories ON blogs.blog_category = blog_categories.category_id 
                  WHERE blogs.blog_category = '" . mysqli_real_escape_string($conn, $blog_category_id) . "' 
                  AND blogs.blog_slug != '" . mysqli_real_escape_string($conn, $blog_slug) . "'
                  ORDER BY blogs.blog_posting_date DESC 
                  LIMIT 4";
                $result = mysqli_query($conn, $sql_read_more);




                
// $current_category_id = $blog_category; 
// $current_blog_slug = $blog_slug;

// $stmt = $conn->prepare("SELECT * FROM blogs 
//                         INNER JOIN blog_categories ON blogs.blog_category = blog_categories.category_id 
//                         WHERE blogs.blog_category = ? 
//                         AND blogs.blog_slug != ?
//                         ORDER BY blogs.blog_posting_date DESC 
//                         LIMIT 4");
// $stmt->bind_param("is", $current_category_id, $current_blog_slug);
// $stmt->execute();
// $result = $stmt->get_result();


                while ($row = mysqli_fetch_assoc($result)) {

                    $read_more_blog_id = $row['blog_id'];
                    $read_more_blog_meta_description = $row['blog_meta_description'];
                    $read_more_blog_meta_keywords = $row['blog_meta_keywords'];
                    $read_more_blog_page_title = $row['blog_page_title'];
                    $read_more_blog_title = $row['blog_title'];
                    $read_more_blog_image = $row['blog_image'];
                    $read_more_blog_img_alt_desc = $row['blog_img_alt_desc'];
                    $read_more_blog_image_credit_link = $row['blog_image_credit_link'];
                    $read_more_blog_tagline = $row['blog_tagline'];
                    $read_more_blog_tags = $row['blog_tags'];
                    $read_more_blog_content  = $row['blog_content'];
                    $read_more_blog_posting_date = date('M d, Y', strtotime($row['blog_posting_date']));
                    $read_more_blog_category_name = $row['category_name'];
                    $read_more_blog_category = $row['blog_category'];
                    $read_more_blog_category_name_button = $read_more_blog_category_name;
                    $read_more_blog_category_name_button = str_replace(" ", "-", $read_more_blog_category_name_button);
                    $read_more_blog_category_name_cat_link = strtolower($read_more_blog_category_name);
                    $read_more_blog_category_name_cat_link = str_replace(" ", "_", $read_more_blog_category_name_cat_link);
                    $read_more_blog_slug = $row['blog_slug'];

                    echo '

                            <div class="similar-blog">
                                <div class="image">
                                    <a href="https://theunitedindian.com/news/' . $read_more_blog_slug . '" class="sim-blog-link">
                                        <img src="https://theunitedindian.com/images/' . $read_more_blog_image . '" alt="' . $read_more_blog_img_alt_desc . '">
                                    </a>
                                    <a href="https://theunitedindian.com/news/' . $read_more_blog_category_name_cat_link . '" class="sim-blog-tag-link ' . $read_more_blog_category_name_button . '-link"> ' . $read_more_blog_category_name . ' </a>
                                </div>
                                <div class="similar-blog-details">
                                    <a href="https://theunitedindian.com/news/' . $read_more_blog_slug . '"> ' . $read_more_blog_title . ' </a>
                                </div>
                                <div class="similar-blog-date-details">
                                    <div class="similar-blog-time">
                                        <p>' . $read_more_blog_posting_date . '</p>
                                    </div>
    
                                    <span>&bull;</span>
    
                                    <div class="similar-blog-credit">
                                        <p>TUI Staff</p>
                                    </div>
                                </div>
                            </div>

                        ';
                }
                ?>



            </div>
        </div>

    </div>

    <!-- Footer -->
    <?php
    include "../partials/footer.php";
    ?>
    
    <!-- FAQs start -->
    <script src="../js/blog2_faq.js"></script>
    <!-- FAQs end -->
    
    <!-- Load embed JS for all platforms -->
    <script async src="https://platform.twitter.com/widgets.js"></script>
    <script async src="https://www.instagram.com/embed.js"></script>
    <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v15.0"></script>

</body>

</html>