<?php
ob_start();
/**
 * Tom's Java Jive - Admin Customers Management
 */

$pageTitle = 'Customers';
require_once __DIR__ . '/includes/header.php';

// Handle actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'create') {
        $email = strtolower(trim($_POST['email'] ?? ''));
        $name = trim($_POST['name'] ?? '');
        $phone = trim($_POST['phone'] ?? '');
        $walletBalance = floatval($_POST['wallet_balance'] ?? 0);
        $rewardPoints = intval($_POST['reward_points'] ?? 0);
        
        if (empty($email)) {
            setFlash('error', 'Email is required');
        } else {
            $existing = db()->fetch("SELECT id FROM customers WHERE email = :email", ['email' => $email]);
            if ($existing) {
                setFlash('error', 'Customer with this email already exists');
            } else {
                db()->insert('customers', [
                    'customer_id' => generateId('cust_'),
                    'email' => $email,
                    'name' => $name ?: null,
                    'phone' => $phone ?: null,
                    'wallet_balance' => $walletBalance,
                    'reward_points' => $rewardPoints,
                    'is_active' => 1
                ]);
                setFlash('success', 'Customer created successfully');
            }
        }
        header('Location: /admin/customers.php');
        exit;
    }
    
    if ($action === 'update' && !empty($_POST['customer_id'])) {
        $customerId = $_POST['customer_id'];
        $name = trim($_POST['name'] ?? '');
        $phone = trim($_POST['phone'] ?? '');
        $walletBalance = floatval($_POST['wallet_balance'] ?? 0);
        $rewardPoints = intval($_POST['reward_points'] ?? 0);
        $isActive = isset($_POST['is_active']) ? 1 : 0;
        
        db()->update('customers', [
            'name' => $name ?: null,
            'phone' => $phone ?: null,
            'wallet_balance' => $walletBalance,
            'reward_points' => $rewardPoints,
            'is_active' => $isActive
        ], 'customer_id = :id', ['id' => $customerId]);
        
        setFlash('success', 'Customer updated successfully');
        header('Location: /admin/customers.php');
        exit;
    }
    
    if ($action === 'delete' && !empty($_POST['customer_id'])) {
        db()->delete('customers', 'customer_id = :id', ['id' => $_POST['customer_id']]);
        setFlash('success', 'Customer deleted');
        header('Location: /admin/customers.php');
        exit;
    }
    
    if ($action === 'adjust_wallet' && !empty($_POST['customer_id'])) {
        $amount = floatval($_POST['amount'] ?? 0);
        $reason = trim($_POST['reason'] ?? '');
        
        if ($amount != 0) {
            db()->query(
                "UPDATE customers SET wallet_balance = wallet_balance + :amt WHERE customer_id = :id",
                ['amt' => $amount, 'id' => $_POST['customer_id']]
            );
            
            // Log transaction
            db()->insert('wallet_transactions', [
                'transaction_id' => generateId('wt_'),
                'customer_id' => $_POST['customer_id'],
                'amount' => $amount,
                'type' => $amount > 0 ? 'deposit' : 'withdrawal',
                'description' => $reason ?: 'Admin adjustment',
                'balance_after' => db()->fetch("SELECT wallet_balance FROM customers WHERE customer_id = :id", ['id' => $_POST['customer_id']])['wallet_balance'] ?? 0
            ]);
            
            setFlash('success', 'Wallet adjusted by $' . number_format($amount, 2));
        }
        header('Location: /admin/customers.php');
        exit;
    }
}

// Filters
$search = $_GET['search'] ?? '';
$status = $_GET['status'] ?? '';
$page = max(1, intval($_GET['page'] ?? 1));

$where = ['1=1'];
$params = [];

if ($search) {
    $where[] = '(email LIKE :search OR name LIKE :search OR phone LIKE :search)';
    $params['search'] = '%' . $search . '%';
}

if ($status === 'active') {
    $where[] = 'is_active = 1';
} elseif ($status === 'inactive') {
    $where[] = 'is_active = 0';
}

