<?php
/**
 * View: Contacts & Lists Manager (Premium UI)
 */

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

global $wpdb;

// Check if we are viewing a specific list's contacts
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$pexemail_view_list_id = isset( $_GET['list_id'] ) ? intval( wp_unslash( $_GET['list_id'] ) ) : 0;
$pexemail_current_list = null;
$pexemail_contacts = [];

if ( $pexemail_view_list_id > 0 ) {
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $pexemail_current_list = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}pexemail_lists WHERE id = %d", $pexemail_view_list_id ) );
    if ( $pexemail_current_list ) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $pexemail_contacts = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}pexemail_contacts WHERE list_id = %d ORDER BY id DESC", $pexemail_view_list_id ) );
    }
} else {
    // Fetch all lists with contact counts
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $pexemail_lists = $wpdb->get_results( "
        SELECT l.*, COUNT(c.id) as total_contacts 
        FROM {$wpdb->prefix}pexemail_lists l 
        LEFT JOIN {$wpdb->prefix}pexemail_contacts c ON l.id = c.list_id 
        GROUP BY l.id 
        ORDER BY l.id DESC
    " );
}
?>

<style>
    /* Global Reset & Typography */
    .pex-premium-wrap { 
        max-width: 1400px; 
        margin: 25px 0 0 0; 
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; 
        color: #334155; 
    }
    
    /* Hero Section (Brand Green) */
    .pex-hero { 
        background: linear-gradient(135deg, #0f5132 0%, #064e3b 100%); 
        color: #fff; 
        padding: 45px 50px; 
        border-radius: 16px; 
        margin-bottom: 35px; 
        box-shadow: 0 15px 35px -5px rgba(6, 78, 59, 0.3), inset 0 1px 0 rgba(255,255,255,0.1); 
        display: flex; 
        align-items: center; 
        justify-content: space-between;
        position: relative;
        overflow: hidden;
    }
    .pex-hero::after {
        content: '';
        position: absolute;
        top: 0; right: 0; bottom: 0; left: 0;
        background: url('data:image/svg+xml;utf8,<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="40" fill="rgba(255,255,255,0.03)"/></svg>') repeat;
        opacity: 0.8;
        pointer-events: none;
    }
    .pex-hero-text { position: relative; z-index: 2; }
    .pex-hero h1 { color: #fff; margin: 0 0 10px 0; font-size: 30px; font-weight: 800; letter-spacing: -0.5px; text-shadow: 0 2px 4px rgba(0,0,0,0.1); }
    .pex-hero p { margin: 0; font-size: 16px; color: #d1fae5; line-height: 1.6; max-width: 600px; font-weight: 400; }
    
    /* Buttons */
    .pex-btn-white { 
        position: relative; z-index: 2; background: #ffffff; color: #064e3b; padding: 14px 28px; border-radius: 30px; 
        font-weight: 700; font-size: 15px; text-decoration: none; box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); 
        transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); display: inline-flex; align-items: center; gap: 8px; border: none; cursor: pointer;
    }
    .pex-btn-white:hover { background: #f8fafc; color: #047857; transform: translateY(-2px); box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15); }

    .pex-btn-primary { 
        background: #0f172a; color: white; padding: 12px 24px; border-radius: 10px; font-weight: 600; font-size: 14px; 
        text-decoration: none; display: inline-flex; align-items: center; justify-content: center; transition: all 0.2s; border: none; cursor: pointer; box-shadow: 0 2px 4px rgba(15, 23, 42, 0.1); gap: 8px;
    }
    .pex-btn-primary:hover { background: #1e293b; transform: translateY(-1px); box-shadow: 0 4px 12px rgba(15, 23, 42, 0.15); color: white; }

    /* Action Pills (Tables) - Deep Premium Colors */
    .pex-action-pill { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 8px; font-size: 13px; font-weight: 600; text-decoration: none; transition: all 0.2s; border: none; cursor: pointer; color: #fff; box-shadow: 0 2px 4px rgba(0,0,0,0.05); }
    .pex-pill-view { background: #2563eb; color: #fff; }
    .pex-pill-view:hover { background: #1d4ed8; color: #fff; transform: translateY(-1px); }
    .pex-pill-edit { background: #475569; color: #fff; }
    .pex-pill-edit:hover { background: #334155; color: #fff; transform: translateY(-1px); }
    .pex-pill-del { background: #dc2626; color: #fff; }
    .pex-pill-del:hover { background: #b91c1c; color: #fff; transform: translateY(-1px); }

    /* Layout & Cards */
    .pex-grid { display: grid; grid-template-columns: 1fr 2fr; gap: 30px; }
    @media (max-width: 950px) { .pex-grid { grid-template-columns: 1fr; } }

    .pex-card { background: #ffffff; border-radius: 16px; border: 1px solid #eef2f6; box-shadow: 0 4px 6px -1px rgba(15, 23, 42, 0.02), 0 2px 4px -1px rgba(15, 23, 42, 0.02); overflow: hidden; margin-bottom: 30px; }
    .pex-card-header { padding: 24px 30px; border-bottom: 1px solid #f1f5f9; background: #ffffff; }
    .pex-card-header h2 { margin: 0; font-size: 18px; font-weight: 800; color: #0f172a; letter-spacing: -0.3px; }
    .pex-card-body { padding: 30px; }

    /* Form Styles */
    .pex-form-label { display: block; font-size: 13.5px; font-weight: 700; color: #1e293b; margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.5px; }
    .pex-input { width: 100%; padding: 12px 16px; border: 1px solid #cbd5e1; border-radius: 10px; font-size: 14.5px; outline: none; transition: all 0.2s; background: #f8fafc; color: #0f172a; box-shadow: inset 0 1px 2px rgba(0,0,0,0.01); box-sizing: border-box; margin-bottom: 15px; }
    .pex-input:focus { border-color: #10b981; background: #ffffff; box-shadow: 0 0 0 4px rgba(16, 185, 129, 0.15); }
    .pex-desc { font-size: 13px; color: #64748b; margin-bottom: 15px; line-height: 1.5; }

    /* Table Styles */
    .pex-table { width: 100%; border-collapse: collapse; font-size: 14.5px; }
    .pex-table th { background: #f8fafc; padding: 16px 24px; text-align: left; font-weight: 700; color: #475569; border-bottom: 1px solid #e2e8f0; font-size: 13px; text-transform: uppercase; letter-spacing: 0.5px; }
    .pex-table td { padding: 18px 24px; border-bottom: 1px solid #f1f5f9; vertical-align: middle; color: #334155; }
    .pex-table tr:hover td { background: #f8fafc; }
    .pex-table tr:last-child td { border-bottom: none; }

    /* Badges */
    .pex-badge { display: inline-flex; align-items: center; justify-content: center; background: #f1f5f9; color: #0f172a; padding: 6px 14px; border-radius: 20px; font-weight: 800; font-size: 13px; border: 1px solid #e2e8f0; }
    
    /* Tabs for Importing */
    .pex-tabs-wrapper { background: #f8fafc; padding: 6px; border-radius: 12px; display: flex; gap: 4px; margin-bottom: 25px; border: 1px solid #e2e8f0; }
    .pex-tab { flex: 1; text-align: center; padding: 12px 0; font-size: 14px; font-weight: 600; color: #64748b; cursor: pointer; border-radius: 8px; transition: all 0.2s; }
    .pex-tab.active { background: #ffffff; color: #0f172a; box-shadow: 0 2px 4px rgba(0,0,0,0.05); }
    .pex-tab:hover:not(.active) { color: #0f172a; }
    .pex-tab-content { display: none; }
    .pex-tab-content.active { display: block; }

    /* Import Progress Bar */
    .pex-progress-wrapper { display: none; margin-top: 20px; }
    .pex-progress-bg { background: #f1f5f9; border-radius: 20px; height: 12px; overflow: hidden; margin-bottom: 10px; border: 1px solid #e2e8f0; box-shadow: inset 0 1px 2px rgba(0,0,0,0.02); }
    .pex-progress-bar { background: #10b981; height: 100%; width: 0%; transition: width 0.3s ease; border-radius: 20px; }
    .pex-progress-text { font-size: 13px; font-weight: 600; color: #64748b; text-align: center; }

    /* Modal Overlay (Premium) */
    .pex-modal-overlay { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(15, 23, 42, 0.7); z-index: 99999; backdrop-filter: blur(4px); justify-content: center; align-items: center; }
    .pex-modal { background: #ffffff; width: 100%; max-width: 450px; border-radius: 16px; padding: 35px; box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25); position: relative; animation: modalIn 0.3s ease; }
    @keyframes modalIn { from { opacity: 0; transform: translateY(20px) scale(0.95); } to { opacity: 1; transform: translateY(0) scale(1); } }
    .pex-modal h3 { margin: 0 0 25px 0; font-size: 22px; font-weight: 800; color: #0f172a; }
    .pex-modal-close { position: absolute; top: 20px; right: 20px; background: #f1f5f9; border: none; width: 32px; height: 32px; border-radius: 50%; font-size: 18px; color: #64748b; cursor: pointer; transition: all 0.2s; display: flex; align-items: center; justify-content: center; }
    .pex-modal-close:hover { background: #e2e8f0; color: #0f172a; }

    /* Notices */
    .pex-error-notice { background: #fef2f2; border: 1px solid #fecaca; border-left: 4px solid #ef4444; padding: 16px 20px; border-radius: 10px; color: #991b1b; font-weight: 600; margin-bottom: 25px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.02); font-size: 14.5px; display: flex; align-items: center; gap: 10px; }

    /* Empty State */
    .pex-empty-state { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 70px 20px; text-align: center; }
    .pex-empty-icon-wrap { background: #f8fafc; border-radius: 50%; width: 80px; height: 80px; display: flex; align-items: center; justify-content: center; margin-bottom: 20px; box-shadow: inset 0 2px 4px rgba(0,0,0,0.02); border: 1px solid #f1f5f9; }
    .pex-empty-state h3 { font-size: 20px; color: #0f172a; margin: 0 0 10px 0; font-weight: 800; }
    .pex-empty-state p { color: #64748b; margin: 0 auto 0 auto; max-width: 400px; font-size: 15px; line-height: 1.6; }
</style>

<div class="wrap pex-premium-wrap">

    <?php if ( $pexemail_current_list ) : ?>
        <!-- ==========================================
             SUB-VIEW: VIEW & EDIT CONTACTS INSIDE A LIST 
             ========================================== -->
        <div class="pex-hero">
            <div class="pex-hero-text">
                <h1><?php echo esc_html( $pexemail_current_list->name ); ?></h1>
                <p><?php esc_html_e( 'Manage the subscribers inside this specific list.', 'pexally-email-campaigns' ); ?></p>
            </div>
            <div>
                <a href="?page=pexemail-contacts" class="pex-btn-white">
                    <svg width="18" height="18" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
                    <?php esc_html_e( 'Back to All Lists', 'pexally-email-campaigns' ); ?>
                </a>
            </div>
        </div>

        <div class="pex-card">
            <div class="pex-card-body" style="padding:0;">
                <table class="pex-table">
                    <thead>
                        <tr>
                            <th><?php esc_html_e( 'Email Address', 'pexally-email-campaigns' ); ?></th>
                            <th><?php esc_html_e( 'First Name', 'pexally-email-campaigns' ); ?></th>
                            <th><?php esc_html_e( 'Last Name', 'pexally-email-campaigns' ); ?></th>
                            <th style="text-align:right;"><?php esc_html_e( 'Action', 'pexally-email-campaigns' ); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if ( empty( $pexemail_contacts ) ) : ?>
                            <tr>
                                <td colspan="4">
                                    <div class="pex-empty-state">
                                        <div class="pex-empty-icon-wrap">
                                            <svg width="36" height="36" fill="none" stroke="#94a3b8" stroke-width="1.5" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"></path></svg>
                                        </div>
                                        <h3><?php esc_html_e( 'No contacts found', 'pexally-email-campaigns' ); ?></h3>
                                        <p><?php esc_html_e( 'This list is currently empty. Go back to upload a CSV or manually add contacts.', 'pexally-email-campaigns' ); ?></p>
                                    </div>
                                </td>
                            </tr>
                        <?php else : foreach ( $pexemail_contacts as $pexemail_contact ) : ?>
                            <tr>
                                <td style="font-weight:700; color: #0f172a;"><?php echo esc_html( $pexemail_contact->email ); ?></td>
                                <td><?php echo esc_html( $pexemail_contact->first_name ) ?: '-'; ?></td>
                                <td><?php echo esc_html( $pexemail_contact->last_name ) ?: '-'; ?></td>
                                <td style="text-align:right;">
                                    <button type="button" class="pex-action-pill pex-pill-edit pex-edit-contact" 
                                        data-id="<?php echo esc_attr( $pexemail_contact->id ); ?>"
                                        data-email="<?php echo esc_attr( $pexemail_contact->email ); ?>"
                                        data-fname="<?php echo esc_attr( $pexemail_contact->first_name ); ?>"
                                        data-lname="<?php echo esc_attr( $pexemail_contact->last_name ); ?>">
                                        <?php esc_html_e( 'Edit', 'pexally-email-campaigns' ); ?>
                                    </button>
                                    <button type="button" class="pex-action-pill pex-pill-del pex-delete-contact" data-id="<?php echo esc_attr( $pexemail_contact->id ); ?>">
                                        <?php esc_html_e( 'Delete', 'pexally-email-campaigns' ); ?>
                                    </button>
                                </td>
                            </tr>
                        <?php endforeach; endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

    <?php else : ?>
        <!-- ==========================================
             MAIN VIEW: MANAGE LISTS & IMPORTS
             ========================================== -->
        <div class="pex-hero">
            <div class="pex-hero-text">
                <h1><?php esc_html_e( 'Contacts & Lists Manager', 'pexally-email-campaigns' ); ?></h1>
                <p><?php esc_html_e( 'Create lists, import thousands of contacts instantly, and manage individual subscribers.', 'pexally-email-campaigns' ); ?></p>
            </div>
            <div>
                <!-- Decorative Icon for Hero -->
                <svg width="48" height="48" fill="none" stroke="#d1fae5" stroke-width="1.5" viewBox="0 0 24 24" style="opacity: 0.8;"><path stroke-linecap="round" stroke-linejoin="round" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"></path></svg>
            </div>
        </div>
        
        <div class="pex-grid">
            
            <!-- LEFT COLUMN: ADD CONTACTS & CREATE LIST -->
            <div>
                <!-- Create New List -->
                <div class="pex-card">
                    <div class="pex-card-header">
                        <h2><?php esc_html_e( '1. Create a New List', 'pexally-email-campaigns' ); ?></h2>
                    </div>
                    <div class="pex-card-body">
                        <label class="pex-form-label"><?php esc_html_e( 'List Name', 'pexally-email-campaigns' ); ?></label>
                        <div style="display:flex; gap:10px;">
                            <input type="text" id="new_list_name" class="pex-input" style="margin-bottom:0;" placeholder="e.g. Premium Customers">
                            <button type="button" id="btn_create_list" class="pex-btn-primary" style="white-space: nowrap;"><?php esc_html_e( 'Create List', 'pexally-email-campaigns' ); ?></button>
                        </div>
                    </div>
                </div>

                <!-- Add/Import Contacts -->
                <div class="pex-card">
                    <div class="pex-card-header">
                        <h2><?php esc_html_e( '2. Add Contacts to List', 'pexally-email-campaigns' ); ?></h2>
                    </div>
                    <div class="pex-card-body">
                        <?php if ( empty( $pexemail_lists ) ) : ?>
                            <div class="pex-error-notice">
                                <span>⚠️</span>
                                <div><?php esc_html_e( 'Please create a list first before adding contacts.', 'pexally-email-campaigns' ); ?></div>
                            </div>
                        <?php else : ?>
                            <label class="pex-form-label"><?php esc_html_e( 'Select Target List', 'pexally-email-campaigns' ); ?></label>
                            <select id="target_list_id" class="pex-input" style="margin-bottom: 25px;">
                                <?php foreach ( $pexemail_lists as $pexemail_list ) : ?>
                                    <option value="<?php echo esc_attr( $pexemail_list->id ); ?>"><?php echo esc_html( $pexemail_list->name ); ?></option>
                                <?php endforeach; ?>
                            </select>

                            <div class="pex-tabs-wrapper">
                                <div class="pex-tab active" data-target="tab-csv"><?php esc_html_e( 'Upload CSV', 'pexally-email-campaigns' ); ?></div>
                                <div class="pex-tab" data-target="tab-paste"><?php esc_html_e( 'Paste Emails', 'pexally-email-campaigns' ); ?></div>
                                <div class="pex-tab" data-target="tab-single"><?php esc_html_e( 'Add Single', 'pexally-email-campaigns' ); ?></div>
                            </div>

                            <div id="tab-csv" class="pex-tab-content active">
                                <div class="pex-desc">
                                    <?php esc_html_e( 'Upload a CSV file containing your contacts. Please ensure your columns are strictly ordered as: ', 'pexally-email-campaigns' ); ?>
                                    <strong>Email, First Name, Last Name</strong>.
                                </div>
                                <input type="file" id="csv_file" accept=".csv" class="pex-input" style="padding: 10px; background:#ffffff; border-style: dashed; border-width: 2px;">
                            </div>

                            <div id="tab-paste" class="pex-tab-content">
                                <div class="pex-desc"><?php esc_html_e( 'Paste a list of email addresses. Enter exactly one email per line.', 'pexally-email-campaigns' ); ?></div>
                                <textarea id="paste_emails" class="pex-input" rows="6" placeholder="john.doe@example.com&#10;jane.smith@domain.com"></textarea>
                            </div>

                            <div id="tab-single" class="pex-tab-content">
                                <input type="email" id="single_email" class="pex-input" placeholder="Email Address *">
                                <input type="text" id="single_fname" class="pex-input" placeholder="First Name (Optional)">
                                <input type="text" id="single_lname" class="pex-input" placeholder="Last Name (Optional)" style="margin-bottom:0;">
                            </div>

                            <!-- Import Progress Bar -->
                            <div class="pex-progress-wrapper" id="import_progress_wrap">
                                <div class="pex-progress-bg">
                                    <div class="pex-progress-bar" id="import_progress_bar"></div>
                                </div>
                                <div class="pex-progress-text" id="import_progress_text">Preparing data for import...</div>
                            </div>

                            <button type="button" id="btn_import_contacts" class="pex-btn-primary" style="margin-top: 25px; width: 100%; padding: 14px; font-size: 15px;">
                                <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4"></path></svg>
                                <?php esc_html_e( 'Import Contacts', 'pexally-email-campaigns' ); ?>
                            </button>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <!-- RIGHT COLUMN: MANAGE LISTS -->
            <div class="pex-card">
                <div class="pex-card-header">
                    <h2><?php esc_html_e( 'Manage Existing Lists', 'pexally-email-campaigns' ); ?></h2>
                </div>
                <div class="pex-card-body" style="padding:0;">
                    <table class="pex-table">
                        <thead>
                            <tr>
                                <th><?php esc_html_e( 'List Name', 'pexally-email-campaigns' ); ?></th>
                                <th><?php esc_html_e( 'Total Contacts', 'pexally-email-campaigns' ); ?></th>
                                <th style="text-align:right;"><?php esc_html_e( 'Action', 'pexally-email-campaigns' ); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ( empty( $pexemail_lists ) ) : ?>
                                <tr>
                                    <td colspan="3">
                                        <div class="pex-empty-state">
                                            <div class="pex-empty-icon-wrap">
                                                <svg width="36" height="36" fill="none" stroke="#94a3b8" stroke-width="1.5" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"></path></svg>
                                            </div>
                                            <h3><?php esc_html_e( 'No Lists Found', 'pexally-email-campaigns' ); ?></h3>
                                            <p><?php esc_html_e( 'Use the form on the left to create your first subscriber list.', 'pexally-email-campaigns' ); ?></p>
                                        </div>
                                    </td>
                                </tr>
                            <?php else : foreach ( $pexemail_lists as $pexemail_list ) : ?>
                                <tr>
                                    <td style="font-weight:700; color:#0f172a;">
                                        <span id="list-name-<?php echo esc_attr( $pexemail_list->id ); ?>"><?php echo esc_html( $pexemail_list->name ); ?></span>
                                    </td>
                                    <td>
                                        <span class="pex-badge"><?php echo esc_html( number_format_i18n( $pexemail_list->total_contacts ) ); ?></span>
                                    </td>
                                    <td style="text-align:right; white-space:nowrap;">
                                        <a href="?page=pexemail-contacts&list_id=<?php echo esc_attr( $pexemail_list->id ); ?>" class="pex-action-pill pex-pill-view"><?php esc_html_e( 'View', 'pexally-email-campaigns' ); ?></a>
                                        <button type="button" class="pex-action-pill pex-pill-edit pex-edit-list" data-id="<?php echo esc_attr( $pexemail_list->id ); ?>"><?php esc_html_e( 'Rename', 'pexally-email-campaigns' ); ?></button>
                                        <button type="button" class="pex-action-pill pex-pill-del pex-delete-list" data-id="<?php echo esc_attr( $pexemail_list->id ); ?>"><?php esc_html_e( 'Delete', 'pexally-email-campaigns' ); ?></button>
                                    </td>
                                </tr>
                            <?php endforeach; endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

        </div>

    <?php endif; ?>

</div>

<!-- The Edit Contact Modal (Premium Style) -->
<div id="pex-edit-contact-modal" class="pex-modal-overlay">
    <div class="pex-modal">
        <button type="button" class="pex-modal-close" onclick="document.getElementById('pex-edit-contact-modal').style.display='none';">
            <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"></path></svg>
        </button>
        <h3><?php esc_html_e( 'Edit Contact', 'pexally-email-campaigns' ); ?></h3>
        
        <input type="hidden" id="edit_c_id">
        
        <label class="pex-form-label">Email Address</label>
        <input type="email" id="edit_c_email" class="pex-input">
        
        <label class="pex-form-label">First Name</label>
        <input type="text" id="edit_c_fname" class="pex-input">
        
        <label class="pex-form-label">Last Name</label>
        <input type="text" id="edit_c_lname" class="pex-input" style="margin-bottom: 25px;">
        
        <button type="button" id="btn_save_contact" class="pex-btn-primary" style="width:100%; padding: 14px; font-size: 15px;">
            <?php esc_html_e( 'Save Changes', 'pexally-email-campaigns' ); ?>
        </button>
    </div>
</div>

<script>
// UI Tab Controller Logic
document.addEventListener('DOMContentLoaded', function() {
    const tabs = document.querySelectorAll('.pex-tab');
    const contents = document.querySelectorAll('.pex-tab-content');
    
    tabs.forEach(tab => {
        tab.addEventListener('click', () => {
            tabs.forEach(t => t.classList.remove('active'));
            contents.forEach(c => c.classList.remove('active'));
            
            tab.classList.add('active');
            const target = document.getElementById(tab.getAttribute('data-target'));
            if(target) target.classList.add('active');
        });
    });
});
</script>