<?php 
    define('_FEXEC',1);

	require('includes/application_top.php');
	require(DIR_WS_INCLUDES . '/tweak/general.php');
	frequire($FSESSION->language.'/popups.php',RLANG);	
	
	
	function sanitize_hex_color($v) {
  $v = trim($v ?? '');
  if (preg_match('/^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$/', $v)) return $v;
  return ''; // invalid colors become empty (will fall back to default)
}

	
// Process form submission
if (isset($_POST['action']) && $_POST['action'] == 'save') {
  // For URL and background image fields, you may still sanitize
  $popup1_target     = tep_db_prepare_input($_POST['popup1_target']);
  $popup1_bg      = tep_db_prepare_input($_POST['popup1_bg']);
  $popup2_target     = tep_db_prepare_input($_POST['popup2_target']);
  $popup2_bg      = tep_db_prepare_input($_POST['popup2_bg']);

  // Do not sanitize the HTML field here if you want to preserve the HTML code.
  
  $popup1_page_id = tep_db_prepare_input($_POST['popup1_page_id']);
  $popup2_page_id = tep_db_prepare_input($_POST['popup2_page_id']);

  $popup1_enabled = isset($_POST['popup1_enabled']) ? 1 : 0;
  $popup2_enabled = isset($_POST['popup2_enabled']) ? 1 : 0;
  
  define('POPUP1_BG', HTTP_CATALOG_SERVER . DIR_WS_CATALOG . $_POST['popup1_bg']);

  define('POPUP2_BG', HTTP_CATALOG_SERVER . DIR_WS_CATALOG . $_POST['popup2_bg']);

$popup1_bg_color = sanitize_hex_color($_POST['popup1_bg_color'] ?? '');
$popup2_bg_color = sanitize_hex_color($_POST['popup2_bg_color'] ?? '');

tep_db_query("UPDATE " . TABLE_CONFIGURATION . " SET configuration_value = '" . tep_db_input($popup1_bg_color) . "' WHERE configuration_key = 'POPUP1_BG_COLOR'");
tep_db_query("UPDATE " . TABLE_CONFIGURATION . " SET configuration_value = '" . tep_db_input($popup2_bg_color) . "' WHERE configuration_key = 'POPUP2_BG_COLOR'");



  // Update configuration table for Popup 1
  tep_db_query("UPDATE " . TABLE_CONFIGURATION . " SET configuration_value = '" . tep_db_input($popup1_target) . "' WHERE configuration_key = 'POPUP1_TARGET'");
  tep_db_query("UPDATE " . TABLE_CONFIGURATION . " SET configuration_value = '" . tep_db_input($popup1_page_id) . "' WHERE configuration_key = 'POPUP1_PAGE_ID'");

  tep_db_query("UPDATE " . TABLE_CONFIGURATION . " SET configuration_value = '" . tep_db_input($popup1_bg) . "' WHERE configuration_key = 'POPUP1_BG'");
  tep_db_query("UPDATE " . TABLE_CONFIGURATION . " SET configuration_value = '" . tep_db_input($popup1_enabled) . "' WHERE configuration_key = 'POPUP1_ENABLED'");

  // Update configuration table for Popup 2
  tep_db_query("UPDATE " . TABLE_CONFIGURATION . " SET configuration_value = '" . tep_db_input($popup2_target) . "' WHERE configuration_key = 'POPUP2_TARGET'");
  tep_db_query("UPDATE " . TABLE_CONFIGURATION . " SET configuration_value = '" . tep_db_input($popup2_page_id) . "' WHERE configuration_key = 'POPUP2_PAGE_ID'");

  tep_db_query("UPDATE " . TABLE_CONFIGURATION . " SET configuration_value = '" . tep_db_input($popup2_bg) . "' WHERE configuration_key = 'POPUP2_BG'");
  tep_db_query("UPDATE " . TABLE_CONFIGURATION . " SET configuration_value = '" . tep_db_input($popup2_enabled) . "' WHERE configuration_key = 'POPUP2_ENABLED'");
  
  $messageStack->add_session(TEXT_POPUP_SUCCESS, 'success');
  tep_redirect(tep_href_link('popups.php'));
}