$whereClause = implode(' AND ', $where);
$total = db()->count('customers', $whereClause, $params);
$pagination = paginate($total, $page, ADMIN_ITEMS_PER_PAGE);

$customers = db()->fetchAll(
    "SELECT c.customer_id, c.email, c.name, c.phone, c.wallet_balance, c.reward_points, c.is_active, c.created_at,
            COALESCE((SELECT COUNT(*) FROM orders o WHERE o.customer_id = c.customer_id), 0) as order_count,
            COALESCE((SELECT SUM(total) FROM orders o WHERE o.customer_id = c.customer_id AND o.payment_status = 'paid'), 0) as total_spent
     FROM customers c
     WHERE {$whereClause}
     ORDER BY c.created_at DESC
     LIMIT " . (int)$pagination['per_page'] . " OFFSET " . (int)$pagination['offset'],
    $params
);

// Stats
$totalCustomers = db()->count('customers');
$activeCustomers = db()->count('customers', 'is_active = 1');
$totalWalletBalance = (float)(db()->fetch("SELECT COALESCE(SUM(wallet_balance),0) as total FROM customers")['total'] ?? 0);
?>

<div class="page-header">
    <h1 class="page-title">Customers</h1>
    <button class="btn btn-primary" onclick="openCustomerModal()">
        <i class="fas fa-plus"></i> Add Customer
    </button>
</div>

<?php if (hasFlash('success')): ?>
    <div class="alert alert-success"><i class="fas fa-check-circle"></i> <?= getFlash('success') ?></div>
<?php endif; ?>
<?php if (hasFlash('error')): ?>
    <div class="alert alert-error"><i class="fas fa-exclamation-circle"></i> <?= getFlash('error') ?></div>
<?php endif; ?>

<!-- Stats -->
<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-card-icon primary"><i class="fas fa-users"></i></div>
        <div>
            <div class="stat-card-value"><?= $totalCustomers ?></div>
            <div class="stat-card-label">Total Customers</div>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-card-icon success"><i class="fas fa-user-check"></i></div>
        <div>
            <div class="stat-card-value"><?= $activeCustomers ?></div>
            <div class="stat-card-label">Active</div>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-card-icon warning"><i class="fas fa-wallet"></i></div>
        <div>
            <div class="stat-card-value"><?= formatCurrency($totalWalletBalance) ?></div>
            <div class="stat-card-label">Total Wallet Balance</div>
        </div>
    </div>
</div>

<!-- Filters -->
<div class="admin-card">
    <div class="admin-card-body">
        <form method="GET" style="display: flex; gap: 1rem; flex-wrap: wrap; align-items: end;">
            <div class="form-group mb-0" style="flex: 1; min-width: 200px;">
                <label class="form-label">Search</label>
                <input type="text" name="search" class="form-input" placeholder="Email, name, phone..." value="<?= htmlspecialchars($search) ?>">
            </div>
            <div class="form-group mb-0">
                <label class="form-label">Status</label>
                <select name="status" class="form-select">
                    <option value="">All</option>
                    <option value="active" <?= $status === 'active' ? 'selected' : '' ?>>Active</option>
                    <option value="inactive" <?= $status === 'inactive' ? 'selected' : '' ?>>Inactive</option>
                </select>
            </div>
            <button type="submit" class="btn btn-secondary"><i class="fas fa-filter"></i> Filter</button>
            <?php if ($search || $status): ?>
                <a href="/admin/customers.php" class="btn btn-secondary">Clear</a>
            <?php endif; ?>
        </form>
    </div>
</div>

