<?php

/* 
** Create admin menu
**/
add_action('admin_menu', 'wcgen_main_menu');
function wcgen_main_menu()
{
    add_menu_page(__('Warranty Card Generator', 'wcgen'), __('Digital WCards', 'wcgen'), 'manage_options', 'digital-warranty-card', '', 'dashicons-pdf', 2);
    add_submenu_page('digital-warranty-card', __('Digital WCards Settings', 'wcgen'), __('DWC Settings', 'wcgen'), 'manage_options', 'digital-warranty-card', 'wcgen_main_menu_html');
    //add_submenu_page('digital-warranty-card', __('Digital WCards List', 'wcgen'), __('DWC List', 'wcgen'), 'manage_options', 'digital-warranty-list', 'wcgen_sub_menu_html');
}


/* 
** Insert all setting in option table
** @use insert key is _custom_stateredirection_all_settings
**/
add_action('wp_ajax_wcgen_save_option', 'wcgen_save_option');
function wcgen_save_option()
{
    $final_data = array();
    parse_str($_POST['form_data'], $final_data);
    $btn_action =    sanitize_text_field($_POST['action']);

    if (isset($btn_action) && $btn_action == "wcgen_save_option" && isset($_POST['security_nonce']) && wp_verify_nonce($_POST['security_nonce'], 'ajax_public_nonce')) {
        foreach ($final_data as $final_data_key => $final_data_val) {
           
            if ($final_data_key == 'column1value') {
                $cbtn_setting  = sanitize_textarea_field($final_data_val);
            }elseif ($final_data_key == 'haddress') {
                $cbtn_setting  = sanitize_textarea_field($final_data_val);
            }else {
                $cbtn_setting  = $final_data_val;
            }

            $save_data[sanitize_key($final_data_key)] = $cbtn_setting;
        }
        // echo "<pre>";
        // print_r($save_data);
        // echo "</pre>";
        update_option('_custom_wcgen_all_settings', $save_data);
        echo json_encode("Success");
        die;
    }
}

?>