<?php
/**
 * Member display functionality for Member Minder Pro plugin
 * 
 * This file contains the member display functions used across various shortcodes.
 * It provides different styles of displaying member information while maintaining
 * backward compatibility with existing implementations.
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

/**
 * Array of default display attributes for member cards
 *
 * @return array Default display attributes
 */
function mmpro_get_default_display_atts() {
    return array(
        'cols' => 3,
        'pics' => 1,
        'show_committee_name' => 0,
        'show_committee_position' => 0,
        'show_club_position' => 0,
        'show_district_number' => 0,
        'show_business_position' => 0,
        'show_classification' => 0,
        'show_contact' => 1,
        'org_data_entity' => 0,
        'org_data_account' => 0,
        'name_as_contact' => 0,
        'v323_compat' => 0
    );
}

/**
 * Main function to display member cards
 *
 * @param object $xml            XML data containing member information
 * @param int    $style          Display style (0-5)
 * @param array  $atts           Display attributes
 *
 * @global string $data_path     Data retrieval path
 * @global string $file_path     File operations path
 * @global array $options        Plugin options
 *
 * @return string                HTML markup for member display
 */
// function dacdb_DisplayClub($xml, $style, $atts) {
//     // Maintain backward compatibility while introducing new naming
//     return mmpro_display_members($xml, $style, $atts);
// }

/**
 * Helper function to get member name, preferring membername over username
 * 
 * @param object $member The member object containing name information
 * @return string The member's name
 */
function mmpro_get_member_name($member) {
    return (!empty($member->membername)) ? $member->membername : $member->username;
}

/**
 * Modern implementation of member display function
 *
 * @param object $xml            XML data containing member information
 * @param int    $style          Display style (0-5)
 * @param array  $atts           Display attributes
 *
 * @global string $data_path     Data retrieval path
 *
 * @return string                HTML markup for member display
 */