<!-- Table -->
<div class="admin-card">
    <div class="admin-card-header">
        <span><?= $total ?> customers found</span>
    </div>
    <div class="admin-card-body" style="padding: 0;">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Customer</th>
                    <th>Phone</th>
                    <th>Orders</th>
                    <th>Total Spent</th>
                    <th>Wallet</th>
                    <th>Points</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($customers)): ?>
                    <tr><td colspan="8" class="text-muted" style="text-align: center; padding: 2rem;">No customers found</td></tr>
                <?php else: ?>
                    <?php foreach ($customers as $customer): ?>
                        <tr>
                            <td>
                                <strong><?= htmlspecialchars($customer['name'] ?? 'No Name') ?></strong><br>
                                <small class="text-muted"><?= htmlspecialchars($customer['email']) ?></small>
                            </td>
                            <td><?= htmlspecialchars($customer['phone'] ?? '-') ?></td>
                            <td><?= $customer['order_count'] ?? 0 ?></td>
                            <td><?= formatCurrency($customer['total_spent'] ?? 0) ?></td>
                            <td>
                                <strong class="<?= ($customer['wallet_balance'] ?? 0) > 0 ? 'text-success' : '' ?>">
                                    <?= formatCurrency($customer['wallet_balance'] ?? 0) ?>
                                </strong>
                            </td>
                            <td><?= $customer['reward_points'] ?? 0 ?></td>
                            <td>
                                <?php if ($customer['is_active']): ?>
                                    <span class="badge badge-success">Active</span>
                                <?php else: ?>
                                    <span class="badge badge-error">Inactive</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <button class="btn btn-sm btn-secondary" onclick='openCustomerModal(<?= json_encode($customer) ?>)' title="Edit">
                                    <i class="fas fa-edit"></i>
                                </button>
                                <button class="btn btn-sm btn-secondary" onclick='openWalletModal("<?= $customer['customer_id'] ?>", "<?= htmlspecialchars($customer['name'] ?? $customer['email']) ?>", <?= $customer['wallet_balance'] ?? 0 ?>)' title="Adjust Wallet">
                                    <i class="fas fa-wallet"></i>
                                </button>
                                <form method="POST" style="display: inline;">
                                    <input type="hidden" name="action" value="delete">
                                    <input type="hidden" name="customer_id" value="<?= $customer['customer_id'] ?>">
                                    <button type="submit" class="btn btn-sm btn-danger" data-confirm="Delete this customer?">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php if ($pagination['total_pages'] > 1): ?>
    <div style="display: flex; justify-content: center; margin-top: 1rem;">
        <?= renderPagination($pagination, '/admin/customers.php?search=' . urlencode($search) . '&status=' . $status) ?>
    </div>
<?php endif; ?>

