<?php
/** @var array $events */
/** @var int $unread */
$typeLabel = [
    'sales_order'    => '新销售订单',
    'purchase_order' => '新采购订单',
    'customer_order' => '新客户订单',
    'low_stock'      => '低库存预警',
    'manual'         => '手动提醒',
];
?>
<div class="page-head">
    <div>
        <h2>紧急提醒</h2>
        <p class="muted">系统将“新订单 / 新事件”作为紧急事件推送；未读 <b style="color:#dc2626"><?php echo $unread; ?></b> 条。</p>
    </div>
    <div class="head-actions">
        <a class="btn-primary" href="<?php echo \Core\App::url('PSI/reminders/create'); ?>"><?php echo admin_icon('bell', 16); ?> 发起提醒</a>
        <?php if ($unread > 0): ?>
        <form method="post" action="<?php echo \Core\App::url('PSI/reminders/markAll'); ?>" class="inline-form">
            <?php echo csrf_field(); ?>
            <button class="btn-ghost" type="submit">全部标为已读</button>
        </form>
        <?php endif; ?>
    </div>
</div>

<?php if (empty($events)): ?>
    <div class="empty-box">暂无紧急事件。</div>
<?php else: ?>
    <div class="ev-list">
        <?php foreach ($events as $ev):
            $type = $ev['type'] ?? 'manual';
            $channels = json_decode($ev['channels'] ?? '[]', true) ?: [];
            $isRead = !empty($ev['_read']);
        ?>
        <div class="ev-card<?php echo $isRead ? ' read' : ''; ?>">
            <div class="ev-left">
                <span class="ev-dot"></span>
            </div>
            <div class="ev-main">
                <div class="ev-top">
                    <span class="ev-tag"><?php echo e($typeLabel[$type] ?? $type); ?></span>
                    <?php if (!$isRead): ?><span class="ev-new">未读</span><?php endif; ?>
                    <span class="ev-time"><?php echo e($ev['created_at'] ?? ''); ?></span>
                </div>
                <div class="ev-title"><?php echo e($ev['title'] ?? ''); ?></div>
                <div class="ev-body"><?php echo nl2br(e($ev['body'] ?? '')); ?></div>
                <div class="ev-foot">
                    <span class="ev-channels">
                        <?php foreach ($channels as $c): ?>
                            <span class="ch ch-<?php echo e($c); ?>"><?php echo e($c === 'inapp' ? '站内' : ($c === 'email' ? '邮件' : ($c === 'wechat' ? '微信' : $c))); ?></span>
                        <?php endforeach; ?>
                    </span>
                    <span class="ev-ops">
                        <?php if (!empty($ev['url'])): ?>
                            <a class="link-ok" href="<?php echo \Core\App::url($ev['url']); ?>">查看详情</a>
                        <?php endif; ?>
                        <?php if (!$isRead): ?>
                            <form method="post" action="<?php echo \Core\App::url('PSI/reminders/mark/' . $ev['id']); ?>" class="inline-form">
                                <?php echo csrf_field(); ?>
                                <button class="link-ok" type="submit">标为已读</button>
                            </form>
                        <?php else: ?>
                            <span class="muted">已读</span>
                        <?php endif; ?>
                    </span>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>