// Retrieve existing configuration values for popup1
$popup1_target_query = tep_db_query("SELECT configuration_value FROM " . TABLE_CONFIGURATION . " WHERE configuration_key = 'POPUP1_TARGET'");
$popup1_target           = tep_db_fetch_array($popup1_target_query);
$popup1_target_value = $popup1_target['configuration_value'];

$popup1_page_id_query = tep_db_query("SELECT configuration_value FROM " . TABLE_CONFIGURATION . " WHERE configuration_key = 'POPUP1_PAGE_ID'");
$popup1_page_id       = tep_db_fetch_array($popup1_page_id_query);
$popup1_page_id_value = $popup1_page_id['configuration_value'];

$popup1_bg_query = tep_db_query("SELECT configuration_value FROM " . TABLE_CONFIGURATION . " WHERE configuration_key = 'POPUP1_BG'");
$popup1_bg       = tep_db_fetch_array($popup1_bg_query);
$popup1_bg_value = $popup1_bg['configuration_value'];

$popup1_enabled_query = tep_db_query("SELECT configuration_value FROM " . TABLE_CONFIGURATION . " WHERE configuration_key = 'POPUP1_ENABLED'");
$popup1_enabled_arr   = tep_db_fetch_array($popup1_enabled_query);
$popup1_enabled_value = $popup1_enabled_arr['configuration_value'];

$popup1_bg_color_value = tep_db_fetch_array(tep_db_query("SELECT configuration_value FROM " . TABLE_CONFIGURATION . " WHERE configuration_key = 'POPUP1_BG_COLOR'"))['configuration_value'];
$popup2_bg_color_value = tep_db_fetch_array(tep_db_query("SELECT configuration_value FROM " . TABLE_CONFIGURATION . " WHERE configuration_key = 'POPUP2_BG_COLOR'"))['configuration_value'];


// Retrieve existing configuration values for popup2
$popup2_target_query = tep_db_query("SELECT configuration_value FROM " . TABLE_CONFIGURATION . " WHERE configuration_key = 'POPUP2_TARGET'");
$popup2_target       = tep_db_fetch_array($popup2_target_query);
$popup2_target_value = $popup2_target['configuration_value'];

$popup2_page_id_query = tep_db_query("SELECT configuration_value FROM " . TABLE_CONFIGURATION . " WHERE configuration_key = 'POPUP2_PAGE_ID'");
$popup2_page_id       = tep_db_fetch_array($popup2_page_id_query);
$popup2_page_id_value = $popup2_page_id['configuration_value'];

$popup2_bg_query = tep_db_query("SELECT configuration_value FROM " . TABLE_CONFIGURATION . " WHERE configuration_key = 'POPUP2_BG'");
$popup2_bg       = tep_db_fetch_array($popup2_bg_query);
$popup2_bg_value = $popup2_bg['configuration_value'];

$popup2_enabled_query = tep_db_query("SELECT configuration_value FROM " . TABLE_CONFIGURATION . " WHERE configuration_key = 'POPUP2_ENABLED'");
$popup2_enabled_arr   = tep_db_fetch_array($popup2_enabled_query);
$popup2_enabled_value = $popup2_enabled_arr['configuration_value'];
?>
<!doctype html>
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>

<link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/tweak/js/payment_general_templates.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript" src="includes/http.js"></script>
<script type="text/javascript" src="includes/aim.js"></script>
<script type="text/javascript" src="includes/tweak/js/ajax.js"></script>
<script src="includes/javascript/jquery-3.6.0.min.js"></script>
<script>
  // raw relative values from DB
  var popupBgRel = {
    popup1: "<?php echo trim($popup1_bg_value); ?>",
    popup2: "<?php echo trim($popup2_bg_value); ?>"
  };

  // prebuilt bases
  var CATALOG_BASE = "<?php echo HTTP_CATALOG_SERVER . DIR_WS_CATALOG; ?>";

  // colors from DB
  var popupColors = {
    popup1: "<?php echo htmlspecialchars($popup1_bg_color_value); ?>",
    popup2: "<?php echo htmlspecialchars($popup2_bg_color_value); ?>"
  };

  var defaultBackground = "#FF0000";
</script>

