<?php
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
function ucm_register_settings() {
register_setting('ucm_options_group', 'ucm_enabled', 'absint');
register_setting('ucm_options_group', 'ucm_bg_image', 'esc_url_raw');
register_setting('ucm_options_group', 'ucm_bg_image_id', 'absint');
register_setting('ucm_options_group', 'ucm_bg_color', 'sanitize_hex_color');
register_setting('ucm_options_group', 'ucm_font_size', 'absint');
register_setting('ucm_options_group', 'ucm_font_family', 'sanitize_text_field');
register_setting('ucm_options_group', 'ucm_heading_text', 'sanitize_text_field');
register_setting('ucm_options_group', 'ucm_message_text', 'sanitize_textarea_field');
register_setting('ucm_options_group', 'ucm_logo_url', 'esc_url_raw');
register_setting('ucm_options_group', 'ucm_logo_id', 'absint');

}
add_action('admin_init', 'ucm_register_settings');
function ucm_menu() {
add_options_page(
'Under Construction Settings',
'Under Construction',
'manage_options',
'kamran-under-construction-mode',
'ucm_options_page'
);
}
add_action('admin_menu', 'ucm_menu');
function ucm_options_page() {
?>
<div class="wrap">
    <h1>Under Construction Mode Settings</h1>
    <form method="post" action="options.php">
        <?php settings_fields('ucm_options_group'); ?>
        <table class="form-table">
            <tr valign="top">
                <th scope="row">Enable Under Construction Mode</th>
                <td>
                    <input type="checkbox" name="ucm_enabled" value="1"
                        <?php checked(1, get_option('ucm_enabled'), true); ?> />
                </td>
            </tr>
            <tr valign="top">
                <th scope="row">Company Logo</th>
                <td>
                    <input type="text" id="ucm_logo_url" name="ucm_logo_url"
                        value="<?php echo esc_attr(get_option('ucm_logo_url')); ?>" class="regular-text" />
                    <input type="hidden" id="ucm_logo_id" name="ucm_logo_id"
                        value="<?php echo esc_attr(get_option('ucm_logo_id')); ?>" />
                    <input type="button" id="upload_logo_button" class="button" value="Select Logo" />
                    <div id="logo_preview" style="margin-top:10px;">
                        <?php
            $logo_id = get_option('ucm_logo_id');
            if ($logo_id) {
                echo wp_get_attachment_image($logo_id, 'full', false, array('style' => 'max-width:150px; height:auto;'));
            }
            ?>
                    </div>
                </td>
            </tr>

            <tr valign="top">
                <th scope="row">Background Image</th>
                <td>
                    <input type="text" id="ucm_bg_image" name="ucm_bg_image"
                        value="<?php echo esc_attr(get_option('ucm_bg_image')); ?>" class="regular-text" />
                    <input type="hidden" id="ucm_bg_image_id" name="ucm_bg_image_id"
                        value="<?php echo esc_attr(get_option('ucm_bg_image_id')); ?>" />
                    <input type="button" id="upload_bg_image_button" class="button" value="Select Image" />
                    <div id="bg_image_preview" style="margin-top:10px;">
                        <?php
                        $image_id = get_option('ucm_bg_image_id');
                        if ($image_id) {
                        echo wp_get_attachment_image($image_id, 'full', false, array('style' => 'max-width:300px; height:auto;'));
                        }
                        ?>
                    </div>
                </td>
            </tr>
            <tr valign="top">
                <th scope="row">Background Color</th>
                <td>
                    <input type="color" name="ucm_bg_color"
                        value="<?php echo esc_attr(get_option('ucm_bg_color', '#f0f0f0')); ?>" />
                </td>
            </tr>
            <tr valign="top">
                <th scope="row">Font Size (px)</th>
                <td>
                    <input type="number" name="ucm_font_size"
                        value="<?php echo esc_attr(get_option('ucm_font_size', '18')); ?>" min="10" />
                </td>
            </tr>
            <tr valign="top">
                <th scope="row">Font Family</th>
                <td>
                    <select name="ucm_font_family">
                        <?php
                        $fonts = ['Arial', 'Helvetica', 'Georgia', 'Times New Roman', 'Courier New', 'Verdana'];
                        $selected_font = get_option('ucm_font_family', 'Arial');
                        foreach ($fonts as $font) {
                        echo '<option value="' . esc_attr($font) . '" ' . selected($selected_font, $font, false) . '>' . esc_html($font) . '</option>';
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <tr valign="top">
                <th scope="row"><?php esc_html_e('Heading Text', 'kamran-under-construction-mode'); ?></th>
                <td>
                    <input type="text" name="ucm_heading_text"
                        value="<?php echo esc_attr(get_option('ucm_heading_text', 'Coming Soon')); ?>"
                        class="regular-text" />
                </td>
            </tr>
            <tr valign="top">
                <th scope="row"><?php esc_html_e('Message Text', 'kamran-under-construction-mode'); ?></th>
                <td>
                    <textarea name="ucm_message_text" rows="4"
                        class="large-text"><?php echo esc_textarea(get_option('ucm_message_text', 'We are working on something awesome. Stay tuned!')); ?></textarea>
                </td>
            </tr>
        </table>
        <?php submit_button(); ?>
    </form>
</div>
<?php
}