<?php
/**
 * DACdb Plus District Committee Chair (DCC) Display Function
 *
 * Generates a responsive grid layout displaying District Committee Chair officers
 * with their images, names, committee names, and positions. Supports various
 * display configurations through options and attributes.
 *
 * @package    DACdbPlus
 * @subpackage Display
 * @author     Member Minder Pro, LLC
 * @link       https://github.com/memberminderpro/dacdbplus/
 * @since      1.0.0
 */

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

/**
 * Displays District Committee Chair (DCC) officers in a grid layout
 *
 * @param array $atts Shortcode attributes (optional)
 *                    - imgwidth: Custom image width in pixels
 * 
 * @global boolean $fDebug       Debug mode flag
 * @global string  $data_path    Path to data files
 * @global string  $file_path    Path to file storage
 * @global array   $options      Plugin options array
 * 
 * @return string HTML markup for DCC officers display
 */
function dacdb_dcc($atts)
{
    global $fDebug, $data_path, $file_path, $options;

    // SECTION: Initialize Variables
    $dccs = ''; // Stores the generated HTML code for DCC officers
    $account_id = $options['dacdb_district'];
    $club_id    = $options['dacdb_club'];
    $thumbnail_width = isset($options['thumbnail_width']) && $options['thumbnail_width'] > 0 
        ? $options['thumbnail_width'] 
        : (isset($atts['imgwidth']) && isset($atts['imgwidth']) > 0 ? $atts['imgwidth'] : 150);

    // SECTION: Debug Output
    if ($fDebug) {
        $dccs .= "=================================================================<BR>";
        $dccs .= "function dacdb_dcc called<br>";
        $dccs .= "AccountID=" . $account_id . "<br>";
        $dccs .= "ClubID=" . $club_id . "<br>";
        $dccs .= "URL=" . $data_path . 'wp_dcc.cfm?AccountID=' . $account_id . "<br>";
        $dccs .= "=================================================================<BR>";
    }

    // SECTION: Data Retrieval
    $xml = simplexml_load_file($data_path . 'wp_dcc.cfm?AccountID=' . $account_id . '&R=' . rand());

    if (strlen($xml) == 0) {
        $dccs .= 'There are no DCC officers to display at this time.';
        return $dccs;
    }

    // SECTION: Grid Layout Generation
    $counter = 1; // Counter for tracking iterations
    $grid_counter = 0; // Counter for tracking grid positions
    $lastCounter = count($xml->dcc); // Total number of DCC officers

    // SECTION: Officer Display Loop
    foreach ($xml->dcc as $dcc) {
        $grid_counter = $grid_counter + 1;

        if ($options['dcc_per_row'] == 1) {
            $grid_counter = 1; // Force single row if dcc_per_row is 1
        }

        // Start new row if needed
        if ($grid_counter == 1) {
            $dccs .= '<div class="dac-row"><!-- start row -->';
        }

        // SECTION: Layout Configuration
        $setDivWidth = 160; // Width of table cell
        $setImgWidth = 150; // Width of image

        // Configure column classes based on dcc_per_row setting
        if ($options['dcc_per_row'] == 1) {
            $dccs .= '<div class="members dac-col-xs-12">'; // One column per row
        } else if ($options['dcc_per_row'] == 2) {
            $dccs .= '<div class="members dac-col-xs-12 dac-col-sm-6">'; // Two columns per row
        } else if ($options['dcc_per_row'] == 3) {
            $dccs .= '<div class="members dac-col-xs-12 dac-col-sm-12 dac-col-md-4">'; // Three columns per row
        } else if ($options['dcc_per_row'] == 4) {
            $dccs .= '<div class="members dac-col-xs-12 dac-col-sm-6 dac-col-md-3 dac-col-lg-3">'; // Four columns per row
            $setDivWidth = 120; // Adjusted table cell width for four columns
            $setImgWidth = 110; // Adjusted image width for four columns
        }

        // SECTION: Officer Image Display
        if ($dcc->image == '') {
            // Display default image if officer's image is empty
            $dccs .= '<table style="vertical-align: top;padding-right: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: ' . $thumbnail_width  . 'px; object-fit: cover;" width="' . $thumbnail_width  . '"></td>';
        } else {
            // Display officer's image
            $dccs .= '<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="' . $dcc->imgpath . '' . $dcc->image . '"  style="aspect-ratio: 4 / 5; width: ' . $thumbnail_width  . 'px; object-fit: cover;" width="' . $thumbnail_width  . '"></td>';
        }

        // SECTION: Officer Details Display
        $dccs .= '<td style="border:0 !important;max-width:1px;width:1px;padding:0;">&nbsp;</td><td style="border:0 !important;vertical-align: top;padding:0;"><strong>' . $dcc->committeename . '</strong><br><strong>' . $dcc->membername . '</strong>';
        $dccs .= '<br>' . $dcc->committeeposition . '<br>';
        $dccs .= '</td>';
        $dccs .= '</tr></table></div>';

        // Close row if needed
        if ($grid_counter == $options['dcc_per_row']) {
            $dccs .= '</div><!-- end row -->'; 
            $grid_counter = 0; // Reset grid counter
        }

        $counter = $counter + 1;
    }

    // SECTION: Cleanup
    if ($grid_counter > 0) {
        $dccs .= '</div><!-- end incomplete row -->'; // Close the last row if incomplete
    }

    return $dccs; // Return the generated HTML code for DCC officers
}