<?php
/**
 * List page template for announcements
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}
?>

<div class="wrap pc-announcements-274-wrap">
    <h1 class="wp-heading-inline">
        <?php _e('Announcements', 'pc-announcements-274'); ?>
    </h1>
    <a href="<?php echo admin_url('admin.php?page=pc-announcements-274-add'); ?>" class="page-title-action">
        <?php _e('Add New', 'pc-announcements-274'); ?>
    </a>
    <hr class="wp-header-end">
    
    <?php if (isset($_GET['message']) && $_GET['message'] === 'success'): ?>
        <div class="notice notice-success is-dismissible">
            <p><?php _e('Announcement saved successfully!', 'pc-announcements-274'); ?></p>
        </div>
    <?php endif; ?>
    
    <div class="pc-announcements-274-card">
        <div class="pc-announcements-274-card-header">
            <h2><?php _e('All Announcements', 'pc-announcements-274'); ?></h2>
        </div>
        <div class="pc-announcements-274-card-body">
            <?php if (!empty($announcements) && is_array($announcements)): ?>
                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th scope="col" class="manage-column column-title"><?php _e('Title', 'pc-announcements-274'); ?></th>
                            <th scope="col" class="manage-column column-status"><?php _e('Status', 'pc-announcements-274'); ?></th>
                            <th scope="col" class="manage-column column-date"><?php _e('Schedule', 'pc-announcements-274'); ?></th>
                            <th scope="col" class="manage-column column-date"><?php _e('Created', 'pc-announcements-274'); ?></th>
                            <th scope="col" class="manage-column column-actions"><?php _e('Actions', 'pc-announcements-274'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($announcements as $announcement): ?>
                            <?php
                            $current_time = current_time('timestamp');
                            $start_timestamp = $announcement->start_date ? strtotime($announcement->start_date) : 0;
                            $end_timestamp = $announcement->end_date ? strtotime($announcement->end_date) : 9999999999;
                            
                            $is_active = $announcement->status === 'active' && 
                                        (!$start_timestamp || $current_time >= $start_timestamp) && 
                                        (!$end_timestamp || $current_time <= $end_timestamp);
                            ?>
                            <tr>
                                <td class="column-title">
                                    <strong>
                                        <a href="<?php echo admin_url('admin.php?page=pc-announcements-274-add&id=' . $announcement->id); ?>">
                                            <?php echo esc_html($announcement->title); ?>
                                        </a>
                                    </strong>
                                    <?php if ($is_active): ?>
                                        <span class="pc-announcements-274-active-badge"><?php _e('Active', 'pc-announcements-274'); ?></span>
                                    <?php endif; ?>
                                </td>
                                <td class="column-status">
                                    <span class="pc-announcements-274-status pc-announcements-274-status-<?php echo esc_attr($announcement->status); ?>">
                                        <?php
                                        $status_labels = array(
                                            'active' => __('Active', 'pc-announcements-274'),
                                            'inactive' => __('Inactive', 'pc-announcements-274'),
                                            'scheduled' => __('Scheduled', 'pc-announcements-274')
                                        );
                                        echo esc_html($status_labels[$announcement->status] ?? $announcement->status);
                                        ?>
                                    </span>
                                </td>
                                <td class="column-date">
                                    <?php if ($announcement->start_date): ?>
                                        <?php _e('From', 'pc-announcements-274'); ?> <?php echo date_i18n(get_option('date_format'), strtotime($announcement->start_date)); ?><br>
                                    <?php endif; ?>
                                    <?php if ($announcement->end_date): ?>
                                        <?php _e('Until', 'pc-announcements-274'); ?> <?php echo date_i18n(get_option('date_format'), strtotime($announcement->end_date)); ?>
                                    <?php endif; ?>
                                    <?php if (!$announcement->start_date && !$announcement->end_date): ?>
                                        <?php _e('Always', 'pc-announcements-274'); ?>
                                    <?php endif; ?>
                                </td>
                                <td class="column-date">
                                    <?php echo date_i18n(get_option('date_format'), strtotime($announcement->created_at)); ?>
                                </td>
                                <td class="column-actions">
                                    <a href="<?php echo admin_url('admin.php?page=pc-announcements-274-add&id=' . $announcement->id); ?>" class="button">
                                        <?php _e('Edit', 'pc-announcements-274'); ?>
                                    </a>
                                    <button class="button button-danger pc-announcements-274-delete-btn" data-id="<?php echo $announcement->id; ?>">
                                        <?php _e('Delete', 'pc-announcements-274'); ?>
                                    </button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                
                <?php if (!empty($total_pages) && $total_pages > 1): ?>
                    <div class="tablenav bottom">
                        <div class="tablenav-pages">
                            <?php
                            $current_url = admin_url('admin.php?page=pc-announcements-274');
                            echo paginate_links(array(
                                'base' => add_query_arg('paged', '%#%', $current_url),
                                'format' => '',
                                'prev_text' => __('&laquo; Previous', 'pc-announcements-274'),
                                'next_text' => __('Next &raquo;', 'pc-announcements-274'),
                                'total' => $total_pages,
                                'current' => $current_page,
                            ));
                            ?>
                        </div>
                    </div>
                <?php endif; ?>
            <?php else: ?>
                <div class="pc-announcements-274-empty-state">
                    <div class="pc-announcements-274-empty-icon">📢</div>
                    <h3><?php _e('No announcements found', 'pc-announcements-274'); ?></h3>
                    <p><?php _e('Create your first announcement to get started.', 'pc-announcements-274'); ?></p>
                    <a href="<?php echo admin_url('admin.php?page=pc-announcements-274-add'); ?>" class="button button-primary">
                        <?php _e('Add New Announcement', 'pc-announcements-274'); ?>
                    </a>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>