<script>
function openMediaPicker(targetInputId, type) {
  window.mediaPickerTarget = targetInputId;
  window.mediaPickerType = (type === 'absolute') ? 'absolute' : 'relative';
  const w=1200,h=800,left=(screen.width-w)/2,top=(screen.height-h)/2;
  window.open('media_picker.php?picker=1', 'mediaPicker',
    `width=${w},height=${h},left=${left},top=${top},resizable=yes,scrollbars=yes`);
}

window.addEventListener('message', function(e){
  const d = e.data || {};
  if (d.type === 'media-pick' && window.mediaPickerTarget) {
    const input = document.getElementById(window.mediaPickerTarget);
    if (!input) return;
    input.value = (window.mediaPickerType === 'absolute') ? d.url : d.rel;
    input.dispatchEvent(new Event('change', { bubbles:true }));
  }
}, false);

</script>


<script>
function testPopup(popupId) {
  var pageInputId = popupId + '_page_id';
  var pageId = $('#' + pageInputId).val();
  if (!pageId) { alert("Please enter a static page ID for " + popupId); return; }

  var rel = popupBgRel[popupId] ? popupBgRel[popupId].trim() : "";
  var color = popupColors[popupId] ? popupColors[popupId].trim() : "";

  if (rel !== "") {
    // have an image path: build full URL and apply
    var full = CATALOG_BASE + rel;
    $('#testPopupModalContent').css({
      'background-image': "url('" + full + "')",
      'background-position': 'top center',
      'background-size': 'cover',
      'background-color': '' // clear any color override
    });
  } else if (color !== "") {
    // no image: use color, and make sure any previous image is cleared
    $('#testPopupModalContent').css({
      'background-image': 'none',
      'background-size': '',
      'background': '',              // clear shorthand if previously set
      'background-color': color
    });
  } else {
    // final fallback
    $('#testPopupModalContent').css({
      'background-image': 'none',
      'background-size': '',
      'background': '',
      'background-color': defaultBackground
    });
  }

  $.ajax({
    url: 'ajax_get_static_page.php',
    method: 'GET',
    data: { page_id: pageId, language_id: 1 },
    success: function(response) {
      $('#testPopupModalBody').html(response);
      var testModal = new bootstrap.Modal(document.getElementById('testPopupModal'));
      testModal.show();
    },
    error: function() { alert("Error retrieving static page content for " + popupId); }
  });
}

</script>

</head>
<body marginwidth="0"  marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr class="dataTableHeadingRow">
					<td valign="top">
					<table border="0" cellpadding="0" cellspacing="0" width="100%">
					<tr>
							<td class="main">
								<b><?php echo HEADING_TITLE;?></b>
							</td>
					</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td valign="top">
					<?php
					if(SHOW_OSCONCERT_HELP=='yes')
					{
					?>
					<div class="alert alert-info" role="alert">
					<?php echo TEXT_IMPORTANT; ?>
					</div>
					<?php
					}
					?>
					</td>		
				</tr>
	<tr>
	<td>
<section id="popups" class="popups">
<div class="container">

<br>

<?php include ('how-to-use-popups.php'); ?>

  <form action="popups.php" method="post">
     <input type="hidden" name="action" value="save">

    <fieldset>
      <legend><?php echo TEXT_POPUP1; ?></legend>
      
	  <label for="popup1_target"><?php echo TEXT_POPUP1_TARGET; ?></label><br>
		<input type="text" id="popup1_target" class="form-control" name="popup1_target" value="<?php echo htmlspecialchars($popup1_target_value); ?>" size="60"><br>
      <label for="popup1_bg"><?php echo TEXT_POPUP1_BACKGROUND_URL; ?></label><br>
<div class="input-group mb-2">
  <input type="text" id="popup1_bg" class="form-control"
         name="popup1_bg"
         value="<?php echo htmlspecialchars($popup1_bg_value); ?>" size="60">
  <button type="button" class="btn btn-outline-secondary"
          onclick="openMediaPicker('popup1_bg','relative')">
    <?php echo TEXT_SELECT_FROM; ?>
  </button>
</div>

	  <label for="popup1_bg_color"><?php echo TEXT_POPUP_BACKGROUND_COLOR; ?></label><br>
<input type="color" id="popup1_bg_color_picker" value="<?php echo htmlspecialchars($popup1_bg_color_value ?: '#333333'); ?>"
       oninput="document.getElementById('popup1_bg_color').value=this.value">
