<?php


$show_pop_up = false;
// $lp_eu_get_visibility check if global setting is on of off
$lp_eu_get_visibility = get_option('lp_eu_cookie_enable');

//get post id here.
$post_id = get_the_ID();

//$lp_eu_get_visibility_post_id  check if cookies is enable for perticular page 
$lp_eu_get_visibility_post_id   = get_option('lp_eu_cookie_enable_' . $post_id );


/**
 *Step1) first step is to check if it's on or off for htis page.
 *Step2) second step is to check if it's enable in global settings.
*/

?>

                   <?php 
                   //if post id is turn ON show cookies 
                   //if post id is turn OFF dont show cookies


                   //tasks: if cookies is turned on for page id
                   //create cookies for this page with lp_eu_agree_{PAGE_ID} default value: NOT_ACCEPTED
                   //now on each page check if cookies is setup for current page. if value is NOT_ACCEPTED show popup
                   //if value is ACCEPTED don't show popup.

                    if ($lp_eu_get_visibility_post_id == "ON" ) {
                        //if its on mean it's turn on for this page no matter what's the global settings

			              //  if( $_COOKIE['lp_eu_agree'] != 'YES' )  { 
			              //  	$show_pop_up = true;
			              //  }

                         $cookies_var_name = 'lp_eu_agree_' . $post_id;

                        
                             $setup_page_cookie = false;

                            if ($_COOKIE[$cookies_var_name] != 'ACCEPTED') {
                                // echo "&&&";
                               // setcookie($cookies_var_name, "NOT_ACCEPTED", time()+60*60*24*365, '/'); 
                                $setup_page_cookie = true;
                                $show_pop_up = true;
                            }

			            } else if($lp_eu_get_visibility_post_id == "OFF" ) {
                            //if $lp_eu_get_visibility_post_id == "OFF" means it set to turn of for this page. 
                          

                   } else {
                            //code will only reach here when cookies is touched at page level

                    // echo "I m here";
			                
			                if($lp_eu_get_visibility=='ON' && !isset($_COOKIE['lp_eu_agree'])) {

			                    if($_COOKIE['lp_eu_agree'] != 'ACCEPTED')  {
			                    	$show_pop_up = true;
			                    }
			                }
			                
			            }


          //$show_pop_up will be true only when we need to show popup
           if ($show_pop_up) {
                $lp_eu_theme_css        = get_option('lp_eu_theme_css');
                    $lp_eu_title            = get_option('lp_eu_cookie_title');
                    $lp_eu_message          = get_option('lp_eu_cookie_message');
                    $lp_eu_box_color        = get_option('lp_eu_box_color');
                    $lp_eu_button_color     = get_option('lp_eu_button_color');
                    $lp_eu_button_text_color= get_option('lp_eu_button_text_color');
                    $lp_eu_text_color       = get_option('lp_eu_text_color');
                    $lp_eu_button_text      = get_option('lp_eu_button_text');
                    $lp_eu_link_text        = get_option('lp_eu_link_text');
                    $lp_eu_link_url         = get_option('lp_eu_link_url');
                    $lp_eu_text_size        = get_option('lp_eu_text_size');
                    $lp_eu_link_color       = get_option('lp_eu_link_color');
                    $lp_eu_head_text_size   = $lp_eu_text_size + 4;

                    $lp_eu_html='<div id="lp_eu_container">';

                    $lp_eu_html.='<table id="lp_eu_table" class="lp_eu_table" style="border:none;"><tr><td width="90%">';

                    if(!empty($lp_eu_title)){
                        $lp_eu_html.='<b id="lp_eu_title">'.$lp_eu_title.' </b>';
                     
                    }

                    $lp_eu_html.='<p id="lp_eu_body">'.stripslashes(html_entity_decode($lp_eu_message));
                    $lp_eu_html.=' <a id="lp_eu_link" target="_blank" href="'.$lp_eu_link_url.'">'.$lp_eu_link_text.'</a></p></td>';
                    $lp_eu_html.='<td width="10%"><p id="lp_eu_btnContainer"><button type="button" id="lp_eu_btn_agree">'.($lp_eu_button_text != '' ? $lp_eu_button_text : 'I agree').'</button></p></td></tr></table>';
                    $lp_eu_html.='</div>';
          

            ?>

<style> .lp_eu_table td{
                            border:none;
                        }
                        #lp_eu_table{
                            border-color:rgba(255,255,255,0.9);
                            margin-bottom : 0em;
                            margin-top : 0em;
                            width: 100%;
                        }
                        #lp_eu_table td {
                            vertical-align: middle;
                        }
                        #lp_eu_table th, td {
                            padding: inherit;
                        }
                        #lp_eu_container{
                            display: none;
                            margin: 1%;
                            padding: 5px 10px;
                            width: 98%;
                            z-index: 9999;
                            position: fixed;
                            bottom: 0px;
                            border-radius: 10px;
                            box-shadow: 2px 2px 5px #888 inset;
                            box-sizing : border-box;
                            opacity: 0.8;
                        }
                        #lp_eu_btnContainer{
                        text-align: center;
                        }

                        #lp_eu_title{
                            margin: inherit;
                        }
                        #lp_eu_body,#lp_eu_btnContainer{
                            margin: 0px;
                        }
                        a#lp_eu_link{
                          border-bottom: 1px dotted;
                          text-decoration: none;
                        }
                        @media only screen and (max-width: 360px) {
                            #lp_eu_table td {
                                border-width: 0 1px 1px 0;
                                box-sizing: border-box;
                                display: block;
                                width: 100%;
                            }
                        }
                    </style>

            <script type="text/javascript">

            	jQuery(document).ready(function(){
    
                    var page_cookies_activate = false;

                  

                   <?php if ($setup_page_cookie) { ?>  
                        //if $setup_page_cookies is ture it mean jquery need to add cookies for this page
                        page_cookies_activate = true;
                        jQuery.cookie('lp_eu_agree_<?php echo $post_id ?>' , 'NOT_ACCEPTED', { expires: 7, path: '/' });
                     <?php } else if($setup_global_cookie){ ?>
                      jQuery.cookie('lp_eu_agree' , 'NOT_ACCEPTED', { expires: 7, path: '/' });
                      <?php }  ?>




                     <?php if ($show_pop_up) { ?>
                       lp_eu_show_cookie_bar();
                     <?php }  ?>




                     jQuery('#lp_eu_btn_agree').click(function (){

                        if (page_cookies_activate) {
                               if (jQuery.cookie('lp_eu_agree_<?php echo $post_id ?>') == "NOT_ACCEPTED") {
                          jQuery.cookie('lp_eu_agree_<?php echo $post_id ?>', 'ACCEPTED', { expires: 7, path: '/' });
                     // jQuery.cookie('lp_eu_agree', 'NO', { expires: 7, path: '/' });
                       // lp_eu_show_cookie_bar();
                       jQuery('#lp_eu_container').hide(500);
                     }

                        } else {
                            jQuery.cookie('lp_eu_agree', 'ACCEPTED', { expires: 7, path: '/' });
                            jQuery('#lp_eu_container').hide(500);
                        }
                      
                   


                      
                     });



                    });


            	function lp_eu_show_cookie_bar(){
                        jQuery('body').prepend('<?php echo $lp_eu_html; ?>');
                        <?php if($lp_eu_theme_css == 0){ ?>
                                 // container deisgn
                                 jQuery('#lp_eu_container').css( { 'background-color' : '<?php echo $lp_eu_box_color; ?>',
                                                                   'border-color'     : '<?php echo $lp_eu_text_color; ?>',
                                                                   'color'            : '<?php echo $lp_eu_text_color; ?>' });

                                 //Text font
                                 jQuery('p#lp_eu_body').css('font-size', '<?php echo $lp_eu_text_size."px"; ?>');

                                 // Title design
                                 jQuery('#lp_eu_title').css('font-size','<?php echo $lp_eu_head_text_size."px"; ?>');

                                 // agree button design
                                 jQuery('#lp_eu_btn_agree').css( { 'background-color' : '<?php echo $lp_eu_button_color; ?>',
                                                                   'color'            : '<?php echo $lp_eu_button_text_color; ?>',
                                                                   'border-style'     : 'none',
                                                                   'border'           : '1px solid #bbb',
                                                                   'border-radius'    : '5px',
                                                                   'box-shadow'       : 'inset 0 0 1px 1px #f6f6f6',
                                                                   'line-height'      : 1,
                                                                   'padding'          : '3px 5px',
                                                                   'text-align'       : 'center',
                                                                   'text-shadow'      : '0 1px 0 #fff',
                                                                   'cursor'           : 'pointer',
                                                                   'font-size'        : '<?php echo $lp_eu_text_size."px"; ?>'
                                                                });

                                // link color
                                jQuery('#lp_eu_link').css({ 'color' : '<?php echo $lp_eu_link_color; ?>' });

                        <?php }
                              else {
                                // container deisgn
                                ?>
                                jQuery('#lp_eu_container').css({ 'background-color' : '<?php echo "inherit"; ?>',
                                                                 'color'            : '<?php echo "inherit"; ?>' });

                       <?php  }
                        ?>
                        jQuery('#lp_eu_container').show(500);
                       }


            </script>

            <?php

             } // if show_popup if condition ends here.