<?php
session_start();
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Pragma: no-cache");
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
$expire = 1800;
if (empty($_SESSION['user_id']) || (time() - $_SESSION['login_time'] > $expire)) {
    session_destroy();
    header("Location: login.php");
    exit;
}
$_SESSION['login_time'] = time();
$userName = htmlspecialchars($_SESSION['username']);

// 读取配置库
$dbHost = "10.150.117.190";
$dbUser = "root";
$dbPwd = "hp93000";
$dbName = "alert_mail_stat";
$conn = mysqli_connect($dbHost, $dbUser, $dbPwd, $dbName);
mysqli_set_charset($conn, "utf8mb4");
$currentLang = mysqli_fetch_assoc(mysqli_query($conn, "SELECT v FROM sys_config WHERE k='lang'"))['v'] ?? "zh";

// 多语言
$langMap = [
    'zh' => [
        'page_subtitle' => '爱德万测试 · SMTP邮箱配置',
        'smtp_config' => '邮箱SMTP配置',
        'smtp_host' => 'SMTP服务器',
        'smtp_port' => '端口',
        'smtp_ssl' => '启用SSL/TLS',
        'smtp_account' => '发件账号',
        'smtp_pwd' => '授权密码',
        'smtp_from' => '发件人邮箱',
        'save_smtp' => '保存配置',
        'save_success' => '保存成功',
    ],
    'en' => [
        'page_subtitle' => 'Advantest · SMTP Mail Config',
        'smtp_config' => 'SMTP Mail Config',
        'smtp_host' => 'SMTP Host',
        'smtp_port' => 'Port',
        'smtp_ssl' => 'Enable SSL/TLS',
        'smtp_account' => 'SMTP Account',
        'smtp_pwd' => 'Auth Password',
        'smtp_from' => 'Sender Email',
        'save_smtp' => 'Save Config',
        'save_success' => 'Saved successfully',
    ]
];
$t = $langMap[$currentLang];

