// Handle adding client data via AJAX
function add_client_data() {
    $client_name = sanitize_text_field($_POST['client_name']);
    $fees = sanitize_text_field($_POST['fees']);
    $consultation_time = sanitize_text_field($_POST['consultation_time']);
    $remedies = sanitize_textarea_field($_POST['remedies']);

    // Store client data in a custom post type or custom table
    $post_id = wp_insert_post(array(
        'post_title'   => $client_name,
        'post_type'    => 'client',
        'post_status'  => 'publish',
        'meta_input'   => array(
            'fees'               => $fees,
            'consultation_time'  => $consultation_time,
            'remedies'           => $remedies,
        ),
    ));

    if ($post_id) {
        wp_send_json_success(array('message' => 'Client added successfully!'));
    } else {
        wp_send_json_error(array('message' => 'Failed to add client.'));
    }
}
add_action('wp_ajax_add_client', 'add_client_data');
add_action('wp_ajax_nopriv_add_client', 'add_client_data');

// Handle searching for a client via AJAX
function search_client_data() {
    $client_name = sanitize_text_field($_POST['client_name']);

    $client_query = new WP_Query(array(
        'post_type' => 'client',
        'title'     => $client_name,
        'posts_per_page' => 1,
    ));

    if ($client_query->have_posts()) {
        while ($client_query->have_posts()) {
            $client_query->the_post();
            $post_id = get_the_ID();
            $fees = get_post_meta($post_id, 'fees', true);
            $consultation_time = get_post_meta($post_id, 'consultation_time', true);
            $remedies = get_post_meta($post_id, 'remedies', true);

            wp_send_json_success(array(
                'client_name'       => get_the_title(),
                'fees'              => $fees,
                'consultation_time' => $consultation_time,
                'remedies'          => $remedies,
            ));
        }
    } else {
        wp_send_json_error(array('message' => 'Client not found.'));
    }
}
add_action('wp_ajax_search_client', 'search_client_data');
add_action('wp_ajax_nopriv_search_client', 'search_client_data');