<?php
session_start();
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
$expire = 1800;
if (empty($_SESSION['user_id']) || (time() - $_SESSION['login_time'] > $expire)) {
    session_destroy();
    header("Location: login.php");
    exit;
}
$userName = $_SESSION['user_name'] ?? '';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>告警订阅管理</title>
<link rel="stylesheet" href="/static/css/font-awesome.min.css">
<script src="/static/js/jquery.min.js"></script>
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{background:#F5F7FA;font-family:"Microsoft YaHei",sans-serif;color:#1D2129;font-size:14px;}
.container{max-width:1000px;margin:0 auto;padding:20px;}
.header-bar{display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;}
.back-link{color:#165DFF;text-decoration:none;}
.user-info{display:flex;gap:12px;align-items:center;}
.card{background:#fff;border-radius:8px;padding:24px;box-shadow:0 1px 2px rgba(0,0,0,0.06);}
.card-title{font-size:16px;font-weight:500;margin-bottom:20px;}
.form-row{margin-bottom:18px;}
.form-row label{display:block;margin-bottom:6px;color:#4E5969;}
.form-row input{width:100%;height:38px;padding:0 14px;border:1px solid #DCDFE6;border-radius:6px;font-size:14px;}
.form-desc{font-size:12px;color:#86909C;margin-top:4px;}
.switch-row{display:flex;align-items:center;gap:8px;margin-bottom:24px;font-size:14px;}
.btn{height:38px;padding:0 20px;border:none;border-radius:6px;cursor:pointer;font-size:14px;display:flex;align-items:center;gap:6px;}
.btn-primary{background:#165DFF;color:#fff;}
.btn-outline{background:#fff;color:#4E5969;border:1px solid #DCDFE6;}
.btn-group{display:flex;gap:12px;}
.tip{color:#86909C;font-size:13px;margin-top:12px;}
</style>
</head>
<body>
<div class="container">
    <div class="header-bar">
        <a href="alert_index.php" class="back-link"><i class="fa fa-arrow-left"></i> 返回告警监控面板</a>
        <div class="user-info"><i class="fa fa-user"></i> <?php echo $userName; ?></div>
    </div>
    <div class="card">
        <h3 class="card-title">告警推送订阅配置</h3>
        <div class="switch-row">
            <input type="checkbox" id="subEnable">
            <label for="subEnable">全局启用告警推送订阅</label>
        </div>
        <div class="form-row">
            <label>订阅告警级别（多值逗号分隔，为空匹配全部）</label>
            <input type="text" id="subSeverity" placeholder="critical,warning">
            <div class="form-desc">可选：critical / warning / info</div>
        </div>
        <div class="form-row">
            <label>订阅实例模糊匹配（多实例逗号分隔）</label>
            <input type="text" id="subInstance" placeholder="10.150,192.168">
            <div class="form-desc">填写实例片段，为空订阅所有实例告警</div>
        </div>
        <div class="form-row">
            <label>订阅告警名称模糊匹配（多告警逗号分隔）</label>
            <input type="text" id="subAlertname" placeholder="CPU使用率过高,磁盘满">
            <div class="form-desc">只推送包含该名称的告警，为空订阅全部告警</div>
        </div>
        <div class="form-row">
            <label>钉钉机器人 Webhook</label>
            <input type="text" id="subDingtalk" placeholder="https://oapi.dingtalk.com/robot/send?access_token=xxx">
        </div>
        <div class="form-row">
            <label>企业微信机器人 Webhook</label>
            <input type="text" id="subWecom" placeholder="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx">
        </div>
        <div class="form-row">
            <label>接收邮箱（多邮箱逗号分隔）</label>
            <input type="text" id="subMail" placeholder="ops@company.com,admin@company.com">
        </div>
        <div class="btn-group">
            <button class="btn btn-primary" id="saveSubscribe"><i class="fa fa-save"></i> 保存订阅配置</button>
            <button class="btn btn-outline" id="resetForm"><i class="fa fa-refresh"></i> 重置表单</button>
        </div>
        <div class="tip">说明：配置保存后，符合筛选条件的新增告警将通过配置渠道推送；关闭全局开关则暂停所有推送。</div>
    </div>
</div>
<script>
const apiSub = "./api/subscribe_api.php";
// 页面加载读取当前订阅
$(function(){
    $.getJSON(apiSub,{action:"get_subscribe"},function(res){
        if(res.code===0){
            const d=res.data;
            $("#subEnable").prop("checked",d.enable==1);
            $("#subSeverity").val(d.severity);
            $("#subInstance").val(d.instance);
            $("#subAlertname").val(d.alertname);
            $("#subDingtalk").val(d.dingtalk_webhook);
            $("#subWecom").val(d.wecom_webhook);
            $("#subMail").val(d.mail_receiver);
        }
    });
});
// 保存配置
$("#saveSubscribe").click(function(){
    const data={
        severity:$("#subSeverity").val().trim(),
        instance:$("#subInstance").val().trim(),
        alertname:$("#subAlertname").val().trim(),
        dingtalk_webhook:$("#subDingtalk").val().trim(),
        wecom_webhook:$("#subWecom").val().trim(),
        mail_receiver:$("#subMail").val().trim(),
        enable:$("#subEnable").prop("checked")?1:0
    };
    $.ajax({
        url:apiSub+"?action=save_subscribe",
        method:"POST",
        contentType:"application/json",
        data:JSON.stringify(data),
        dataType:"json",
        success:function(res){alert(res.msg);}
    });
});
// 重置表单
$("#resetForm").click(function(){
    $("#subSeverity,#subInstance,#subAlertname,#subDingtalk,#subWecom,#subMail").val("");
    $("#subEnable").prop("checked",false);
});
</script>
</body>
</html>
