<?php

//ANCHOR - Display Club
/**
 * Displays club information based on the provided XML data.
 *
 * @param SimpleXMLElement $xml   The XML data containing club information.
 * @param int|string       $style The style of display to use (0, 1, 3).
 * @param array            $atts  Additional attributes for customization.
 *
 * @return string The generated HTML for displaying club information.
 */
function dacdb_DisplayClub($xml, $style, $atts)
{
    global $fDebug, $data_path, $file_path, $options;

    $ldiv = "";
    $members = "";
    $counter = 1;

    // Set default values for optional attributes
    $cols = isset($atts['leaders_per_row']) ? $atts['leaders_per_row'] : 1;
    $leader_pics = isset($atts['leader_pics']) ? $atts['leader_pics'] : 1;
    $leaders_per_row = isset($atts['leaders_per_row']) ? $atts['leaders_per_row'] : 1;
    $flex = isset($atts['leader_pics']) ? $atts['leader_pics'] : 1;
    $thumbnail_width = isset($options['thumbnail_width']) && $options['thumbnail_width'] > 0 ? $options['thumbnail_width'] : (isset($atts['imgwidth']) && isset($atts['imgwidth']) > 0 ? $atts['imgwidth'] : 150);

    if ($fDebug) {
        // Debug information
        $members .= "=================================================================<BR>";
        $members .= "function dacdb_DisplayClubs called 2<br>";
        $members .= "cols=" . $leaders_per_row . "<br>";
        $members .= "leader_pics=" . $leader_pics . "<br>";
        $members .= "flex=" . $flex . "<br>";
        $members .= "style=" . $style . "<br>";
        $members .= "cols=" . $cols . "<br>";
        $members .= "imgWidth=" . $thumbnail_width . "<br>";
        $members .= "Thumbnail Width =" . $options['thumbnail_width'] . "<br>";
        $members .= "=================================================================<BR>";
    }

    switch ($style) {

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

        case '1': //  dacdb_leaders display Aligned Columns
            $setDivWidth = 160;
            $setImgWidth = 150;

            $div  = '<div class="col">';
            if ($cols == 1) {
                $div = '<div class="members dac-col-xs-12">';
                $setDivWidth = 155;
                $setImgWidth = 150;
            } else if ($cols == 2) {
                $div = '<div class="members dac-col-xs-12 dac-col-sm-6">';
                $setDivWidth = 140;
                $setImgWidth = 135;
            } else if ($cols == 3) {
                $div = '<div class="members dac-col-xs-12 dac-col-sm-12 dac-col-md-4">';
                $setDivWidth = 125;
                $setImgWidth = 120;
            } else if ($cols == 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;
            }

            $counter = 0;
            $records = count($xml->club);

            foreach ($xml->club as $club) {
                if ($records > 1) {
                    // if more than one club, show the club name
                    $members .= '<div class="dac-row">';
                    $members .= $div;
                    $members .=   '<h4>' . $club->clubname . '</h4>';
                    $members .= $div;
                    $members .= '</div>';
                }

                $members .= '<div class="dac-row">';
                foreach ($club->officers->officer as $officer) {
                    // Check if an officer name is available in membername, and if not, use username
                    $name = !empty($officer->membername) ? $officer->membername : $officer->username;

                    $members .= $div;  // members dac-col-xs-12 dac-col-sm-6 dac-col-md-3 dac-col-lg-3
                    $members .= '<table border="0" cellpadding="0" cellspacing="0">';
                    $members .= '<tr>';
                    if ($leader_pics == 1) {
                        $members .=   '<td style="border:0 !important;vertical-align: top; padding:0; width:' . $setDivWidth . 'px;">';
                        if ($officer->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="' . $officer->image . '" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . '; object-fit: cover;"  width="' . $setImgWidth . '"></div>';
                        }
                        $members .=   '</td>';
                    }
                    $members .=   '<td style="border:0 !important; text-align: left; vertical-align: top; padding:0;">';
                    $members .=       '<strong>' . $name . '</strong><BR>';
                    $members .=    $officer->clubposition . '<BR>';
                    if ($options['leaders_contact_link'] == 1) {
                        $members .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $officer->userid . '" name="dlc-inline-link-' . $counter . '" class="iframe3 cboxElement">Contact</a>';
                    }
                    $members .=   '</td>';
                    $members .= '</tr>';
                    $members .= '</table>';
                    $members .= '</div>';   // <div class="col">
                    $counter = $counter + 1;
                }
                $members .= '</div>';  // dac-row
            }

            $members .= '</div>'; // dac-cols...
            return $members;
            break;

        case '3':       //  New 2 columns
            $setDivWidth = 100;
            $setImgWidth = 95;
            $members .= '<div class="dac-row">';
            $members .= '<table border="5" cellpadding="3" cellspacing="0">';
            foreach ($xml->club as $club) {

                $members .= '<tr>';
                $members .= '<td valign="top" align="left" width="40%">';
                $members .= '<table border="1" cellpadding="3" cellspacing="0">';

                foreach ($club->officers->officer as $officer) {
                    // Check if an officer name is available in membername, and if not, use username
                    $name = !empty($officer->membername) ? $officer->membername : $officer->username;

                    $members .= '<tr>';
                    if ($leader_pics  == 1) {
                        $members .= '<td style="border:0 !important;vertical-align: top; padding:0; width:' . $setDivWidth . 'px;">';
                        if ($officer->image == '') {
                            $members .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . MMPDB_PLUGIN_URL . 'img/avatar.svg" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . '; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                        } else {
                            $members .= '<div style="width:' . $setDivWidth . 'px;"><img src="' . $officer->image . '" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth . '; object-fit: cover;" width="' . $setImgWidth . '"></div>';
                        }
                        $members .= '</td>';
                    }
                    $members .= '<td style="border:0 !important; text-align: left; vertical-align: top; padding:0;">';
                    $members .= '<strong>' . $officer->username . '</strong><BR>';
                    $members .= $officer->clubposition . '<BR>';

                    if ($options['leaders_contact_link'] == 1) {
                        $members .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $officer->userid . '" name="dlc-inline-link-' . $counter . '" class="iframe3 cboxElement">Contact</a>';
                    }

                    $members .= '</td>';
                    $members .= '</tr>';
                    $counter = $counter + 1;
                }
                $members .= '</table>';
                $members .= '</td>';
                $members .= '<td valign="top" width="60%">';
                $members .= '<a href="' . $club->cluburl . '" target="new"><strong>' . $club->clubname . '</strong></a><br>';
                $members .= '<i>' . $club->clubaddress . '<br>';
                if ($club->clubcity != '')
                    $members .= '' . $club->clubcity . ', ' . $club->clubstatecode . ' ' . $club->clubpostalzip . '</i><br>';
                else
                    $members .= '' . $club->clubstatecode . ' ' . $club->clubpostalzip . '</i><br>';
                $members .= '<strong>Where We Meet </strong><br>';
                $members .= '' . $club->meetingplace . '<br>';

                if ($club->meetingaddress != '') {
                    $members .= '' . $club->meetingaddress . '<br>';
                }
                if ($club->meetingcity != '')
                    $members .= '' . $club->meetingcity . ', ' . $club->meetingstatecode . ' ' . $club->meetingpostalcode . '<br>';
                else
                    $members .= '' . $club->meetingstatecode . ' ' . $club->meetingpostalcode . '<br>';

                $members .= '<strong>When</strong><br>';
                $members .= '' . $club->meetingday . ' at ' . $club->meetingtime . '';
                $members .= '<span style="float: right;"><a href="https://maps.google.com/maps?q=' . $club->latitude . ',' . $club->longitude . '" target="new">Map It</a></span>';
                $members .= '</td>';
                $members .= '</tr>';
                $members .= '<tr><td colspan="2"><hr style="margin:10px 0 5px 0;"></td></tr>';
            }
            $members .= '</table>';
            $members .= '</div>'; // <!-- dac-row -->

            break;

        case '4':
            $counter = 0;
            $records = count($xml->club);

            // Determine the number of columns based on the $leaders_per_row variable
            $columns = 2; //$options['leaders_per_row'];

            // Calculate the appropriate column width based on the number of columns
            $columnWidth = 100 / $columns;
            $div = isset($div) ? $div : '';
            foreach ($xml->club as $club) {
                if ($records > 1) {
                    // if more than one club, show the club name
                    $members .= '<div class="dac-row">';
                    $members .= $div;
                    $members .=   '<h4>' . $club->clubname . '</h4>';
                    $members .= $div;
                    $members .= '</div>';
                }

                foreach ($club->officers->officer as $officer) {
                    // Calculate the width for each officer card based on the number of columns
                    $cardWidth = "calc({$columnWidth}% - 10px)";

                    $members .= '<div class="clubleader-card" style="width: ' . $cardWidth . ';">';

                    // Check if an officer name is available in membername, and if not, use username
                    $name = !empty($officer->membername) ? $officer->membername : $officer->username;

                    // Check if an officer image is available
                    if ($officer->image != '') {
                        // Display officer image to the left
                        $members .= '<div class="clubleader-image">';
                        $members .= '<img src="' . $officer->image . '" alt="' . $name . '">';
                        $members .= '</div>';
                    } else {
                        // If no image available, display gray div with "Image not available" text
                        $members .= '<div class="clubleader-no-image">';
                        $members .= 'Image not available';
                        $members .= '</div>';
                    }
                    // Display officer information to the right
                    $members .= '<div class="clubleader-info">';
                    // Officer Name
                    $members .= '<h4>' . $name . '</h4>';

                    // Officer Position
                    $members .= '<p>' . $officer->clubposition . '</p>';

                    // Contact Link (if needed)
                    if ($options['leaders_contact_link'] == 1) {
                        $members .= '<p><a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $officer->userid . '" name="dlc-inline-link-' . $counter . '" class="iframe3 cboxElement">Contact ' . $name . '</a></p>';
                    }

                    $members .= '</div>'; // Closing officer-info div

                    $members .= '</div>'; // Closing officer-card div
                    $counter = $counter + 1;
                }
            }
            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;
}
