<?php
/**
 * DACdb Plus Club Presidents Display Function
 *
 * Generates a responsive grid display of club presidents with their photos and contact information.
 * Supports multiple layout options (1-4 presidents per row) and includes fallback avatar images.
 *
 * @package    DACdbPlus
 * @subpackage Display
 * @author     Member Minder Pro, LLC <support@memberminderpro.com>
 * @copyright  2024 Member Minder Pro, LLC
 * @license    GPL-2.0-or-later
 * @link       https://www.memberminderpro.com
 *
 * @since      1.0.0
 */

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

/**
 * Displays club presidents in a responsive grid layout
 *
 * @param array $atts Shortcode attributes (currently unused but maintained for future expansion)
 * @return string HTML output of club presidents grid
 *
 * @global boolean $fDebug       Debug flag for detailed logging
 * @global string  $data_path    Base path for data requests
 * @global array   $options      Plugin options array containing:
 *                              - dacdb_district: District account ID
 *                              - dacdb_club: Club ID
 *                              - leaders_contact_link: Whether to show contact links (0/1)
 *                              - club_presidents_per_row: Number of presidents per row (1-4)
 */
function dacdb_club_presidents($atts)
{
    global $fDebug, $data_path, $options;

    // SECTION: Initialize Variables
    $account_id = $options['dacdb_district'];
    $club_id    = $options['dacdb_club'];
    $leaders = '';

    // SECTION: Debug Output
    if ($fDebug) {
        $leaders .= "=================================================================<BR>";
        $leaders .= "function dacdb_club_presidents called<br>";
        $leaders .= "AccountID=" . $account_id . "<br>";
        $leaders .= "leaders_contact_link=" . $options['leaders_contact_link'] . "<br>";
        $leaders .= "data_path=" . $data_path . "<br>";
        $leaders .= "ClubID=" . $club_id . "<br>";
        $leaders .= "URL=" . $data_path . 'wp_leaders.cfm?AccountID=' . $account_id . '&ClubID=' . $club_id . '<br>';
        $leaders .= "=================================================================<BR>";
    }

    // SECTION: Data Retrieval
    try {
        $xml = simplexml_load_file($data_path . 'wp_ccp.cfm?AccountID=' . $account_id . '&RoleKey=P&SortBy=C' . '&R=' . rand());
        if (strlen($xml) == 0) {
            $leaders .= 'There are no club presidents to display at this time.';
            return $leaders;
        }
    } catch (Exception $e) {
        return 'Error: ' . $e->getMessage();
    }

    // SECTION: Grid Layout Generation
    $counter = 1;
    $cnt = 0;
    $lastCounter = count($xml->ccp);

    // SECTION: President Display Loop
    foreach ($xml->ccp as $ccp) {
        $grid_counter = isset($grid_counter) ? $grid_counter + 1 : 1;

        $presidentsPerRow = isset($options['club_presidents_per_row']) ? $options['club_presidents_per_row'] : 1;

        if ($presidentsPerRow == 1) {
            $grid_counter = 1; // force single row
        }

        if ($grid_counter == 1) {
            $leaders .= '<div class="dac-row">';
        }

        $presidentClasses = [
            1 => 'members dac-col-xs-12',
            2 => 'members dac-col-xs-12 dac-col-sm-6',
            3 => 'members dac-col-xs-12 dac-col-sm-12 dac-col-md-4',
            4 => 'members dac-col-xs-12 dac-col-sm-6 dac-col-md-3 dac-col-lg-3'
        ];

        $leaders .= '<div class="' . ($presidentClasses[$presidentsPerRow] ?? 'members') . '">';

        // Adjust div and image widths based on number of presidents per row
        $setDivWidth = ($presidentsPerRow == 4) ? 120 : 150;
        $setImgWidth = ($presidentsPerRow == 4) ? 110 : 140;

        if (
            $ccp->image == ''
        ) {
            $leaders .= '<table style="vertical-align: top;padding:0;border:0 !important;"><tr><td style="border:0 !important;vertical-align: top;padding:0;max-width:' . $setDivWidth . 'px;width:' . $setDivWidth . 'px"><img src="' . MMPDB_PLUGIN_URL . 'img/avatar.svg" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth  . 'px; object-fit: cover;" width="' . $setImgWidth  . '"></td>';
        } else {
            $leaders .= '<table style="vertical-align: top;padding:0;border:0 !important;"><tr><td style="border:0 !important;vertical-align: top;padding:0;max-width:' . $setDivWidth . 'px;width:' . $setDivWidth . 'px"><img src="' . $ccp->imgpath . '' . $ccp->image . '" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth  . 'px; object-fit: cover;" width="' . $setImgWidth  . '"></td>';
        }
        $leaders .= '<td style="border:0 !important;max-width:1px;width:1px;padding:0;">&nbsp;</td>';
        $leaders .= '<td style="border:0 !important;vertical-align: top;padding:0;">';
        $leaders .= '<strong>' . $ccp->membername . '</strong><br>' . $ccp->clubname . '<br>';
        if ($options['leaders_contact_link'] == 1) {
            $leaders .= '       <a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $ccp->userid . '" name="dlc-inline-link-' . $cnt . '" class="iframe3 cboxElement">Contact</a>';
        }
        $leaders .= '</td>';


        $leaders .= '</tr></table></div>';
        $cnt++;
        if ($grid_counter == $options['club_presidents_per_row']) {
            $leaders .= '</div>';
            $grid_counter = 0;
        }
    }
    if ($grid_counter > 0) {
        $leaders .= '</div><!-- end  -->';
    }
    return $leaders;
}