<?php
// Check if the user is allowed to manage options.
if (!current_user_can('manage_options')) {
    return;
}

// Handle form submission to save all color options
if (isset($_POST['sport_submit6in1']) && check_admin_referer('bongdako_settings_save', 'bongdako_nonce')) {
    if (isset($_POST['bg_color_6in1'])) {
        update_option('bg_color_6in1', sanitize_text_field($_POST['bg_color_6in1']));
    }

    if (isset($_POST['text_color1_6in1'])) {
        update_option('text_color1_6in1', sanitize_text_field($_POST['text_color1_6in1']));
    }
    // Show success message after saving
    echo '<div class="updated"><p>Settings saved successfully.</p></div>';
}

if (isset($_POST['sport_submitResult']) && check_admin_referer('bongdako_settings_save', 'bongdako_nonce')) {
    if (isset($_POST['bg_color_result'])) {
        update_option('bg_color_result', sanitize_text_field($_POST['bg_color_result']));
    }

    if (isset($_POST['text_color1_result'])) {
        update_option('text_color1_result', sanitize_text_field($_POST['text_color1_result']));
    }
    // Show success message after saving
    echo '<div class="updated"><p>Settings saved successfully.</p></div>';
}

if (isset($_POST['sport_submitFixture']) && check_admin_referer('bongdako_settings_save', 'bongdako_nonce')) {
    if (isset($_POST['bg_color_fixture'])) {
        update_option('bg_color_fixture', sanitize_text_field($_POST['bg_color_fixture']));
    }

    if (isset($_POST['text_color1_fixture'])) {
        update_option('text_color1_fixture', sanitize_text_field($_POST['text_color1_fixture']));
    }
    // Show success message after saving
    echo '<div class="updated"><p>Settings saved successfully.</p></div>';
}

if (isset($_POST['sport_submitRate']) && check_admin_referer('bongdako_settings_save', 'bongdako_nonce')) {
    if (isset($_POST['bg_color_rate'])) {
        update_option('bg_color_rate', sanitize_text_field($_POST['bg_color_rate']));
    }

    if (isset($_POST['text_color1_rate'])) {
        update_option('text_color1_rate', sanitize_text_field($_POST['text_color1_rate']));
    }
    // Show success message after saving
    echo '<div class="updated"><p>Settings saved successfully.</p></div>';
}

if (isset($_POST['sport_reset6in1']) && check_admin_referer('bongdako_settings_save', 'bongdako_nonce')) {
    if (isset($_POST['bg_color_6in1'])) {
        update_option('bg_color_6in1', '#e6e6e6');
    }

    if (isset($_POST['text_color1_6in1'])) {
        update_option('text_color1_6in1', '#070f03');
    }
    // Show success message after saving
    echo '<div class="updated"><p>Settings saved successfully.</p></div>';
}

if (isset($_POST['sport_resetResult']) && check_admin_referer('bongdako_settings_save', 'bongdako_nonce')) {
    if (isset($_POST['bg_color_result'])) {
        update_option('bg_color_result', '#e6e6e6');
    }

    if (isset($_POST['text_color1_result'])) {
        update_option('text_color1_result', '#070f03');
    }
    // Show success message after saving
    echo '<div class="updated"><p>Settings saved successfully.</p></div>';
}

if (isset($_POST['sport_resetFixture']) && check_admin_referer('bongdako_settings_save', 'bongdako_nonce')) {
    if (isset($_POST['bg_color_fixture'])) {
        update_option('bg_color_fixture', '#e6e6e6');
    }

    if (isset($_POST['text_color1_fixture'])) {
        update_option('text_color1_fixture', '#070f03');
    }
    // Show success message after saving
    echo '<div class="updated"><p>Settings saved successfully.</p></div>';
}