// 保存提交
if ($_POST['act'] === "save_smtp") {
    $host = trim($_POST['smtp_host'] ?? '');
    $port = intval($_POST['smtp_port'] ?? 25);
    $ssl = intval($_POST['smtp_ssl'] ?? 0);
    $user = trim($_POST['smtp_user'] ?? '');
    $pwd = trim($_POST['smtp_pass'] ?? '');
    $from = trim($_POST['smtp_from'] ?? '');
    mysqli_query($conn, "UPDATE sys_smtp_config SET smtp_host='$host',smtp_port=$port,smtp_ssl=$ssl,smtp_user='$user',smtp_pass='$pwd',send_from='$from' WHERE id=1");
    echo "<script>alert('".$t['save_success']."');location.href='smtp_config.php';</script>";
    exit;
}
// 1. 先判断SQL不为空，避免空查询警告
$sql = "SELECT * FROM sys_smtp_config WHERE id = 1";
if (!empty($sql)) {
    $res = mysqli_query($conn, $sql);
    // 2. 判断查询成功且返回有效结果集再执行fetch
    if ($res instanceof mysqli_result) {
        $smtpRow = mysqli_fetch_assoc($res);
    } else {
        $smtpRow = [];
    }
} else {
    $smtpRow = [];
}
$smtpConfig = $smtpRow ?? [];
?>
<!DOCTYPE html>
<html lang="<?php echo $currentLang==='zh'?'zh-CN':'en'; ?>">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SMTP Config - Advantest</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:#f9fafc;font-family:"Inter","PingFang SC","Microsoft YaHei";color:#1d2939;line-height:1.6;display:flex;flex-direction:column;height:100vh;overflow:hidden;}
.wrap-main{display:flex;flex:1;overflow:hidden;}
.sidebar{width:240px;background:#152c5b;color:#e2e8f0;flex-shrink:0;overflow-y:auto;}
.sidebar-brand{padding:26px 24px;border-bottom:1px solid rgba(255,255,255,0.1);}
.sidebar-brand-en{font-size:20px;font-weight:700;letter-spacing:1px;color:#fff;}
.sidebar-brand-cn{font-size:13px;color:#a5b4fc;margin-top:4px;}
.sidebar-menu{padding:16px 0;}
.menu-title{padding:10px 24px;font-size:12px;color:#94a3b8;letter-spacing:1px;}
.menu-item{display:flex;align-items:center;gap:12px;padding:14px 24px;cursor:pointer;font-size:14px;transition:0.2s;color:#cbd5e1;}
.menu-item i{font-size:16px;width:20px;text-align:center;}
.menu-item.active{background:rgba(255,255,255,0.12);color:#fff;border-left:4px solid #3b82f6;}
.menu-item:hover:not(.active){background:rgba(255,255,255,0.06);color:#fff;}
.main-content{flex:1;overflow-y:auto;display:flex;flex-direction:column;}
.header{background:#ffffff;border-bottom:1px solid #eef2fb;color:#152c5b;padding:20px 40px;display:flex;justify-content:space-between;align-items:center;box-shadow:0 2px 12px rgba(21,44,91,0.05);flex-shrink:0;}
.header-left{display:flex;align-items:center;gap:14px;}
.header-brand-en{font-size:24px;font-weight:700;letter-spacing:2px;color:#152c5b;}
.header-brand-cn{font-size:15px;color:#64748b;}
.header-right{display:flex;align-items:center;gap:18px;font-size:14px;flex-wrap:wrap;}
.user-info{color:#475569;display:flex;align-items:center;gap:6px;}
.btn-base{border:none;padding:9px 20px;border-radius:8px;cursor:pointer;font-size:14px;display:flex;align-items:center;gap:6px;transition:all 0.2s ease;}
.btn-primary{background:#152c5b;color:#fff;}
.container{padding:36px 40px;flex:1;}
.base-card{background:#fff;border-radius:16px;padding:32px;box-shadow:0 3px 18px rgba(21,44,91,0.06);border:1px solid #eef2fb;max-width:700px;}
.title-wrap{display:flex;align-items:center;gap:12px;font-size:19px;font-weight:600;color:#152c5b;margin-bottom:26px;padding-bottom:16px;border-bottom:1px solid #eef2fb;}
.form-row{margin-bottom:20px;}
.form-row label{display:block;margin-bottom:8px;color:#475569;font-weight:500;}
.form-row input{width:100%;padding:10px 14px;border:1px solid #cbd5e1;border-radius:8px;font-size:14px;}
</style>
</head>
<body>
<div class="wrap-main">
    <div class="sidebar">
        <div class="sidebar-brand">
            <div class="sidebar-brand-en">Advantest</div>
            <div class="sidebar-brand-cn"><?php echo $t['page_subtitle']; ?></div>
        </div>
        <div class="sidebar-menu">
            <div class="menu-title">Monitor Panel</div>
            <div class="menu-item" onclick="location.href='dashboard.php'">
                <i class="fa fa-line-chart"></i><span>Alert Overview</span>
            </div>
            <div class="menu-item" onclick="location.href='work_order.php'">
                <i class="fa fa-ticket"></i><span>Work Order System</span>
            </div>
            <div class="menu-title">System Manage</div>
            <div class="menu-item" onclick="location.href='admin.php'">
                <i class="fa fa-cog"></i><span>System Config</span>
            </div>
            <div class="menu-item active">
                <i class="fa fa-envelope-o"></i><span><?php echo $t['smtp_config']; ?></span>
            </div>
        </div>
    </div>
    <div class="main-content">
        <div class="header">
            <div class="header-left">
                <div>
                    <div class="header-brand-en">Advantest</div>
                    <div class="header-brand-cn"><?php echo $t['page_subtitle']; ?></div>
                </div>
            </div>
            <div class="header-right">
                <span class="user-info"><i class="fa fa-user-circle"></i>User: <?php echo $userName; ?></span>
                <button class="btn-base btn-primary" onclick="location.href='logout.php'"><i class="fa fa-sign-out"></i>Logout</button>
            </div>
        </div>
        <div class="container">
            <div class="base-card">
                <div class="title-wrap"><i class="fa fa-envelope-o"></i><?php echo $t['smtp_config']; ?></div>
                <form method="post">
                    <input type="hidden" name="act" value="save_smtp">
                    <div class="form-row">
                        <label><?php echo $t['smtp_host']; ?></label>
                        <input name="smtp_host" value="<?php echo htmlspecialchars($cfg['smtp_host']); ?>">
                    </div>
                    <div class="form-row">
                        <label><?php echo $t['smtp_port']; ?></label>
                        <input name="smtp_port" type="number" value="<?php echo intval($cfg['smtp_port']); ?>">
                    </div>
                    <div class="form-row">
                        <label><?php echo $t['smtp_ssl']; ?></label>
                        <input type="checkbox" name="smtp_ssl" value="1" <?php echo $cfg['smtp_ssl']==1?'checked':''; ?>>
                    </div>
                    <div class="form-row">
                        <label><?php echo $t['smtp_account']; ?></label>
                        <input name="smtp_user" value="<?php echo htmlspecialchars($cfg['smtp_user']); ?>">
                    </div>
                    <div class="form-row">
                        <label><?php echo $t['smtp_pwd']; ?></label>
                        <input name="smtp_pass" type="password" value="<?php echo htmlspecialchars($cfg['smtp_pass']); ?>">
                    </div>
                    <div class="form-row">
                        <label><?php echo $t['smtp_from']; ?></label>
                        <input name="smtp_from" value="<?php echo htmlspecialchars($cfg['send_from']); ?>">
                    </div>
                    <button class="btn-base btn-primary" type="submit"><i class="fa fa-save"></i><?php echo $t['save_smtp']; ?></button>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
</html>
