<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly 

// Supported languages
$languages = array(
    'en' => 'English',
    'es' => 'Spanish',
    'fr' => 'French',
    'de' => 'German',
    'it' => 'Italian',
    'pt' => 'Portuguese',
    'ru' => 'Russian',
    'zh' => 'Chinese',
    'ja' => 'Japanese',
    'ko' => 'Korean',
    'ar' => 'Arabic',
    'hi' => 'Hindi',
    'bn' => 'Bengali',
    'ur' => 'Urdu',
    'tr' => 'Turkish',
    'nl' => 'Dutch',
    'sv' => 'Swedish',
    'da' => 'Danish',
    'no' => 'Norwegian',
    'fi' => 'Finnish',
    'pl' => 'Polish',
    'cs' => 'Czech',
    'hu' => 'Hungarian',
    'el' => 'Greek',
    'he' => 'Hebrew',
    'th' => 'Thai',
    'vi' => 'Vietnamese',
    'id' => 'Indonesian',
    'ms' => 'Malay',
    'ro' => 'Romanian',
    'uk' => 'Ukrainian',
    'fa' => 'Persian',
    'sr' => 'Serbian',
    'bg' => 'Bulgarian',
    'hr' => 'Croatian',
    'sk' => 'Slovak',
    'sl' => 'Slovenian',
    'lt' => 'Lithuanian',
    'lv' => 'Latvian',
    'et' => 'Estonian',
    'ta' => 'Tamil',
    'te' => 'Telugu',
    'ml' => 'Malayalam',
    'mr' => 'Marathi',
    'kn' => 'Kannada',
    'gu' => 'Gujarati',
    'pa' => 'Punjabi',
    'si' => 'Sinhala',
    'my' => 'Burmese',
    'km' => 'Khmer',
    'ne' => 'Nepali',
    'am' => 'Amharic',
    'af' => 'Afrikaans',
    'sw' => 'Swahili',
    'yo' => 'Yoruba',
    'zu' => 'Zulu'
);

// Get JSON options
$general_settings = json_decode(get_option('wtdp_general', '{}'), true);

// Get settings with validation
$api_key = isset($general_settings['wtdp_api_key']) ? $general_settings['wtdp_api_key'] : '';
$time_difference = isset($general_settings['wtdp_time_difference']) ? (int) $general_settings['wtdp_time_difference'] : 0;
$default_city = isset($general_settings['wtdp_default_city']) ? $general_settings['wtdp_default_city'] : 'London';
$latitude = isset($general_settings['wtdp_latitude']) ? (float) $general_settings['wtdp_latitude'] : '51.5085';
$longitude = isset($general_settings['wtdp_longitude']) ? (float) $general_settings['wtdp_longitude'] : '-0.1257';
$unit_system = isset($general_settings['wtdp_unit_system']) ? $general_settings['wtdp_unit_system'] : 'metric';
$language = isset($general_settings['wtdp_language']) ? $general_settings['wtdp_language'] : 'en';

// Translate columns
$column_temperature = isset($general_settings['wtdp_column_temperature']) ? $general_settings['wtdp_column_temperature'] : 'Temperature';
$column_humidity = isset($general_settings['wtdp_column_humidity']) ? $general_settings['wtdp_column_humidity'] : 'Humidity';
$column_wind_speed = isset($general_settings['wtdp_column_wind_speed']) ? $general_settings['wtdp_column_wind_speed'] : 'Wind Speed';
$column_pressure = isset($general_settings['wtdp_column_pressure']) ? $general_settings['wtdp_column_pressure'] : 'Pressure';
$column_sunrise = isset($general_settings['wtdp_column_sunrise']) ? $general_settings['wtdp_column_sunrise'] : 'Sunrise';
$column_sunset = isset($general_settings['wtdp_column_sunset']) ? $general_settings['wtdp_column_sunset'] : 'Sunset';





