<?php
/**
 * DACdb Plus Committee Display Functions
 *
 * Provides functionality to display Rotary committee information including
 * committee listings and committee member details with various display options.
 *
 * @package    DACdbPlus
 * @subpackage Display
 * @author     Member Minder Pro, LLC
 * @link       https://memberminderpro.com
 * @copyright  Copyright (c) 2023 Member Minder Pro, LLC
 * @license    GPL-2.0+
 */

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

/**
 * Generates and displays committee information based on provided attributes
 *
 * @since 1.0.0
 *
 * @param array $atts {
 *     Optional. An array of attributes for configuring the committee display.
 *
 *     @type string $description Whether to show committee descriptions (default: '0')
 *     @type string $orgyear     Whether to show organization year (default: '')
 * }
 * @return string HTML markup for committee display
 */
function dacdb_committees($atts)
{
    global $fDebug, $data_path, $file_path, $options;

    $committees = ''; // Initialize an empty string to store the generated list of committees.

    $account_id = $options['dacdb_district']; // Retrieve the district account ID from options.
    $club_id    = $options['dacdb_club']; // Retrieve the club ID from options.

    $description = $_GET['description'] ?? $atts['description'] ?? $options['committee_global_description'] ?? '0';

    $orgyear = isset($atts['orgyear']) ? $atts['orgyear'] : ''; // Retrieve the "orgyear" attribute from the function arguments.
    $members = isset($_GET["members"]) ? $_GET["members"] : ''; // Get the "members" value from the query string parameters.

    $options['committee_members_per_row'] ??= 1;
    $current_url = "";

    #region Debug Information
    if ($fDebug) {
        // Generate debug information if debugging is enabled.
        $committees .= "=================================================================<BR>";
        $committees .= "function dacdb_committees called<br>";
        $committees .= "AccountID=" . $account_id . "<br>";
        $committees .= "ClubID=" . $club_id . "<br>";
        $committees .= "description=" . $description . "<br>";
        $committees .= "orgyear=" . $orgyear . "<br>";
        $committees .= "members=" . $members . "<br>";
        if ($members)
            $committees .= "URL=" . $data_path . 'wp_ById2.cfm?AccountID=' . $account_id . '&ClubID=' . $club_id . '&id=' . $members . '&RoleKey=' . "<br>";
        else
            $committees .= "URL=" . $data_path . 'wp_committees.cfm?AccountID=' . $account_id . '&ClubID=' . $club_id . "<br>";
        $committees .= "=================================================================<BR>";
    }
    #endregion

    #region Committee Member Display
    if ($members) {
        // List members on the selected committee.
        $counter = 1;
        $grid_counter = 0;

        $xml = simplexml_load_file($data_path . 'wp_ById2.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&id=' . $members . '&R=' . rand());

        if (strlen($xml) == 0) {
            $committees .= 'There are no committee members to display at this time.';
            return $committees;
        }

        $committees .= '<style>h1.entry-title { display:none !important; }</style>';
        $lastCounter = count($xml->committeemembers);

        foreach ($xml->committeemembers as $member) {
            if ($counter == 1) {
                if ($options['committee_global_name'] == 1) {
                    $committees .= '<div style="overflow: auto;margin-bottom:10px;"><h1>' . $member->committeename . ' Committee Members</h1></div>';
                }
                $url = strtok($_SERVER["REQUEST_URI"], '?');

                if ($description == 1) {
                    $committees .= '<div style="overflow: auto;">' . $member->description . '</div><br><br>';
                }
                $counter = 2;
            }

            $grid_counter = $grid_counter + 1;

            if ($options['committee_members_per_row'] == 1) {
                $grid_counter = 1;
            }

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

            // Set the width for the committee member div and image based on the number of members per row.
            $setDivWidth = 150;
            $setImgWidth = 140;

            if ($options['committee_members_per_row'] == 1) {
                $committees .= '<div class="members dac-col-xs-12">';
            } else if ($options['committee_members_per_row'] == 2) {
                $committees .= '<div class="members dac-col-xs-12 dac-col-sm-6">';
            } else if ($options['committee_members_per_row'] == 3) {
                $committees .= '<div class="members dac-col-xs-12 dac-col-sm-12 dac-col-md-4">';
            } else if ($options['committee_members_per_row'] == 4) {
                $committees .= '<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['committee_members_pics'] == 0) {
                $committees .= '<table style="vertical-align: top;padding:0;border:0 !important;"><tr>';
            } else {
                if ($member->image == '') {
                    $committees .= '<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" width="' . $setImgWidth . '"></td>';
                } else {
                    $committees .= '<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->imgpath . '' . $member->image . '" width="' . $setImgWidth . '"></td>';
                }
            }

            if ($options['committee_members_contact_link'] == 1) {
                $committees .= '<td style="border:0 !important;max-width:1px;width:1px;padding:0;">&nbsp;</td><td style="border:0 !important;vertical-align: top;padding:0;"><a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" class="iframe3 cboxElement"><strong>' . $member->username . '</strong></a>';
            } else {
                $committees .= '<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>';
            }

            // Display additional information based on the configured options.
            if ($options['committee_members_committeeposition'] == 1) {
                if ($member->committeeposition != '') {
                    $committees .= '<br>' . $member->committeeposition . '';
                }
            }

            $formattedUrl = formatDomainToUrl($member->cluburl);
            if (!empty($formattedUrl)) {
                $committees .= '<br><a href="' . $formattedUrl . '" target="new">' . $member->clubname . '</a><br>';
            } else {
                $committees .= '<span class="clubname"><br>' . $member->clubname . '<br></span>';
            }

            if ($options['committee_global_show_district_number'] == 1) {
                if ($member->accountid != '') {
                    $committees .= '<br>District ' . $member->accountid . '';
                }
            }
            if ($options['committee_members_busposition'] == 1) {
                if ($member->busposition != '') {
                    $committees .= '<br>' . $member->busposition . '';
                }
            }
            if ($options['committee_members_classification'] == 1) {
                if ($member->classification != '') {
                    $committees .= '<br>' . $member->classification . '';
                }
            }
            if ($options['committee_members_contact_link'] == 1) {
                $committees .= '<br><a href="' . $data_path . 'R_Emailer.cfm?UserID=' . $member->userid . '" class="iframe3 cboxElement">Contact</a>';
            }

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

            if ($grid_counter == $options['committee_members_per_row']) {
                $committees .= '</div><!--even-->';
                $grid_counter = 0;
            }
        }

        if ($grid_counter > 0) {
            $committees .= '</div><!--end-->';
        }
    }
    #endregion

    #region Committee List Display
    else {
        // List all committees - Club or District.
        $xml = simplexml_load_file($data_path . 'wp_committees.cfm?AccountID=' . $options['dacdb_district'] . '&ClubID=' . $options['dacdb_club'] . '&R=' . rand());

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

        $counter = 1;
        $committees .= '<table width="100%" class="dcc" style="vertical-align: top;padding:0;">';

        foreach ($xml->committee as $committee) {
            $pad = "";
            // level is being returned as 0 right now.
            //if ( $committee->level > 0 ) {
            //    $w = $committee->level*30;
            //    $pad = 'style="padding-left:$wpx;"';
            //}

            if ($committee->gdg == "Y") {
                $committees .= '<tr><td ' . $pad . ' style="vertical-align: top;padding:0;"><span style="color:red"><strong>' . $committee->name . '</strong></span></td>';
            } else {
                $committees .= '<tr><td ' . $pad . ' style="vertical-align: top;padding:0;"><a href="' . $current_url . '?members=' . $committee->committeeid . '"><strong>' . $committee->name . '</strong></a></td>';
            }

            if ($orgyear == 1 || strtoupper($orgyear) == 'Y') {
                $committees .= '<td style="vertical-align: top; padding:0; width: 100px">' . $committee->year . '</td>';
            }

            $committees .= '</tr>';

            if ($description == 1 || strtoupper($description) == 'Y') {
                if (strlen(trim($committee->description)))
                    $committees .= '<tr><td>' . trim($committee->description) . '</td></tr>';
            }

            $counter = $counter + 1;
        }

        $committees .= '</table>';
        $committee_total = count($xml->committee);
    }
    #endregion

    return $committees; // Return the generated list of committees.
}
