<?php

GLOBAL $wpdb,$ftm_geo_target_plugin_path;
$status = "";
$sucess_alert = '<div class="alert alert-warning alert-dismissible show" role="alert">{MESSAGE}<button type="button" class="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="close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>';

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

    ftm_geo_target_deleteSiteFromGroups($site_delete_id);
    ftm_geo_target_deleteSiteOptionValues($site_delete_id);

    if(ftm_geo_target_deleteSite($site_delete_id)){
        $status = true;
    }
    else
    {
        $status = false;
    }

}

$sites = ftm_geo_target_currentSites();

require $ftm_geo_target_plugin_path . '/includes/modal-delete-form.php';
?>

<?php
if($status !== "") {
    if ($status === true) {
        echo str_replace("{MESSAGE}", "<strong>Done!</strong> The site has been successfully deleted.", $sucess_alert);
    } elseif ($status === false) {
        echo str_replace("{MESSAGE}", "<strong>Oops!</strong> There was a problem deleting this site, please contact support.", $error_alert);
    }
}
?>
<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="wrap ftm_geo_target">
    <div id="icon-options-general" class="icon32"></div>
    <h1><?php esc_attr_e( 'Geo Target Sites', '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="meta-box-sortables ui-sortable">
                    <div class="postbox">
                        <div class="handlediv" title="Click to toggle"><br></div>
                        <!-- Toggle -->
                        <h2 class="hndle"><span><?php esc_attr_e( 'Current sites', 'wp_admin_style' ); ?></span>
                        </h2>
                        <div class="inside">
                            <p><?php esc_attr_e( 'Here you can create, edit and update sites that can be added to a group that can be set to follow geo target rules for display tables of site information.', 'wp_admin_style' ); ?></p>
                            <hr>
                            <table id="site_groups_list" class="sites_list">
                                <thead>
                                    <tr class="sites_heading">
                                        <th>Site Name</th>
                                        <th>Site Description</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php
                                $sites = ftm_geo_target_currentSites();
                                foreach ($sites as $site){?>
                                    <tr class="site_entry">
                                        <td><?php echo $site->name; ?></td>
                                        <td><?php echo $site->site_description; ?></td>
                                        <td class="site_actions">
                                            <form method="post" action="<?php echo FTM_WP_INSTALL_PREFIX; ?>/wp-admin/admin.php?page=ftm-geo-target-site-edit"> <input id="sedit" name="sedit" type="hidden" value="<?php echo($site->siteid);?>"><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_site_form','site_delete_id','<?php echo($site->siteid);?>');" name="delete_site" type="submit">Delete</button> |
                                            <a href="<?php echo admin_url('admin.php?page=wpml-string-translation/menu/string-translation.php&context=site-fields-site_'.$site->siteid)?>" class="btn btn-info">Translate</a>
                                        </td>
                                    </tr>
                                    <?php
                                }
                                ?>
                                </tbody>
                            </table>
                        </div>
                        <!-- .inside -->
                    </div>
                    <!-- .postbox -->
                </div>
                <!-- .meta-box-sortables .ui-sortable -->
            </div>
            <!-- post-body-content -->
            <!-- sidebar -->
	        <?php
	        require_once $ftm_geo_target_plugin_path . '/includes/sidebar-sites.php'
	        ?>
            <!-- sidebar -->
            <!-- #postbox-container-1 .postbox-container -->
        </div>
        <!-- #post-body .metabox-holder .columns-2 -->
        <br class="clear">
    </div>
    <!-- #poststuff -->
</div> <!-- .wrap -->

