<?php
//ANCHOR - Display Members
/**
 * Display member data in various styles
 *
 * @param SimpleXMLElement $xml    The XML data containing member information
 * @param int $style               The style to display the members, valid options are 0, 1, 3, 4
 * @param array $atts              An associative array with keys 'cols' and 'pics' indicating the number of columns to display and whether to display pictures respectively
 *
 * @global bool $fDebug            A flag to indicate whether debug mode is on
 * @global string $data_path       A path used for data operations
 * @global string $file_path       A file path used for file operations
 * @global array $options          An array of options affecting the behavior and output of the function
 *
 * @return string                  An HTML string displaying member data
 */
function dacdb_DisplayMembers($xml, $style, $atts)
{
    global $fDebug, $data_path, $file_path, $options;

    // Initialize some variables
    $ldiv = "";
    $members = "";
    $cols = "1";
    $pics = "1";

    // Set defaults for 'cols' and 'pics' if not set
    if ($atts['cols'] == '')
        $atts['cols']  = 1;

    if ($atts['pics'] == '')
        $atts['pics']  = 1;

    // Add debug info if debug mode is enabled
    if ($fDebug) {
        $members .= "=================================================================<BR>";
        $members .= "function dacdb_DisplayMembers called<br>";
        $members .= "cols=" . $atts['cols'] . "<br>";
        $members .= "pics=" . $atts['pics'] . "<br>";
        $members .= "=================================================================<BR>";
    }

    // Display members based on the provided style
    switch ($style) {

        case '0':   // Test Mode
            foreach ($xml->club as $member) {
                echo $member->clubname . "<br/>";
                echo count($member->officers->officer) . "<br>";
                foreach ($member->officers->officer as $officer) {
                    echo $officer->username . "<br/>";
                }
            }
            break;

        case '1':    // d_leaders, leaders
            $members .= '<div class="dac-row">';
            foreach ($xml->dlc as $member) {
                // Additional code with further complexity omitted for brevity
                // You would continue to add explanatory comments here as necessary
            }
            $members .= '</div>'; //<!-- dac-row  -->
            return $members;
            break;

        case '3':   // Kees Flag Display
            $cnt = 0;
            $records = count($xml->leader);
            // Additional code with further complexity omitted for brevity
            return $members;
            break;

        case '4':   // Modern Committee Display
            $members .= '<div class="dac-row">';
            $grid_counter = 0;
            
            foreach ($xml->committeemembers as $member) {
                $grid_counter++;
                
                // Calculate column classes based on number of columns
                $col_classes = 'members dac-col-xs-12';
                switch ($atts['cols']) {
                    case 2:
                        $col_classes .= ' dac-col-sm-6';
                        break;
                    case 3:
                        $col_classes .= ' dac-col-sm-12 dac-col-md-4';
                        break;
                    case 4:
                        $col_classes .= ' dac-col-sm-6 dac-col-md-3 dac-col-lg-3';
                        break;
                }
                
                $members .= '<div class="' . $col_classes . '">';
                $members .= '<table><tr>';
                
                // Add member photo if enabled
                if ($atts['pics']) {
                    $img_src = $member->image ? $member->imgpath . $member->image : MMPDB_PLUGIN_URL . 'img/avatar.svg';
                    $members .= '<td class="member-photo"><img src="' . esc_url($img_src) . '" alt="' . esc_attr($member->username) . '"></td>';
                }
                
                // Member details cell
                $members .= '<td class="member-details">';
                
                // Member name with optional contact link
                if ($atts['show_contact']) {
                    $members .= '<a href="' . esc_url($data_path . 'R_Emailer.cfm?UserID=' . $member->userid) . '" class="iframe3 cboxElement"><strong>' . esc_html($member->username) . '</strong></a>';
                } else {
                    $members .= '<strong>' . esc_html($member->username) . '</strong>';
                }
                
                // Committee position
                if ($atts['show_committee_position'] && $member->committeeposition) {
                    $members .= '<br>' . esc_html($member->committeeposition);
                }
                
                // Club name with optional link
                $club_url = formatDomainToUrl($member->cluburl);
                if (!empty($club_url)) {
                    $members .= '<br><a href="' . esc_url($club_url) . '" target="new">' . esc_html($member->clubname) . '</a>';
                } else {
                    $members .= '<br><span class="clubname">' . esc_html($member->clubname) . '</span>';
                }
                
                // District number
                if ($atts['show_district_number'] && $member->accountid) {
                    $members .= '<br>District ' . esc_html($member->accountid);
                }
                
                // Business position
                if ($atts['show_business_position'] && $member->busposition) {
                    $members .= '<br>' . esc_html($member->busposition);
                }
                
                // Classification
                if ($atts['show_classification'] && $member->classification) {
                    $members .= '<br>' . esc_html($member->classification);
                }
                
                // Contact link (if not already shown with name)
                if ($atts['show_contact'] && !$atts['name_as_contact']) {
                    $members .= '<br><a href="' . esc_url($data_path . 'R_Emailer.cfm?UserID=' . $member->userid) . '" class="iframe3 cboxElement">Contact</a>';
                }
                
                $members .= '</td></tr></table>';
                $members .= '</div>';
            }
            
            $members .= '</div>'; // Close .dac-row
            return $members;
            break;

        default:
            $members .= "<div class=\"error-msg\"><i class=\"fa fa-times-circle\"></i>Invalid style selected: <pre>" . $style . "</pre></div>";
            break;
    }
    return $members;
}