<!-- Customer Modal -->
<div class="modal-overlay" id="customerModal">
    <div class="modal" style="max-width:680px;width:95vw">
        <div class="modal-header">
            <h3 class="modal-title" id="customerModalTitle">Add Customer</h3>
            <button type="button" class="modal-close" onclick="Modal.close('customerModal')">&times;</button>
        </div>
        <div id="customerTabs" style="display:none;border-bottom:1px solid var(--color-border);padding:0 1.5rem">
            <button type="button" onclick="switchCTab('details')" id="ctab-details"
                style="padding:.6rem 1rem;border:none;border-bottom:2px solid var(--color-primary);background:none;cursor:pointer;font-weight:600;color:var(--color-primary);margin-bottom:-1px">
                <i class="fas fa-user"></i> Details
            </button>
            <button type="button" onclick="switchCTab('orders')" id="ctab-orders"
                style="padding:.6rem 1rem;border:none;border-bottom:2px solid transparent;background:none;cursor:pointer;font-weight:600;color:var(--color-text-muted);margin-bottom:-1px">
                <i class="fas fa-shopping-bag"></i> Orders
                <span id="cOrderBadge" style="background:var(--color-border);border-radius:10px;padding:1px 7px;font-size:.75rem;margin-left:3px"></span>
            </button>
            <button type="button" onclick="switchCTab('emails')" id="ctab-emails"
                style="padding:.6rem 1rem;border:none;border-bottom:2px solid transparent;background:none;cursor:pointer;font-weight:600;color:var(--color-text-muted);margin-bottom:-1px">
                <i class="fas fa-envelope-open-text"></i> Emails
                <span id="cEmailBadge" style="background:var(--color-border);border-radius:10px;padding:1px 7px;font-size:.75rem;margin-left:3px"></span>
            </button>
        </div>
        <div id="cpanel-details">
            <form method="POST" id="customerForm">
                <div class="modal-body">
                    <input type="hidden" name="action" id="customerAction" value="create">
                    <input type="hidden" name="customer_id" id="customerId">
                    <div class="form-group">
                        <label class="form-label">Email *</label>
                        <input type="email" name="email" id="customerEmail" class="form-input" required>
                    </div>
                    <div class="form-group">
                        <label class="form-label">Name</label>
                        <input type="text" name="name" id="customerName" class="form-input">
                    </div>
                    <div class="form-group">
                        <label class="form-label">Phone</label>
                        <input type="tel" name="phone" id="customerPhone" class="form-input">
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">Wallet Balance ($)</label>
                            <input type="number" name="wallet_balance" id="customerWallet" class="form-input" step="0.01" min="0" value="0">
                        </div>
                        <div class="form-group">
                            <label class="form-label">Reward Points</label>
                            <input type="number" name="reward_points" id="customerPoints" class="form-input" min="0" value="0">
                        </div>
                    </div>
                    <div class="form-group mb-0" id="statusGroup" style="display:none">
                        <label style="display:flex;align-items:center;gap:.5rem;cursor:pointer">
                            <input type="checkbox" name="is_active" id="customerActive" checked>
                            Active
                        </label>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" onclick="Modal.close('customerModal')">Cancel</button>
                    <button type="submit" class="btn btn-primary" id="customerSubmitBtn">Add Customer</button>
                </div>
            </form>
        </div>
        <div id="cpanel-orders" style="display:none">
            <div class="modal-body" style="padding:0;max-height:440px;overflow-y:auto">
                <div id="cOrdLoading" style="text-align:center;padding:2rem;color:var(--color-text-muted)">
                    <i class="fas fa-spinner fa-spin"></i> Loading orders...
                </div>
                <div id="cOrdContent" style="display:none">
                    <div id="cOrdEmpty" style="display:none;text-align:center;padding:2rem;color:var(--color-text-muted)">
                        <i class="fas fa-shopping-bag" style="font-size:2rem;opacity:.3;display:block;margin-bottom:.5rem"></i>
                        No orders yet
                    </div>
                    <table id="cOrdTable" style="width:100%;border-collapse:collapse;display:none">
                        <thead>
                            <tr style="background:var(--color-surface)">
                                <th style="padding:.6rem 1rem;font-size:.75rem;color:var(--color-text-muted);text-transform:uppercase;text-align:left">Order</th>
                                <th style="padding:.6rem 1rem;font-size:.75rem;color:var(--color-text-muted);text-transform:uppercase;text-align:left">Date</th>
                                <th style="padding:.6rem 1rem;font-size:.75rem;color:var(--color-text-muted);text-transform:uppercase;text-align:left">Total</th>
                                <th style="padding:.6rem 1rem;font-size:.75rem;color:var(--color-text-muted);text-transform:uppercase;text-align:left">Status</th>
                                <th style="padding:.6rem 1rem"></th>
                            </tr>
                        </thead>
                        <tbody id="cOrdBody"></tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" onclick="Modal.close('customerModal')">Close</button>
                <a id="cOrdViewAll" href="/admin/orders.php" class="btn btn-primary" target="_blank">View All Orders</a>
            </div>
        </div>

        <!-- Emails Panel -->
        <div id="cpanel-emails" style="display:none">
            <div class="modal-body" style="padding-top:.5rem">
                <div id="cEmailLoading" style="text-align:center;padding:2rem;color:var(--color-text-muted)">
                    <i class="fas fa-spinner fa-spin"></i> Loading emails...
                </div>
                <div id="cEmailContent" style="display:none">
                    <div id="cEmailEmpty" style="display:none;text-align:center;padding:2rem;color:var(--color-text-muted)">
                        <i class="fas fa-envelope" style="font-size:2rem;margin-bottom:.5rem;display:block"></i>
                        No emails sent to this customer yet.
                    </div>
                    <table id="cEmailTable" style="width:100%;border-collapse:collapse;display:none;font-size:.85rem">
                        <thead>
                            <tr style="border-bottom:2px solid var(--color-border)">
                                <th style="padding:.5rem;text-align:left">Subject</th>
                                <th style="padding:.5rem;text-align:left">Status</th>
                                <th style="padding:.5rem;text-align:left">Opened</th>
                                <th style="padding:.5rem;text-align:left">Sent</th>
                            </tr>
                        </thead>
                        <tbody id="cEmailBody"></tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <a id="cEmailViewAll" href="#" class="btn btn-primary" target="_blank">
                    <i class="fas fa-external-link-alt"></i> View All Emails
                </a>
            </div>
        </div>
    </div>
