coupon/query_code.php

91 lines
3.4 KiB
PHP
Executable File

<?php
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/functions.php';
requireLogin();
$pageTitle = '查询兑换码';
require __DIR__ . '/includes/header.php';
$results = [];
$searched = false;
$q = trim($_GET['q'] ?? '');
$page = max(1, (int)($_GET['p'] ?? 1));
$perPage = 100;
$pid = getCurrentProductId();
if ($q !== '') {
$searched = true;
$offset = ($page - 1) * $perPage;
// 统计总数
$stmt = $pdo->prepare('SELECT COUNT(*) FROM redemption_codes WHERE product_id = ? AND (code = ? OR batch_no = ?)');
$stmt->execute([$pid, $q, $q]);
$total = (int)$stmt->fetchColumn();
$totalPages = max(1, ceil($total / $perPage));
// 分页查询
$stmt = $pdo->prepare("SELECT r.*, u.username FROM redemption_codes r LEFT JOIN users u ON r.claim_user_id = u.id WHERE r.product_id = ? AND (r.code = ? OR r.batch_no = ?) ORDER BY r.id DESC LIMIT ? OFFSET ?");
$stmt->execute([$pid, $q, $q, $perPage, $offset]);
$results = $stmt->fetchAll();
}
?>
<div class="card">
<h2>查询兑换码</h2>
<form method="get" style="display:flex;gap:8px;flex-wrap:wrap;">
<input type="text" name="q" class="form-control" placeholder="输入兑换码或批次号" value="<?= h($q) ?>" required style="flex:1;min-width:200px;">
<button type="submit" class="btn btn-primary">查询</button>
</form>
</div>
<?php if ($searched): ?>
<div class="card">
<h2>查询结果 <?php if ($total > 0): ?><span style="font-size:14px;color:#999;font-weight:normal;">(共 <?= $total ?> 条)</span><?php endif; ?></h2>
<?php if (empty($results)): ?>
<div class="alert alert-info">未找到匹配的兑换码</div>
<?php else: ?>
<div class="table-wrapper">
<table>
<thead>
<tr>
<th>序号</th>
<th>兑换码名称</th>
<th>批次号</th>
<th>类型</th>
<th>兑换码</th>
<th>面值</th>
<th>状态</th>
<th>过期时间</th>
<?php if (isAdmin()): ?>
<th>创建时间</th>
<th>用户</th>
<th>领取时间</th>
<?php endif; ?>
</tr>
</thead>
<tbody>
<?php $idx = $offset; foreach ($results as $r): $idx++; ?>
<tr>
<td><?= $idx ?></td>
<td><?= h($r['name']) ?></td>
<td><?= h($r['batch_no']) ?></td>
<td><?= h($r['type']) ?></td>
<td><code><?= h(maskCode($r['code'])) ?></code></td>
<td><?= h($r['value']) ?></td>
<td><?= claimStatusBadge((int)$r['status']) ?></td>
<td><?= formatDateTime($r['expired_at']) ?></td>
<?php if (isAdmin()): ?>
<td><?= formatDateTime($r['created_at']) ?></td>
<td><?= h($r['username'] ?? '-') ?></td>
<td><?= formatDateTime($r['claimed_at']) ?></td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php renderPagination($page, $totalPages, ['q' => $q]); ?>
<?php endif; ?>
</div>
<?php endif; ?>
<?php require __DIR__ . '/includes/footer.php'; ?>