<?php
require_once dirname(__DIR__,2).'/core/lib/admin_notifications.php';$adminId=intval($_SESSION['admin_id']??0);$items=cmsFilterNotificationsByAccess(cmsApplyNotificationReadState($db,$adminId,cmsCollectAdminNotifications($db,dirname(__DIR__,2))),(array)($adminAccess??[]));
if($_SERVER['REQUEST_METHOD']==='POST'){requireAdminCsrfToken(true);while(ob_get_level()>0)ob_end_clean();header('Content-Type: application/json; charset=utf-8');$action=trim((string)($_POST['notification_action']??''));if($action==='read'){foreach($items as$item){if($item['key']===$_POST['key']&&hash_equals($item['fingerprint'],(string)($_POST['fingerprint']??'')))cmsMarkNotificationRead($db,$adminId,$item['key'],$item['fingerprint']);}}elseif($action==='read_all'){foreach($items as$item)cmsMarkNotificationRead($db,$adminId,$item['key'],$item['fingerprint']);}else{echo json_encode(['code'=>400,'msg'=>'无效操作']);exit;}echo json_encode(['code'=>200,'msg'=>'已标记为已读'],JSON_UNESCAPED_UNICODE);exit;}
$unread=cmsUnreadNotificationCount($items);$severityLabels=['high'=>'重要','medium'=>'关注','low'=>'提示'];?>
<div class="page-container fade-in notification-page"><div class="page-header"><div class="page-title"><div class="page-icon"><i class="fa fa-bell-o"></i></div><div><h1>通知中心</h1><p>聚合需要管理员关注和处理的真实业务状态</p></div></div><div class="page-actions"><?php if($unread): ?><button class="btn btn-secondary" onclick="readAll(this)">全部已读</button><?php endif; ?><button class="btn btn-primary" onclick="CMSAdminUI.reload(this,'刷新中')"><i class="fa fa-refresh"></i> 刷新</button></div></div>
<div class="notification-summary"><div><small>当前通知</small><strong><?php echo count($items); ?></strong><span>条</span></div><div><small>未读通知</small><strong><?php echo $unread; ?></strong><span>条</span></div><div><small>重要事项</small><strong><?php echo count(array_filter($items,function($i){return$i['severity']==='high';})); ?></strong><span>条</span></div><div><small>需要关注</small><strong><?php echo count(array_filter($items,function($i){return$i['severity']==='medium';})); ?></strong><span>条</span></div></div>
<div class="notification-list"><?php if(!$items): ?><div class="cms-ui-empty"><div><span class="cms-ui-empty-icon"><i class="fa fa-check"></i></span><h3>当前没有待处理通知</h3><p>系统会持续聚合待审核、后台任务、404、审计和环境状态。</p></div></div><?php endif; ?><?php foreach($items as$item): ?><article class="notification-item is-<?php echo$item['severity']; ?> <?php echo$item['read']?'is-read':'is-unread'; ?>"><span class="notification-dot"></span><div class="notification-main"><div><span class="notification-source"><?php echo htmlspecialchars($item['source']); ?></span><span class="notification-level"><?php echo$severityLabels[$item['severity']]; ?></span><?php if(!$item['read']): ?><em>未读</em><?php endif; ?></div><h2><?php echo htmlspecialchars($item['title']); ?></h2><p><?php echo htmlspecialchars($item['description']); ?></p></div><div class="notification-actions"><a class="btn btn-primary" href="<?php echo htmlspecialchars($item['url']); ?>" onclick="return openNotification(this,'<?php echo$item['key']; ?>','<?php echo$item['fingerprint']; ?>')">立即处理</a><?php if(!$item['read']): ?><button class="btn btn-secondary" onclick="markRead(this,'<?php echo$item['key']; ?>','<?php echo$item['fingerprint']; ?>')">标为已读</button><?php endif; ?></div></article><?php endforeach; ?></div></div>
<style>.notification-page{max-width:1300px}.notification-summary{display:grid;grid-template-columns:repeat(4,1fr);gap:12px;margin-bottom:16px}.notification-summary>div{padding:16px 18px;border:1px solid #e2e8f0;border-radius:14px;background:#fff}.notification-summary small,.notification-summary span{display:block;color:#94a3b8;font-size:11px}.notification-summary strong{display:inline-block;margin:5px 7px 2px 0;color:#1e293b;font-size:23px}.notification-list{overflow:hidden;border:1px solid #dfe6ef;border-radius:16px;background:#fff}.notification-item{display:grid;grid-template-columns:10px minmax(0,1fr) auto;gap:16px;align-items:center;padding:20px;border-bottom:1px solid #edf2f7}.notification-item:last-child{border-bottom:0}.notification-item.is-read{background:#fbfcfe;opacity:.76}.notification-dot{width:9px;height:9px;border-radius:50%;background:#94a3b8}.notification-item.is-high .notification-dot{background:#ef4444;box-shadow:0 0 0 5px #fee2e2}.notification-item.is-medium .notification-dot{background:#f59e0b;box-shadow:0 0 0 5px #fef3c7}.notification-item.is-low .notification-dot{background:#3b82f6;box-shadow:0 0 0 5px #dbeafe}.notification-main>div{display:flex;align-items:center;gap:8px}.notification-source,.notification-level,.notification-main em{padding:4px 7px;border-radius:7px;background:#f1f5f9;color:#64748b;font-size:10px;font-style:normal}.notification-level{background:#fff7ed;color:#c2410c}.notification-main em{background:#eff6ff;color:#1d4ed8}.notification-main h2{margin:8px 0 4px;color:#25324a;font-size:15px}.notification-main p{margin:0;color:#64748b;font-size:12px}.notification-actions{display:flex;gap:8px}.notification-empty{display:flex;flex-direction:column;align-items:center;padding:65px;color:#94a3b8}.notification-empty i{font-size:40px;color:#10b981}.notification-empty strong{margin-top:12px;color:#334155}.notification-empty span{margin-top:5px;font-size:12px}@media(max-width:800px){.notification-summary{grid-template-columns:repeat(2,1fr)}.notification-item{grid-template-columns:10px 1fr}.notification-actions{grid-column:2;flex-wrap:wrap}}@media(max-width:500px){.notification-summary{grid-template-columns:1fr}}</style>
<script>function notifyPost(data){return fetch('?act=notification',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:data+'&csrf_token=<?php echo rawurlencode(getAdminCsrfToken()); ?>'}).then(function(r){return r.json();});}function markRead(button,key,fingerprint){CMSAdminUI.setLoading(button,true,'处理中');notifyPost('notification_action=read&key='+encodeURIComponent(key)+'&fingerprint='+encodeURIComponent(fingerprint)).then(function(d){if(d.code===200){CMSAdminUI.flash('通知已标记为已读','success');location.reload();return;}CMSAdminUI.toast(d.msg,'error');CMSAdminUI.setLoading(button,false);}).catch(function(){CMSAdminUI.toast('操作失败，请稍后重试','error');CMSAdminUI.setLoading(button,false);});}function openNotification(link,key,fingerprint){var url=link.href;CMSAdminUI.setLoading(link,true,'打开中');notifyPost('notification_action=read&key='+encodeURIComponent(key)+'&fingerprint='+encodeURIComponent(fingerprint)).finally(function(){location.href=url;});return false;}function readAll(button){CMSAdminUI.setLoading(button,true,'处理中');notifyPost('notification_action=read_all').then(function(d){if(d.code===200){CMSAdminUI.flash(d.msg,'success');location.reload();return;}CMSAdminUI.toast(d.msg,'error');CMSAdminUI.setLoading(button,false);}).catch(function(){CMSAdminUI.toast('操作失败，请稍后重试','error');CMSAdminUI.setLoading(button,false);});}</script>