if (isset($_POST['sport_resetRate']) && check_admin_referer('bongdako_settings_save', 'bongdako_nonce')) {
    if (isset($_POST['bg_color_rate'])) {
        update_option('bg_color_rate', '#e6e6e6');
    }

    if (isset($_POST['text_color1_rate'])) {
        update_option('text_color1_rate', '#070f03');
    }
    // Show success message after saving
    echo '<div class="updated"><p>Settings saved successfully.</p></div>';
}
if (isset($_POST['bongdako_submit_key']) && check_admin_referer('bongdako_settings_save', 'bongdako_nonce')) {
    if (isset($_POST['my_plugin_license_key'])) {
        update_option('my_plugin_license_key', $_POST['my_plugin_license_key']);
    }
    $check = tm_12_check_license();
    
    if ($check['status']) {
        // Gắn query param để hiển thị thông báo
        wp_redirect(add_query_arg(['license_status' => 'success', 'message' => urlencode($check['message'])]));
        exit;
    }
    // Gắn query param để hiển thị thông báo
    wp_redirect(add_query_arg(['license_status' => 'error', 'message' => urlencode($check['message'])]));
    exit;
}
// Handle form submission to save schema options
if (isset($_POST['bongdako_submit_schema']) && check_admin_referer('bongdako_settings_save', 'bongdako_nonce')) {
    if (isset($_POST['schema1_link'])) {
        update_option('schema1_link', esc_url_raw($_POST['schema1_link']));
    }

    if (isset($_POST['schema2_link'])) {
        update_option('schema2_link', esc_url_raw($_POST['schema2_link']));
    }

    if (isset($_POST['type_text'])) {
        update_option('type_text', sanitize_text_field($_POST['type_text']));
    }

    if (isset($_POST['name_text'])) {
        update_option('name_text', sanitize_text_field($_POST['name_text']));
    }

    if (isset($_POST['alternateName_text'])) {
        update_option('alternateName_text', sanitize_text_field($_POST['alternateName_text']));
    }

    if (isset($_POST['logo_link'])) {
        update_option('logo_link', esc_url_raw($_POST['logo_link']));
    }

    if (isset($_POST['image_link'])) {
        update_option('image_link', esc_url_raw($_POST['image_link']));
    }

    if (isset($_POST['description'])) {
        update_option('description', sanitize_textarea_field($_POST['description']));
    }

    if (isset($_POST['hasMap'])) {
        update_option('hasMap', esc_url_raw($_POST['hasMap']));
    }

    if (isset($_POST['url'])) {
        update_option('url', esc_url_raw($_POST['url']));
    }

    if (isset($_POST['address'])) {
        update_option('address', sanitize_text_field($_POST['address']));
    }

    if (isset($_POST['type_geo'])) {
        update_option('type_geo', sanitize_text_field($_POST['type_geo']));
    }

    if (isset($_POST['latitude'])) {
        update_option('latitude', sanitize_text_field($_POST['latitude']));
    }

    if (isset($_POST['longitude'])) {
        update_option('longitude', sanitize_text_field($_POST['longitude']));
    }

    if (isset($_POST['enable_schema_head'])) {
        update_option('enable_schema_head', 1);
    } else {
        update_option('enable_schema_head', 0);
    }

    // Show success message after saving
    echo '<div class="updated"><p>Settings saved successfully.</p></div>';
}

// Retrieve saved colors or set default values
$saved_bgcolor_6in1 = get_option('bg_color_6in1', '#e6e6e6');
$saved_bgcolor_result = get_option('bg_color_result', '#e6e6e6');
$saved_bgcolor_fixture = get_option('bg_color_fixture', '#e6e6e6');
$saved_bgcolor_rate = get_option('bg_color_rate', '#e6e6e6');

$saved_textcolor1_6in1 = get_option('text_color1_6in1', '#070f03');
$saved_textcolor1_result = get_option('text_color1_result', '#070f03');
$saved_textcolor1_fixture = get_option('text_color1_fixture', '#070f03');
$saved_textcolor1_rate = get_option('text_color1_rate', '#070f03');
$matches_json = bongdako_display_updateData();