</div>
<style>
.cord-row td{padding:.65rem 1rem;border-bottom:1px solid var(--color-border);font-size:.875rem;vertical-align:middle}
.cord-row:hover td{background:var(--color-surface)}
.cord-detail{display:none}
.cord-detail.open{display:table-row}
.cord-detail td{padding:.75rem 1rem 1rem 1.5rem;border-bottom:1px solid var(--color-border);background:var(--color-surface)}
</style>
<script>
// switchCTab defined above
function _switchCTabDummy(tab){
    ['details','orders','emails'].forEach(function(t){
        document.getElementById('ctab-'+t).style.borderBottomColor=t===tab?'var(--color-primary)':'transparent';
        document.getElementById('ctab-'+t).style.color=t===tab?'var(--color-primary)':'var(--color-text-muted)';
        document.getElementById('cpanel-'+t).style.display=t===tab?'':'none';
    });
    if(tab==='orders'&&_cCustId&&!_cLoaded)loadCOrders(_cCustId);
}
function loadCOrders(cid){
    _cLoaded=true;
    document.getElementById('cOrdLoading').style.display='block';
    document.getElementById('cOrdContent').style.display='none';
    fetch('/admin/api/customer-orders.php?customer_id='+encodeURIComponent(cid))
    .then(function(r){return r.json();})
    .then(function(data){
        document.getElementById('cOrdLoading').style.display='none';
        document.getElementById('cOrdContent').style.display='block';
        var orders=data.orders||[];
        document.getElementById('cOrderBadge').textContent=orders.length;
        if(!orders.length){
            document.getElementById('cOrdEmpty').style.display='block';
            document.getElementById('cOrdTable').style.display='none';
            return;
        }
        document.getElementById('cOrdEmpty').style.display='none';
        document.getElementById('cOrdTable').style.display='table';
        var tb=document.getElementById('cOrdBody');
        tb.innerHTML='';
        var sc={pending:'warning',processing:'primary',confirmed:'primary',shipped:'primary',delivered:'success',cancelled:'error',refunded:'error'};
        orders.forEach(function(o){
            var tr=document.createElement('tr');
            tr.className='cord-row';
            tr.innerHTML='<td><strong>#'+o.order_number+'</strong></td>'+
                '<td>'+new Date(o.created_at).toLocaleDateString()+'</td>'+
                '<td><strong>$'+parseFloat(o.total).toFixed(2)+'</strong></td>'+
                '<td><span class="badge badge-'+(sc[o.order_status]||'primary')+'">'+o.order_status+'</span></td>'+
                '<td><button type="button" class="btn btn-sm btn-secondary" onclick="toggleCOrd(''+o.order_id+'')">'+
                '<i class="fas fa-chevron-down" id="cico-'+o.order_id+'"></i></button></td>';
            tb.appendChild(tr);
            var items=[];
            try{items=JSON.parse(o.items||'[]');}catch(e){}
            var html='<div style="font-weight:600;margin-bottom:.4rem">Items</div>';
            if(items.length){
                items.forEach(function(it){
                    html+='<div style="display:flex;justify-content:space-between;padding:.2rem 0;font-size:.8rem;border-bottom:1px solid var(--color-border)">'+
                        '<span>'+(it.name||it.product_name||'Item')+' &times; '+(it.quantity||1)+'</span>'+
                        '<span>$'+parseFloat(it.total||it.price||0).toFixed(2)+'</span></div>';
                });
            }else{html+='<div style="color:var(--color-text-muted);font-size:.8rem">No item details available</div>';}
            html+='<div style="display:flex;justify-content:space-between;font-weight:700;margin-top:.4rem;padding-top:.4rem;border-top:2px solid var(--color-border)">'+
                '<span>Total</span><span>$'+parseFloat(o.total).toFixed(2)+'</span></div>';
            if(o.tracking_number)html+='<div style="margin-top:.4rem;font-size:.8rem"><strong>Tracking:</strong> '+o.tracking_number+'</div>';
            html+='<div style="margin-top:.6rem"><a href="/admin/order.php?id='+o.order_id+'" class="btn btn-sm btn-primary" target="_blank">'+
                '<i class="fas fa-external-link-alt"></i> Open Order</a></div>';
            var dr=document.createElement('tr');
            dr.className='cord-detail';
            dr.id='cdet-'+o.order_id;
            dr.innerHTML='<td colspan="5">'+html+'</td>';
            tb.appendChild(dr);
        });
    })
    .catch(function(){
        document.getElementById('cOrdLoading').innerHTML='<div style="color:var(--color-error);text-align:center;padding:1rem"><i class="fas fa-exclamation-circle"></i> Failed to load orders</div>';
    });
}
function toggleCOrd(id){
    var d=document.getElementById('cdet-'+id);
    var i=document.getElementById('cico-'+id);
    d.classList.toggle('open');
    i.className=d.classList.contains('open')?'fas fa-chevron-up':'fas fa-chevron-down';
}
</script>

