<?php session_start(); require __DIR__ . '/../config.php'; if(!isset($_SESSION['admin_id'])) header('Location: login.php'); $pdo=get_pdo(); if(isset($_GET['delete']) && intval($_GET['delete'])){ $id=intval($_GET['delete']); $pdo->prepare('DELETE FROM list_contacts WHERE id=?')->execute([$id]); header('Location: contacts.php'); exit;} if(isset($_GET['export']) && intval($_GET['export'])){ $list_id=intval($_GET['export']); $stmt=$pdo->prepare('SELECT email,name,unsubscribed,bounced,created_at FROM list_contacts WHERE list_id=?'); $stmt->execute([$list_id]); $rows=$stmt->fetchAll(); header('Content-Type:text/csv'); header('Content-Disposition: attachment; filename="contacts_list_'.$list_id.'.csv"'); $out=fopen('php://output','w'); fputcsv($out,['email','name','unsubscribed','bounced','created_at']); foreach($rows as $r) fputcsv($out,[$r['email'],$r['name'],$r['unsubscribed'],$r['bounced'],$r['created_at']]); fclose($out); exit;} $list_filter = isset($_GET['list'])?intval($_GET['list']):0; if($list_filter){ $stmt=$pdo->prepare('SELECT lc.*, l.name as list_name FROM list_contacts lc JOIN lists l ON l.id=lc.list_id WHERE lc.list_id=? ORDER BY lc.created_at DESC'); $stmt->execute([$list_filter]); $contacts=$stmt->fetchAll(); } else { $contacts=$pdo->query('SELECT lc.*, l.name as list_name FROM list_contacts lc JOIN lists l ON l.id=lc.list_id ORDER BY lc.created_at DESC')->fetchAll(); } $lists=$pdo->query('SELECT * FROM lists')->fetchAll(); ?><!doctype html><html><head><meta charset='utf-8'><title>Contacts</title><link rel='stylesheet' href='/assets/css/style.css'></head><body><nav class='top'><a href='dashboard.php'>Dashboard</a></nav><div class='container'><h1>Contacts</h1><form method='get'><label>Filter by List</label><select name='list' onchange='this.form.submit()'><option value='0'>All lists</option><?php foreach($lists as $l){ $sel = ($list_filter == $l['id']) ? 'selected' : ''; echo "<option value='{$l['id']}' $sel>".htmlspecialchars($l['name'])."</option>"; } ?></select></form><p><a class='btn' href='contacts.php?export=<?php echo $list_filter?:0; ?>'>Export CSV (current list)</a></p><table class='table'><tr><th>ID</th><th>Email</th><th>Name</th><th>List</th><th>Unsub</th><th>Bounced</th><th>Added</th><th>Action</th></tr><?php foreach($contacts as $c){ echo "<tr><td>".$c['id']."</td><td>".htmlspecialchars($c['email'])."</td><td>".htmlspecialchars($c['name'])."</td><td>".htmlspecialchars($c['list_name'])."</td><td>".($c['unsubscribed']? 'Yes':'No')."</td><td>".($c['bounced']? 'Yes':'No')."</td><td>".$c['created_at']."</td><td><a href='contacts.php?delete=".$c['id']."' onclick='return confirm("Are you sure?")'>Delete</a></td></tr>"; } ?></table></div></body></html>