<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
    $_helper = $this->helper('Smartwave\Porto\Helper\Data');

    $popup = $_helper->getConfig('porto_settings/newsletter');
    if($popup['enable']) {
        $delay = $popup["delay"];
?>
<script type="text/javascript">
require([
        'jquery',
        'jquery/jquery.cookie',
		'fancybox/js/jquery.fancybox'
    ], function ($) {
        $(document).ready(function(){
        <?php if($popup['enable'] == 1): ?>
            if($("body").hasClass("cms-index-index")) {
        <?php endif; ?>
                var check_cookie = $.cookie('newsletter_popup');
                if(window.location!=window.parent.location){
                    $('#newsletter_popup').remove();
                } else {
                    if(check_cookie == null || check_cookie == 'shown') {
                        setTimeout(function(){
                            beginNewsletterForm();
                        }, <?php echo $delay; ?>);
                    }
                    $('#newsletter_popup_dont_show_again').on('change', function(){
                        if($(this).length){
                            var check_cookie = $.cookie('newsletter_popup');
                            if(check_cookie == null || check_cookie == 'shown') {
                                $.cookie('newsletter_popup','dontshowitagain');
                            }
                            else
                            {
                                $.cookie('newsletter_popup','shown');
                                beginNewsletterForm();
                            }
                        } else {
                            $.cookie('newsletter_popup','shown');
                        }
                    });
                }
        <?php if($popup['enable'] == 1): ?>
            }
        <?php endif; ?>
        });

        function beginNewsletterForm() {
            $.fancybox({
                'padding': '0px',
                'autoScale': true,
                'transitionIn': 'fade',
                'transitionOut': 'fade',
                'type': 'inline',
                'href': '#newsletter_popup',
                'onComplete': function() {
                    $.cookie('newsletter_popup', 'shown');
                },
                'tpl': {
                    closeBtn: '<a title="Close" class="fancybox-item fancybox-close fancybox-newsletter-close" href="javascript:;"></a>'
                },
                'helpers': {
                    overlay: {
                        locked: false
                    }
                }
            });
            $('#newsletter_popup').trigger('click');
        }
});
</script>
<style type="text/css">
<?php
    if($popup["custom_style"]){
        echo $popup["custom_style"];
    }
?>
#newsletter_popup{
<?php
    if($popup["width"]){
        echo "width:".$popup["width"].";";
    }
    if($popup["height"]){
        echo "min-height:".$popup["height"].";";
    }
    if($popup["bg_color"]){
        echo "background-color:".$popup["bg_color"].";";
    }
    if(isset($popup["bg_image"]) && $popup["bg_image"]){
        $folderName = \Smartwave\Porto\Model\Config\Backend\Image\Newsletterbg::UPLOAD_DIR;
        $path = $folderName . '/' . $popup["bg_image"];
        $imageUrl = $_helper->getBaseUrl() . $path;
?>
    background-image:url(<?php echo $imageUrl;?>);
<?php
    }
?>
}
</style>
<div class="newsletter row" id="newsletter_popup" style="display: none;">
    <div class="col-sm-8 pr-lg-4 vc_column_container col-md-7 col-10">
        <?php
            if(isset($popup["logo_src"]) && $popup["logo_src"]){
                $folderName = \Smartwave\Porto\Model\Config\Backend\Image\Newsletterlogo::UPLOAD_DIR;
                $path = $folderName . '/' . $popup["logo_src"];
                $logoUrl = $_helper->getBaseUrl() . $path;
        ?>
        <div class="text_left mb-4 pb-1">
          <img src="<?php echo $logoUrl; ?>" alt="" />
        </div>
        <?php
            }
        ?>
        <?php echo $popup["content"]; ?>
        <form class="form subscribe mb-1"
            novalidate
            action="<?php echo $block->getFormActionUrl() ?>"
            method="post"
            data-mage-init='{"validation": {"errorClass": "mage-error"}}'
            id="newsletter-validate-detail">
            <div class="field newsletter">
                <div class="control">
                    <input name="email" type="email" id="footer_newsletter" placeholder="<?php echo __("Your email address");?>"
                                data-validate="{required:true, 'validate-email':true}"/>
                </div>
            </div>
            <div class="actions">
                <button class="action subscribe primary" title="<?php echo __('Submit') ?>" type="submit">
                    <span><?php echo __('Submit') ?></span>
                </button>
            </div>
        </form>
        <p class="mb-0">
          <label class="checkbox checkbox-inline">
            <input type="checkbox" id="newsletter_popup_dont_show_again"/><?php echo __("Don't show this popup again"); ?>
          </label
        </div>
    </div>
</div>
<?php
    }
?>
