<?php
if ( ! defined('ABSPATH')) exit;  
if (isset($_POST['team_mxt_member_submit']) && check_admin_referer('team_mxt_add_member_nonce', 'team_mxt_nonce_field') && current_user_can('manage_options')) {
    // Save process here
    $new_priority = isset($_POST['team_mxt_member_priority']) ? absint($_POST['team_mxt_member_priority']) : 0;

    $photo_id = absint($_POST['team_mxt_member_photo'] ?? 0);
    if (wp_attachment_is_image($photo_id)) {
        update_option('team_mxt_member_photo', $photo_id);
    }

    $name = isset($_POST['team_mxt_member_name']) ? sanitize_text_field(wp_unslash($_POST['team_mxt_member_name'])) : '';
    $designation = isset($_POST['team_mxt_member_designation']) ? sanitize_text_field(wp_unslash($_POST['team_mxt_member_designation'])) : '';
    $phone = isset($_POST['team_mxt_member_phone']) ? sanitize_text_field(wp_unslash($_POST['team_mxt_member_phone'])) : '';
    $email = isset($_POST['team_mxt_member_email']) ? sanitize_email(wp_unslash($_POST['team_mxt_member_email'])) : '';
    $website = isset($_POST['team_mxt_member_website']) ? esc_url_raw(wp_unslash($_POST['team_mxt_member_website'])) : '';
    $description = isset($_POST['team_mxt_member_description']) ? sanitize_textarea_field(wp_unslash($_POST['team_mxt_member_description'])) : '';
    $expertise_1 = isset($_POST['team_mxt_member_expertise_1']) ? sanitize_text_field(wp_unslash($_POST['team_mxt_member_expertise_1'])) : '';
    $expertise_percent_1 = isset($_POST['team_mxt_member_expertise_percent_1']) ? absint($_POST['team_mxt_member_expertise_percent_1']) : 0;
    $expertise_2 = isset($_POST['team_mxt_member_expertise_2']) ? sanitize_text_field(wp_unslash($_POST['team_mxt_member_expertise_2'])) : '';
    $expertise_percent_2 = isset($_POST['team_mxt_member_expertise_percent_2']) ? absint($_POST['team_mxt_member_expertise_percent_2']) : 0;
    $expertise_3 = isset($_POST['team_mxt_member_expertise_3']) ? sanitize_text_field(wp_unslash($_POST['team_mxt_member_expertise_3'])) : '';
    $expertise_percent_3 = isset($_POST['team_mxt_member_expertise_percent_3']) ? absint($_POST['team_mxt_member_expertise_percent_3']) : 0;


    // Additional social media fields
    $social_media_fields = [
        'facebook', 'twitter', 'linkedin', 'whatsapp', 'instagram', 'pinterest', 'discord', 'facebook-messenger', 
        'youtube', 'reddit', 'tiktok', 'telegram', 'quora', 'tumblr', 'vimeo'
    ];

    $social_data = [];
    foreach ($social_media_fields as $field) {
        $social_data[$field] = isset($_POST['team_mxt_member_' . $field]) ? sanitize_text_field(wp_unslash($_POST['team_mxt_member_' . $field])) : '';
    }

    $departments = [];
    if (isset($_POST['team_mxt_member_department']) && is_array($_POST['team_mxt_member_department'])) {
        foreach (array_map('sanitize_text_field', wp_unslash($_POST['team_mxt_member_department'] ?? [])) as $dept) {
            $departments[] = sanitize_text_field($dept);
        }
    }
    $priority = isset($_POST['team_mxt_member_priority']) ? absint($_POST['team_mxt_member_priority']) : 0; 
    $style = isset($_POST['team_mxt_member_style']) ? sanitize_text_field(wp_unslash($_POST['team_mxt_member_style'])) : 'style_1';

    $member_data = [
        'name' => $name,
        'designation' => $designation,
        'phone' => $phone,
        'email' => $email,
        'website' => $website,
        'description' => $description,
        'social' => $social_data,
        'style' => $style,
        'photo_id' => $photo_id,
        'departments' => $departments,
        'priority' => $priority,
        'expertise' => [
            ['area' => $expertise_1, 'percent' => $expertise_percent_1],
            ['area' => $expertise_2, 'percent' => $expertise_percent_2],
            ['area' => $expertise_3, 'percent' => $expertise_percent_3],
        ],
    ];

    $all_members = get_option('team_mxt_all_members', []);
    $all_members[] = $member_data;
    update_option('team_mxt_all_members', $all_members);
    set_transient('team_mxt_add_member_success', 'Member added successfully.');
    wp_redirect(admin_url('admin.php?page=team_mxt_add_member'));
    exit;
}
?>