function mmpro_display_members($xml, $style, $atts) {
    global $data_path, $options;
    
    // Default attributes
    $default_atts = mmpro_get_default_display_atts();
    
    // Merge provided attributes with defaults
    $display_atts = array_merge($default_atts, $atts);
    
    // Extract attributes for easier access
    extract($display_atts);
    
    // Initialize output
    $output = '';
    
    // Set default style to 1 if not specified
    $style = !empty($style) ? $style : 1;
    
    // Begin container
    $output .= '<div class="mmpro-members style-' . esc_attr($style) . '">';
    
    // Determine which XML structure we're dealing with
    $members = null;
    if (isset($xml->committeemembers)) {
        $members = $xml->committeemembers;
    } elseif (isset($xml->club->officers->officer)) {
        $members = $xml->club->officers->officer;
    }
    
    if (!$members) {
        return '<div class="error-msg"><i class="fa fa-times-circle"></i>Error: No valid member data found</div>';
    }
    
    // Choose display style based on style parameter
    switch ($style) {
        case 0: // Test Mode
            if (isset($xml->committeemembers)) {
                foreach ($xml->committeemembers as $member) {
                    $output .= $member->username . "<br/>";
                }
            } else {
                foreach ($xml->club as $club) {
                    $output .= $club->clubname . "<br/>";
                    $output .= count($club->officers->officer) . "<br>";
                    foreach ($club->officers->officer as $officer) {
                        $output .= $officer->username . "<br/>";
                    }
                }
            }
            break;
            
        case 1: // Aligned Columns (non-responsive)
            $setDivWidth = 160;
            $setImgWidth = 150;
            
            // Get number of columns from options or attributes
            $num_columns = 3; // Default
            if (!empty($options['committee_members_per_row'])) {
                $num_columns = intval($options['committee_members_per_row']);
            }
            if (!empty($atts['cols'])) {
                $num_columns = intval($atts['cols']);
            }
            if (!empty($atts['leaders_per_row'])) {
                $num_columns = intval($atts['leaders_per_row']);
            }
            
            // Calculate column width
            $column_width = 100 / $num_columns;
            $div = '<div class="members" style="width: ' . $column_width . '%; display: inline-block; vertical-align: top;">';
            
            $counter = 0;
            
            if (isset($xml->committeemembers)) {
                foreach ($xml->committeemembers as $member) {
                    $name = !empty($member->membername) ? $member->membername : $member->username;
                    
                    $output .= $div;
                    $output .= '<table border="0" cellpadding="0" cellspacing="0">';
                    $output .= '<tr>';
                    
                    if ($pics == 1) {
                        $output .= '<td style="border:0 !important;vertical-align: top; padding:0; width:' . $setDivWidth . 'px;">';
                        if ($member->image == '') {
                            $output .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . plugins_url('img/avatar.svg', dirname(dirname(__FILE__))) . '" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . 'px; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                        } else {
                            $output .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . $member->image . '" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . 'px; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                        }
                        $output .= '</td>';
                    }
                    
                    $output .= '<td style="border:0 !important; text-align: left; vertical-align: top; padding:0;">';
                    $output .= '<strong>' . $name . '</strong><BR>';
                    $output .= $member->clubposition . '<BR>';
                    
                    if ($show_contact == 1) {
                        $modal_classes = dacdb_is_page_builder() ? '' : 'iframe3 cboxElement';
                        $output .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" name="dlc-inline-link-' . $counter . '" class="' . $modal_classes . '">Contact</a>';
                    }
                    
                    $output .= '</td>';
                    $output .= '</tr>';
                    $output .= '</table>';
                    $output .= '</div>';
                    
                    $counter++;
                }
            } else {
                foreach ($xml->club as $club) {
                    foreach ($club->officers->officer as $officer) {
                        $name = !empty($officer->membername) ? $officer->membername : $officer->username;
                        
                        $output .= $div;
                        $output .= '<table border="0" cellpadding="0" cellspacing="0">';
                        $output .= '<tr>';
                        
                        if ($pics == 1) {
                            $output .= '<td style="border:0 !important;vertical-align: top; padding:0; width:' . $setDivWidth . 'px;">';
                            if ($officer->image == '') {
                                $output .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . plugins_url('img/avatar.svg', dirname(dirname(__FILE__))) . '" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . 'px; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                            } else {
                                $output .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . $officer->image . '" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . 'px; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                            }
                            $output .= '</td>';
                        }
                        
                        $output .= '<td style="border:0 !important; text-align: left; vertical-align: top; padding:0;">';
                        $output .= '<strong>' . $name . '</strong><BR>';
                        $output .= $officer->clubposition . '<BR>';
                        
                        if ($show_contact == 1) {
                            $modal_classes = dacdb_is_page_builder() ? '' : 'iframe3 cboxElement';
                            $output .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $officer->userid . '" name="dlc-inline-link-' . $counter . '" class="' . $modal_classes . '">Contact</a>';
                        }
                        
                        $output .= '</td>';
                        $output .= '</tr>';
                        $output .= '</table>';
                        $output .= '</div>';
                        
                        $counter++;
                    }
                }
            }
            break;
            
        case 3: // New 2 columns (full width)
            $setDivWidth = 100;
            $setImgWidth = 95;
            $output .= '<div class="dac-row" style="width: 100%;">';
            $output .= '<table border="5" cellpadding="3" cellspacing="0" style="width: 100%;">';
            
            $counter = 0;
            if (isset($xml->committeemembers)) {
                foreach ($xml->committeemembers as $member) {
                    $name = !empty($member->membername) ? $member->membername : $member->username;
                    
                    $output .= '<tr>';
                    $output .= '<td valign="top" align="left" width="40%">';
                    $output .= '<table border="1" cellpadding="3" cellspacing="0">';
                    
                    $output .= '<tr>';
                    if ($pics == 1) {
                        $output .= '<td style="border:0 !important;vertical-align: top; padding:0; width:' . $setDivWidth . 'px;">';
                        if ($member->image == '') {
                            $output .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . plugins_url('img/avatar.svg', dirname(dirname(__FILE__))) . '" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . 'px; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                        } else {
                            $output .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . $member->image . '" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . 'px; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                        }
                        $output .= '</td>';
                    }
                    
                    $output .= '<td style="border:0 !important; text-align: left; vertical-align: top; padding:0;">';
                    $output .= '<strong>' . $name . '</strong><BR>';
                    $output .= $member->clubposition . '<BR>';
                    
                    if ($show_contact == 1) {
                        $modal_classes = dacdb_is_page_builder() ? '' : 'iframe3 cboxElement';
                        $output .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" name="dlc-inline-link-' . $counter . '" class="' . $modal_classes . '">Contact</a>';
                    }
                    
                    $output .= '</td>';
                    $output .= '</tr>';
                    $output .= '</table>';
                    $output .= '</td>';
                    $output .= '<td valign="top" width="60%">';
                    $output .= '</td>';
                    $output .= '</tr>';
                    $output .= '<tr><td colspan="2"><hr style="margin:10px 0 5px 0;"></td></tr>';
                    $counter++;
                }
            } else {
                foreach ($xml->club as $club) {
                    $output .= '<tr>';
                    $output .= '<td valign="top" align="left" width="40%">';
                    $output .= '<table border="1" cellpadding="3" cellspacing="0">';
                    
                    foreach ($club->officers->officer as $officer) {
                        $name = !empty($officer->membername) ? $officer->membername : $officer->username;
                        
                        $output .= '<tr>';
                        if ($pics == 1) {
                            $output .= '<td style="border:0 !important;vertical-align: top; padding:0; width:' . $setDivWidth . 'px;">';
                            if ($officer->image == '') {
                                $output .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . plugins_url('img/avatar.svg', dirname(dirname(__FILE__))) . '" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . 'px; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                            } else {
                                $output .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . $officer->image . '" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . 'px; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                            }
                            $output .= '</td>';
                        }
                        
                        $output .= '<td style="border:0 !important; text-align: left; vertical-align: top; padding:0;">';
                        $output .= '<strong>' . $name . '</strong><BR>';
                        $output .= $officer->clubposition . '<BR>';
                        
                        if ($show_contact == 1) {
                            $modal_classes = dacdb_is_page_builder() ? '' : 'iframe3 cboxElement';
                            $output .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $officer->userid . '" name="dlc-inline-link-' . $counter . '" class="' . $modal_classes . '">Contact</a>';
                        }
                        
                        $output .= '</td>';
                        $output .= '</tr>';
                        $counter++;
                    }
                    
                    $output .= '</table>';
                    $output .= '</td>';
                    $output .= '<td valign="top" width="60%">';
                    $output .= '<a href="' . $club->cluburl . '" target="new"><strong>' . $club->clubname . '</strong></a><br>';
                    $output .= '<i>' . $club->clubaddress . '<br>';
                    if ($club->clubcity != '') {
                        $output .= $club->clubcity . ', ' . $club->clubstatecode . ' ' . $club->clubpostalzip . '</i><br>';
                    } else {
                        $output .= $club->clubstatecode . ' ' . $club->clubpostalzip . '</i><br>';
                    }
                    $output .= '<strong>Where We Meet </strong><br>';
                    $output .= $club->meetingplace . '<br>';
                    
                    if ($club->meetingaddress != '') {
                        $output .= $club->meetingaddress . '<br>';
                    }
                    if ($club->meetingcity != '') {
                        $output .= $club->meetingcity . ', ' . $club->meetingstatecode . ' ' . $club->meetingpostalcode . '<br>';
                    } else {
                        $output .= $club->meetingstatecode . ' ' . $club->meetingpostalcode . '<br>';
                    }
                    
                    $output .= '<strong>When</strong><br>';
                    $output .= $club->meetingday . ' at ' . $club->meetingtime;
                    $output .= '<span style="float: right;"><a href="https://maps.google.com/maps?q=' . $club->latitude . ',' . $club->longitude . '" target="new">Map It</a></span>';
                    $output .= '</td>';
                    $output .= '</tr>';
                    $output .= '<tr><td colspan="2"><hr style="margin:10px 0 5px 0;"></td></tr>';
                }
            }
            
            $output .= '</table>';
            $output .= '</div>';
            break;
            
        case 4: // Modern card style (horizontal layout)
            $counter = 0;
            
            if (isset($xml->committeemembers)) {
                foreach ($xml->committeemembers as $member) {
                    $name = !empty($member->membername) ? $member->membername : $member->username;
                    
                    $output .= '<div class="mmpro-member-card">';
                    
                    if ($pics == 1) {
                        $output .= '<div class="mmpro-card-image">';
                        if (empty($member->image)) {
                            $output .= '<img src="' . plugins_url('img/avatar.svg', dirname(dirname(__FILE__))) . '" alt="Member avatar">';
                        } else {
                            $output .= '<img src="' . $member->image . '" alt="' . esc_attr($name) . '">';
                        }
                        $output .= '</div>';
                    }
                    
                    $output .= '<div class="mmpro-card-content">';
                    $output .= '<h3 class="mmpro-card-name">' . $name . '</h3>';
                    $output .= '<div class="mmpro-card-details">';
                    
                    if (!empty($member->clubposition)) {
                        $output .= '<div class="mmpro-card-detail mmpro-card-club-position">' . esc_html($member->clubposition) . '</div>';
                    }
                    
                    $output .= '</div>';
                    
                    if ($show_contact == 1 && !empty($member->userid)) {
                        $modal_classes = dacdb_is_page_builder() ? '' : 'iframe3 cboxElement';
                        $output .= '<div class="mmpro-card-footer">';
                        $output .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" name="dlc-inline-link-' . $counter . '" class="' . $modal_classes . ' mmpro-contact-btn">Contact</a>';
                        $output .= '</div>';
                    }
                    
                    $output .= '</div>';
                    $output .= '</div>';
                    $counter++;
                }
            } else {
                foreach ($xml->club as $club) {
                    foreach ($club->officers->officer as $officer) {
                        $name = !empty($officer->membername) ? $officer->membername : $officer->username;
                        
                        $output .= '<div class="mmpro-member-card">';
                        
                        if ($pics == 1) {
                            $output .= '<div class="mmpro-card-image">';
                            if (empty($officer->image)) {
                                $output .= '<img src="' . plugins_url('img/avatar.svg', dirname(dirname(__FILE__))) . '" alt="Member avatar">';
                            } else {
                                $output .= '<img src="' . $officer->image . '" alt="' . esc_attr($name) . '">';
                            }
                            $output .= '</div>';
                        }
                        
                        $output .= '<div class="mmpro-card-content">';
                        $output .= '<h3 class="mmpro-card-name">' . $name . '</h3>';
                        $output .= '<div class="mmpro-card-details">';
                        
                        if (!empty($officer->clubposition)) {
                            $output .= '<div class="mmpro-card-detail mmpro-card-club-position">' . esc_html($officer->clubposition) . '</div>';
                        }
                        
                        $output .= '</div>';
                        
                        if ($show_contact == 1 && !empty($officer->userid)) {
                            $modal_classes = dacdb_is_page_builder() ? '' : 'iframe3 cboxElement';
                            $output .= '<div class="mmpro-card-footer">';
                            $output .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $officer->userid . '" name="dlc-inline-link-' . $counter . '" class="' . $modal_classes . ' mmpro-contact-btn">Contact</a>';
                            $output .= '</div>';
                        }
                        
                        $output .= '</div>';
                        $output .= '</div>';
                        $counter++;
                    }
                }
            }
            break;
            
        case 5: // Modern BEM card style (horizontal layout)
            $counter = 0;
            
            if (isset($xml->committeemembers)) {
                foreach ($xml->committeemembers as $member) {
                    $name = !empty($member->membername) ? $member->membername : $member->username;
                    
                    $output .= '<div class="mmpro-member-card">';
                    
                    if ($pics == 1) {
                        $output .= '<div class="mmpro-card-image">';
                        if (empty($member->image)) {
                            $output .= '<img src="' . plugins_url('img/avatar.svg', dirname(dirname(__FILE__))) . '" alt="Member avatar">';
                        } else {
                            $output .= '<img src="' . $member->image . '" alt="' . esc_attr($name) . '">';
                        }
                        $output .= '</div>';
                    }
                    
                    $output .= '<div class="mmpro-card-content">';
                    $output .= '<h3 class="mmpro-card-name">' . $name . '</h3>';
                    $output .= '<div class="mmpro-card-details">';
                    
                    if (!empty($member->clubposition)) {
                        $output .= '<div class="mmpro-card-detail mmpro-card-club-position">' . esc_html($member->clubposition) . '</div>';
                    }
                    
                    $output .= '</div>';
                    
                    if ($show_contact == 1 && !empty($member->userid)) {
                        $modal_classes = dacdb_is_page_builder() ? '' : 'iframe3 cboxElement';
                        $output .= '<div class="mmpro-card-footer">';
                        $output .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" name="dlc-inline-link-' . $counter . '" class="' . $modal_classes . ' mmpro-contact-btn">Contact</a>';
                        $output .= '</div>';
                    }
                    
                    $output .= '</div>';
                    $output .= '</div>';
                    $counter++;
                }
            } else {
                foreach ($xml->club as $club) {
                    foreach ($club->officers->officer as $officer) {
                        $name = !empty($officer->membername) ? $officer->membername : $officer->username;
                        
                        $output .= '<div class="mmpro-member-card">';
                        
                        if ($pics == 1) {
                            $output .= '<div class="mmpro-card-image">';
                            if (empty($officer->image)) {
                                $output .= '<img src="' . plugins_url('img/avatar.svg', dirname(dirname(__FILE__))) . '" alt="Member avatar">';
                            } else {
                                $output .= '<img src="' . $officer->image . '" alt="' . esc_attr($name) . '">';
                            }
                            $output .= '</div>';
                        }
                        
                        $output .= '<div class="mmpro-card-content">';
                        $output .= '<h3 class="mmpro-card-name">' . $name . '</h3>';
                        $output .= '<div class="mmpro-card-details">';
                        
                        if (!empty($officer->clubposition)) {
                            $output .= '<div class="mmpro-card-detail mmpro-card-club-position">' . esc_html($officer->clubposition) . '</div>';
                        }
                        
                        $output .= '</div>';
                        
                        if ($show_contact == 1 && !empty($officer->userid)) {
                            $modal_classes = dacdb_is_page_builder() ? '' : 'iframe3 cboxElement';
                            $output .= '<div class="mmpro-card-footer">';
                            $output .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $officer->userid . '" name="dlc-inline-link-' . $counter . '" class="' . $modal_classes . ' mmpro-contact-btn">Contact</a>';
                            $output .= '</div>';
                        }
                        
                        $output .= '</div>';
                        $output .= '</div>';
                        $counter++;
                    }
                }
            }
            break;
            
        default:
            $output .= '<div class="error-msg"><i class="fa fa-times-circle"></i>Invalid style selected: <pre>' . $style . '</pre></div>';
            break;
    }
    
    // End container
    $output .= '</div>';
    
    return $output;
}