echo '
    <!-- Tab Contents -->
    <div id="general-settings-tab" class="tab-content ' . esc_attr($current_tab == 'general-settings-tab' ? '' : 'hidden') . '">
        <div class="settings-h2">' . esc_html__('GENERAL', 'extended-weather') . '</div>
        
        <p class="submit">
            <input type="submit" name="wtdp_save_settings" class="button-primary-save" value="' . esc_attr__('Save Settings', 'extended-weather') . '">
        </p>

        <div class="wtdp-admin-settings-container" style="display: flex; flex-wrap: wrap; gap: 20px;">

            <!-- Basic Settings Group -->
            <div class="wtdp-settings-group" style="flex: 1; min-width: 450px;">
                <div class="wtdp-settings-frame">
                    <h3>' . esc_html__('Basic', 'extended-weather') . '</h3>
                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label for="wtdp_api_key">' . esc_html__('OpenWeather API Key', 'extended-weather') . '</label>
                            <input name="wtdp_api_key" type="text" id="wtdp_api_key" value="' . esc_attr($api_key) . '" class="regular-text"  style="min-width: 250px;" />
                        </div>
                    </div>
                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label for="wtdp_time_difference">' . esc_html__('Time Difference', 'extended-weather') . '</label>
                            <input name="wtdp_time_difference" type="number" id="wtdp_time_difference" value="' . esc_attr($time_difference) . '" class="small-text" />
                        </div>
                    </div>


                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label for="wtdp_unit_system">' . esc_html__('Unit System', 'extended-weather') . '</label>
                            <select name="wtdp_unit_system" id="wtdp_unit_system">
                                <option value="metric" ' . selected($unit_system, 'metric', false) . '>' . esc_html__('Metric', 'extended-weather') . '</option>
                                <option value="imperial" ' . selected($unit_system, 'imperial', false) . '>' . esc_html__('Imperial', 'extended-weather') . '</option>
                            </select>
                        </div>
                    </div>
                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label for="wtdp_language">' . esc_html__('Language', 'extended-weather') . '</label>
                            <select name="wtdp_language" id="wtdp_language">';
                                foreach ($languages as $lang_code => $lang_name) {
                                    echo '<option value="' . esc_attr($lang_code) . '" ' . selected($language, $lang_code, false) . '>' . esc_html($lang_name) . '</option>';
                                }
                            echo '</select>
                        </div>
                    </div>

                    <h3>' . esc_html__('Translate', 'extended-weather') . '</h3>
                    

                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label>' . esc_html__('Temperature', 'extended-weather') . '</label>
                            <input type="text" name="wtdp_column_temperature" value="' . esc_attr(get_option('wtdp_column_temperature', 'Temperature')) . '" class="regular-text" />
                        </div>
                    </div>

                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label>' . esc_html__('Humidity', 'extended-weather') . '</label>
                            <input type="text" name="wtdp_column_humidity" value="' . esc_attr(get_option('wtdp_column_humidity', 'Humidity')) . '" class="regular-text" />
                        </div>
                    </div>

                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label>' . esc_html__('Wind Speed', 'extended-weather') . '</label>
                            <input type="text" name="wtdp_column_wind_speed" value="' . esc_attr(get_option('wtdp_column_wind_speed', 'Wind')) . '" class="regular-text" />
                        </div>
                    </div>

                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label>' . esc_html__('Pressure', 'extended-weather') . '</label>
                            <input type="text" name="wtdp_column_pressure" value="' . esc_attr(get_option('wtdp_column_pressure', 'Pressure')) . '" class="regular-text" />
                        </div>
                    </div>
                    
                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label>' . esc_html__('Sunrise', 'extended-weather') . '</label>
                            <input type="text" name="wtdp_column_sunrise" value="' . esc_attr(get_option('wtdp_column_sunrise', 'Sunrise')) . '" class="regular-text" />
                        </div>
                    </div>

                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label>' . esc_html__('Sunset', 'extended-weather') . '</label>
                            <input type="text" name="wtdp_column_sunset" value="' . esc_attr(get_option('wtdp_column_sunset', 'Sunset')) . '" class="regular-text" />
                        </div>
                    </div>



                </div>
            </div>

            <!-- Location Group -->
            <div class="wtdp-settings-group" style="flex: 1; min-width: 450px;">
                <div class="wtdp-settings-frame">
                    <h3>' . esc_html__('Location', 'extended-weather') . '</h3>
                    
                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label for="wtdp_default_city">' . esc_html__('City', 'extended-weather') . '</label>
                            <button id="get-coordinates" type="button" class="button">' . esc_html__('Check', 'extended-weather') . '</button>
                            <input name="wtdp_default_city" type="text" id="wtdp_default_city" value="' . esc_attr($default_city) . '" class="regular-text" />
                        </div>
                    </div>
                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label for="wtdp_latitude">' . esc_html__('Latitude', 'extended-weather') . '</label>
                            <input name="wtdp_latitude" type="text" id="wtdp_latitude" value="' . esc_attr($latitude) . '" class="regular-text" />
                        </div>
                    </div>
                    <div class="wtdp-form-group">
                        <div class="wtdp-form-label-container">
                            <label for="wtdp_longitude">' . esc_html__('Longitude', 'extended-weather') . '</label>
                            <input name="wtdp_longitude" type="text" id="wtdp_longitude" value="' . esc_attr($longitude) . '" class="regular-text" />
                        </div>
                    </div>


                    <div class="wtdp-form-group">
                        <p><strong>' . esc_html__('Double click on map to set location.', 'extended-weather') . '</strong></p>

                        <div class="wtdp-form-label-container">
                            <div id="map-container">
                                <div class="map" id="map"></div>
                            </div>
                        </div>
                    </div>


                </div>
            </div>
        </div>
    </div>

';