<input type="text" id="popup1_bg_color" class="form-control" name="popup1_bg_color"
       value="<?php echo htmlspecialchars($popup1_bg_color_value); ?>" size="10" placeholder="#333333"><br>

      <label for="popup1_page_id"><?php echo TEXT_POPUP1_STATIC_ID; ?></label><br>
  <input type="text" id="popup1_page_id" class="form-control" name="popup1_page_id" value="<?php echo htmlspecialchars($popup1_page_id_value); ?>" size="10"><br>
      <label for="popup1_enabled"><?php echo TEXT_POPUP1_ENABLE; ?></label>
      <input type="checkbox" id="popup1_enabled" name="popup1_enabled" <?php echo ($popup1_enabled_value ? 'checked' : ''); ?>><br>
      <button type="button" class="btn btn-primary" onclick="testPopup('popup1');"><?php echo TEXT_POPUP1_TEST; ?></button>
    </fieldset>
    <br>
    <input type="submit" class="btn btn-primary" style="padding: 10px;border: 2px solid red;" value="<?php echo TEXT_POPUP_SAVE; ?>">
    <fieldset>
           <legend><?php echo TEXT_POPUP2; ?></legend>
      
	  <label for="popup2_target"><?php echo TEXT_POPUP2_TARGET; ?></label><br>
		<input type="text" id="popup2_target" class="form-control" name="popup2_target" value="<?php echo htmlspecialchars($popup2_target_value); ?>" size="60"><br>
      <label for="popup2_bg"><?php echo TEXT_POPUP2_BACKGROUND_URL; ?></label><br>
<div class="input-group mb-2">
  <input type="text" id="popup2_bg" class="form-control"
         name="popup2_bg"
         value="<?php echo htmlspecialchars($popup2_bg_value); ?>" size="60">
  <button type="button" class="btn btn-outline-secondary"
          onclick="openMediaPicker('popup2_bg','relative')">
    <?php echo TEXT_SELECT_FROM; ?>
  </button>
</div>

	  <label for="popup2_bg_color"><?php echo TEXT_POPUP_BACKGROUND_COLOR; ?></label><br>
<input type="color" id="popup2_bg_color_picker" value="<?php echo htmlspecialchars($popup2_bg_color_value ?: '#333333'); ?>"
       oninput="document.getElementById('popup1_bg_color').value=this.value">
<input type="text" id="popup2_bg_color" class="form-control" name="popup2_bg_color"
       value="<?php echo htmlspecialchars($popup2_bg_color_value); ?>" size="10" placeholder="#333333"><br>

      <label for="popup2_page_id"><?php echo TEXT_POPUP1_STATIC_ID; ?></label><br>
  <input type="text" id="popup2_page_id" class="form-control" name="popup2_page_id" value="<?php echo htmlspecialchars($popup2_page_id_value); ?>" size="10"><br>
      <label for="popup1_enabled"><?php echo TEXT_POPUP2_ENABLE; ?></label>
      <input type="checkbox" id="popup2_enabled" name="popup2_enabled" <?php echo ($popup2_enabled_value ? 'checked' : ''); ?>><br>
      <button type="button" class="btn btn-primary" onclick="testPopup('popup2');"><?php echo TEXT_POPUP2_TEST; ?></button>
    </fieldset>
    <br>
    <br>
    <input type="submit" class="btn btn-primary" style="padding: 10px;border: 2px solid red;" value="<?php echo TEXT_POPUP_SAVE; ?>">
  </form>
</div>
</section>
</td>
</tr>
</table>
<!-- body_text_eof //-->
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- Test Popup Modal -->
<div class="modal fade" id="testPopupModal" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content" id="testPopupModalContent" style="position: relative;">
      <!-- Close button positioned absolutely -->
      <button type="button" class="btn-close popup-close" data-bs-dismiss="modal" aria-label="Close"></button>
      <div class="modal-body" id="testPopupModalBody">
        <!-- The static page content will be loaded here -->
      </div>
    </div>
  </div>
</div>


<!-- footer_eof //-->
<script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>

</html>

<?php require(DIR_WS_INCLUDES . 'application_bottom.php');?>