<div class="modal-overlay" id="walletModal">
    <div class="modal">
        <div class="modal-header">
            <h3 class="modal-title">Adjust Wallet Balance</h3>
            <button type="button" class="modal-close" onclick="Modal.close('walletModal')">&times;</button>
        </div>
        <form method="POST">
            <div class="modal-body">
                <input type="hidden" name="action" value="adjust_wallet">
                <input type="hidden" name="customer_id" id="walletCustomerId">
                
                <p>Customer: <strong id="walletCustomerName"></strong></p>
                <p>Current Balance: <strong id="walletCurrentBalance"></strong></p>
                
                <div class="form-group">
                    <label class="form-label">Adjustment Amount</label>
                    <input type="number" name="amount" class="form-input" step="0.01" required placeholder="Use negative to deduct">
                    <small class="text-muted">Positive to add, negative to subtract</small>
                </div>
                
                <div class="form-group mb-0">
                    <label class="form-label">Reason</label>
                    <input type="text" name="reason" class="form-input" placeholder="e.g., Refund, Bonus, Correction">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" onclick="Modal.close('walletModal')">Cancel</button>
                <button type="submit" class="btn btn-primary">Apply Adjustment</button>
            </div>
        </form>
    </div>
</div>

<script>
var _cCustId = null;
var _cLoaded = false;

