Files
2026-08-14 10:55:06 +08:00

133 lines
5.6 KiB
PHP

<?php
// 后台仪表盘
require __DIR__ . '/../includes/init.php';
requireStaff();
require '_common.php';
$db = db();
$isAdmin = isAdminStaff();
$me = currentStaff();
$openTickets = $db->query("SELECT COUNT(*) FROM " . tn('tickets') . " WHERE status IN ('open','pending')")->fetchColumn();
$myOpenTickets = 0;
if (!$isAdmin) {
// 客服:仅统计分配给自己的待处理工单
$stmt = $db->prepare("SELECT COUNT(*) FROM " . tn('tickets') . " WHERE status IN ('open','pending') AND assignee_id = ?");
$stmt->execute([$me['id']]);
$myOpenTickets = (int) $stmt->fetchColumn();
}
$recentTickets = $db->query(
'SELECT t.*, u.username FROM ' . tn('tickets') . ' t
LEFT JOIN ' . tn('users') . ' u ON u.id = t.user_id
' . ($isAdmin ? '' : ' WHERE t.assignee_id = ' . (int)$me['id']) . '
ORDER BY t.updated_at DESC LIMIT 5'
)->fetchAll();
$statCards = [];
if ($isAdmin) {
$onSale = $db->query('SELECT COUNT(*) FROM ' . tn('products') . ' WHERE status = 1')->fetchColumn();
$orderCnt = $db->query('SELECT COUNT(*) FROM ' . tn('orders'))->fetchColumn();
$sales = $db->query("SELECT COALESCE(SUM(total),0) FROM " . tn('orders') . " WHERE status IN ('paid','shipped','completed')")->fetchColumn();
$pending = $db->query("SELECT COUNT(*) FROM " . tn('orders') . " WHERE status = 'paid'")->fetchColumn();
$annCnt = $db->query('SELECT COUNT(*) FROM ' . tn('announcements') . ' WHERE status = 1')->fetchColumn();
$groupCnt = $db->query('SELECT COUNT(*) FROM ' . tn('product_groups'))->fetchColumn();
$statCards[] = ['num' => (int)$onSale, 'label' => '在售商品'];
$statCards[] = ['num' => (int)$orderCnt, 'label' => '累计订单'];
$statCards[] = ['num' => money($sales), 'label' => '销售总额'];
$statCards[] = ['num' => (int)$pending, 'label' => '等待发货', 'warn' => true];
$statCards[] = ['num' => (int)$annCnt, 'label' => '发布公告'];
$statCards[] = ['num' => (int)$groupCnt, 'label' => '商品分组'];
}
$statCards[] = ['num' => ($isAdmin ? (int)$openTickets : $myOpenTickets), 'label' => ($isAdmin ? '待处理工单' : '我待处理工单'), 'warn' => true];
adminHeader('仪表盘', 'index');
?>
<h1 class="page-title"><i class="fas fa-tachometer-alt"></i> 仪表盘</h1>
<div class="stat-grid">
<?php foreach ($statCards as $c): ?>
<div class="stat-card<?= !empty($c['warn']) ? ' warn' : '' ?>"><div class="stat-num"><?= $c['num'] ?></div><div class="stat-label"><?= h($c['label']) ?></div></div>
<?php endforeach; ?>
</div>
<?php if ($isAdmin): ?>
<h2 class="sec-title">最新订单</h2>
<?php
$recent = $db->query(
'SELECT o.*, u.username FROM ' . tn('orders') . ' o
LEFT JOIN ' . tn('users') . ' u ON u.id = o.user_id
ORDER BY o.created_at DESC LIMIT 5'
)->fetchAll();
?>
<?php if (empty($recent)): ?>
<p class="empty">暂无订单。</p>
<?php else: ?>
<table class="data-table">
<thead><tr><th>订单号</th><th>用户</th><th>金额</th><th>状态</th><th>时间</th></tr></thead>
<tbody>
<?php foreach ($recent as $o): ?>
<tr>
<td><?= h($o['order_no']) ?></td>
<td><?= h($o['username'] ?? '游客') ?></td>
<td><?= money($o['total']) ?></td>
<td><span class="badge-status status-<?= h($o['status']) ?>"><?= orderStatusLabel($o['status']) ?></span></td>
<td><?= date('m-d H:i', strtotime($o['created_at'])) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<a href="orders.php" class="btn btn-ghost">查看全部订单 →</a>
<?php endif; ?>
<?php endif; ?>
<h2 class="sec-title"><?= $isAdmin ? '最新工单' : '我负责的工单' ?></h2>
<?php if (empty($recentTickets)): ?>
<p class="empty">暂无工单。</p>
<?php else: ?>
<table class="data-table">
<thead><tr><th>编号</th><th>标题</th><th>提交人</th><th>状态</th><th>更新</th><th></th></tr></thead>
<tbody>
<?php foreach ($recentTickets as $t): ?>
<tr>
<td>#<?= (int)$t['id'] ?></td>
<td><?= h($t['subject']) ?></td>
<td><?= h($t['username'] ?? '游客') ?></td>
<td><span class="badge-status status-<?= h($t['status']) ?>"><?= ticketStatusLabel($t['status']) ?></span></td>
<td><?= date('m-d H:i', strtotime($t['updated_at'])) ?></td>
<td><a href="tickets.php?view=<?= (int)$t['id'] ?>" class="mini-btn">处理</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<a href="tickets.php" class="btn btn-ghost">查看全部工单 →</a>
<?php endif; ?>
<?php if ($isAdmin): ?>
<h2 class="sec-title">最新公告</h2>
<?php
$recentAnn = $db->query('SELECT * FROM ' . tn('announcements') . ' ORDER BY pinned DESC, created_at DESC LIMIT 5')->fetchAll();
?>
<?php if (empty($recentAnn)): ?>
<p class="empty">暂无公告。</p>
<?php else: ?>
<table class="data-table">
<thead><tr><th>标题</th><th>置顶</th><th>时间</th></tr></thead>
<tbody>
<?php foreach ($recentAnn as $a): ?>
<tr>
<td><?= h($a['title']) ?><?= !empty($a['pinned']) ? ' <i class="fas fa-thumbtack" style="color:var(--primary)"></i>' : '' ?></td>
<td><?= !empty($a['pinned']) ? '是' : '否' ?></td>
<td><?= date('m-d H:i', strtotime($a['created_at'])) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<a href="announcements.php" class="btn btn-ghost">管理公告 →</a>
<?php endif; ?>
<?php endif; ?>
<?php adminFooter(); ?>