<?php
require_once __DIR__ . '/../MPHX/common.php';

if (!is_admin_login()) {
    header('Location: /admin/login.php');
    exit;
}

$admin = is_admin_login();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>邮箱管理 - 宝塔主机管理系统</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/all.min.css">
    <link rel="stylesheet" href="css/style.css?v=2026040501">
</head>
<body>
    <div class="content-area">
        <div class="card">
            <h3><i class="fas fa-envelope"></i> 邮箱管理</h3>
            
            <div class="config-info">
                <h4><i class="fas fa-cog"></i> 当前配置</h4>
                <table class="table" style="margin-bottom: 0;">
                    <tbody>
                        <tr>
                            <td style="width: 100px; color: #5f6368;">邮箱服务器</td>
                            <td style="color: #202124;"><?php echo $conf['mailhost'] ?: '未配置'; ?></td>
                        </tr>
                        <tr>
                            <td style="color: #5f6368;">邮箱账号</td>
                            <td style="color: #202124;"><?php echo $conf['mailuser'] ?: '未配置'; ?></td>
                        </tr>
                        <tr>
                            <td style="color: #5f6368;">邮箱端口</td>
                            <td style="color: #202124;"><?php echo $conf['mailport'] ?: '465'; ?></td>
                        </tr>
                        <tr>
                            <td style="color: #5f6368;">密码状态</td>
                            <td style="color: #202124;"><?php echo $conf['mailpassword'] ? '已配置' : '未配置'; ?></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            
            <div class="alert">
                <i class="fas fa-info-circle"></i> 
                <strong>提示：</strong> 请先在"系统管理" -> "邮箱设置"中配置SMTP邮箱信息
            </div>
            
            <h4>测试邮件发送</h4>
            <form id="mailForm">
                <div class="form-group">
                    <label for="to">收件人</label>
                    <input type="email" class="form-control" id="to" name="to" placeholder="test@example.com" required>
                    <small>请输入收件人邮箱地址</small>
                </div>
                <div class="form-group">
                    <label for="subject">主题</label>
                    <input type="text" class="form-control" id="subject" name="subject" placeholder="测试邮件" required>
                    <small>请输入邮件主题</small>
                </div>
                <div class="form-group">
                    <label for="message">内容</label>
                    <textarea class="form-control" id="message" name="message" rows="5" placeholder="这是一封测试邮件" required></textarea>
                    <small>请输入邮件内容</small>
                </div>
                <button type="button" class="btn btn-primary" onclick="sendTestMail(this)">
                    <i class="fas fa-paper-plane"></i> 发送邮件
                </button>
            </form>
        </div>
    </div>
    
    <script src="/static/js/jquery.min.js"></script>
    <script>
    function sendTestMail(btn) {
        var $btn = $(btn);
        var originalText = $btn.html();
        $btn.addClass('btn-loading').prop('disabled', true);
        
        $.ajax({
            type: 'POST',
            url: 'ajax.php',
            data: $('#mailForm').serialize() + '&egn=sendtestmail',
            dataType: 'json',
            success: function(data) {
                $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                alert(data.code);
                if(data.code == '邮件发送成功') {
                    $('#mailForm')[0].reset();
                }
            },
            error: function() {
                $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                alert('操作失败，请重试');
            }
        });
    }
    
    $(document).ready(function() {
        $('#mailForm').on('submit', function(e) {
            e.preventDefault();
            sendTestMail($('button[type="button"]'));
        });
    });
    </script>
</body>
</html>