<?php
/**
 * DACdb Plus Member Directory Display Function
 *
 * Generates a responsive grid display of club members with configurable options
 * for member information display, pagination, and layout settings. Pulls member
 * data from DACdb API and formats it according to plugin settings.
 *
 * @package    DACdbPlus
 * @subpackage Display
 * @author     Member Minder Pro, LLC <support@memberminderpro.com>
 * @copyright  2023 Member Minder Pro, LLC
 * @license    GPL-2.0+
 * @version    1.0.0
 * @link       https://memberminderpro.com
 */

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

/**
 * Display club members in a responsive grid layout
 *
 * @param array $atts Shortcode attributes:
 *                    - imgwidth: Override default thumbnail width
 *                    - membertypes: Filter specific member types
 * 
 * @global bool   $fDebug     Debug mode flag
 * @global string $data_path  Path to data API endpoints
 * @global string $file_path  Path to file storage
 * @global array  $options    Plugin options array
 * 
 * @return string HTML output of member directory
 */
function dacdb_members($atts)
{
    global $fDebug, $data_path, $file_path, $options;

    // #region Initialize Variables
    $members = '';
    $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);
    $membertypes = $_GET['membertypes'] ?? $atts['membertypes'] ?? $options['membertypes'] ?? '';
    // #endregion

    // #region Debug Output
    if ($fDebug) {
        $members .= "=================================================================<BR>";
        $members .= "function dacdb_members called<br>";
        $members .= "AccountID=" . $options['dacdb_district'] . "<br>";
        $members .= "ClubID=" . $options['dacdb_club'] . "<br>";
        $members .= "Membertypes=" . $membertypes . "<br>";
        $members .= "URL=" . $data_path . 'wp_club_members.cfm?AccountID=' . $account_id . '&ClubID=' . $club_id . '&MemberTypeIDs=' . $membertypes . '<br>';
        $members .= "=================================================================<BR>";
    }
    // #endregion

    // #region Load Member Data
    $xml = simplexml_load_file($data_path . 'wp_club_members.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&MemberTypeIDs=' . $options['membertypes'] . '&R=' . rand());
    if (strlen($xml) == 0) {
        $members .= 'There are no members to display at this time.';
        return $members;
    }
    // #endregion

    // #region Pagination Setup
    $counter = 1;
    $lastCounter = count($xml->member);
    $pag_total = count($xml->member);
    $pag_pages = $pag_total / $options['mem_per_page'];
    $pag_pages = round_down($pag_pages, 0);
    $temp_pag_pages = ($pag_pages * $options['mem_per_page']);
    if ($temp_pag_pages > 0) {
        $pag_pages = $pag_pages + 1;
    }

    $members .= '<script>mypagenumber = ' . $pag_pages . ';</script>';
    $pagcounter = $options['mem_per_page'];
    $pagcountersec = 1;

    // check member cound start pagnation

    add_action('wp_footer', 'myscript', 200);
    if ($pag_pages != 0) {
        if ($options['pagnationposition_tb'] == 'Top') {
            if ($options['pagnationposition'] == 'Right') {
                $members .= '<div style="text-align:right;"><div class="pagination-dac-right" style="display:inline-block;"><ul id="pagination-dacdb" class="pagination-dac pagination"></ul></div></div>';
            } else {
                $members .= '<div><ul id="pagination-dacdb" class="pagination-dac pagination"></ul></div>';
            }
        }
    }
    // #endregion

    // #region Generate Member Grid
    $members .= '<div id="page-wrapper">';
    $grid_counter = 0;
    foreach ($xml->member as $member) {
        if ($pagcounter == $options['mem_per_page']) {
            $members .= '<div class="page-content"  id="pagsec' . $pagcountersec . '">';
            $pagcountersec = $pagcountersec + 1;
        } else {
            if ($pagcounter % $options['mem_per_page'] == 0) {
                $members .= '<div class="page-content"  id="pagsec' . $pagcountersec . '" style="display:none;">';
                $pagcountersec = $pagcountersec + 1;
            }
        }
        $grid_counter = $grid_counter + 1;
        if ($options['members_per_row'] == 1) {
            $grid_counter = 1; // force single row
        }
        if ($grid_counter == 1) {
            $members .= '<div class="dac-row">';
        }

        $setDivWidth = 150;
        $setImgWidth = 140;

        if ($options['members_per_row'] == 1) {
            $members .= '<div class="members dac-col-xs-12">';
        } else if ($options['members_per_row'] == 2) {
            $members .= '<div class="members dac-col-xs-12 dac-col-sm-6">';
        } else if ($options['members_per_row'] == 3) {
            $members .= '<div class="members dac-col-xs-12 dac-col-sm- dac-col-md-4">';
        } else if ($options['members_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;
        }

        if ($options['member_pics'] == 1) {
            if ($member->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="aspect-ratio: 4 / 5; width: ' . $setImgWidth  . 'px; object-fit: cover;" width="' . $setImgWidth  . '"></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="' . $member->image . '" style="aspect-ratio: 4 / 5; width: ' . $setImgWidth  . 'px; object-fit: cover;" width="' . $setImgWidth  . '"></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>' . $member->username . '</strong><br>';

        if ($options['member_club_pos'] == 1) {
            if ($member->clubposition != '') {
                $members .= '(' . $member->clubposition . ')<br>';
            }
        }
        if ($options['members_since'] == 1) {
            $startdate = strtotime($member->startdate);
            if ($options['members_since_format'] == 1) {
                $newformat = date('n/j/Y', $startdate);
            } elseif ($options['members_since_format'] == 2) {
                $newformat = date('n/Y', $startdate);
            } else {
                $newformat = date('Y', $startdate);
            }
            $members .= 'Member Since: ' . $newformat . '<br>';
        }
        if ($options['member_bus_name'] == 1) {
            if ($member->busweb != '') {
                if (strpos($member->busweb, "http://") == false) {
                    $newURL = 'http://' . $member->busweb;
                }
                $members .= '<a href="' . $newURL . '" target="_blank">' . $member->busname . '</a><br>';
            } else {
                $members .= $member->busname . '<br>';
            }
        }
        if ($options['member_bus_name'] == 0 && $options['member_bus_url'] == 1) {
            if ($member->busweb != '') {
                if (strpos($member->busweb, "http://") == false) {
                    $newURL = 'http://' . $member->busweb;
                }
                $members .= '<a href="' . $newURL . '" target="_blank">' . $member->busweb . '</a><br>';
            }
        }

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

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

        if ($options['members_contact_link'] == 1) {
            $members .= '<a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" name="cmc-inline-link-' . $counter . '" class="iframe3 cboxElement">Contact</a>';
        }
        $members .= '</td></tr></table></div>';
        $counter = $counter + 1;
        $pagcounter = $pagcounter + 1;

        if ($pagcounter % $options['mem_per_page'] == 0) {
            $members .= '</div>';
        }

        if ($grid_counter == $options['members_per_row']) {
            $members .= '</div>';
            $grid_counter = 0;
        }
    }
    $members .= '<!-- ' . $lastCounter . '-' . $counter . '-' . $grid_counter . ' -->';
    if ($grid_counter > 0) {
        $members .= '</div><!-- end  -->';
    }
    $members .= '</div>';
    $members .= '</div>';
    // #endregion

    // #region Pagination Controls
    if ($pag_pages != 0) {
        if ($options['pagnationposition_tb'] == 'Bottom') {
            if ($options['pagnationposition'] == 'Right') {
                $members .= '<div style="text-align:right;"><div class="pagination-dac-right" style="display:inline-block;"><ul id="pagination-dacdb" class="pagination-dac pagination"></ul></div></div>';
            } else {
                $members .= '<div><ul id="pagination-dacdb" class="pagination-dac pagination"></ul></div>';
            }
        }
    }
    // #endregion

    return $members;
}