<?php
require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../../includes/header.php';
$db = (new Database())->getConnection();
$db->exec("CREATE TABLE IF NOT EXISTS mkt_alert_log (alert_id INT AUTO_INCREMENT PRIMARY KEY, alert_type ENUM('오류','경고','정보','성공') DEFAULT '정보', module VARCHAR(50), message TEXT, resolved TINYINT(1) DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, resolved_at DATETIME, resolved_by INT)");

if (isset($_GET['resolve'])) {
    $user = currentUser();
    $db->prepare("UPDATE mkt_alert_log SET resolved=1, resolved_at=NOW(), resolved_by=? WHERE alert_id=?")->execute([$user['user_id'], (int)$_GET['resolve']]);
}

$alerts = $db->query("SELECT a.*, s.name AS resolver_name FROM mkt_alert_log a LEFT JOIN user_account u ON a.resolved_by=u.user_id LEFT JOIN staff s ON u.staff_id=s.staff_id ORDER BY a.resolved ASC, a.created_at DESC LIMIT 100")->fetchAll();
$unresolvedCount = $db->query("SELECT COUNT(*) FROM mkt_alert_log WHERE resolved=0")->fetchColumn();
?>
<div class="page-header"><h2>🛡️ 리스크 컨트롤</h2><div class="breadcrumb">마케팅 &gt; 리스크 컨트롤</div></div>

<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(10rem,1fr));gap:1rem;margin-bottom:1.25rem;">
    <div class="card" style="padding:1rem;text-align:center;"><div style="font-size:var(--fs-xs);color:var(--text-tertiary);">미해결 이슈</div><div style="font-size:var(--fs-2xl);font-weight:800;color:var(--accent-text);"><?= $unresolvedCount ?></div></div>
    <div class="card" style="padding:1rem;text-align:center;"><div style="font-size:var(--fs-xs);color:var(--text-tertiary);">전체 로그</div><div style="font-size:var(--fs-2xl);font-weight:800;color:var(--text-primary);"><?= count($alerts) ?></div></div>
</div>

<div class="card">
    <div class="card-header"><h3>알림 로그</h3></div>
    <div style="overflow-x:auto;">
    <table class="data-table">
        <thead><tr><th>유형</th><th>모듈</th><th>내용</th><th>발생일</th><th>상태</th><th>해결</th></tr></thead>
        <tbody>
        <?php foreach($alerts as $a): ?>
        <tr style="<?= !$a['resolved']?'background:rgba(var(--accent-rgb),0.05);':'' ?>">
            <td><span class="badge <?= ['오류'=>'badge-danger','경고'=>'badge-warning','정보'=>'badge-info','성공'=>'badge-success'][$a['alert_type']] ?>"><?= $a['alert_type'] ?></span></td>
            <td><?= htmlspecialchars($a['module']??'-') ?></td>
            <td style="font-size:var(--fs-sm);max-width:20rem;"><?= htmlspecialchars($a['message']) ?></td>
            <td style="font-size:var(--fs-xs);"><?= date('m/d H:i', strtotime($a['created_at'])) ?></td>
            <td><?= $a['resolved'] ? '<span class="badge badge-success">해결</span>' : '<span class="badge badge-danger">미해결</span>' ?></td>
            <td><?php if(!$a['resolved']): ?><a href="?resolve=<?= $a['alert_id'] ?>" class="btn btn-outline" style="font-size:var(--fs-xs);padding:0.2rem 0.5rem;" onclick="return confirm('해결 처리하시겠습니까?')">해결</a><?php else: ?><span style="font-size:var(--fs-xs);color:var(--text-tertiary);"><?= htmlspecialchars($a['resolver_name']??'') ?></span><?php endif; ?></td>
        </tr>
        <?php endforeach; ?>
        <?php if(!$alerts): ?><tr><td colspan="6" style="text-align:center;color:var(--text-tertiary);">알림이 없습니다</td></tr><?php endif; ?>
        </tbody>
    </table>
    </div>
</div>
<?php require_once __DIR__ . '/../../includes/footer.php'; ?>