function loadCustomerEmails(customerId) {
    if (!customerId) return;
    document.getElementById('cEmailLoading').style.display = 'block';
    document.getElementById('cEmailContent').style.display = 'none';
    var colors = {"sent":"#3B82F6","delivered":"#10B981","bounced":"#EF4444","failed":"#EF4444","unknown":"#9CA3AF"};
    fetch('/admin/api/customer-emails.php?customer_id=' + encodeURIComponent(customerId))
        .then(r => r.json()).then(function(data) {
            document.getElementById('cEmailLoading').style.display = 'none';
            document.getElementById('cEmailContent').style.display = 'block';
            var emails = data.emails || [];
            document.getElementById('cEmailBadge').textContent = emails.length;
            document.getElementById('cEmailViewAll').href = '/admin/email-log.php?customer=' + encodeURIComponent(customerId);
            if (!emails.length) {
                document.getElementById('cEmailEmpty').style.display = 'block';
                document.getElementById('cEmailTable').style.display = 'none';
                return;
            }
            document.getElementById('cEmailEmpty').style.display = 'none';
            document.getElementById('cEmailTable').style.display = 'table';
            var html = '';
            emails.forEach(function(e) {
                var color = colors[e.status] || '#9CA3AF';
                var badge = '<span style="background:'+color+';color:white;padding:2px 7px;border-radius:3px;font-size:.75rem;">'+e.status+'</span>';
                var opened = e.opened == '1' ? '<span style="color:#10B981">✓'+(e.open_count > 1 ? ' ('+e.open_count+')' : '')+'</span>' : '<span style="color:#9CA3AF">—</span>';
                html += '<tr style="border-bottom:1px solid var(--color-border)">'
                    + '<td style="padding:.5rem;max-width:180px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="'+e.subject+'">'+e.subject+'</td>'
                    + '<td style="padding:.5rem">'+badge+'</td>'
                    + '<td style="padding:.5rem">'+opened+'</td>'
                    + '<td style="padding:.5rem;white-space:nowrap;color:#666">'+e.sent_at+'</td>'
                    + '</tr>';
            });
            document.getElementById('cEmailBody').innerHTML = html;
        }).catch(function() {
            document.getElementById('cEmailLoading').innerHTML = '<div style="color:var(--color-error)"><i class="fas fa-exclamation-circle"></i> Failed to load emails</div>';
        });
}

function switchCTab(tab) {
    ['details','orders','emails'].forEach(function(t) {
        var tabBtn = document.getElementById('ctab-' + t);
        var panel  = document.getElementById('cpanel-' + t);
        if (tabBtn) {
            tabBtn.style.borderBottomColor = t === tab ? 'var(--color-primary)' : 'transparent';
            tabBtn.style.color = t === tab ? 'var(--color-primary)' : 'var(--color-text-muted)';
        }
        if (panel) panel.style.display = t === tab ? '' : 'none';
    });
    if (tab === 'orders' && _cCustId && !_cLoaded) loadCOrders(_cCustId);
    if (tab === 'emails' && _cCustId) loadCustomerEmails(_cCustId);
}


function openCustomerModal(customer = null) {
    const isEdit = !!customer;
    _cCustId = isEdit ? customer.customer_id : null;
    _cLoaded = false;

    document.getElementById('customerModalTitle').textContent = isEdit ? 'Edit Customer' : 'Add Customer';
    document.getElementById('customerSubmitBtn').textContent = isEdit ? 'Update Customer' : 'Add Customer';
    document.getElementById('customerAction').value = isEdit ? 'update' : 'create';
    document.getElementById('customerId').value = isEdit ? customer.customer_id : '';
    document.getElementById('customerEmail').value = isEdit ? customer.email : '';
    document.getElementById('customerEmail').readOnly = isEdit;
    document.getElementById('customerName').value = isEdit ? (customer.name || '') : '';
    document.getElementById('customerPhone').value = isEdit ? (customer.phone || '') : '';
    document.getElementById('customerWallet').value = isEdit ? (customer.wallet_balance || 0) : 0;
    document.getElementById('customerPoints').value = isEdit ? (customer.reward_points || 0) : 0;
    document.getElementById('customerActive').checked = isEdit ? customer.is_active : true;
    document.getElementById('statusGroup').style.display = isEdit ? 'block' : 'none';
    document.getElementById('customerTabs').style.display = isEdit ? 'flex' : 'none';
    document.getElementById('cOrderBadge').textContent = '';
    switchCTab('details');
    Modal.open('customerModal');
}

function openWalletModal(customerId, name, balance) {
    document.getElementById('walletCustomerId').value = customerId;
    document.getElementById('walletCustomerName').textContent = name;
    document.getElementById('walletCurrentBalance').textContent = '$' + parseFloat(balance).toFixed(2);
    Modal.open('walletModal');
}
</script>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
