<?php
if ( ! defined( 'ABSPATH' ) ) exit; ?>

    <head>
        <meta name="viewport" content="width=device-width; initial-scale=0.75; maximum-scale=1.0; user-scalable=0;">
        <meta name="viewport" content="width=device-width" /> </head>
    <?php
require_once( 'legalPages.php' );
global $wpdb;
$lpObj = new legalPages();
$lpObj->lp_enqueue_editor();
$mode = isset($_REQUEST['mode'])?$_REQUEST['mode']:'';
$page = isset($_REQUEST['page'])?$_REQUEST['page']:'';
$baseurl = esc_url(get_bloginfo('url'));
?>
        <div style="width:1000px;float:left;">
            <h1 class="logo-hh"><img style="width: 142px;" src="<?php echo plugin_dir_url(__FILE__); ?>image/wpLegalEagle2.png"></h1> </div>
        <div style="clear:both;"></div>
        <div class="wrap">
            <?php
if(isset($_REQUEST['mode']) && $_REQUEST['mode']=='delete' && current_user_can('manage_options'))
{
    $wpdb->delete($lpObj->popuptable,array('id' => $_REQUEST['lpid']),array('%d'));
}
if(!empty($_POST) && isset($_POST['lp-submit'])) :
$title = isset($_POST['lp-title'])?$_POST['lp-title']:'';
$content = isset($_POST['lp-content'])?$_POST['lp-content']:'';
$content = stripslashes_deep($content);
if($mode=='edit'){
	$update=$wpdb->update($lpObj->popuptable,array('popupName'=>sanitize_text_field(esc_attr($_POST['lp-name'])),'content'=>$content),array('id'=>$_REQUEST['lpid']),array('%s','%s'));

	if($update){
 ?>
                <div id="message">
                    <p><span class="label label-success myAlert"><?php _e('Popup Successfully Updated.','wplegalpages');?></span></p>
                </div>
                <?php
}else{
	echo "<span class='label label-danger myAlert'> _e('Error Updating Template','wplegalpages');</span>";
}
}else{
$wpdb->insert($lpObj->popuptable,array('popupName'=>sanitize_text_field(esc_attr($_POST['lp-name'])),'content'=>$content),array('%s','%s'));
if($wpdb->insert_id){
 ?>
                    <div id="message">
                        <p> <span class="label label-success myAlert"><?php _e('Popup Successfully Created.','wplegalpages');?></span></p>
                    </div>
                    <?php
}else{
	echo "<span class='label label-danger myAlert'>_e('Error Saving Popup','wplegalpages');</span>";
}
}
	  endif;

	  $checked = 'checked="checked"'; $selected = 'selected="selected"';
