
<?php
GLOBAL $wpdb;
$status = "";
$sucess_alert = '<div class="alert alert-warning alert-dismissible show" role="alert">{MESSAGE}<button type="button" class="btn btn-secondary close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>';
$error_alert = '<div class="alert alert-danger alert-dismissible show" role="alert">{MESSAGE}<button type="button" class="btn btn-secondary close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>';

if(isset($_POST["content_delete_id"])){
    $content_delete_id = $_POST["content_delete_id"];

    if(ftm_geo_target_deleteContent($content_delete_id)){
        $status = true;
    }
    else
    {
        $status = false;
    }
}


$contents  = ftm_geo_target_currentContent();

?>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

<div class="modal fade" id="delete_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="btn btn-secondary close" data-bs-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Delete Confirmation</h4>
            </div>
            <div class="modal-body">
                <form id="delete_content_form" name="delete_content_form" action="/wp-admin/admin.php?page=ftm-geo-target-content"  method="post">
                    <fieldset>
                        <div class="form-group">
                            <h4>Are you sure you want to delete this content?</h4>
                        </div>
                        <br>
                        <div class="form-group">
                            <p>By deleting this content, you will also delete it's geo targeting rules and settings. You could also deactivate it if not 100% sure you want to re-enter the data later on.</p>
                        </div>
                        <!-- Allow form submission with keyboard without duplicating the dialog button -->
                        <input type="hidden" name="content_delete_id" id="content_delete_id">
                        <input type="submit" id="diag_form_submit" tabindex="-1" style="position:absolute; top:-1000px">
                    </fieldset>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-bs-dismiss="modal">Close</button>
                <button type="submit" form="delete_content_form" class="btn btn-primary">Delete Content</button>
            </div>
        </div>
    </div>
</div>

<?php
if($status !== "") {
    if ($status === true) {
        echo str_replace("{MESSAGE}", "<strong>Done!</strong> The content has been successfully deleted.", $sucess_alert);
    } elseif ($status === false) {
        echo str_replace("{MESSAGE}", "<strong>Oops!</strong> There was a problem deleting this content, please contact support.", $error_alert);
    }
}
?>

<div class="wrap ftm_geo_target">
    <div id="icon-options-general" class="icon32"></div>
    <h1><?php esc_attr_e( 'Geo-Target Content', 'wp_admin_style' ); ?></h1>
    <div id="poststuff">
        <div id="post-body" class="metabox-holder columns-2">
            <!-- main content -->
            <div id="post-body-content">
                <div class="postbox">
                    <h2 class="hndle"><i><?php esc_attr_e('Here you can content by sets of geo target restrictions.', 'wp_admin_style'); ?></i></h2>
                    <div class="inside">
                        <b>Current Content</b>
                        <div>
                            <?php if(sizeof($contents) === 0){ ?>
                                <div><i><?php esc_attr_e('-- No content added yet --', 'wp_admin_style'); ?></i></div>
                            <?php } else {?>
                                <br><br>
                                <table id="site_groups_list" class="sites_list">
                                    <thead>
                                    <tr>
                                        <th>Content Name</th>
                                        <th>Content ShortCode</th>
                                        <th>Description</th>
                                        <th>Active</th>
                                        <th>Actions</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php
                                    foreach ($contents as $content){?>
                                        <tr class="site_entry">
                                            <td><?php echo $content->name; ?></td>
                                            <td><?php echo '[ftm_content g="' . str_replace(" ", "-", $content->name) . '"]'; ?></td>
                                            <td><?php echo $content->description; ?></td>
                                            <td><?php echo $content->active ? "Yes":"No"; ?></td>
                                            <td class="site_actions">
                                                <form method="post" action="/wp-admin/admin.php?page=ftm-geo-target-content-edit"> <input id="gedit" name="gedit" type="hidden" value="<?php echo($content->ContentId);?>"><button class="btn btn-warning" name="edits" type="submit">Edit</button> </form> |
                                                <button class="btn btn-danger"  data-bs-toggle="modal" data-bs-target="#delete_modal"  onclick="fill_confirm_popup_key_value('delete_content_form','content_delete_id','<?php echo($content->ContentId);?>');" name="delete_content" type="submit">Delete</button>
                                            </td>
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                    </tbody>
                                </table>
                            <?php } ?>
                        </div>
                    </div>
                </div>
                <!-- .meta-box-sortables .ui-sortable -->
            </div>
            <!-- post-body-content -->
            <!-- sidebar -->
            <?php
            GLOBAL $ftm_geo_target_plugin_path;
            require_once $ftm_geo_target_plugin_path . '/includes/sidebar-content.php';
            ?>
            <!-- sidebar -->
            <!-- #postbox-container-1 .postbox-container -->
        </div>
        <!-- #post-body .metabox-holder .columns-2 -->
        <br class="clear">
    </div>
    <!-- #poststuff -->
</div> <!-- .wrap -->

