<?php
if (!defined('ABSPATH')) {
    exit;
}

if (isset($_GET['member_index']) && current_user_can('manage_options') && (wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? '')), 'member_action_nonce') || !isset($_GET['_wpnonce']))) {
    $member_index = intval($_GET['member_index']);
    $all_members = get_option('team_mxt_all_members', []);

    if (!isset($all_members[$member_index])) {
        wp_die('Member not found');
    }

    $member = $all_members[$member_index];
    $photo_id = isset($member['photo_id']) ? $member['photo_id'] : '';
    ?>
<nav style="margin-top:20px;" class="team-mxt-nav">
    <ul class="team-mxt-tabs">
        <li class="active"><a href="?page=team_mxt_add_member">ADD MEMBER</a></li>
        <li><a href="?page=team_mxt_all_members">ALL MEMBERS</a></li>
        <li><a href="?page=team_mxt_settings">SETTINGS</a></li>
        <li><a href="?page=team_mxt_license_management">ADD LICENSE</a></li>
        <li class="gp"><a href="https://www.webmxt.com/product/team-mxt-plugin/" target="_blank">Get Premium</a></li>
        <li><a href="?page=team_mxt_tutorial">Tutorial</a></li>
    </ul>
</nav>

<div class="team-mxt-wrap">
    <h1>Edit Team Member</h1>
    <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
        <input type="hidden" name="action" value="team_mxt_update_member">
        <input type="hidden" name="member_index" value="<?php echo esc_attr($member_index); ?>">
        <?php wp_nonce_field('team_mxt_edit_member_nonce');?>
        <div class="team-mxt-image-field">
            <label for="team_mxt_member_photo">Photo:</label>
            <div id="team_mxt_image_preview">
                <?php if ($photo_id): ?>
                <?php echo wp_get_attachment_image($photo_id, 'thumbnail'); ?>
                <?php endif;?>
            </div>
            <input type="hidden" id="team_mxt_member_photo" name="team_mxt_member_photo"
                value="<?php echo esc_attr($photo_id); ?>">
            <button type="button" class="button" id="team_mxt_upload_button">Choose Image</button>
        </div>

        <label for="team_mxt_member_name">Name:</label>
        <input type="text" id="team_mxt_member_name" name="team_mxt_member_name"
            value="<?php echo esc_attr($member['name']); ?>">

        <label for="team_mxt_member_designation">Designation:</label>
        <input type="text" id="team_mxt_member_designation" name="team_mxt_member_designation"
            value="<?php echo esc_attr($member['designation']); ?>">

        <label for="team_mxt_member_phone">Phone:</label>
        <input type="text" id="team_mxt_member_phone" name="team_mxt_member_phone"
            value="<?php echo esc_attr($member['phone']); ?>">

        <label for="team_mxt_member_email">Email:</label>
        <input type="email" id="team_mxt_member_email" name="team_mxt_member_email"
            value="<?php echo esc_attr($member['email']); ?>">

        <label for="team_mxt_member_website">Website:</label>
        <input type="url" id="team_mxt_member_website" name="team_mxt_member_website"
            value="<?php echo esc_attr($member['website']); ?>">

        <?php
    $social_media_platforms = [
        'facebook' => 'Facebook URL',
        'twitter' => 'Twitter URL',
        'linkedin' => 'LinkedIn URL'
    ];
    $member_social_data = $member['social'] ?? [];
    
    foreach ($social_media_platforms as $platform => $placeholder) {
        $value = $member_social_data[$platform] ?? '';
        echo '<div class="team-mxt-social-field">';
        echo '<label for="team_mxt_member_' . esc_attr($platform) . '">' . esc_html($placeholder) . ':</label>';
        echo '<input type="text" id="team_mxt_member_' . esc_attr($platform) . '" name="team_mxt_member_' . esc_attr($platform) . '" value="' . esc_attr($value) . '">';
        echo '</div>';
    }
    ?>

    <?php for ($i = 1; $i <= 3; $i++): ?>
        <?php
        $expertise_area = $member['expertise'][$i - 1]['area'] ?? '';
        $expertise_percent = $member['expertise'][$i - 1]['percent'] ?? '';
        ?>
        <div class="team-mxt-field expertise-group">
            <div class="expertise-area">
                <label for="team_mxt_member_expertise_<?php echo esc_attr($i); ?>">Expertise Area <?php echo esc_html($i); ?>:</label>
                <input type="text" 
                       id="team_mxt_member_expertise_<?php echo esc_attr($i); ?>"
                       name="team_mxt_member_expertise_<?php echo esc_attr($i); ?>" 
                       class="info-value"
                       value="<?php echo esc_attr($expertise_area); ?>">
            </div>
            <div class="expertise-percent">
                <label for="team_mxt_member_expertise_percent_<?php echo esc_attr($i); ?>">Proficiency Level (%):</label>
                <input type="number" 
                       id="team_mxt_member_expertise_percent_<?php echo esc_attr($i); ?>"
                       name="team_mxt_member_expertise_percent_<?php echo esc_attr($i); ?>" 
                       class="info-value"
                       value="<?php echo esc_attr($expertise_percent); ?>" 
                       min="0" max="100">
            </div>
        </div>
    <?php endfor; ?>

        <div class="input-wrapper">
            <label for="team_mxt_member_priority">Priority:</label>
            <input type="number" id="team_mxt_member_priority" name="team_mxt_member_priority" min="1"
                class="info-value" style="margin-bottom: 25px;"
                value="<?php echo esc_attr($member['priority'] ?? ''); ?>">
        </div>

        <button type="submit" class="button button-primary">Update Member</button>
    </form>
</div>
<?php
} else {
    wp_die('Go to all member section to edit');
}
?>