<?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>
<form class="card dashboard-filterbar" method="get" data-dashboard-filter>
  <label class="dashboard-group-filter">Nach Gruppe filtern
    <select name="group" data-submit-on-change>
      <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>
  <label class="dashboard-search-filter">QR-Codes durchsuchen
    <input type="search" name="q" value="" placeholder="Durchsuche deine Codes" autocomplete="off" data-dashboard-search>
  </label>
</form>
<table class="table dashboard-table" data-dashboard-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']; $haystack=trim(implode(' ', [$r['title'] ?? '', $r['target_url'] ?? '', $r['code'] ?? '', $short, $r['group_name'] ?? '', $r['owner_email'] ?? '', (string)($r['scan_count'] ?? '')])); ?>
<tr data-dashboard-row data-search="<?= e(mb_strtolower($haystack, 'UTF-8')) ?>"><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; ?>
<tr data-dashboard-empty hidden><td colspan="5">Keine QR-Codes zur aktuellen Suche gefunden.</td></tr>
</tbody></table><?php page_footer(); ?>
