<?php
GLOBAL $wpdb;
GLOBAL $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>';

$groupId = intval($_REQUEST['group']);
$data = ftm_geo_target_getGroupById($groupId);
$themeData = read_theme_render_file($data[0]->ThemeName);
$replaceable = ftm_gt_find_replaceable_in_template_content($themeData);

if (isset($_POST['edit_internal'])) {
    // Clean old values:
    $table_name = $wpdb->prefix . 'ftm_geo_customization';
    $wpdb->delete($table_name, array('GroupId' => $groupId));
    // Save new values
    $sentence_number = 0;
    foreach($replaceable as $sentence) {
        $value = $_POST['value_'.$sentence_number];
        if ($value) {
            $wpdb->insert($table_name, array(
                'GroupId' => $groupId,
                'key' => $sentence,
                'value' => $value,
            ));
        }
        $sentence_number++;
    }
}
$customized_values = ftm_geo_target_getGroupCustomization($groupId);

?>

<div class="wrap ftm_geo_target">

    <div id="icon-options-general" class="icon32"></div>
    <h1><?php esc_attr_e( 'Customize Group', 'wp_admin_style' ); ?></h1>

    <div id="poststuff">

        <div id="post-body" class="metabox-holder columns-2">

                    <form id="edit_group_form" method="post" action="">
            <!-- main content -->
            <div id="post-body-content">

                <div class="ftm_geo_target">

                    <?php
                    if($result === true){
                        echo str_replace("{MESSAGE}","<strong>Cool!</strong> The group has been updated. Check out the <a href='/wp-admin/admin.php?page=ftm-geo-target-groups/'>groups list</a> or <a href='/wp-admin/admin.php?page=ftm-geo-target-group-add'>add a new one</a>.", $sucess_alert);
                    }elseif ($result === false){
                        echo str_replace("{MESSAGE}","<strong>Oops!</strong> The group wasn't updated. Plase contact support", $error_alert);
                    }
                    ?>
                        <div class="postbox">
                            <h2 class="hndle"><span><b><?php esc_attr_e( 'Group Information', 'wp_admin_style' ); ?></b></span></h2>
                            <div class="inside">
                            <table>
                            <tr><th><?php esc_attr_e('Group Name') ?>:</th><td><?php echo $data[0]->GroupName ?></td></tr>
                            <tr><th><?php esc_attr_e('Group Description') ?>:</th><td><?php echo $data[0]->GroupDescription ?></td></tr>
                            <tr><th><?php esc_attr_e('Theme Name') ?>:</th><td><?php echo $data[0]->ThemeName ?></td></tr>
                            <tr><th><?php esc_attr_e('ShortCode') ?>:</th><td>[ftm_gt g="<?php echo str_replace(" ", "-", $data[0]->GroupName) ?>"]</td></tr>
                            </table>
                            </div>
                        </div>

                        <input type="hidden" id="groupid" name="groupid" value="<?php echo $groupId; ?>">

                        <div class="postbox">
                            <h2 class="hndle"><span><b><?php esc_attr_e( 'Sentences that can be replaced in the template. ', 'wp_admin_style' ); ?></b></span></h2>
                            <div class="inside">
<?php $sentence_number = 0; ?>
<?php foreach($replaceable as $sentence): ?> 

<?php $sentence_value = isset($customized_values[$sentence])?$customized_values[$sentence]:'' ?>
                                <input type="hidden" name="<?php echo 'sentence_'.$sentence_number ?>" value="<?php echo $sentence ?>"/>
                                <div><?php echo $sentence ?>:<br /> <input type="text" class="ftm_geo_target_big_input" name="<?php echo 'value_'.$sentence_number ?>" size="60" value="<?php echo $sentence_value ?>"/></div>
                                <hr>
<?php $sentence_number++; ?>
<?php endforeach; ?>
                            </div>
                        </div>
                        <!-- .postbox -->

                </div>
                <!-- .meta-box-sortables .ui-sortable -->

            </div>
            <!-- post-body-content -->

            <!-- sidebar -->
            <?php
            $sidebar_action = "edit";
            require $ftm_geo_target_plugin_path . '/includes/sidebar-customize.php';
            ?>
                    </form>

        </div>

        <br class="clear">
    </div>

    <!-- #poststuff -->

</div> <!-- .wrap -->
