<?php
/**
 * DACdb Plus Show Areas Function
 *
 * Displays area/region information with officer details in various layout styles.
 * Provides flexible display options for showing district area/region officers and 
 * their associated clubs. Supports multiple layout formats including linear lists,
 * grouped displays, and detailed club listings.
 *
 * @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
 * 
 * @wordpress-plugin
 * Plugin URI:  https://www.memberminderpro.com
 */

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

/**
 * Generates HTML display of area/region information with officers
 *
 * Retrieves and formats area/region data including officer details and club listings.
 * Supports multiple display styles and customization options through attributes.
 *
 * @since 1.0.0
 * 
 * @param array $atts {
 *     Attributes for customizing the areas display.
 *     
 *     @type string  $account     District account ID (defaults to option setting)
 *     @type int     $imgwidth    Width for officer thumbnails (defaults to 150)
 *     @type string  $rolekeys    Role keys to filter officers (defaults to 'AG')
 *     @type string  $region      Region filter (defaults to '0')
 *     @type string  $style       Display style (0-3, defaults to '1')
 * }
 * @return string HTML output for area display
 */
function dacdb_areas($atts)
{
    //* #region Configuration and Setup
    global $fDebug, $data_path, $file_path, $options;

    $areas = '';
    $account_id   = isset($atts['account']) ? $atts['account'] : $options['dacdb_district'];
    $club_id    = $options['dacdb_club'];
    $thumbnail_width = isset($options['thumbnail_width']) && $options['thumbnail_width'] > 0 ? $options['thumbnail_width'] : (isset($atts['imgwidth']) && $atts['imgwidth'] > 0 ? $atts['imgwidth'] : 150);
    $rolekeys   = isset($atts['rolekeys']) ? $atts['rolekeys'] : 'AG';
    $region     = isset($atts['region']) ? $atts['region'] : '0';
    $style      = isset($atts['style']) ? $atts['style'] : '1';
    //* #endregion

    //* #region Debug Output
    if ($fDebug) {
        $areas .= "=================================================================<BR>";
        $areas .= "function dacdb_areas called<br>";
        $areas .= "AccountID=" . $account_id . "<br>";
        $areas .= "ClubID=" . $options['dacdb_club'] . "<br>";
        $areas .= "imgWidth=" . $thumbnail_width . "<br>";
        $areas .= "RoleKeys=" . $rolekeys . "<br>";
        $areas .= "Style=" . $style . "<br>";
        $areas .= "Thumbnail Width =" . $options['thumbnail_width'] . "<br>";
        $areas .= "url=" . $data_path . 'wp_areav2.cfm?AccountID=' . $account_id . '&rolekeys=' . $rolekeys . '&Region=' . $region . "<br>";
        $areas .= "=================================================================<BR>";
    }
    //* #endregion

    //* #region XML Data Loading
    $xml = simplexml_load_file($data_path . 'wp_areav2.cfm?AccountID=' . $account_id . '&rolekeys=' . $rolekeys . '&Region=' . $region . '&R=' . rand());
    if (strlen($xml) == 0) {
        $areas .= 'There are no areas/regions to display at this time.';
        return $areas;
    }
    //* #endregion

    //* #region Display Style Processing
    switch ($style) {

        //* #region Style 0 & 1 - Linear List
        case '0':   // linear list of AGs (1 column)
        case '1':   // linear list of AGs (1 column)
            $counter = 1;
            $areas .= '<table  style="vertical-align: top;padding:0;border:0 !important;">';
            foreach ($xml->area as $area) {
                $officer_total = count($area->officers);
                if ($officer_total > 0) {
                    foreach ($area->officers->officer as $officer) {
                        $areas .= '<tr><td style="padding-right:15px;width:150px;vertical-align: top;padding:5px;border:0 !important;"><img src="' . $officer->imgpath . '' . $officer->image . '" style="aspect-ratio: 4 / 5; width: ' . $thumbnail_width  . 'px; object-fit: cover;" width="' . $thumbnail_width  . '"></td>';
                        $areas .= '<td valign="top" style="vertical-align: top;padding:0;border:0 !important;">';
                        $areas .= '<strong>' . $officer->areaposition . ' ' .  $area->regionlabel . ' ' . $area->area . '</strong><br>' . $officer->membername . '';
                        $areas .= '<br><strong>Club:</strong> ' . $officer->clubname . '<br><strong>Area ' . $area->area . ' Clubs:</strong> ' . $area->areaclubs . '';
                        $areas .= '<br><br><a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $officer->userid . '" name="dlc-inline-link-' . $counter . '" class="iframe3 cboxElement">Contact</a>';
                        $areas .= '</td></tr>';
                        $areas .= '<tr><td colspan="2" style="vertical-align: top;padding:0;border:0 !important;"><hr></td></tr>';
                        $counter = $counter + 1;
                    }
                }
            }
            $areas .= '</table>';
            return $areas;
            break;
        //* #endregion

        //* #region Style 2 - Areas Grouped
        case '2':    // areas grouped
            $counter = 1;
            foreach ($xml->area as $area) {
                $areas .= '<h3>' . $area->regionlabel . ' ' . $area->area . '</h3><br>';
                $areas .= '<table  style="border:0 !important; vertical-align: top;padding:0;border:5 !important;" width="100%">';
                $areas .= '    <tr>';
                $areas .= '        <td style="border:0 !important;vertical-align: top; padding:0;" >';

                $officer_total = count($area->officers);
                if ($officer_total > 0) {
                    $areas .= '            <table style="width: 400px; vertical-align: top;padding:0; border:0">';
                    foreach ($area->officers->officer as $officer) {
                        $areas .= '            <tr>';
                        $areas .= '                <td style="padding-right:15px;width:150px;vertical-align: top;padding:5px; border:0 !important;"><img src="' . $officer->imgpath . '' . $officer->image . '" style="aspect-ratio: 4 / 5; width: ' . $thumbnail_width  . 'px; object-fit: cover;" width="' . $thumbnail_width  . '"></td>';
                        $areas .= '                <td valign="top" style="width: 250px; vertical-align: top;padding:0;border:0 !important;">';
                        $areas .= '                    <strong>' . $officer->areaposition . '</strong><br/>';
                        $areas .= '                    ' . $officer->membername . '<br/>';
                        $areas .= '                    <strong>Club:</strong> ' . $officer->clubname . '<br/>';
                        $areas .= '                    <a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $officer->userid . '" name="dlc-inline-link-' . $counter . '" class="iframe3 cboxElement">Contact</a>';
                        $areas .= '                </td>';
                        $areas .= '            </tr>';
                        $counter = $counter + 1;
                    }
                    $areas .= '        </table>';
                }
                $areas .= '        </td>';
                $areas .= '        <td style="border:0 !important;vertical-align: top; padding:0;">';
                $areas .= '            <strong>Area ' . $area->area . ' Clubs:</strong> <br />';
                $areas .= '            ' . $area->areaclubs . '<br />';
                $areas .= '        </td>';
                $areas .= '    </tr>';
                $areas .= '<tr><td colspan="2" style="vertical-align: top;padding:0;border:0 !important;"><hr></td></tr>';
                $areas .= '</table>';
            }
            return $areas;
            break;
        //* #endregion

        //* #region Style 3 - Areas Grouped with Club List
        case '3':    // areas grouped - list clubs
            $counter = 1;
            foreach ($xml->area as $area) {
                $clubs = explode(', ', $area->areaclubs);

                $areas .= '<h3>' . $area->regionlabel . ' ' . $area->area . '</h3><br>';
                $areas .= '<table  style="vertical-align: top;padding:0;border: 0 !important;">';
                $areas .= '    <tr>';
                $areas .= '        <td style="border:0 !important; vertical-align: top; padding:0; width: 400px;" >';

                $officer_total = count($area->officers);
                if ($officer_total > 0) {
                    $areas .= '            <table style="vertical-align: top;padding:0; border:0 ">';
                    foreach ($area->officers->officer as $officer) {
                        $areas .= '            <tr>';
                        $areas .= '                <td style="padding-right:15px;width:150px;vertical-align: top;padding:5px; border:0 !important;"><img src="' . $officer->imgpath . '' . $officer->image . '" style="aspect-ratio: 4 / 5; width: ' . $thumbnail_width  . 'px; object-fit: cover;" width="' . $thumbnail_width  . '"></td>';
                        $areas .= '                <td valign="top" style="width: 250px; vertical-align: top;padding:0;border:0 !important;">';
                        $areas .= '                    <strong>' . $officer->areaposition . '</strong><br/>';
                        $areas .= '                    ' . $officer->membername . '<br/>';
                        $areas .= '                    <strong>Club:</strong> ' . $officer->clubname . '<br/>';
                        $areas .= '                    <a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $officer->userid . '" name="dlc-inline-link-' . $counter . '" class="iframe3 cboxElement">Contact</a>';
                        $areas .= '                </td>';
                        $areas .= '            </tr>';
                        $counter = $counter + 1;
                    }
                    $areas .= '        </table>';
                }
                $areas .= '        </td>';
                $areas .= '        <td align="left" style="border: 0!important;vertical-align: top; padding:0;">';
                $areas .= '            <strong>Area ' . $area->area . ' Clubs:</strong> <br />';
                $areas .= '            <UL>';
                foreach ($clubs as $Club) {
                    $areas .= '            <LI>' .  $Club . '</LI>';
                }
                $areas .= '            </UL>';
                $areas .= '        </td>';
                $areas .= '    </tr>';
                $areas .= '<tr><td colspan="2" style="vertical-align: top;padding:0;border:0 !important;"><hr></td></tr>';
                $areas .= '</table>';
            }
            return $areas;
            break;
        //* #endregion

        default:
            $areas .= "=================================================================<BR>";
            $areas .= "&nbsp;&nbsp;&nbsp;Invalid style selected [" . $style . "]<BR>";
            $areas .= "=================================================================<BR>";
            return $areas;
            break;
    }
    //* #endregion
    
    return $areas;
}