<?php

if (!defined('ABSPATH')) { exit; }
?>
<div class="aieo-dmm-section">
    <p class="description" style="font-size:12px;">
        <?php
        $display_url = add_query_arg(array(
            'page'    => AIEO_DMM::MENU_SLUG,
            'dmm_tab' => 'waitlist_display',
        ), admin_url('admin.php'));
        echo wp_kses_post(sprintf(
            
            __('Looking for the waitlist form copy + restock email template? They now live under %1$sVisual Display → Waitlist Display%2$s.', 'ai-eshop-optimizer'),
            '<a href="' . esc_url($display_url) . '">',
            '</a>'
        ));
        ?>
    </p>
</div>

<div class="aieo-dmm-section">
    <h2><?php esc_html_e('Subscribers', 'ai-eshop-optimizer'); ?> <span class="count">(<?php echo (int) $total; ?>)</span></h2>
    <p class="description">
        <?php esc_html_e('Every active subscription. Notified rows stay so you can see the historical demand for each product.', 'ai-eshop-optimizer'); ?>
    </p>

    <div class="aieo-dmm-card">
        <table class="widefat striped">
            <thead>
                <tr>
                    <th><?php esc_html_e('Email', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Product', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Variation', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Status', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Subscribed', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Notified', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Action', 'ai-eshop-optimizer'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($subscribers)) : ?>
                    <tr><td colspan="7" style="text-align:center;color:#646970;">
                        <?php esc_html_e('No subscribers yet.', 'ai-eshop-optimizer'); ?>
                    </td></tr>
                <?php else :
                    foreach ($subscribers as $sub) :
                        $title = get_the_title((int) $sub->product_id) ?: '#' . (int) $sub->product_id;
                        $edit_url = admin_url('post.php?post=' . (int) $sub->product_id . '&action=edit');
                        $status_pill = $sub->status === 'notified'
                            ? '<span style="display:inline-block;padding:2px 8px;background:#dcfce7;color:#166534;border-radius:10px;font-size:11px;">notified</span>'
                            : '<span style="display:inline-block;padding:2px 8px;background:#fff7ed;color:#9a3412;border-radius:10px;font-size:11px;">subscribed</span>';
                ?>
                    <tr data-id="<?php echo (int) $sub->id; ?>">
                        <td><?php echo esc_html($sub->email); ?></td>
                        <td><a href="<?php echo esc_url($edit_url); ?>"><?php echo esc_html($title); ?></a> <span style="color:#646970;">#<?php echo (int) $sub->product_id; ?></span></td>
                        <td><?php echo (int) $sub->variation_id > 0 ? '#' . (int) $sub->variation_id : '—'; ?></td>
                        <td><?php echo wp_kses($status_pill, array('span' => array('class' => array(), 'style' => array()))); ?></td>
                        <td><?php echo esc_html($sub->created_at); ?></td>
                        <td><?php echo $sub->notified_at ? esc_html($sub->notified_at) : '—'; ?></td>
                        <td>
                            <button type="button" class="button button-small aieo-dmm-waitlist-delete" data-id="<?php echo (int) $sub->id; ?>">
                                <?php esc_html_e('Delete', 'ai-eshop-optimizer'); ?>
                            </button>
                        </td>
                    </tr>
                <?php endforeach; endif; ?>
            </tbody>
        </table>

        <?php
        $pages = max(1, (int) ceil($total / max(1, $per_page)));
        if ($pages > 1) :
            $base_url = add_query_arg(array('page' => 'ai-eshop-dmm', 'dmm_tab' => 'waitlist'), admin_url('admin.php'));
        ?>
            <p style="margin-top:12px;">
                <?php for ($p = 1; $p <= $pages; $p++) : $url = add_query_arg('waitlist_paged', $p, $base_url); ?>
                    <a href="<?php echo esc_url($url); ?>" class="button button-small<?php echo $p === $current_page ? ' button-primary' : ''; ?>"><?php echo (int) $p; ?></a>
                <?php endfor; ?>
            </p>
        <?php endif; ?>
    </div>
</div>

<script>
jQuery(function($){
    $(document).on('click', '.aieo-dmm-waitlist-delete', function(){
        if (!confirm('<?php echo esc_js(__('Delete this subscriber?', 'ai-eshop-optimizer')); ?>')) return;
        var $btn = $(this);
        var id = parseInt($btn.data('id'), 10);
        $.post(aieoDmm.ajaxurl, {
            action: 'aieo_dmm_waitlist_delete',
            nonce:  aieoDmm.nonce,
            id:     id
        }).done(function(resp){
            if (resp && resp.success) {
                $btn.closest('tr').fadeOut(150, function(){ $(this).remove(); });
            }
        });
    });
});
</script>
