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

include plugin_dir_path(__FILE__) . '/admin-nav.php';
$all_members = get_option('team_mxt_all_members', []);

wp_enqueue_style('team-mxt-admin-style');
wp_enqueue_script('team-mxt-admin-script');

// Pagination settings
$members_per_page = 10;
$total_members = count($all_members);
$total_pages = ceil($total_members / $members_per_page);
$current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
$offset = ($current_page - 1) * $members_per_page;

// Get members for current page
$current_members = array_slice($all_members, $offset, $members_per_page);
?>
<div class="team-mxt-body all-member-section">
    <div class="team-mxt-wrap">
        <h1 class="team-mxt-title">All Team Members</h1>
        <div class="team-mxt-members-container">
            <?php foreach ($current_members as $index => $member): 
                $actual_index = $offset + $index; // Calculate actual index for edit/delete operations
            ?>
            <div class="team-mxt-member-card">
                <div class="team-mxt-member-details">
                    <div class="team-mxt-member-info" style="text-align: left;">
                        <p><strong>Name:</strong> <?php echo esc_html($member['name']); ?></p>
                        <p><strong>Designation:</strong> <?php echo esc_html($member['designation']); ?></p>
                        <p><strong>Email:</strong> <a
                                href="mailto:<?php echo esc_attr($member['email']); ?>"><?php echo esc_html($member['email']); ?></a>
                        </p>
                        <p><strong>Phone:</strong> <?php echo esc_html($member['phone']); ?></p>
                        <p><strong>Website:</strong> <a href="<?php echo esc_url($member['website']); ?>"
                                target="_blank"><?php echo esc_html($member['website']); ?></a></p>
                        <p><strong>Priority:</strong>
                            <?php echo isset($member['priority']) ? esc_html($member['priority']) : 'N/A'; ?></p>
                    </div>
                    <div class="team-mxt-member-social" style="text-align: left;">
                        <?php
                        $social_media_fields = [
                            'facebook' => 'Facebook',
                            'twitter' => 'Twitter',
                            'linkedin' => 'LinkedIn',
                            'whatsapp' => 'WhatsApp',
                            'instagram' => 'Instagram',
                            'pinterest' => 'Pinterest',
                            'discord' => 'Discord',
                            'facebook-messenger' => 'facebook-messenger',
                            'youtube' => 'YouTube',
                            'reddit' => 'Reddit',
                            'tiktok' => 'TikTok',
                            'telegram' => 'Telegram',
                            'quora' => 'Quora',
                            'tumblr' => 'Tumblr',
                            'vimeo' => 'Vimeo',
                        ];

                        $count = 0;
                        foreach ($social_media_fields as $key => $label) {
                            if (!empty($member['social'][$key]) && $count < 5) {
                                echo '<p class="social-link ' . esc_attr($key) . '"><strong>' . esc_html($label) . ':</strong> ' . esc_html($member['social'][$key]) . '</p>';
                                $count++;
                            }
                        }
                        ?>
                    </div>
                </div>
                <div class="team-mxt-member-photo">
                    <?php echo !empty($member['photo_id']) ? wp_get_attachment_image($member['photo_id'], 'thumbnail', false, array('class' => 'team-mxt-photo')) : '<div class="team-mxt-no-photo">No image</div>'; ?>
                </div>
                <div class="team-mxt-member-actions">
                    <a href="<?php echo esc_url(admin_url('admin.php?page=team_mxt_edit_member&member_index=' . $actual_index)); ?>"
                        class="team-mxt-edit">Edit</a>
                    <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=team_mxt_all_members&action=delete&member_index=' . $actual_index), 'delete_member_' . $actual_index)); ?>"
                        class="team-mxt-delete"
                        onclick='return confirm("Are you sure you want to delete this member?")'>Delete</a>
                </div>
            </div>
            <?php endforeach; ?>
        </div>

        <?php if ($total_pages > 1): ?>
        <div class="team-mxt-pagination">
            <?php
            // Previous page
            if ($current_page > 1) {
                echo '<a href="' . esc_url(add_query_arg('paged', ($current_page - 1))) . '" class="page-numbers prev">&laquo; Previous</a>';
            }

            // Page numbers
            for ($i = 1; $i <= $total_pages; $i++) {
                if ($i == $current_page) {
                    echo '<span class="page-numbers current">' . $i . '</span>';
                } else {
                    echo '<a href="' . esc_url(add_query_arg('paged', $i)) . '" class="page-numbers">' . $i . '</a>';
                }
            }

            // Next page
            if ($current_page < $total_pages) {
                echo '<a href="' . esc_url(add_query_arg('paged', ($current_page + 1))) . '" class="page-numbers next">Next &raquo;</a>';
            }
            ?>
        </div>
        <?php endif; ?>
    </div>
    <div class="notification">
        <div class="close-btn">&times;</div>
        <div class="character">
            <i class="fas fa-male"></i>
        </div>
        <div class="speech-bubble">
            <div class="message">Here you'll be able to <a style="color:green;">edit</a>/<a style="color:red;">delete</a> information and your existing all members
                preview.
            </div>
        </div>
    </div>
</div>