<?php
/**
 * @copyright (C) 2016 - 2025 Holger Brandt IT Solutions
 * @license GNU/GPL, see license.txt
 * WP Mailster is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.
 *
 * WP Mailster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WP Mailster; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * or see http://www.gnu.org/licenses/.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Not the droids
}
?>
<div class="mst_container">
	<div class="wrap">
		<h4>
			<?php _e("List Groups", 'wp-mailster');  ?>
			<a href="?page=mst_mailing_lists&amp;subpage=managegroups&amp;lid=<?php echo $lid; ?>" class="add-new-h2"><?php _e( "Edit List Groups", 'wp-mailster' ); ?></a>
		</h4>			
			<table id="mst_table" class="wp-list-table widefat fixed striped posts">
				<thead>
					<tr>
						<td width="8%"><?php _e("Num", 'wp-mailster'); ?></td>
						<td><?php _e("Name", 'wp-mailster'); ?></td>
                        <td><?php _e("#Users in Group", 'wp-mailster'); ?></td>
					</tr>
				</thead>
			<tfoot>
				<tr>
					<td width="8%"><?php _e("Num", 'wp-mailster'); ?></td>
					<td><?php _e("Name", 'wp-mailster'); ?></td>
                    <td><?php _e("#Users in Group", 'wp-mailster'); ?></td>
				</tr>
			</tfoot>						
			<tbody id="the-list">
			<?php
			if( !empty( $listGroups ) ){
				$index = 1;


                foreach($listGroups as $key => $listGroup){
                    $current_group = new MailsterModelGroup($listGroup->group_id);
                    $groupData = $current_group->getFormData();
                    $listGroups[$key]->groupUserCount = $current_group->getTotal();
                    $listGroups[$key]->groupName = $groupData->name;
                }

                usort($listGroups, function($a, $b) {return strcasecmp($a->groupName, $b->groupName);}); // sort by name

                $edit_nonce = wp_create_nonce( 'mst_edit_group' );
				foreach($listGroups as $listGroup){
                    $groupEditLink = sprintf(
                        '<a href="?page=mst_groups&amp;subpage=%s&amp;sid=%s&amp;_wpnonce=%s" title="%s">%s</a>',
                        'edit',
                        absint( $listGroup->group_id ),
                        $edit_nonce,
                        __('Edit Group', 'wp-mailster').': '.$listGroup->groupName,
                        $listGroup->groupName
                    );
			?>
				<tr>
					<td class="post-title page-title column-title"><?php echo $index++; ?></td>
					<td><?php echo $groupEditLink; ?></td>
                    <td><?php echo $listGroup->groupUserCount; ?></td>
				</tr>
			<?php	
				}
			}
			?>
			</tbody>
		</table>
	</div>
</div>