<?php
require_once __DIR__.'/../app/bootstrap.php';
require_once __DIR__.'/_layout.php';
$user=require_login($pdo);
$groups=accessible_groups($pdo,(int)$user['id'], false);
$activeGroup=isset($_GET['group']) ? selected_group_id_view($pdo,(int)$user['id'],$_GET['group']) : null;
$accessWhere = qr_access_where('q');
$editExpr = 'CASE WHEN q.user_id=? OR EXISTS (SELECT 1 FROM '.qtbl('qr_group_shares').' es WHERE es.group_id=q.group_id AND es.user_id=? AND es.access_role="edit") THEN 1 ELSE 0 END AS can_edit';
if($activeGroup){
    $stmt=$pdo->prepare('SELECT q.*,g.name AS group_name,u.email AS owner_email,'.$editExpr.' FROM '.qtbl('qr_codes').' q LEFT JOIN '.qtbl('qr_groups').' g ON g.id=q.group_id LEFT JOIN '.qtbl('users').' u ON u.id=g.user_id WHERE q.group_id=? AND '.$accessWhere.' ORDER BY q.created_at DESC');
    $stmt->execute([$user['id'],$user['id'],$activeGroup,$user['id'],$user['id']]);
} else {
    $stmt=$pdo->prepare('SELECT q.*,g.name AS group_name,u.email AS owner_email,'.$editExpr.' FROM '.qtbl('qr_codes').' q LEFT JOIN '.qtbl('qr_groups').' g ON g.id=q.group_id LEFT JOIN '.qtbl('users').' u ON u.id=g.user_id WHERE '.$accessWhere.' ORDER BY COALESCE(g.name,""), q.created_at DESC');
    $stmt->execute([$user['id'],$user['id'],$user['id'],$user['id']]);
}
$rows=$stmt->fetchAll();
page_header('QR-Code Dashboard'); ?>
<p><a class="btn" href="create.php">Neuen QR-Code erstellen</a> <a class="btn secondary" href="groups.php">Gruppen verwalten</a></p>
<?php if($groups): ?><form class="card compact" method="get"><label>Nach Gruppe filtern<select name="group" onchange="this.form.submit()"><option value="0">Alle Gruppen</option><?php foreach($groups as $g): ?><option value="<?= (int)$g['id'] ?>" <?= $activeGroup===(int)$g['id']?'selected':'' ?>><?= e($g['name']) ?><?= !empty($g['owner_email']) ? ' · geteilt von '.e($g['owner_email']) : '' ?></option><?php endforeach; ?></select></label></form><?php endif; ?>
<table class="table"><thead><tr><th>Titel</th><th>Gruppe</th><th>Kurz-URL</th><th>Scans</th><th>Aktionen</th></tr></thead><tbody>
<?php foreach($rows as $r): $short=base_url($config).'/q/'.$r['code']; ?>
<tr><td><?php if((int)$r['can_edit']): ?><a class="qr-title-link" href="edit.php?id=<?= (int)$r['id'] ?>"><?= e($r['title']) ?></a><?php else: ?><strong><?= e($r['title']) ?></strong><?php endif; ?><div class="muted"><?= e($r['target_url']) ?></div></td><td><?= e($r['group_name'] ?? '–') ?><?php if(!empty($r['owner_email']) && (int)$r['user_id'] !== (int)$user['id']): ?><div class="muted">geteilt von <?= e($r['owner_email']) ?></div><?php endif; ?></td><td><a href="<?= e($short) ?>" target="_blank"><?= e($short) ?></a></td><td><?= (int)$r['scan_count'] ?></td><td class="actions"><?php if((int)$r['can_edit']): ?><a href="edit.php?id=<?= (int)$r['id'] ?>">Bearbeiten</a><?php endif; ?><a href="download.php?id=<?= (int)$r['id'] ?>">Download</a></td></tr>
<?php endforeach; if(!$rows): ?><tr><td colspan="5">Noch keine QR-Codes vorhanden.</td></tr><?php endif; ?>
</tbody></table><?php page_footer(); ?>