/**
 * Display members in the original v3.2.3 legacy style
 *
 * @param object $xml            XML data containing member information
 * @param array  $atts           Display attributes
 *
 * @global string $data_path     Data retrieval path
 *
 * @return string                HTML markup for member display
 */
function mmpro_display_members_style_legacy($xml, $atts) {
    global $data_path;
    
    // Extract attributes for easier access
    extract($atts);
    
    // Initialize output
    $output = '';
    
    // Set column div classes based on columns
    $col_class = 'members dac-col-xs-12';
    $div_width = 155;
    $img_width = 150;
    
    if ($cols == 2) {
        $col_class = 'members dac-col-xs-12 dac-col-sm-6';
        $div_width = 140;
        $img_width = 135;
    } else if ($cols == 3) {
        $col_class = 'members dac-col-xs-12 dac-col-sm-12 dac-col-md-4';
        $div_width = 125;
        $img_width = 120;
    } else if ($cols == 4) {
        $col_class = 'members dac-col-xs-12 dac-col-sm-6 dac-col-md-3 dac-col-lg-3';
        $div_width = 110;
        $img_width = 105;
    }
    
    // Begin row
    $output .= '<div class="dac-row">';
    
    // Counter for tracking members
    $counter = 0;
    
    // Loop through each member
    foreach ($xml as $member) {
        // Begin column
        $output .= '<div class="' . $col_class . '">';
        
        // Start table
        $output .= '<table border="0" cellpadding="0" cellspacing="0">';
        $output .= '<tr>';
        
        // Display member picture if enabled
        if ($pics == 1) {
            $output .= '<td style="border:0 !important;vertical-align: top; padding:0; width:' . $div_width . 'px;">';
            
            // Use default image if member has no image
            if (empty($member->image)) {
                $output .= '<div style="width:' . $div_width . 'px;"><img src="' . plugins_url('img/avatar.svg', dirname(dirname(__FILE__))) . '" style="aspect-ratio: 4 / 5; width: ' . $img_width . 'px; object-fit: cover;" width="' . $img_width . '"></div>';
            } else {
                $output .= '<div style="width:' . $div_width . 'px;"><img src="' . $member->image . '" style="aspect-ratio: 4 / 5; width: ' . $img_width . 'px; object-fit: cover;" width="' . $img_width . '"></div>';
            }
            
            $output .= '</td>';
        }
        
        // Member info column
        $output .= '<td style="border:0 !important; text-align: left; vertical-align: top; padding:0;">';
        
        // Member name - not as contact
        $output .= '<strong>' . mmpro_get_member_name($member) . '</strong><br>';
        
        // Club information if enabled
        if (!empty($org_data_entity)) {
            // Display club name with link if URL available
            if (!empty($member->cluburl)) {
                $cluburl = $member->cluburl;
                if (strpos($cluburl, 'http') === false) {
                    $cluburl = 'https://' . $cluburl;
                }
                $output .= '<a href="' . esc_url($cluburl) . '" target="_blank">' . esc_html($member->clubname) . '</a><br>';
            } else {
                $output .= '<span class="clubname">' . esc_html($member->clubname) . '</span><br>';
            }
        }
        
        // District number if enabled
        if ($show_district_number == 1 && !empty($member->accountid)) {
            $output .= 'District ' . esc_html($member->accountid) . '<br>';
        }
        
        // Committee position if enabled
        if ($show_committee_position == 1 && !empty($member->committeeposition)) {
            $output .= esc_html($member->committeeposition) . '<br>';
        }
        
        // Club position if enabled
        if ($show_club_position == 1 && !empty($member->clubposition)) {
            $output .= esc_html($member->clubposition) . ' club position<br>';
        }
        
        // Business position if enabled
        if ($show_business_position == 1 && !empty($member->busposition)) {
            $output .= esc_html($member->busposition) . '<br>';
        }
        
        // Classification if enabled
        if ($show_classification == 1 && !empty($member->classification)) {
            $output .= esc_html($member->classification) . '<br>';
        }
        
        // Contact link if enabled
        if ($show_contact == 1 && !empty($member->userid)) {
            $modal_classes = dacdb_is_page_builder() ? '' : 'iframe3 cboxElement';
            $output .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" name="dlc-inline-link-' . $counter . '" class="' . $modal_classes . '">Contact</a>';
        }
        
        $output .= '</td>';
        $output .= '</tr>';
        $output .= '</table>';
        $output .= '</div>'; // End column
        
        $counter++;
    }
    
    $output .= '</div>'; // End row
    
    return $output;
}

