<meta charset="utf-8">
<title><?php echo $title; ?></title>
<base href="<?php echo $base; ?>"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">

<?php foreach ($links as $link) { ?>
<link href="<?php echo $link['href']; ?>" rel="<?php echo $link['rel']; ?>"/>
<?php } ?>

<?php if ( is_file( DIR_RESOURCE . $icon ) ) {  ?>
<link href="resources/<?php echo $icon; ?>" type="<?php echo mime_content_type(DIR_RESOURCE . $icon)?>>" rel="icon" />
<?php } ?>

<link rel="stylesheet" type="text/css" href="<?php echo $template_dir; ?>stylesheet/stylesheet.css" />

<?php foreach ($styles as $style) { ?>
<link rel="<?php echo $style['rel']; ?>" type="text/css" href="<?php echo $style['href']; ?>"
      media="<?php echo $style['media']; ?>"/>
<?php } ?>

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
    if (typeof jQuery == 'undefined') {
        var include = '<script type="text/javascript" src="<?php echo $template_dir; ?>javascript/jquery/jquery-3.7.1.min.js"><\/script>';
        document.write(include);
    }
<?php if($retina){
    $samesite = ((defined('HTTPS') && HTTPS) ? 'None; secure=1;' : 'lax; secure=0;');
?>
    if ((window.devicePixelRatio === undefined ? 1 : window.devicePixelRatio) > 1) {
        document.cookie = 'HTTP_IS_RETINA=1; path=/; samesite=<?php echo $samesite; ?>';
    }
<?php } ?>
</script>
<script type="text/javascript" src="<?php echo $template_dir; ?>javascript/jquery/jquery-migrate.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/js-cookie@3.0.5/dist/js.cookie.min.js"></script>
<script type="text/javascript" src="<?php echo $template_dir; ?>javascript/jquery/jqueryui.min.js"></script>
<script type="text/javascript">
    jQuery.browser = {};
    (function () {
        jQuery.browser.msie = false;
        jQuery.browser.version = 0;
        if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
            jQuery.browser.msie = true;
            jQuery.browser.version = RegExp.$1;
        }
    })();
</script>
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.js"></script>

<script type="text/javascript" src="<?php echo $template_dir; ?>javascript/bootstrap.min.js"></script>
<script defer type="text/javascript" src="<?php echo $template_dir; ?>javascript/tinymce/tinymce.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.13/flatpickr.min.css"/>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.13/flatpickr.min.js"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.13/plugins/rangePlugin.min.js"></script>
<?php if($code != 'en'){ ?>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.13/l10n/<?php echo $code; ?>.min.js"></script>
<?php } ?>


<?php foreach ($scripts as $script) { ?>
<script type="text/javascript" src="<?php echo $script; ?>"></script>
<?php } ?>
<script type="text/javascript" src="<?php echo $template_dir; ?>javascript/aform.js"></script>

<script type="text/javascript">
const amp_connect_url = '<?php echo $amp_connect_url;?>';
//define tinymce config
var mcei = {
    formats: {
        alignleft: [{
            selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li",
            styles: {textAlign: "left"}
        }, {selector: "img,table,dl.wp-caption", classes: "alignleft"}],
        aligncenter: [{
            selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li",
            styles: {textAlign: "center"}
        }, {selector: "img,table,dl.wp-caption", classes: "aligncenter"}],
        alignright: [{
            selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li",
            styles: {textAlign: "right"}
        }, {selector: "img,table,dl.wp-caption", classes: "alignright"}],
        strikethrough: {inline: "del"}
    },
    forced_root_block: false,
    cleanup: false,
    verify_html: false,
    trim_span_elements: false,
    fix_list_elements: false,
    relative_urls: false,
    remove_script_host: false,
    convert_urls: false,
    browser_spellcheck: true,
    entities: "38,amp,60,lt,62,gt",
    entity_encoding: "raw",
    keep_styles: false,
    cache_suffix: "abc-mce-433-20160114",
    preview_styles: "font-family font-size font-weight font-style text-decoration text-transform",
    end_container_on_empty_block: true,
    editimage_disable_captions: false,
    editimage_html5_captions: true,
    plugins: "charmap,lists,media,fullscreen,link,table",
    resize: true,
    menubar: false,
    autop: true,
    indent: false,
    toolbar: "undo redo bold italic strikethrough bullist forecolor backcolor numlist blockquote hr alignleft aligncenter alignright link spellchecker dfw fullscreen table",
    selector: 'textarea',
    valid_elements: '*[*]',
    valid_children: "+body[style]",
    extended_valid_elements: 'script[language|type|src]',
    invalid_elements: "...",
    tabfocus_elements: "content-html,save-post",
    body_class: "content post-type-post post-status-auto-draft post-format-standard locale-en-gb",
    autoresize_on: true,
    add_unload_trigger: false,
    height: '100%'
};



$(document).ready(function () {
    //system check warnings
    <?php if($system_error ?? '') { ?>
        error_alert(<?php js_echo($system_error); ?>, false);
    <?php } ?>
    <?php if($system_warning ?? '') { ?>
        warning_alert(<?php js_echo($system_warning); ?>, false);
    <?php } ?>
    <?php if($system_notice ?? '') { ?>
        info_alert(<?php js_echo($system_notice); ?>, false);
    <?php } ?>
  
    numberSeparators = {decimal: <?php js_echo($decimal_point); ?>, thousand: <?php js_echo($thousand_point); ?> };
});

