<?php 
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/* Template Name: Popup Default */

$wdpops = WDPOP_Helper::get_all_active_popups();

foreach ( $wdpops as $item_id => $this_popup ) {

  if ( ! WDPOP_Helper::should_show_popup( $this_popup ) ) {
    continue; // Hide popup if it is hidden by display options
  }

  // Base values
  $popup_name              = $this_popup['popup_name'];
  $item_tab                = $this_popup['wdpop_item_tab'];
  $wdpop_image             = $item_tab['popup_image'] ?? '';
  $wdpop_image_link        = $item_tab['popup_image_link']['url'] ?? '';
  $wdpop_image_link_target = $item_tab['popup_image_link']['target'] ?? '';
  $popup_type              = $item_tab['popup_type'] ?? '';

  if ( $popup_type === 'template' ) {
    $template = $item_tab['template'] ?? '';
    if(!empty($template)){
      $template_content = WDPOP_Helper::get_template_content($template);
    }else{
      $template_content = 'Please, Select a Template';
    }
  }
  
  // Width classes
  $width_class      = $item_tab['popup_width'] ? " {$item_tab['popup_width']}" : '';
  $fullscreen_class = $item_tab['fullscreen_popup'] ? " {$item_tab['fullscreen_popup']}" : '';

  // Custom width style
  $style_custom_width = '';
  if ( $width_class === ' modal-custom-width' && ! empty( $item_tab['popup_custom_width'] ) ) {
    $style_custom_width = sprintf(
      " style='max-width:%s%s'",
      esc_attr( $item_tab['popup_custom_width']['width'] ),
      esc_attr( $item_tab['popup_custom_width']['unit'] )
    );
  }

  // Position
  $position = $item_tab['popup_position'] ? " {$item_tab['popup_position']}" : '';
  if ( strpos( $position, 'middle' ) || strpos( $position, 'bottom' ) ) {
    $position .= ' modal-dialog-centered';
  }
  
  // Force Interaction
  $backdrop   = ! empty( $item_tab['force_interaction'] ) ? 'static' : 'true';
  
  // Modal IDs
  $modal_id    = 'wingModal-' . esc_attr( $item_id );
  $modal_label = $modal_id . 'Label';

  // Content values
  $popup_title   = ! empty( $item_tab['popup_title'] ) ? wp_kses_post( $item_tab['popup_title'] ) : '';
  $popup_content = ! empty( $item_tab['Popup_content'] ) ? wp_kses_post( $item_tab['Popup_content'] ) : '';
  $popup_close   = ! empty( $item_tab['popup_close_text'] ) ? esc_html( $item_tab['popup_close_text'] ) : '';
  $cta_text      = ! empty( $item_tab['cta_link']['text'] ) ? esc_html( $item_tab['cta_link']['text'] ) : '';
  $cta_url       = ! empty( $item_tab['cta_link']['url'] ) ? esc_url( $item_tab['cta_link']['url'] ) : '';
  $cta_target    = ! empty( $item_tab['cta_link']['target'] ) ? esc_attr( $item_tab['cta_link']['target'] ) : '';
  $dialog_classes = $width_class . $fullscreen_class . $position;
?>

  <!-- Wing Popup -->
  <div class="modal fade wing-modal--item wing-modal--<?= esc_attr( $item_id ); ?>"
      id="<?= $modal_id; ?>"
      wing-popup-id="<?= esc_attr( $item_id ); ?>"
      tabindex="-1"
      aria-labelledby="<?= $modal_label; ?>"
      data-bs-backdrop="<?= esc_attr( $backdrop ); ?>"
      aria-hidden="true">

    <div class="modal-dialog<?= esc_attr( $dialog_classes ); ?>" <?= wp_kses_post( $style_custom_width ); ?>>
      <div class="modal-content wing--popup-contentX wdpop_core--popup-area">

        <span type="buttonX" class="btn-closeX wing--popup-close-btn" data-bs-dismiss="modal" aria-label="Close">&times;</span>

        <div class="wing--popup-wrapper">

        
          <?php if ( $popup_type === 'template' ) { ?>
            <div class="wing--popup-template-wrap">

              <?= $template_content; ?>

            </div>
          <?php } else{ ?>        

            <?php if ( $wdpop_image ) { ?>
              <div class="wing--popup-image">
                <?php if ( $wdpop_image_link ) { ?>
                  <a href="<?= esc_url( $wdpop_image_link ); ?>" target="<?= esc_attr( $wdpop_image_link_target ); ?>">
                <?php } ?>

                <img src="<?= esc_url( $wdpop_image ); ?>" 
                    alt="<?= esc_attr( $popup_name ); ?>" 
                    loading="lazy" 
                    class="wing-popup--image">

                <?php if ( $wdpop_image_link ) { ?>
                  </a>
                <?php } ?>
              </div>
            <?php } ?>

            <?php if ( $popup_type === 'image_and_content' ) { ?>
              <div class="wing--popup-content-wrap text-center">

                <?php if ( $popup_title ) { ?>
                  <h1 class="fs-5 wing--popup-title"><?= $popup_title; ?></h1>
                <?php } ?>

                <?php if ( $popup_content ) { ?>
                  <div class="wing--popup-content"><?= do_shortcode($popup_content); ?></div>
                <?php } ?>

                <?php if ( $popup_close ) { ?>
                  <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?= $popup_close; ?></button>
                <?php } ?>

                <?php if ( $cta_text ) { ?>
                  <a type="button" class="btn btn-primary" href="<?= $cta_url; ?>" target="<?= $cta_target; ?>"><?= $cta_text; ?></a>
                <?php } ?>

              </div>
            <?php } ?> 
            
          <?php } ?>




        </div>
      </div>
    </div>
  </div>

<?php } ?>