/**
 * Display members in legacy aligned columns style
 *
 * @param object $xml            XML data containing member information
 * @param array  $atts           Display attributes
 *
 * @global string $data_path     Data retrieval path
 *
 * @return string                HTML markup for member display
 */
function mmpro_display_members_style_columns($members, $atts) {
    global $data_path;
    
    // Extract attributes for easier access
    extract($atts);
    
    // Initialize output
    $output = '';
    
    // Set column div classes based on columns
    $col_class = 'mmpro-member dac-col-xs-12';
    
    if ($cols == 2) {
        $col_class = 'mmpro-member dac-col-xs-12 dac-col-sm-6';
    } else if ($cols == 3) {
        $col_class = 'mmpro-member dac-col-xs-12 dac-col-sm-12 dac-col-md-4';
    } else if ($cols == 4) {
        $col_class = 'mmpro-member dac-col-xs-12 dac-col-sm-6 dac-col-md-3 dac-col-lg-3';
    }
    
    // Begin row
    $output .= '<div class="dac-row">';
    
    // Loop through each member
    $counter = 0;
    foreach ($members as $member) {
        // Begin column
        $output .= '<div class="' . $col_class . '">';
        $output .= '<div class="mmpro-member-inner">';
        
        // Display member picture if enabled
        if ($pics == 1) {
            $output .= '<div class="mmpro-member-image">';
            
            // Use default image if member has no image
            if (empty($member->image)) {
                $output .= '<img src="' . plugins_url('img/avatar.svg', dirname(dirname(__FILE__))) . '" alt="Member avatar">';
            } else {
                $output .= '<img src="' . $member->image . '" alt="' . esc_attr(mmpro_get_member_name($member)) . '">';
            }
            
            $output .= '</div>';
        }
        
        // Member info section
        $output .= '<div class="mmpro-member-info">';
        
        // Member name - as contact or plain text
        if ($show_contact == 1 && $name_as_contact == 1 && !empty($member->userid)) {
            $output .= '<h4 class="mmpro-member-name"><a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" name="dlc-inline-link-' . $counter . '" class="dacdb-modal-trigger--fixed cboxElement">' . mmpro_get_member_name($member) . '</a></h4>';
        } else {
            $output .= '<h4 class="mmpro-member-name">' . mmpro_get_member_name($member) . '</h4>';
        }
        
        // Club information if enabled
        if (!empty($org_data_entity)) {
            $output .= '<div class="mmpro-member-club">';
            
            // Display club name with link if URL available
            if (!empty($member->cluburl)) {
                $cluburl = $member->cluburl;
                if (strpos($cluburl, 'http') === false) {
                    $cluburl = 'https://' . $cluburl;
                }
                $output .= '<a href="' . esc_url($cluburl) . '" target="_blank">' . esc_html($member->clubname) . '</a>';
            } else {
                $output .= '<span class="clubname">' . esc_html($member->clubname) . '</span>';
            }
            
            $output .= '</div>';
        }
        
        // District number if enabled
        if ($show_district_number == 1 && !empty($member->accountid)) {
            $output .= '<div class="mmpro-member-district">District ' . esc_html($member->accountid) . '</div>';
        }
        
        // Committee position if enabled
        if ($show_committee_position == 1 && !empty($member->committeeposition)) {
            $output .= '<div class="mmpro-member-committee-position">' . esc_html($member->committeeposition) . '</div>';
        }
        
        // Club position if enabled
        if ($show_club_position == 1 && !empty($member->clubposition)) {
            $output .= '<div class="mmpro-member-club-position">' . esc_html($member->clubposition) . '</div>';
        }
        
        // Business position if enabled
        if ($show_business_position == 1 && !empty($member->busposition)) {
            $output .= '<div class="mmpro-member-business-position">' . esc_html($member->busposition) . '</div>';
        }
        
        // Classification if enabled
        if ($show_classification == 1 && !empty($member->classification)) {
            $output .= '<div class="mmpro-member-classification">' . esc_html($member->classification) . '</div>';
        }
        
        // Contact link if enabled
        if ($show_contact == 1 && !$name_as_contact && !empty($member->userid)) {
            $modal_classes = dacdb_is_page_builder() ? '' : 'iframe3 cboxElement';
            $output .= '<div class="mmpro-member-contact"><a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" name="dlc-inline-link-' . $counter . '" class="' . $modal_classes . '">Contact</a></div>';
        }
        
        $output .= '</div>'; // End member info
        $output .= '</div>'; // End member inner
        $output .= '</div>'; // End column
        
        $counter++;
    }
    
    $output .= '</div>'; // End row
    
    return $output;
}

