<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * The class for the admin-specific functionality of the plugin.
 */
class SIMPPONO_Frontend
{
    /**
     * Initialize the class sets its properties.
     *
     * @since 1.1
     */
    function __construct()
    {
        $this->init();
    } // __construct end;

    /**
     * Initialize the class sets its properties.
     *
     * @since 1.1
     */
    public function init()
    {
        add_action('wp_enqueue_scripts', array($this, 'simppono_enqueue_scripts'));
        add_action('wp_footer', array($this, 'simppono_popup_model'));
    } // init(); end

    /**
     * Frontend Script enqueue
     *
     * @since 1.1
     */
    public function simppono_enqueue_scripts()
    {
        wp_enqueue_style('simppono-frontend-style', SIMPPONO_PLUGINURL . 'includes/frontend/css/popup-frontend.css', array(), '1.1');
        wp_enqueue_script('simppono-script', SIMPPONO_PLUGINURL . 'includes/frontend/js/bootstrap.bundle.min.js', array('jquery'), 'v4.5.2', true);
        wp_enqueue_script('simppono-bootstrap-modal', SIMPPONO_PLUGINURL . 'includes/frontend/js/modal-script.js', array('jquery'), '1.1', true);

        // Localize the script with new data
        wp_localize_script('simppono-bootstrap-modal', 'simppono_ajax_object', array(
            'ajax_url' => admin_url('admin-ajax.php'),
            'simppono_setcookieexpire'    => get_option('simppono_setcookieexpire')
        ));
    } // simppono_enqueue_scripts(); end

    /**
     * Function to display the popup modal
     *
     * @since 1.1
     */
    public function simppono_popup_model()
    {
        $background_color       = get_option('simppono_background_color', '');
        $text_color             = get_option('simppono_text_color', '');
        $heading                = get_option('simppono_heading', '');
        $description            = get_option('simppono_description', '');
        $cta_button_text        = get_option('simppono_cta_button', '');
        $cta_button_link        = get_option('simppono_cta_button_link', '');
        $cta_bg_color           = get_option('simppono_cta_button_bg_color', '');
        $cta_text_color         = get_option('simppono_cta_button_text_color', '');
        $image_id               = get_option('simppono_image_upload', '');
        $selected_option        = get_option('simppono_select_option', '');
        $simppono_popup_width        = get_option('simppono_popup_width', '');
        $simppono_enabled       = get_option('simppono_enabled', 'off');

        if ($simppono_enabled === 'on') {

            if (!empty($background_color) || !empty($text_color) || !empty($heading) || !empty($description) || !empty($cta_button_text) || !empty($cta_button_link) || !empty($image_url)) {

                // Things that you want to do.
                $imgclass = "no-image";
                $image_attributes = '';
                if (!empty($image_id)) {
                    $imgclass = "";
                    $image_attributes = wp_get_attachment_image($image_id, 'large');
                }
                $section_class = $selected_option . ' ' . $imgclass;

                $modal_width = '1000px';
                if($simppono_popup_width != ''){
                    $modal_width = $simppono_popup_width. 'px';
                }
                ?>
                <style>
                .modal.fade.simppono-popup-model .modal-dialog {
                    width: <?php echo esc_attr($modal_width); ?>;
                    max-width: 100%;
                }
                </style>
                
                <?php 
?>

                <div id="spnModalSection" class="modal fade simppono-popup-model" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content" style="background-color: <?php echo esc_attr($background_color); ?>;">

                            <div class="modal-header">
                                <button id="spnClosePopup" type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <svg width="35px" height="35px" viewBox="0 0 24 24" fill="#333333" xmlns="http://www.w3.org/2000/svg">
                                        <path fill-rule="evenodd" clip-rule="evenodd" d="M5.29289 5.29289C5.68342 4.90237 6.31658 4.90237 6.70711 5.29289L12 10.5858L17.2929 5.29289C17.6834 4.90237 18.3166 4.90237 18.7071 5.29289C19.0976 5.68342 19.0976 6.31658 18.7071 6.70711L13.4142 12L18.7071 17.2929C19.0976 17.6834 19.0976 18.3166 18.7071 18.7071C18.3166 19.0976 17.6834 19.0976 17.2929 18.7071L12 13.4142L6.70711 18.7071C6.31658 19.0976 5.68342 19.0976 5.29289 18.7071C4.90237 18.3166 4.90237 17.6834 5.29289 17.2929L10.5858 12L5.29289 6.70711C4.90237 6.31658 4.90237 5.68342 5.29289 5.29289Z" fill="#333333" />
                                    </svg>
                                </button>
                            </div>

                            <div class="modal-body <?php echo esc_attr($section_class); ?> ">

                                <div class="model-body-left">
                                    <?php if (!empty($heading)): ?>
                                        <h5 class="modal-title" id="exampleModalLabel" style="color: <?php echo esc_attr($text_color); ?>"><?php echo esc_html($heading); ?></h5>
                                    <?php endif; ?>

                                    <?php if (!empty($description)): ?>
                                        <p style="color: <?php echo esc_attr($text_color); ?>"><?php echo esc_html($description); ?></p>
                                    <?php endif; ?>

                                    <?php if (!empty($cta_button_text) && !empty($cta_button_link)): ?>
                                        <a href=" <?php echo esc_url($cta_button_link); ?>" class="btn btn-secondary" style="background-color: <?php echo esc_attr($cta_bg_color); ?>; color: <?php echo esc_attr($cta_text_color); ?>;"><?php echo esc_html($cta_button_text); ?></a>
                                    <?php endif; ?>
                                </div>
                                <?php if (!empty($image_id)): ?>
                                    <div class="model-body-right">
                                        <?php echo wp_kses_post($image_attributes); ?>
                                    </div>
                                <?php endif; ?>
                            </div>

                        </div>
                    </div>
                </div>
<?php
            }
        }
    } // simppono_popup_model(); end
} // SIMPPONO_Frontend(); end

$frontend = new SIMPPONO_Frontend();
