<?php
/**
 * DACdb Plus Show Committee Function
 *
 * Displays committee information and members in a responsive grid layout
 * with configurable options for display format and member details.
 *
 * @package    DACdbPlus
 * @subpackage Display
 */

if (!defined('ABSPATH')) {
    exit;
}

function dacdb_committee($atts)
{
    // Global variables used within the function
    global $fDebug, $data_path, $file_path, $options;

    // Retrieve attribute values from the $atts array
    $account_id = $options['dacdb_district'];
    $club_id = $options['dacdb_club'];
    $cid = isset($atts['cid']) ? strtolower($atts['cid']) : ""; // Committee ID obtained from the shortcode (always lowercase)
    $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 default values for committee members per row if not specified in options
    $options['committee_members_per_row'] = $options['committee_members_per_row'] ?? 1;

    // Handle deprecated attribute values (rows and row)
    $options['committee_members_per_row'] = $atts['rows'] ?? $atts['row'] ?? $options['committee_members_per_row'];

    // Set committee members per row based on the provided attribute value (cols)
    $options['committee_members_per_row'] = $atts['cols'] ?? $options['committee_members_per_row'];

    // Set committee members' committee position based on the provided attribute value (committeeposition)
    $options['committee_members_committeeposition'] = $atts['committeeposition'] ?? $options['committee_members_committeeposition'];

    // Set show club position based on the provided attribute value (clubposition)
    $ShowClubPos = $atts['clubposition'] ?? 0;

    // Set global show district number based on the provided attribute value (district)
    $options['committee_global_show_district_number'] = $atts['district'] ?? $options['committee_global_show_district_number'];

    // Retrieve additional options
    $cols = $options['committee_members_per_row'];

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

    // Display debugging information
    if ($fDebug) {
        $members .= "<div class=\"debug\">";
        $members .= "<h5>function <em>dacdb_committee</em> called</h5><pre>";
        $members .= "AccountID= " . $account_id . "<br>";
        $members .= "ClubID= " . $club_id . "<br>";
        $members .= "cid= " . $cid . "<br>";
        $members .= "xml_path= <a href=\"" . $xml_path . "\" target=\"debug-window\">" . $xml_path . "</a><br>";
        $members .= "cols= " . $cols . "<br>";
        $members .= "Pics= " . $options['committee_members_pics'] . "<br>";
        $members .= "CommitteeName= " . $options['committee_global_name'] . "<br>";
        $members .= "Description= " . $options['committee_global_description'] . "<br>";
        //TODO: Create ATT for -> $members .= "Committeeposition=" . $ShowCommPos . "<br>";
        $members .= "Show Clubposition= " . $ShowClubPos . "<br>";
        $members .= "Show DistrictNumber= " . $options['committee_global_show_district_number'] . "<br>";
        $members .= "BusPos= " . $options['committee_members_busposition'] . "<br>";
        $members .= "Classification= " . $options['committee_members_classification'] . "<br>";
        $members .= "Contact= " . $options['leaders_contact_link'] . "<br>";
        $members .= "</pre></div>";
    }

    // Load XML data from the specified URL
    $xml = simplexml_load_file($xml_path);

    // Check if XML data is empty
    if (strlen($xml) == 0) {
        $members .= 'There are no committee members to display at this time.';
        return $members;
    }

    // Set the div and image widths based on the number of 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;
    }
    $thumbnail_width = isset($options['thumbnail_width']) && $options['thumbnail_width'] > 0 ? $options['thumbnail_width'] : (isset($atts['imgwidth']) && isset($atts['imgwidth']) > 0 ? $atts['imgwidth'] : 150);
    // Counter for tracking members
    $counter = 0;

    // Display committee name if enabled
    if ($options['committee_global_name'] == 1) {
        $members .= '<div class="dac-row">';
        $members .= '   <div class="dac-col-xs-12">';
        $members .= '       <h1 style="display:none;">' . $xml->committeemembers->committeename . ' Committee Members</h1>';
        $members .= '   </div>'; // dac-col-xs-12
        $members .= '</div>'; // dac-row
    }

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

    // Display committee members
    $members .= '<div class="dac-row">';

    foreach ($xml->committeemembers as $member) {
        $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 ($options['committee_members_pics'] == 1) {
            $members .=   '<td style="border:0 !important;vertical-align: top; padding: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 ($options['committee_global_show_district_number'] == 1) {
            if ($member->accountid != '') {
                $members .= 'District ' . $member->accountid . '<br />';
            }
        }

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

        if ($ShowClubPos == 1) {
            if ($member->clubposition != '') {
                $members .= $member->clubposition . ' club position<br />';
            }
        }

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

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

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

        $members .=   '</td>';
        $members .= '</tr>';
        $members .= '</table>';
        $members .= '</div>'; // members dac-col-xs-12 dac-col-sm-6 dac-col-md-3 dac-col-lg-3
        $counter = $counter + 1;
    }

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