<?php
require_once __DIR__ . '/../lib/audience_rules.php';
$id=crmId($_GET['id']??0); $search=trim($_GET['search']??'');
$date_from=trim($_GET['date_from']??'');
$date_to=trim($_GET['date_to']??''); $page_num=max(1,(int)($_GET['p']??1)); $limit=defined('CRM_DEFAULT_PER_PAGE')?CRM_DEFAULT_PER_PAGE:30; $offset=($page_num-1)*$limit;
$a=$conn->query("SELECT * FROM audience WHERE id=$id AND account_id=$account_id LIMIT 1")->fetch_assoc(); if(!$a){echo '<div class="alert alert-error">Audience not found</div>';return;}
$sw=$search!==''?" AND (c.customer_name LIKE '%".sanitize($conn,$search)."%' OR c.phone LIKE '%".sanitize($conn,$search)."%' OR c.email LIKE '%".sanitize($conn,$search)."%')":"";
if($date_from!=='') $sw .= " AND DATE(COALESCE(ac.created_at, ac.date_created)) >= '".sanitize($conn,$date_from)."'";
if($date_to!=='') $sw .= " AND DATE(COALESCE(ac.created_at, ac.date_created)) <= '".sanitize($conn,$date_to)."'";
$totalQ=$conn->query("SELECT COUNT(DISTINCT c.id) total FROM audience_customers ac JOIN customers c ON c.id=ac.customer_id WHERE ac.audience_id=$id AND c.account_id=$account_id $sw"); $total=$totalQ?(int)$totalQ->fetch_assoc()['total']:0; $pages=max(1,ceil($total/$limit));
$list=$conn->query("SELECT DISTINCT c.*, ac.created_at AS audience_created_at, ac.updated_at AS audience_updated_at, ac.date_created AS audience_date_created FROM audience_customers ac JOIN customers c ON c.id=ac.customer_id WHERE ac.audience_id=$id AND c.account_id=$account_id $sw ORDER BY c.customer_name LIMIT $offset,$limit");
?>
<div class="fade-in"><div class="toolbar" style="justify-content:space-between"><a href="index.php?page=audience" class="btn btn-secondary">← Back</a><div><strong><?= htmlspecialchars($a['name']) ?></strong> <span class="badge badge-blue"><?= $total ?> members</span></div></div>
<div class="card" style="padding:18px"><form method="get" class="audience-view-search"><input type="hidden" name="page" value="audience_view"><input type="hidden" name="id" value="<?= crmEid($id) ?>"><div><label class="field-label">Search</label><input class="form-control" name="search" value="<?= htmlspecialchars($search) ?>" placeholder="Search name, phone, email..."></div><div><label class="field-label">From Date</label><input type="date" class="form-control" name="date_from" value="<?= htmlspecialchars($date_from) ?>"></div><div><label class="field-label">To Date</label><input type="date" class="form-control" name="date_to" value="<?= htmlspecialchars($date_to) ?>"></div><button class="btn btn-primary">Search</button><?php if($search || $date_from || $date_to): ?><a class="btn btn-secondary" href="index.php?page=audience_view&id=<?= crmEid($id) ?>">Clear</a><?php endif; ?></form></div>
<div class="card"><div class="table-wrapper"><table class="crm-table"><thead><tr><th>Customer</th><th>Phone</th><th>Email</th><th>Added At</th><th>Updated At</th><th>Action</th></tr></thead><tbody><?php if(!$list||!$list->num_rows): ?><tr><td colspan="6" class="text-center text-muted">No customers found</td></tr><?php else: while($c=$list->fetch_assoc()): ?><tr><td><?= htmlspecialchars($c['customer_name']) ?></td><td><?= htmlspecialchars($c['phone']?:'—') ?></td><td><?= htmlspecialchars($c['email']?:'—') ?></td><td><?= formatDate($c['audience_created_at'] ?: $c['audience_date_created']) ?></td><td><?= formatDate($c['audience_updated_at'] ?: $c['audience_date_created']) ?></td><td><a class="btn btn-secondary btn-sm" href="index.php?page=customer_view&id=<?= crmEid($c['id']) ?>">View</a></td></tr><?php endwhile; endif; ?></tbody></table></div><div class="pagination" style="padding:14px;display:flex;justify-content:space-between"><span class="text-xs text-muted">Showing <?= $total?($offset+1):0 ?>-<?= min($offset+$limit,$total) ?> of <?= $total ?></span><span><?php for($i=1;$i<=$pages;$i++): ?><a class="btn btn-<?= $i==$page_num?'primary':'secondary' ?> btn-sm" href="index.php?page=audience_view&id=<?= crmEid($id) ?>&search=<?= urlencode($search) ?>&date_from=<?= urlencode($date_from) ?>&date_to=<?= urlencode($date_to) ?>&p=<?= $i ?>"><?= $i ?></a> <?php endfor; ?></span></div></div></div>
