<?php
function dacdb_leaders($atts)
{
    global $fDebug, $data_path, $file_path, $options;
    $options = get_mmp_options(); // Retrieve the plugin options

    $members = ''; // Initialize the variable to store the generated HTML markup for leaders

    $account_id = $options['dacdb_district']; // Retrieve the AccountID from options
    $club_id = $options['dacdb_club']; // Retrieve the ClubID from options

    // Enqueue the leader styles
    wp_enqueue_style('dacdb-leaders', MMPDB_PLUGIN_URL . 'css/show-leaders.css', array(), MMPDB_PLUGIN_VERSION);

    // Set leaders_per_row attribute or use default value from options if not specified
    // If leaders_per_row is not set, use the default from the options, or 1 if options is empty.

    if (!isset($atts['leaders_per_row']) || $atts['leaders_per_row'] === '') {
        $atts['leaders_per_row'] = isset($options['leaders_per_row']) && $options['leaders_per_row'] !== '' ? $options['leaders_per_row'] : 1;
    }

    if (!isset($atts['leader_pics']) || $atts['leader_pics'] === '') {
        $atts['leader_pics'] = isset($options['leader_pics']) && $options['leader_pics'] !== '' ? $options['leader_pics'] : 1;
    }

    // Set default value for leaders_per_row if not specified in options
    if (isset($options['leaders_per_row']) && $options['leaders_per_row'] == '')
        $atts['leaders_per_row'] = 1;

    // Set leader_pics attribute or use default value from options if not specified
    if (isset($atts['leader_pics']) && $atts['leader_pics'] == '')
        $atts['leader_pics'] = $options['leader_pics'];

    // Set default value for leader_pics if not specified in options
    if (isset($atts['leader_pics']) && $options['leader_pics'] == '')
        $atts['leader_pics'] = 1;

    // Set leaders_contact_link attribute or use default value from options if not specified
    if (isset($atts['leaders_contact_link']) && $atts['leaders_contact_link'] == '')
        $atts['leaders_contact_link'] = $options['leaders_contact_link'];

    // Set default value for leaders_contact_link if not specified in options
    if (isset($atts['leaders_contact_link']) && $options['leaders_contact_link'] == '')
        $atts['leaders_contact_link'] = 1;

    // Set flex attribute or use 'N' as default value if not specified
    if (isset($atts['flex']) && $atts['flex'] == '')
        $atts['flex'] = 'N';

    // Check if the ClubID is 0, and return an error message if so
    if ($club_id == 0) {
        $members .= "=================================================================<BR>";
        $members .= "Error: This function can only be used to display club, chapter, branch leaders. The ClubID configured is  0.<br>";
        $members .= "AccountID=" . $options['dacdb_district'] . "<br>";
        $members .= "ClubID=" . $options['dacdb_club'] . "<br>";
        $members .= "=================================================================<BR>";
        return $members;
    }

    // If debug mode is enabled, generate a debug message with function information
    if ($fDebug && dacdb_is_admin()) {
        $members .= "=================================================================<BR>";
        $members .= "function dacdb_leaders called<br>";
        $members .= "AccountID=" . $options['dacdb_district'] . "<br>";
        // $data_file is not defined in this scope, skip outputting it
        $members .= "data_path=" . $data_path . "<br>";
        $members .= "ClubID=" . $options['dacdb_club'] . "<br>";
        $members .= "leaders_per_row=" . $options['leaders_per_row'] . "<br>";
        $members .= "leaders_per_row override=" . $atts['leaders_per_row'] . "<br>";
        $members .= "leader_pics=" . $options['leader_pics'] . "<br>";
        $members .= "leader_pics override=" . $atts['leader_pics'] . "<br>";
        $members .= "flex=" . $atts['flex'] . "<br>";
        $members .= "URL=" . $data_path . 'wp_leaders.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $club_id . '<br>';
        $members .= "=================================================================<BR>";
    }

    // Load the XML data containing leaders information
    $xml = simplexml_load_file($data_path . 'wp_leaders.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $club_id . '&R=' . rand());

    // If XML is empty, return a message indicating no leaders to display
    if (strlen($xml) == 0) {
        $members .= 'There are no club leaders to display at this time.';
        return $members;
    }

    // Loop through each leader and generate HTML markup for their information
    $counter = 1;
    $lastCounter = count($xml->leader);
    $grid_counter = 0;
    foreach ($xml->leader as $leader) {
        $grid_counter = $grid_counter + 1;

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

        // Open a new row div if it's the first leader in the row
        if ($grid_counter == 1) {
            $members .= '<div class="dac-row">';
        }

        // Set the width of div and image based on leaders_per_row value
        $setDivWidth = 160;
        $setImgWidth = 150;

        // Adjust div and image width for different leaders_per_row values
        if ($options['leaders_per_row'] == 4) {
            $members .= '<div class="members dac-col-xs-12 dac-col-sm-6 dac-col-md-3 dac-col-lg-3">';
            $setDivWidth = 120;
            $setImgWidth = 110;
        } else if ($options['leaders_per_row'] == 3) {
            $members .= '<div class="members dac-col-xs-12 dac-col-sm-12 dac-col-md-4">';
        } else if ($options['leaders_per_row'] == 2) {
            $members .= '<div class="members dac-col-xs-12 dac-col-sm-6">';
        } else {
            $members .= '<div class="members dac-col-xs-12">';
        }

        // Generate HTML markup for leader information and append it to $members variable
        if ($options['leader_pics'] == 1) {
            if ($leader->image == '') {
                $members .= '<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="max-width:' . $setImgWidth . 'px; aspect-ratio: 4 / 5; width: 160px; height: 200px; object-fit: cover;" class="leadership_image portrait"></td>';
            } else {
                $members .= '<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="' . $leader->imgpath . '' . $leader->image . '" style="max-width:' . $setImgWidth . 'px; aspect-ratio: 4 / 5; width: 160px; height: 200px; object-fit: cover;" class="leadership_image portrait"></td>';
            }
        }
        $members .= '<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>' . $leader->username . '</strong><br>' . $leader->clubposition . '<br>';

        // Append contact link if leaders_contact_link is enabled
        if ($options['leaders_contact_link'] == 1) {
            
            $members .= '<div style=""><strong><a class="modal-link leader-directory member-grid__contact-btn" href="' . $data_path . 'R_Emailer.cfm?UserID=' . $leader->userid . '">Contact</a></strong></div>';

        }

        $members .= '</td></tr></table></div>';

        // Close the row div if it's the last leader in the row
        if ($grid_counter == $options['leaders_per_row']) {
            $members .= '</div>';
            $grid_counter = 0;
        }

        $counter = $counter + 1;
    }

    // Append a comment with counter values for debugging purposes
    $members .= '<!-- ' . $lastCounter . '-' . $counter . '-' . $grid_counter . ' -->';

    // Close any remaining open divs
    if ($grid_counter > 0) {
        $members .= '</div><!-- end  -->';
    }

    // Calculate the total number of leaders
    $leader_total = count($xml->leader);

    // If there are no leaders, append a message indicating so
    if ($leader_total == 0) {
        $members .= 'There are no leaders to display at this time.';
    }

    // Return the generated HTML markup for leaders
    return $members;
}