?>
                        <style type="text/css">
                            .clear {
                                clear: both;
                            }

                            #lp_generalid {
                                padding: 5px 20px 20px 20px;
                                width: 800px;
                                float: left;
                            }

                            #lp_generalid_right {
                                float: left;
                                width: 280px;
                                margin-left: 20px;
                                margin-top: 20px;
                                border-radius: 5px;
                                -moz-border-radius: 5px;
                                -webkit-border-radius: 5px;
                                padding-bottom: 10px;
                            }

                            #lp_generalid p {
                                font-size: 16px;
                            }

                            #lp_generalid input[type=text] {
                                width: 800px;
                                height: 30px;
                                color: #666;
                                font-size: 20px;
                                padding: 3px 4px;
                            }

                            #lp_generalid input[type=submit] {
                                width: 100px;
                                height: 30px;
                                color: #000;
                                cursor: pointer;
                                font-size: 20px;
                                padding: 3px 4px;
                            }

                            #lp_generalid_right li {
                                list-style: square;
                                line-height: 20px;
                                list-style-position: inside;
                                padding-left: 10px;
                            }

                            #lp_generalid_right li span {
                                font-size: 16px;
                                cursor: pointer;
                            }

                            #lp_generalid p a {
                                text-decoration: none;
                            }

                            #content p {
                                width: 800px;
                            }
                        </style>
                        <h2 class="hndle title-head"> <?php _e('Available Popups','wplegalpages');?> : </h2>
                        <table class="widefat fixed comments table table-striped">
                            <thead>
                                <tr>
                                    <th width="5%"><?php _e('S.No.','wplegalpages');?></th>
                                    <th width="30%"><?php _e('Template Title','wplegalpages');?></th>
                                    <th width="20%"><?php _e('Shortcode','wplegalpages');?></th>
                                    <th width="15%"><?php _e('Action','wplegalpages');?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
	$result = $wpdb->get_results("select * from $lpObj->popuptable");
	if( $result ) { ?>
                                    <?php
            $count = 1;
            $class = '';
            foreach( $result as $res ) {
               ?>
                                        <tr<?php echo $class; ?>>
                                            <td>
                                                <?php echo $count; ?>
                                            </td>
                                            <td>
                                                <?php echo $res -> popupName; ?>
                                            </td>
                                            <td>
                                                <?php echo "[wp-legalpopup pid=$res->id]"; ?>
                                            </td>
                                            <td><a href="<?php echo esc_url($baseurl);?>/wp-admin/admin.php?page=<?php echo $page;?>&lpid=<?php echo $res->id;?>&mode=edit"><?php _e('Edit','wplegalpages');?></a> | <a href="<?php echo esc_url($baseurl);?>/wp-admin/admin.php?page=<?php echo $page;?>&lpid=<?php echo $res->id;?>&mode=delete" onclick="return confirm('Popup will be permanently deleted. Are you sure you want to delete?')"><?php _e('Delete','wplegalpages');?></a></td>
                                            </tr>
                                            <?php
                $count++;
            }
            ?>
                                                <?php } else { ?>
                                                    <tr>
                                                        <td colspan="3"><?php _e('No popups yet','wplegalpages');?></td>
                                                    </tr>
                                                    <?php } ?>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th width="5%"><?php _e('S.No.','wplegalpages');?></th>
                                    <th width="30%"><?php _e('Template Title','wplegalpages');?></th>
                                    <th width="20%"><?php _e('Shortcode','wplegalpages');?></th>
                                    <th width="15%"><?php _e('Action','wplegalpages');?></th>
                                </tr>
                            </tfoot>
                        </table>
                        <br/>
                        <br/>
                       
                        <div class="postbox" style="width: 100%; float:left;">
                            <?php $row='';?>
                                <?php if(isset($_REQUEST['mode']) && $_REQUEST['mode']=='edit' && isset($_REQUEST['lpid']))

    $row = $wpdb->get_row($wpdb->prepare('SELECT * from '.$lpObj->popuptable.' where id= %d',$_REQUEST['lpid']));
	?>
                                    <h3 class="hndle title-head" style="padding:7px 10px;"> <?php _e('Create Popups','wplegalpages');?> :</h3>
                                    <p style="padding:0 10px; line-height:18px;"><b><?php _e('Note:','wplegalpages');?> </b><?php _e('You can use Available Template shortcodes inside the popup to display Legal Pages on Popup. Checkbox to agree for the legal pages is added at the end of every popup forcing the user to agree the legal contents to view the page.','wplegalpages');?></p>
                                    <div id="lp_generalid">
                                        <form name="popup" method="post" enctype="multipart/form-data">
                                            <p>
                                                <input type="text" class="form-control myText" name="lp-name" id="lp-name" value="<?php echo $row?$row->popupName:'';?>" /> </p>
                                            <p>
                                                <div id="poststuff">
                                                    <div id="<?php echo user_can_richedit() ? 'postdivrich' : 'postdiv'; ?>">
                                                        <?php wp_editor($row?$row->content:'','content'); ?>
                                                    </div>
                                                    <script type="text/javascript">
                                                        function sp_content_save() {
                                                            var obj = document.getElementById('lp-content');
                                                            var content = document.getElementById('content');
                                                            tinyMCE.triggerSave(0, 1);
                                                            obj.value = content.value;
                                                        }
                                                    </script>
                                                    <textarea id="lp-content" name="lp-content" value="5" style="display:none" rows="10"></textarea>
                                                </div>
                                            </p>
                                            <p>
                                                <input type="submit" class="btn btn-primary mybtn" onclick="sp_content_save();" name="lp-submit" value="<?php if($mode=='edit')echo _e('Update','wplegalpages');else _e('Save','wplegalpages');?>" /> </p>
                                        </form>
                                    </div>
                                    <div class="clear"></div>
                        </div>