/**
 * Display members in modern card styles
 *
 * @param object $xml            XML data containing member information
 * @param array  $atts           Display attributes
 * @param int    $card_style     Card style number (3-5)
 *
 * @global string $data_path     Data retrieval path
 *
 * @return string                HTML markup for member display
 */
function mmpro_display_members_style_cards($xml, $atts, $card_style = 3) {
    global $data_path;
    
    // Extract attributes for easier access
    extract($atts);
    
    // Initialize output
    $output = '';
    
    // Set column div classes based on columns
    $col_class = 'mmpro-member dac-col-xs-12';
    
    if ($cols == 2) {
        $col_class = 'mmpro-member dac-col-xs-12 dac-col-sm-6';
    } else if ($cols == 3) {
        $col_class = 'mmpro-member dac-col-xs-12 dac-col-sm-6 dac-col-md-4';
    } else if ($cols == 4) {
        $col_class = 'mmpro-member dac-col-xs-12 dac-col-sm-6 dac-col-md-3';
    }
    
    // Add card style class
    $card_class = 'mmpro-card-style-' . $card_style;
    
    // Begin row
    $output .= '<div class="dac-row">';
    
    // Loop through each member
    $counter = 0;
    foreach ($xml as $member) {
        // Begin column
        $output .= '<div class="' . $col_class . '">';
        $output .= '<div class="mmpro-member-card ' . $card_class . '">';
        
        // Display member picture if enabled
        if ($pics == 1) {
            $output .= '<div class="mmpro-card-image">';
            
            // Use default image if member has no image
            if (empty($member->image)) {
                $output .= '<img src="' . plugins_url('img/avatar.svg', dirname(dirname(__FILE__))) . '" alt="Member avatar">';
            } else {
                $output .= '<img src="' . $member->image . '" alt="' . esc_attr(mmpro_get_member_name($member)) . '">';
            }
            
            $output .= '</div>';
        }
        
        // Member info section
        $output .= '<div class="mmpro-card-content">';
        
        // Member name - as contact or plain text
        if ($show_contact == 1 && $name_as_contact == 1 && !empty($member->userid)) {
            $output .= '<h3 class="mmpro-card-name"><a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" name="dlc-inline-link-' . $counter . '" class="dacdb-modal-trigger--fixed cboxElement">' . mmpro_get_member_name($member) . '</a></h3>';
        } else {
            $output .= '<h3 class="mmpro-card-name">' . mmpro_get_member_name($member) . '</h3>';
        }
        
        // Begin member details
        $output .= '<div class="mmpro-card-details">';
        
        // Club information if enabled
        if (!empty($org_data_entity)) {
            $output .= '<div class="mmpro-card-detail mmpro-card-club">';
            
            // Display club name with link if URL available
            if (!empty($member->cluburl)) {
                $cluburl = $member->cluburl;
                if (strpos($cluburl, 'http') === false) {
                    $cluburl = 'https://' . $cluburl;
                }
                $output .= '<a href="' . esc_url($cluburl) . '" target="_blank">' . esc_html($member->clubname) . '</a>';
            } else {
                $output .= '<span class="clubname">' . esc_html($member->clubname) . '</span>';
            }
            
            $output .= '</div>';
        }
        
        // District number if enabled
        if ($show_district_number == 1 && !empty($member->accountid)) {
            $output .= '<div class="mmpro-card-detail mmpro-card-district">District ' . esc_html($member->accountid) . '</div>';
        }
        
        // Committee position if enabled
        if ($show_committee_position == 1 && !empty($member->committeeposition)) {
            $output .= '<div class="mmpro-card-detail mmpro-card-committee-position">' . esc_html($member->committeeposition) . '</div>';
        }
        
        // Club position if enabled
        if ($show_club_position == 1 && !empty($member->clubposition)) {
            $output .= '<div class="mmpro-card-detail mmpro-card-club-position">' . esc_html($member->clubposition) . '</div>';
        }
        
        // Business position if enabled
        if ($show_business_position == 1 && !empty($member->busposition)) {
            $output .= '<div class="mmpro-card-detail mmpro-card-business-position">' . esc_html($member->busposition) . '</div>';
        }
        
        // Classification if enabled
        if ($show_classification == 1 && !empty($member->classification)) {
            $output .= '<div class="mmpro-card-detail mmpro-card-classification">' . esc_html($member->classification) . '</div>';
        }
        
        $output .= '</div>'; // End member details
        
        // Footer section with contact link if enabled
        if ($show_contact == 1 && !$name_as_contact && !empty($member->userid)) {
            $modal_classes = dacdb_is_page_builder() ? '' : 'iframe3 cboxElement';
            $output .= '<div class="mmpro-card-footer">';
            $output .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" name="dlc-inline-link-' . $counter . '" class="' . $modal_classes . ' mmpro-contact-btn">Contact</a>';
            $output .= '</div>';
        }
        
        $output .= '</div>'; // End card content
        $output .= '</div>'; // End card
        $output .= '</div>'; // End column
        
        $counter++;
    }
    
    $output .= '</div>'; // End row
    
    return $output;
}