<?php 
function dacdb_committee($atts)
{
    // Global variables used within the function
    global $fDebug, $data_path, $options;
    $options = get_mmp_options(); // Retrieve the plugin options

    // Register and enqueue required styles
    if (!wp_style_is('show-committee', 'registered')) {
        wp_register_style(
            'show-committee',
            plugins_url('css/show-committee.css', dirname(__FILE__)),
            array(),
            MMPDB_PLUGIN_VERSION
        );
    }
    wp_enqueue_style('show-committee');

    $members = '';
    
    // Debug output for stylesheet
    if ($fDebug && dacdb_is_admin()) {
        $members .= "<div class=\"debug\">";
        $members .= "<h5>Stylesheet Debug</h5>";
        $members .= "<p>show-committee stylesheet registered: " . (wp_style_is('show-committee', 'registered') ? 'Yes' : 'No') . "</p>";
        $members .= "<p>show-committee stylesheet enqueued: " . (wp_style_is('show-committee', 'enqueued') ? 'Yes' : 'No') . "</p>";
        $members .= "</div>";
    }

    // Register and enqueue ColorBox if not already registered
    if (!wp_script_is('jquery-colorbox', 'registered')) {
        wp_register_script('jquery-colorbox', plugins_url('js/vendor/jquery.colorbox-min.js', dirname(__FILE__)), array('jquery'), '1.6.4', true);
        wp_register_style('jquery-colorbox-style', plugins_url('css/vendor/colorbox.css', dirname(__FILE__)), array(), '1.6.4');
    }
    wp_enqueue_script('jquery-colorbox');
    wp_enqueue_style('jquery-colorbox-style');

    // Retrieve attribute values from the $atts array
    $account_id = $options['dacdb_district'];
    $club_id = $options['dacdb_club'];

    // Fix for cid parameter handling - accept either string or numeric values
    $cid = "";
    if (isset($atts['cid'])) {
        // Convert to string first if it's numeric, and ensure it's cleaned of any HTML
        $cid = (string) $atts['cid'];
        // Remove any HTML that might be in the parameter
        $cid = strip_tags($cid);
    }

    $members = ""; // HTML markup for displaying the committee members

    // Check if Committee ID is missing
    if (empty($cid)) {
        return "<div class=\"error-msg\"><i class=\"fa fa-times-circle\"></i>CommitteeID (cid=) is missing</div>";
    }

    $members = ""; // HTML markup for displaying the committee members

    // Check if Committee ID is missing
    if (empty($cid)) {
        return "<div class=\"error-msg\"><i class=\"fa fa-times-circle\"></i>CommitteeID (cid=) is missing</div>";
    }

    // Set committee_show_members with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    $committee_show_members = 2;  // Default value - show all members by default
    
    // First check options (only if explicitly set)
    if (isset($options['committee_show_members']) && $options['committee_show_members'] !== '') {
        $committee_show_members = intval($options['committee_show_members']);
    }
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['show_members']) && $atts['show_members'] !== '') {
        $committee_show_members = intval($atts['show_members']);
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['show_members']) && $_GET['show_members'] !== '') {
        $committee_show_members = intval($_GET['show_members']);
    }

    // Set committee_global_description with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    $committee_global_description = 1;  // Default value
    
    // First check options (only if explicitly set)
    if (isset($options['committee_global_description']) && $options['committee_global_description'] !== '') {
        $committee_global_description = intval($options['committee_global_description']);
    }
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['description']) && $atts['description'] !== '') {
        $committee_global_description = intval($atts['description']);
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['description']) && $_GET['description'] !== '') {
        $committee_global_description = intval($_GET['description']);
    }

    // Add debug logging
    if ($fDebug && dacdb_is_admin()) {
        error_log('DACdb Plus: committee_show_members = ' . $committee_show_members);
        error_log('DACdb Plus: committee_global_description = ' . $committee_global_description);
        error_log('DACdb Plus: options[committee_show_members] = ' . ($options['committee_show_members'] ?? 'not set'));
        error_log('DACdb Plus: options[committee_global_description] = ' . ($options['committee_global_description'] ?? 'not set'));
        error_log('DACdb Plus: atts[show_members] = ' . ($atts['show_members'] ?? 'not set'));
        error_log('DACdb Plus: atts[members] = ' . ($atts['members'] ?? 'not set'));
        error_log('DACdb Plus: show_leaders value = ' . ($show_leaders ?? 'null'));
        error_log('DACdb Plus: Final committee_show_members after leaders logic = ' . $committee_show_members);
    }

    // Set committee_global_name with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    $committee_global_name = 1;  // Default value
    
    // First check options (only if explicitly set)
    if (isset($options['committee_global_name']) && $options['committee_global_name'] !== '') {
        $committee_global_name = intval($options['committee_global_name']);
    }
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['name']) && $atts['name'] !== '') {
        $committee_global_name = intval($atts['name']);
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['name']) && $_GET['name'] !== '') {
        $committee_global_name = intval($_GET['name']);
    }

    // Set committee_show_account_id with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    $committee_show_account_id = 0;  // Default value
    
    // First check options (only if explicitly set)
    if (isset($options['committee_org_data_account']) && $options['committee_org_data_account'] !== '') {
        $committee_show_account_id = intval($options['committee_org_data_account']);
    }
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['accountid']) && $atts['accountid'] !== '') {
        $committee_show_account_id = intval($atts['accountid']);
    } elseif (isset($atts['district']) && $atts['district'] !== '') {
        $committee_show_account_id = intval($atts['district']);
    } elseif (isset($atts['grandlodge']) && $atts['grandlodge'] !== '') {
        $committee_show_account_id = intval($atts['grandlodge']);
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['accountid']) && $_GET['accountid'] !== '') {
        $committee_show_account_id = intval($_GET['accountid']);
    } elseif (isset($_GET['district']) && $_GET['district'] !== '') {
        $committee_show_account_id = intval($_GET['district']);
    } elseif (isset($_GET['grandlodge']) && $_GET['grandlodge'] !== '') {
        $committee_show_account_id = intval($_GET['grandlodge']);
    }

    // Set committee_show_entity_name with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    $committee_show_entity_name = 1;  // Default value
    
    // First check options (only if explicitly set)
    if (isset($options['committee_org_data_entity']) && $options['committee_org_data_entity'] !== '') {
        $committee_show_entity_name = intval($options['committee_org_data_entity']);
    }
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['entityname']) && $atts['entityname'] !== '') {
        $committee_show_entity_name = intval($atts['entityname']);
    } elseif (isset($atts['lodgename']) && $atts['lodgename'] !== '') {
        $committee_show_entity_name = intval($atts['lodgename']);
    } elseif (isset($atts['clubname']) && $atts['clubname'] !== '') {
        $committee_show_entity_name = intval($atts['clubname']);
    } elseif (isset($atts['chaptername']) && $atts['chaptername'] !== '') {
        $committee_show_entity_name = intval($atts['chaptername']);
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['entityname']) && $_GET['entityname'] !== '') {
        $committee_show_entity_name = intval($_GET['entityname']);
    } elseif (isset($_GET['lodgename']) && $_GET['lodgename'] !== '') {
        $committee_show_entity_name = intval($_GET['lodgename']);
    } elseif (isset($_GET['clubname']) && $_GET['clubname'] !== '') {
        $committee_show_entity_name = intval($_GET['clubname']);
    } elseif (isset($_GET['chaptername']) && $_GET['chaptername'] !== '') {
        $committee_show_entity_name = intval($_GET['chaptername']);
    }
    
    // Fix row/column handling to follow proper override hierarchy:
    // Parameters > Attributes > Options > Defaults
    $committee_members_per_row = 3; // Default value
    
    // First check options (only if explicitly set)
    if (isset($options['committee_members_per_row']) && $options['committee_members_per_row'] !== '') {
        $committee_members_per_row = intval($options['committee_members_per_row']);
    }
    
    // Then check attributes (these override options)
    if (isset($atts['row']) && $atts['row'] !== '') {
        $committee_members_per_row = intval($atts['row']);
    } elseif (isset($atts['rows']) && $atts['rows'] !== '') {
        $committee_members_per_row = intval($atts['rows']);
    } elseif (isset($atts['cols']) && $atts['cols'] !== '') {
        $committee_members_per_row = intval($atts['cols']);
    }
    
    // Finally check parameters (these override everything)
    if (isset($_GET['row']) && $_GET['row'] !== '') {
        $committee_members_per_row = intval($_GET['row']);
    } elseif (isset($_GET['rows']) && $_GET['rows'] !== '') {
        $committee_members_per_row = intval($_GET['rows']);
    } elseif (isset($_GET['cols']) && $_GET['cols'] !== '') {
        $committee_members_per_row = intval($_GET['cols']);
    }
    
    // Ensure value is between 1 and 4
    if ($committee_members_per_row < 1 || $committee_members_per_row > 4) {
        $committee_members_per_row = 3; // Default to 3 if invalid value
    }

    // Set committee_members_pics with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    $committee_members_pics = 1;  // Default value - show photos by default
    
    // First check options (only if explicitly set)
    if (isset($options['committee_members_pics']) && $options['committee_members_pics'] !== '') {
        $committee_members_pics = intval($options['committee_members_pics']);
    }
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['pics']) && $atts['pics'] !== '') {
        if (is_string($atts['pics'])) {
            $pics_lower = strtolower($atts['pics']);
            if ($pics_lower === 'true' || $pics_lower === '1' || $pics_lower === 'yes') {
                $committee_members_pics = 1;
            } elseif ($pics_lower === 'false' || $pics_lower === '0' || $pics_lower === 'no') {
                $committee_members_pics = 0;
            } else {
                $committee_members_pics = intval($atts['pics']);
            }
        } else {
            $committee_members_pics = intval($atts['pics']);
        }
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['pics']) && $_GET['pics'] !== '') {
        if (is_string($_GET['pics'])) {
            $pics_lower = strtolower($_GET['pics']);
            if ($pics_lower === 'true' || $pics_lower === '1' || $pics_lower === 'yes') {
                $committee_members_pics = 1;
            } elseif ($pics_lower === 'false' || $pics_lower === '0' || $pics_lower === 'no') {
                $committee_members_pics = 0;
            } else {
                $committee_members_pics = intval($_GET['pics']);
            }
        } else {
            $committee_members_pics = intval($_GET['pics']);
        }
    }

    // Set committee_members_contact_link with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    $committee_members_contact_link = 1;  // Default value - show contact links by default
    
    // First check options (only if explicitly set)
    if (isset($options['committee_members_contact_link']) && $options['committee_members_contact_link'] !== '') {
        $committee_members_contact_link = intval($options['committee_members_contact_link']);
    }
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['contact']) && $atts['contact'] !== '') {
        if (is_string($atts['contact'])) {
            $contact_lower = strtolower($atts['contact']);
            if ($contact_lower === 'true' || $contact_lower === '1' || $contact_lower === 'yes') {
                $committee_members_contact_link = 1;
            } elseif ($contact_lower === 'false' || $contact_lower === '0' || $contact_lower === 'no') {
                $committee_members_contact_link = 0;
            } else {
                $committee_members_contact_link = intval($atts['contact']);
            }
        } else {
            $committee_members_contact_link = intval($atts['contact']);
        }
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['contact']) && $_GET['contact'] !== '') {
        if (is_string($_GET['contact'])) {
            $contact_lower = strtolower($_GET['contact']);
            if ($contact_lower === 'true' || $contact_lower === '1' || $contact_lower === 'yes') {
                $committee_members_contact_link = 1;
            } elseif ($contact_lower === 'false' || $contact_lower === '0' || $contact_lower === 'no') {
                $committee_members_contact_link = 0;
            } else {
                $committee_members_contact_link = intval($_GET['contact']);
            }
        } else {
            $committee_members_contact_link = intval($_GET['contact']);
        }
    }

    // Set committee_members_committeeposition with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    $committee_members_committeeposition = 1;  // Default value - show committee positions by default
    
    // First check options (only if explicitly set)
    if (isset($options['committee_members_committeeposition']) && $options['committee_members_committeeposition'] !== '') {
        $committee_members_committeeposition = intval($options['committee_members_committeeposition']);
    }
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['committeeposition']) && $atts['committeeposition'] !== '') {
        if (is_string($atts['committeeposition'])) {
            $committeeposition_lower = strtolower($atts['committeeposition']);
            if ($committeeposition_lower === 'true' || $committeeposition_lower === '1' || $committeeposition_lower === 'yes') {
                $committee_members_committeeposition = 1;
            } elseif ($committeeposition_lower === 'false' || $committeeposition_lower === '0' || $committeeposition_lower === 'no') {
                $committee_members_committeeposition = 0;
            } else {
                $committee_members_committeeposition = intval($atts['committeeposition']);
            }
        } else {
            $committee_members_committeeposition = intval($atts['committeeposition']);
        }
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['committeeposition']) && $_GET['committeeposition'] !== '') {
        if (is_string($_GET['committeeposition'])) {
            $committeeposition_lower = strtolower($_GET['committeeposition']);
            if ($committeeposition_lower === 'true' || $committeeposition_lower === '1' || $committeeposition_lower === 'yes') {
                $committee_members_committeeposition = 1;
            } elseif ($committeeposition_lower === 'false' || $committeeposition_lower === '0' || $committeeposition_lower === 'no') {
                $committee_members_committeeposition = 0;
            } else {
                $committee_members_committeeposition = intval($_GET['committeeposition']);
            }
        } else {
            $committee_members_committeeposition = intval($_GET['committeeposition']);
        }
    }

    // Set committee_members_busposition with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    $committee_members_busposition = 0;  // Default value - hide business positions by default
    
    // First check options (only if explicitly set)
    if (isset($options['committee_members_busposition']) && $options['committee_members_busposition'] !== '') {
        $committee_members_busposition = intval($options['committee_members_busposition']);
    }
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['busposition']) && $atts['busposition'] !== '') {
        if (is_string($atts['busposition'])) {
            $busposition_lower = strtolower($atts['busposition']);
            if ($busposition_lower === 'true' || $busposition_lower === '1' || $busposition_lower === 'yes') {
                $committee_members_busposition = 1;
            } elseif ($busposition_lower === 'false' || $busposition_lower === '0' || $busposition_lower === 'no') {
                $committee_members_busposition = 0;
            } else {
                $committee_members_busposition = intval($atts['busposition']);
            }
        } else {
            $committee_members_busposition = intval($atts['busposition']);
        }
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['busposition']) && $_GET['busposition'] !== '') {
        if (is_string($_GET['busposition'])) {
            $busposition_lower = strtolower($_GET['busposition']);
            if ($busposition_lower === 'true' || $busposition_lower === '1' || $busposition_lower === 'yes') {
                $committee_members_busposition = 1;
            } elseif ($busposition_lower === 'false' || $busposition_lower === '0' || $busposition_lower === 'no') {
                $committee_members_busposition = 0;
            } else {
                $committee_members_busposition = intval($_GET['busposition']);
            }
        } else {
            $committee_members_busposition = intval($_GET['busposition']);
        }
    }

    // Set committee_members_classification with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    $committee_members_classification = 0;  // Default value - hide classifications by default
    
    // First check options (only if explicitly set)
    if (isset($options['committee_members_classification']) && $options['committee_members_classification'] !== '') {
        $committee_members_classification = intval($options['committee_members_classification']);
    }
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['classification']) && $atts['classification'] !== '') {
        if (is_string($atts['classification'])) {
            $classification_lower = strtolower($atts['classification']);
            if ($classification_lower === 'true' || $classification_lower === '1' || $classification_lower === 'yes') {
                $committee_members_classification = 1;
            } elseif ($classification_lower === 'false' || $classification_lower === '0' || $classification_lower === 'no') {
                $committee_members_classification = 0;
            } else {
                $committee_members_classification = intval($atts['classification']);
            }
        } else {
            $committee_members_classification = intval($atts['classification']);
        }
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['classification']) && $_GET['classification'] !== '') {
        if (is_string($_GET['classification'])) {
            $classification_lower = strtolower($_GET['classification']);
            if ($classification_lower === 'true' || $classification_lower === '1' || $classification_lower === 'yes') {
                $committee_members_classification = 1;
            } elseif ($classification_lower === 'false' || $classification_lower === '0' || $classification_lower === 'no') {
                $committee_members_classification = 0;
            } else {
                $committee_members_classification = intval($_GET['classification']);
            }
        } else {
            $committee_members_classification = intval($_GET['classification']);
        }
    }

    // Check for leaders/show_leaders with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    // Only process if leaders or show_leaders attributes are explicitly set
    $show_leaders = null;  // Default to null (no override)
    
    // First check options (if any leader-specific options exist)
    // Note: Currently no specific options for leaders, but this is where they would go
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['leaders']) && $atts['leaders'] !== '') {
        $show_leaders = $atts['leaders'];
    } elseif (isset($atts['show_leaders']) && $atts['show_leaders'] !== '') {
        $show_leaders = $atts['show_leaders'];
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['leaders']) && $_GET['leaders'] !== '') {
        $show_leaders = $_GET['leaders'];
    } elseif (isset($_GET['show_leaders']) && $_GET['show_leaders'] !== '') {
        $show_leaders = $_GET['show_leaders'];
    }
    
    // Apply the leaders setting if it was explicitly set (not null)
    if ($show_leaders !== null) {
        if ($show_leaders === true || $show_leaders === '1' || $show_leaders === 1) {
            $committee_show_members = 1; // Show leaders only
        } elseif ($show_leaders === false || $show_leaders === '0' || $show_leaders === 0) {
            // If leaders are explicitly hidden, and we were showing all members,
            // switch to showing no members
            if ($committee_show_members === 2) {
                $committee_show_members = 0;
            }
        }
    }

    // Construct path to xml feed
    $xml_path = $data_path . 'wp_ById.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&id=' . $cid . '&R=' . rand();

    // Debug output for committee
    if ($fDebug && dacdb_is_admin()) {
        $members .= "<div class=\"debug\">";
        $members .= "<h5>function <em>dacdb_committee</em> called</h5>";
        $members .= "<table class=\"debug-table\">";
        $members .= "<tr><th>Variable</th><th>Option</th><th>Attribute</th><th>Default</th><th>Value</th></tr>";

        // Committee ID
        $members .= "<tr>";
        $members .= "<td><code>cid</code></td>";
        $members .= "<td>N/A</td>";
        $members .= "<td>" . ($atts['cid'] ?? 'N/A') . "</td>";
        $members .= "<td>N/A</td>";
        $members .= "<td>" . $cid . "</td>";
        $members .= "</tr>";

        // Committee Name
        $members .= "<tr>";
        $members .= "<td><code>committee_global_name</code></td>";
        $members .= "<td>" . ($options['committee_global_name'] ?? 'N/A') . "</td>";
        $members .= "<td>" . ($atts['name'] ?? 'N/A') . "</td>";
        $members .= "<td>1</td>";
        $members .= "<td>" . $committee_global_name . "</td>";
        $members .= "</tr>";

        // Committee Description
        $members .= "<tr>";
        $members .= "<td><code>committee_global_description</code></td>";
        $members .= "<td>" . ($options['committee_global_description'] ?? 'N/A') . "</td>";
        $members .= "<td>" . ($atts['description'] ?? 'N/A') . "</td>";
        $members .= "<td>1</td>";
        $members .= "<td>" . $committee_global_description . "</td>";
        $members .= "</tr>";

        // Show Account ID
        $members .= "<tr>";
        $members .= "<td><code>committee_show_account_id</code></td>";
        $members .= "<td>" . ($options['committee_org_data_account'] ?? 'N/A') . "</td>";
        $members .= "<td>" . ($atts['accountid'] ?? $atts['district'] ?? $atts['grandlodge'] ?? 'N/A') . "</td>";
        $members .= "<td>0</td>";
        $members .= "<td>" . $committee_show_account_id . "</td>";
        $members .= "</tr>";

        // Show Entity Name
        $members .= "<tr>";
        $members .= "<td><code>committee_show_entity_name</code></td>";
        $members .= "<td>" . ($options['committee_org_data_entity'] ?? 'N/A') . "</td>";
        $members .= "<td>" . ($atts['entityname'] ?? $atts['lodgename'] ?? $atts['clubname'] ?? $atts['chaptername'] ?? 'N/A') . "</td>";
        $members .= "<td>1</td>";
        $members .= "<td>" . $committee_show_entity_name . "</td>";
        $members .= "</tr>";

        // Members Per Row
        $members .= "<tr>";
        $members .= "<td><code>committee_members_per_row</code></td>";
        $members .= "<td>" . ($options['committee_members_per_row'] ?? 'N/A') . "</td>";
        $members .= "<td>" . ($atts['cols'] ?? $atts['rows'] ?? $atts['row'] ?? 'N/A') . "</td>";
        $members .= "<td>3</td>";
        $members .= "<td>" . $committee_members_per_row . "</td>";
        $members .= "</tr>";

        // Show Members
        $members .= "<tr>";
        $members .= "<td><code>committee_show_members</code></td>";
        $members .= "<td>" . ($options['committee_show_members'] ?? 'N/A') . "</td>";
        $members .= "<td>" . ($atts['show_members'] ?? $atts['members'] ?? 'N/A') . "</td>";
        $members .= "<td>2</td>";
        $members .= "<td>" . $committee_show_members . "</td>";
        $members .= "</tr>";

        $members .= "</table>";
        $members .= "</div>";

        // XML Path
        $members .= "<div class=\"debug-row\">";
        $members .= "<p><code>xml_path</code>: ";
        $members .= "<a href=\"" . $xml_path . "\" target=\"debug-window\">" . $xml_path . "</a></p>";
        $members .= "</div>";

        // Debug output for committee_members_per_row
        $members .= "<ul class=\"debug-row\"><li><code>committee_members_per_row</code> value: " . $committee_members_per_row . "</li>";
        $members .= "<li><code>committee_members_per_row</code> type: " . gettype($committee_members_per_row) . "</li></ul>";
        
        // Debug output for photo settings
        $members .= "<div class=\"debug\">";
        $members .= "<h5>Photo Settings Debug</h5>";
        $members .= "<p>committee_members_pics: " . $committee_members_pics . "</p>";
        $members .= "<p>committee_members_pics type: " . gettype($committee_members_pics) . "</p>";
        $members .= "<p>options[committee_members_pics]: " . ($options['committee_members_pics'] ?? 'not set') . "</p>";
        $members .= "<p>atts[pics]: " . ($atts['pics'] ?? 'not set') . "</p>";
        $members .= "<p>isset(options[committee_members_pics]): " . (isset($options['committee_members_pics']) ? 'true' : 'false') . "</p>";
        $members .= "<p>options[committee_members_pics] !== '': " . (isset($options['committee_members_pics']) && $options['committee_members_pics'] !== '' ? 'true' : 'false') . "</p>";
        $members .= "<p>isset(atts[pics]): " . (isset($atts['pics']) ? 'true' : 'false') . "</p>";
        $members .= "<p>options[committee_members_pics] value: " . var_export($options['committee_members_pics'] ?? null, true) . "</p>";
        $members .= "<p>atts[pics] value: " . var_export($atts['pics'] ?? null, true) . "</p>";
        $members .= "</div>";

        // Debug output for all member display settings
        $members .= "<div class=\"debug\">";
        $members .= "<h5>Final Member Display Settings</h5>";
        $members .= "<table class=\"debug-table\">";
        $members .= "<tr><th>Setting</th><th>Final Value</th><th>Type</th></tr>";
        $members .= "<tr><td>committee_members_pics</td><td>" . $committee_members_pics . "</td><td>" . gettype($committee_members_pics) . "</td></tr>";
        $members .= "<tr><td>committee_members_contact_link</td><td>" . $committee_members_contact_link . "</td><td>" . gettype($committee_members_contact_link) . "</td></tr>";
        $members .= "<tr><td>committee_members_committeeposition</td><td>" . $committee_members_committeeposition . "</td><td>" . gettype($committee_members_committeeposition) . "</td></tr>";
        $members .= "<tr><td>committee_members_busposition</td><td>" . $committee_members_busposition . "</td><td>" . gettype($committee_members_busposition) . "</td></tr>";
        $members .= "<tr><td>committee_members_classification</td><td>" . $committee_members_classification . "</td><td>" . gettype($committee_members_classification) . "</td></tr>";
        $members .= "</table>";
        $members .= "</div>";

    }

    // Load XML data from the specified URL
    libxml_use_internal_errors(true);
    $xml = simplexml_load_file($xml_path);
    
    if ($xml === false) {
        $errors = libxml_get_errors();
        libxml_clear_errors();
        $error_message = '';
        foreach ($errors as $error) {
            $error_message .= "XML Error {$error->code}: {$error->message}\n";
        }
        error_log('DACdb Plus: XML Error in dacdb_committee: ' . $error_message);
        return '<div class="error-msg"><i class="fa fa-times-circle"></i>Error loading committee data. Please check the error logs.</div>';
    }

    // Debug output for XML data
    if ($fDebug && dacdb_is_admin()) {
        $members .= "<div class=\"debug\">";
        $members .= "<h5>XML Data Debug</h5>";
        $members .= "<pre>";
        $members .= htmlspecialchars($xml->asXML());
        $members .= "</pre>";
        $members .= "</div>";
        
        // Add structure debug
        $members .= "<div class=\"debug\">";
        $members .= "<h5>XML Structure Debug</h5>";
        $members .= "<p>Root element name: " . $xml->getName() . "</p>";
        if (isset($xml->committeemembers)) {
            $members .= "<p>committeemembers exists</p>";
            $members .= "<p>committeemembers count: " . count($xml->committeemembers) . "</p>";
            $members .= "<p>committeemembers children count: " . count($xml->committeemembers->children()) . "</p>";
            $members .= "<p>committeemembers children names: ";
            foreach ($xml->committeemembers->children() as $child) {
                $members .= $child->getName() . ", ";
            }
            $members .= "</p>";
            
            // Debug the first member's image field
            if (count($xml->committeemembers) > 0) {
                $first_member = $xml->committeemembers[0];
                $members .= "<p>First member image: " . ($first_member->image ?? 'not set') . "</p>";
                $members .= "<p>First member name: " . ($first_member->membername ?? 'not set') . "</p>";
            }
        } else {
            $members .= "<p>committeemembers does not exist</p>";
        }
        $members .= "</div>";
    }

    // Check if XML data is empty
    if (strlen($xml) == 0) {
        $members .= '<div class="error-msg"><i class="fa fa-times-circle"></i>There are no committee members to display at this time.</div>';
        return $members;
    }

    // Set the div and image widths based on the number of columns
    $setDivWidth = 160;
    $setImgWidth = 150;
    $div = '<div class="dac-col">';

    if ($committee_members_per_row === 1) {
        $div = '<div class="members dac-col-xs-12">';
        $setDivWidth = 155;
        $setImgWidth = 150;
    } else if ($committee_members_per_row === 2) {
        $div = '<div class="members dac-col-xs-12 dac-col-sm-6">';
        $setDivWidth = 140;
        $setImgWidth = 135;
    } else if ($committee_members_per_row === 3) {
        $div = '<div class="members dac-col-xs-12 dac-col-sm-6 dac-col-md-4">';
        $setDivWidth = 125;
        $setImgWidth = 120;
    } else if ($committee_members_per_row === 4) {
        $div = '<div class="members dac-col-xs-12 dac-col-sm-6 dac-col-md-3 dac-col-lg-3">';
        $setDivWidth = 110;
        $setImgWidth = 105;
    }

    if ($fDebug && dacdb_is_admin()) {
        $members .= "<div class=\"debug\">";
        $members .= "<h5>Row/Column Debug</h5>";
        $members .= "<p>committee_members_per_row value: " . $committee_members_per_row . "</p>";
        $members .= "<p>div class: " . $div . "</p>";
        $members .= "</div>";
        
        // Debug output for photo settings
        $members .= "<div class=\"debug\">";
        $members .= "<h5>Photo Settings Debug</h5>";
        $members .= "<p>committee_members_pics: " . $committee_members_pics . "</p>";
        $members .= "<p>committee_members_pics type: " . gettype($committee_members_pics) . "</p>";
        $members .= "<p>options[committee_members_pics]: " . ($options['committee_members_pics'] ?? 'not set') . "</p>";
        $members .= "<p>atts[pics]: " . ($atts['pics'] ?? 'not set') . "</p>";
        $members .= "</div>";
    }

    // Set thumbnail_width with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    $thumbnail_width = 150;  // Default value
    
    // First check options
    if (isset($options['thumbnail_width']) && $options['thumbnail_width'] > 0) {
        $thumbnail_width = intval($options['thumbnail_width']);
    }
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['imgwidth']) && $atts['imgwidth'] > 0) {
        $thumbnail_width = intval($atts['imgwidth']);
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['imgwidth']) && $_GET['imgwidth'] > 0) {
        $thumbnail_width = intval($_GET['imgwidth']);
    }

    // Set thumbnail_height with proper hierarchy: Query String > Shortcode Attributes > Options > Defaults
    $thumbnail_height = 150;  // Default value
    
    // First check options
    if (isset($options['thumbnail_height']) && $options['thumbnail_height'] > 0) {
        $thumbnail_height = intval($options['thumbnail_height']);
    }
    
    // Then check shortcode attributes (these override options)
    if (isset($atts['imgheight']) && $atts['imgheight'] > 0) {
        $thumbnail_height = intval($atts['imgheight']);
    }
    
    // Finally check query string parameters (these override everything)
    if (isset($_GET['imgheight']) && $_GET['imgheight'] > 0) {
        $thumbnail_height = intval($_GET['imgheight']);
    }

    // Counter for tracking members
    $counter = 0;

    // Display committee name if enabled
    if ($committee_global_name == 1) {
        $members .= '<div class="dac-row">';
        $members .= '   <div class="dac-col-xs-12">';
        $members .= '       <h3>' . $xml->committeemembers->committeename . '</h3>';
        $members .= '   </div>';
        $members .= '</div>';
    }

    // Display committee description if enabled
    if ($committee_global_description == 1) {
        $members .= '<div class="dac-row">';
        $members .= '    <div class="dac-col-xs-12">';
        $members .= '        <article>';
        $members .= $xml->committeemembers->description;
        $members .= '        </article>';
        $members .= '    </div>';
        $members .= '</div>';
    }

    // Display committee members
    if ($committee_show_members > 0) {
        $members .= '<div class="dac-row">';

        // The XML structure has multiple committeemembers elements as direct children of the committee root
        if (isset($xml->committeemembers)) {
            // Check if we have multiple committeemembers elements or just one
            $member_count = count($xml->committeemembers);
            
            if ($member_count > 1) {
                // Multiple committeemembers elements - iterate through them
                foreach ($xml->committeemembers as $member) {
                    // Skip non-leaders if we're only showing leaders
                    if ($committee_show_members == 1 && strtolower($member->committeeposition ?? '') === 'member') {
                        continue;
                    }

                    $members .= $div; // Add the Bootstrap classes div here
                    $members .= '<table border="0" cellpadding="0" cellspacing="0">';
                    $members .= '<tr>';

                    if ($committee_members_pics == 1) {
                        $members .= '<td style="border:0 !important;vertical-align: top; padding: 0 1rem 0 0; width:' . $setDivWidth . 'px;">';

                        if ($member->image == '') {
                            $members .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . MMPDB_PLUGIN_URL . 'img/avatar.svg"  style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . 'px; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                        } else {
                            $members .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . $member->image . '"  style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . 'px; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                        }

                        $members .= '</td>';
                    }

                    $members .= '    <td style="border:0 !important; text-align: left; vertical-align: top; padding:0;">';
                    $members .= '       <strong>' . $member->membername . '</strong><BR>';

                    $formattedUrl = formatDomainToUrl($member->cluburl);
                    if (!empty($formattedUrl)) {
                        $members .= '<a href="' . $formattedUrl . '" target="new">' . $member->clubname . '</a><br>';
                    } else {
                        $members .= '<span class="clubname">' . $member->clubname . '<br></span>';
                    }

                    if ($committee_show_account_id == 1) {
                        if ($member->accountid != '') {
                            $members .= 'District ' . $member->accountid . '<br />';
                        }
                    }

                    if ($committee_members_committeeposition == 1) {
                        if ($member->committeeposition != '') {
                            $members .= $member->committeeposition . '<br />';
                        }
                    }

                    if ($committee_members_busposition == 1) {
                        if ($member->busposition != '') {
                            $members .= $member->busposition . '<br />';
                        }
                    }

                    if ($committee_members_classification == 1) {
                        if ($member->classification != '') {
                            $members .= $member->classification . '<br />';
                        }
                    }

                    if ($committee_members_contact_link == 1) {
                        $members .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" name="dlc-inline-link-' . $counter . '" class="modal-link">Contact</a>';
                    }

                    $members .= '</td>';
                    $members .= '</tr>';
                    $members .= '</table>';
                    $members .= '</div>'; // Close the Bootstrap classes div
                    $counter = $counter + 1;
                }
            } else {
                // Single committeemembers element - treat it as one member
                $member = $xml->committeemembers;
                
                // Skip non-leaders if we're only showing leaders
                if ($committee_show_members == 1 && strtolower($member->committeeposition ?? '') === 'member') {
                    // Don't display this member
                } else {
                    $members .= $div; // Add the Bootstrap classes div here
                    $members .= '<table border="0" cellpadding="0" cellspacing="0">';
                    $members .= '<tr>';

                    if ($committee_members_pics == 1) {
                        $members .= '<td style="border:0 !important;vertical-align: top; padding: 0 1rem 0 0; width:' . $setDivWidth . 'px;">';

                        if ($member->image == '') {
                            $members .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . MMPDB_PLUGIN_URL . 'img/avatar.svg"  style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . 'px; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                        } else {
                            $members .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . $member->image . '"  style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . 'px; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                        }

                        $members .= '</td>';
                    }

                    $members .= '    <td style="border:0 !important; text-align: left; vertical-align: top; padding:0;">';
                    $members .= '       <strong>' . $member->membername . '</strong><BR>';

                    $formattedUrl = formatDomainToUrl($member->cluburl);
                    if (!empty($formattedUrl)) {
                        $members .= '<a href="' . $formattedUrl . '" target="new">' . $member->clubname . '</a><br>';
                    } else {
                        $members .= '<span class="clubname">' . $member->clubname . '<br></span>';
                    }

                    if ($committee_show_account_id == 1) {
                        if ($member->accountid != '') {
                            $members .= 'District ' . $member->accountid . '<br />';
                        }
                    }

                    if ($committee_members_committeeposition == 1) {
                        if ($member->committeeposition != '') {
                            $members .= $member->committeeposition . '<br />';
                        }
                    }

                    if ($committee_members_busposition == 1) {
                        if ($member->busposition != '') {
                            $members .= $member->busposition . '<br />';
                        }
                    }

                    if ($committee_members_classification == 1) {
                        if ($member->classification != '') {
                            $members .= $member->classification . '<br />';
                        }
                    }

                    if ($committee_members_contact_link == 1) {
                        $members .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" name="dlc-inline-link-' . $counter . '" class="modal-link">Contact</a>';
                    }

                    $members .= '</td>';
                    $members .= '</tr>';
                    $members .= '</table>';
                    $members .= '</div>'; // Close the Bootstrap classes div
                    $counter = $counter + 1;
                }
            }
        } else {
            // Fallback: if no committeemembers found, show error
            $members .= '<div class="dac-col-xs-12">';
            $members .= '<div class="error-msg"><i class="fa fa-times-circle"></i>No committee members found for this committee.</div>';
            $members .= '</div>';
        }

        $members .= '</div>'; // Close the dac-row
    }

    return $members;
}