<?php
$currentUri = $_SERVER['REQUEST_URI'];
$phpSelf = basename($_SERVER['PHP_SELF']);
$queryString = $_SERVER['QUERY_STRING'];

// Determine if we are on the homepage: index.php (or "/") with no query string.
$isHomepage = ($phpSelf === "index.php" || $currentUri === "/") && empty($queryString);

// For Popup 1:
if (defined('POPUP1_ENABLED') && POPUP1_ENABLED && defined('POPUP1_TARGET')) {
    // If target is set to "HOME", then only trigger if it's truly the homepage.
    if (POPUP1_TARGET === "HOME" && $isHomepage) {
        // Retrieve the static page content using the stored page ID for Popup 1
        if (defined('POPUP1_PAGE_ID') && !empty(POPUP1_PAGE_ID)) {
            $popup1_query = tep_db_query("SELECT description FROM main_page_description WHERE page_id = '" . tep_db_input(POPUP1_PAGE_ID) . "' AND language_id = '1'");
            $popup1_page  = tep_db_fetch_array($popup1_query);
            define('POPUP1_CONTENT', $popup1_page['description']);
        } else {
            define('POPUP1_CONTENT', '');
        }
        include 'templates/marketing_modal.php';
        echo "<script>
          document.addEventListener('DOMContentLoaded', function () {
            if (document.cookie.indexOf('popup1_shown=1') === -1) {
              setTimeout(function () {
                var popup1 = new bootstrap.Modal(document.getElementById('marketingModal'));
                popup1.show();
                document.cookie = 'popup1_shown=1; max-age=' + (24*60*60) + '; path=/';
              }, 5000);
            }
          });
        </script>";
    }
    // Otherwise, for explicit targets like 'index.php?cPath=3'
    else if (POPUP1_TARGET !== "HOME" && strpos($currentUri, POPUP1_TARGET) !== false) {
        if (defined('POPUP1_PAGE_ID') && !empty(POPUP1_PAGE_ID)) {
            $popup1_query = tep_db_query("SELECT description FROM main_page_description WHERE page_id = '" . tep_db_input(POPUP1_PAGE_ID) . "' AND language_id = '1'");
            $popup1_page  = tep_db_fetch_array($popup1_query);
            define('POPUP1_CONTENT', $popup1_page['description']);
        } else {
            define('POPUP1_CONTENT', '');
        }
        include 'templates/marketing_modal.php';
        echo "<script>
          document.addEventListener('DOMContentLoaded', function () {
            if (document.cookie.indexOf('popup1_shown=1') === -1) {
              setTimeout(function () {
                var popup1 = new bootstrap.Modal(document.getElementById('marketingModal'));
                popup1.show();
                document.cookie = 'popup1_shown=1; max-age=' + (24*60*60) + '; path=/';
              }, 5000);
            }
          });
        </script>";
    }
}

// Similar logic for Popup 2:
if (defined('POPUP2_ENABLED') && POPUP2_ENABLED && defined('POPUP2_TARGET')) {
    if (POPUP2_TARGET === "HOME" && $isHomepage) {
        if (defined('POPUP2_PAGE_ID') && !empty(POPUP2_PAGE_ID)) {
            $popup2_query = tep_db_query("SELECT description FROM main_page_description WHERE page_id = '" . tep_db_input(POPUP2_PAGE_ID) . "' AND language_id = '1'");
            $popup2_page  = tep_db_fetch_array($popup2_query);
            define('POPUP2_CONTENT', $popup2_page['description']);
        } else {
            define('POPUP2_CONTENT', '');
        }
        include 'templates/marketing_modal2.php';
        echo "<script>
          document.addEventListener('DOMContentLoaded', function () {
            if (document.cookie.indexOf('popup2_shown=1') === -1) {
              setTimeout(function () {
                var popup2 = new bootstrap.Modal(document.getElementById('marketingModal2'));
                popup2.show();
                document.cookie = 'popup2_shown=1; max-age=' + (24*60*60) + '; path=/';
              }, 5000);
            }
          });
        </script>";
    } else if (POPUP2_TARGET !== "HOME" && strpos($currentUri, POPUP2_TARGET) !== false) {
        if (defined('POPUP2_PAGE_ID') && !empty(POPUP2_PAGE_ID)) {
            $popup2_query = tep_db_query("SELECT description FROM main_page_description WHERE page_id = '" . tep_db_input(POPUP2_PAGE_ID) . "' AND language_id = '1'");
            $popup2_page  = tep_db_fetch_array($popup2_query);
            define('POPUP2_CONTENT', $popup2_page['description']);
        } else {
            define('POPUP2_CONTENT', '');
        }
        include 'templates/marketing_modal2.php';
        echo "<script>
          document.addEventListener('DOMContentLoaded', function () {
            if (document.cookie.indexOf('popup2_shown=1') === -1) {
              setTimeout(function () {
                var popup2 = new bootstrap.Modal(document.getElementById('marketingModal2'));
                popup2.show();
                document.cookie = 'popup2_shown=1; max-age=' + (24*60*60) + '; path=/';
              }, 5000);
            }
          });
        </script>";
    }
}
?>