<?php
/*
Plugin Name: Renesse Aan Zee
Plugin URI: http://renesseaanzee.nl
Description: A widget plugin with events and activities.
Version: 1.1
Author: Web & App Easy B.V.
Author URI: http://webandappeasy.com
License: GPL2
*/

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit; 
}

// Save data to the database
include_once('functions/save-data-api.php');

// Check the saved data timestamp
include_once('functions/check-data-timestamp.php');

// Enqueue JavaScript file for updating the data
function renesse_enqueue_scripts() {
    wp_enqueue_script(
        'renesse-save-data',
        plugin_dir_url(__FILE__) . 'update-api.js',
        [],
        time(),
        true
    );
}
add_action('wp_enqueue_scripts', 'renesse_enqueue_scripts');

// Load the widget content
include_once('functions/widget-controller.php');

// Add settings page
function renesse_widget_plugin_menu() {
    add_options_page('Renesse Aan Zee widget instellingen', 'Renesse widget', 'manage_options', 'renesse-widget-plugin', 'renesse_widget_plugin_settings_page');
}
add_action('admin_menu', 'renesse_widget_plugin_menu');

// Register settings
function renesse_widget_plugin_settings() {
    register_setting('renesse-widget-plugin-settings-group', 'renesse_widget_plugin_position');
}
add_action('admin_init', 'renesse_widget_plugin_settings');

// Settings page content
function renesse_widget_plugin_settings_page() {
    ?>
    <div class="wrap">
        <h1><?php esc_html_e('Renesse Aan Zee widget instellingen', 'renesse-aan-zee')?></h1>
        <form method="post" action="options.php">
            <?php settings_fields('renesse-widget-plugin-settings-group'); ?>
            <?php do_settings_sections('renesse-widget-plugin-settings-group'); ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row"><?php esc_html_e('Widget positie', 'renesse-aan-zee')?></th>
                    <td>
                        <select name="renesse_widget_plugin_position">
                            <option value="left" <?php echo esc_attr(get_option('renesse_widget_plugin_position', 'left')) == 'left' ? 'selected' : ''; ?>><?php esc_html_e('Links', 'renesse-aan-zee')?></option>
                            <option value="right" <?php echo esc_attr(get_option('renesse_widget_plugin_position', 'left')) == 'right' ? 'selected' : ''; ?>><?php esc_html_e('Rechts', 'renesse-aan-zee')?></option>
                        </select>
                    </td>
                </tr>
            </table>
            <?php esc_attr(submit_button()); ?>
        </form>
    </div>
    <?php
}

?>