//periodical updater of new message notifier
var growl = null;
var alertcount = 3;
var admin_background_autorefresh_enabled = <?php echo $admin_background_autorefresh_enabled ? 'true' : 'false'; ?>;
var system_checker = function () {
    if (!admin_background_autorefresh_enabled) {
        return;
    }
    if(alertcount <= 0) {
        return;
    }
    $.ajax({
        async: false,
        cache: false,
        url: '<?php echo $system_checker_url?>',
        success: function(data) {
            if(data != null && data != undefined) {
                if(growl != null && growl != undefined ) {
                    growl.close();
                }
                growl = showSystemAlert(data);
            }
        },
        complete: function() {
            // Schedule the next request when the current one's complete
            alertcount--;
            if (admin_background_autorefresh_enabled) {
                setTimeout(system_checker, 600000);
            }
        }
    });
};

var showSystemAlert = function(data){
    if(data.hasOwnProperty('error')){
        return error_alert(data.error, false);
    }
    if(data.hasOwnProperty('warning')){
        return warning_alert(data.warning, false);

    }
    if(data.hasOwnProperty('notice')){
        return info_alert(data.notice, true);
    }
}


var wrapConfirmDelete = function(){
    var wrapper = '<div class="btn-group dropup" />';
    var popover, href;

    $('a[data-confirmation="delete"]').each( function(){
        if($(this).attr('data-toggle') === 'dropdown' ){ return;}
        var action = $(this).attr('onclick');
        if ( action ) {
            action = 'onclick="'+action+'"';
        } else {
            href = $(this).attr('href');
            if(!href){ return;}
            if(href.length ===0 || href === '#'){ return;}
            action = 'href="' + href +'"';
        }
        
        var conf_text = $(this).attr('data-confirmation-text');
        if (!conf_text) {
            conf_text = <?php js_echo($text_confirm); ?>;
        }
        
        $(this).wrap(wrapper);
        popover = '<div class="confirm_popover dropdown-menu dropdown-menu-right alert alert-danger" role="menu">'+
                    '<h5 class="center">'+ conf_text +'</h5>'+
                    '<div class="center">'+
                    '<a class="btn btn-danger" ' + action + ' ><i class="fa fa-trash-o"></i>&nbsp;' +
                    <?php js_echo($text_yes);?> +
                    '</a>&nbsp;&nbsp;' +
                    '<a class="btn btn-default"><i class="fa fa-undo"></i>&nbsp;'+
                    <?php js_echo($text_no);?> +
                    '</a></div></div>';
        $(this).after(popover);
        $(this).attr('onclick','return false;');
        $(this).attr('data-toggle','dropdown').addClass('dropdown-toggle');
    });
}

$(document).on('change', wrapConfirmDelete);

//periodical updater of new message notifier
var notifier_updater = function () {
    if (!admin_background_autorefresh_enabled) {
        return;
    }
    $.ajax({
        url: '<?php echo $notifier_updater_url?>',
        success: buildNotifier,
        complete: function() {
          // Schedule the next request when the current one's complete
          if (admin_background_autorefresh_enabled) {
              setTimeout(notifier_updater, 50000);
          }
        }
    });
}

var buildNotifier = function(data){
    $('.new_messages .badge').html(data.total);
    $('.new_messages .dropdown-menu-head h5.title').html(data.total_title);
    var  list = $('.new_messages ul.dropdown-list.gen-list');
    list.html('');
    var html = '';
    var mes;
    for(var k in data.shortlist){
        mes = data.shortlist[k];

        var iconclass='';
        var badgeclass='';
        if(mes.status === 'N'){
            iconclass = 'fa-info';
            badgeclass = 'success';
        }else if(mes.status === 'W' || mes.status === 'E'){
            iconclass = 'fa-warning';
            badgeclass = 'danger';
        }
        html = '<li '+ (mes.viewed<1 ? 'class="new"': '')+ '>' ;
        html += '<a class="message-'+badgeclass+'" href="'+mes.href+'" data-toggle="modal" data-target="#message_modal"><span class="thumb"><p class="fa ' + iconclass + ' fa-3 '+badgeclass+'"></p></span>';
        html += '<span class="desc"><span class="name">'+mes.title + (mes.viewed<1 ? '<span class="badge badge-'+badgeclass+'">new</span>': '')+'</span>';
        html += '<span class="msg">'+mes.message +'</span></span></a></li>';
        list.append(html);
    }

    list.append('<li class="new"><a href="<?php echo $message_manager_url; ?>"><?php echo $text_read_all_messages; ?></a></li>');
}
<?php if($this->user->isLogged()){?>
$(document).ready(function(){
    if (admin_background_autorefresh_enabled) {
        notifier_updater();
        system_checker();
        $('#message_modal').on('hide.bs.modal', notifier_updater );
    }
<?php
    //do ajax call to check extension updates
    if($check_updates_url ?? ''){ ?>
    $.get('<?php echo $check_updates_url?>');
<?php }?>
});
<?php } ?>
</script>
<?php 
    //NOTE: More JS loaded in page.tpl. This is to improve performance. Do not move above to page.tpl
?>
