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

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

$admin = is_admin_login();

// 处理 AJAX 提交
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['name'])) {
    header('Content-Type: application/json');
    
    $name = isset($_POST['name']) ? trim($_POST['name']) : '';
    $email = isset($_POST['email']) ? trim($_POST['email']) : '';
    $phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';
    $subject = isset($_POST['subject']) ? trim($_POST['subject']) : '';
    $message = isset($_POST['message']) ? trim($_POST['message']) : '';
    
    if (empty($name) || empty($email) || empty($message)) {
        echo json_encode(array('status' => false, 'msg' => '请填写必填项（姓名、邮箱、反馈内容）'));
        exit;
    }
    
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo json_encode(array('status' => false, 'msg' => '请输入有效的邮箱地址'));
        exit;
    }
    
    // 连接到官网数据库
    require_once __DIR__ . '/../dre/includes/Database.php';
    $db = new Database();
    
    $name = $db->escape($name);
    $email = $db->escape($email);
    $phone = $db->escape($phone);
    $subject = $db->escape($subject);
    $message = $db->escape($message);
    
    $sql = "INSERT INTO dre_feedback (name, email, phone, subject, message, source, status, created_at) 
            VALUES ('$name', '$email', '$phone', '$subject', '$message', 'project_admin', 'pending', NOW())";
    
    if ($db->query($sql)) {
        echo json_encode(array('status' => true, 'msg' => '反馈提交成功！官网管理员会尽快查看。'));
    } else {
        echo json_encode(array('status' => false, 'msg' => '提交失败，请稍后重试'));
    }
    exit;
}
?>
<!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>提交反馈 - MLEOE 宝塔系统</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-paper-plane"></i> 提交用户反馈</h3>
            
            <div class="alert alert-info" style="margin-bottom: 20px;">
                <i class="fas fa-info-circle"></i> <strong>使用说明：</strong>
                当您收集到用户使用项目程序时遇到的问题或建议时，请通过此表单提交到官网。
                官网管理员会查看所有反馈，并在后续版本中改进。
            </div>
            
            <form id="feedbackForm">
                <div class="form-group">
                    <label for="name">反馈人姓名 <span style="color: red;">*</span></label>
                    <input type="text" name="name" id="name" class="form-control" placeholder="请输入姓名" required>
                </div>
                
                <div class="form-group">
                    <label for="email">反馈人邮箱 <span style="color: red;">*</span></label>
                    <input type="email" name="email" id="email" class="form-control" placeholder="请输入邮箱地址" required>
                    <small>用于接收反馈处理结果通知</small>
                </div>
                
                <div class="form-group">
                    <label for="phone">联系电话</label>
                    <input type="tel" name="phone" id="phone" class="form-control" placeholder="请输入联系电话（选填）">
                </div>
                
                <div class="form-group">
                    <label for="subject">反馈主题</label>
                    <input type="text" name="subject" id="subject" class="form-control" placeholder="请简要描述反馈主题">
                </div>
                
                <div class="form-group">
                    <label for="message">详细反馈 <span style="color: red;">*</span></label>
                    <textarea name="message" id="message" rows="6" class="form-control" placeholder="请详细描述用户遇到的问题或建议，包括：&#10;1. 问题现象&#10;2. 复现步骤&#10;3. 期望结果&#10;4. 其他补充信息" required></textarea>
                    <small>请尽可能详细地描述，以便我们更好地理解和解决问题</small>
                </div>
                
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-paper-plane"></i> 提交反馈到官网
                </button>
            </form>
        </div>
    </div>
    
    <script src="/static/js/jquery.min.js"></script>
    <script>
    $('#feedbackForm').on('submit', function(e) {
        e.preventDefault();
        
        var name = $('#name').val().trim();
        var email = $('#email').val().trim();
        var phone = $('#phone').val().trim();
        var subject = $('#subject').val().trim();
        var message = $('#message').val().trim();
        
        if (!name) {
            alert('请输入姓名');
            $('#name').focus();
            return false;
        }
        
        if (!email) {
            alert('请输入邮箱');
            $('#email').focus();
            return false;
        }
        
        var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (!emailRegex.test(email)) {
            alert('请输入有效的邮箱地址');
            $('#email').focus();
            return false;
        }
        
        if (!message) {
            alert('请输入反馈内容');
            $('#message').focus();
            return false;
        }
        
        var $btn = $(this).find('button[type="submit"]');
        var originalText = $btn.html();
        $btn.addClass('btn-loading').prop('disabled', true);
        
        $.ajax({
            type: 'POST',
            url: 'submit_feedback.php',
            data: {
                name: name,
                email: email,
                phone: phone,
                subject: subject,
                message: message
            },
            dataType: 'json',
            success: function(data) {
                $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                if (data.status) {
                    alert('反馈提交成功！官网管理员会尽快查看。');
                    $('#feedbackForm')[0].reset();
                } else {
                    alert(data.msg || '提交失败，请稍后重试');
                }
            },
            error: function() {
                $btn.removeClass('btn-loading').prop('disabled', false).html(originalText);
                alert('操作失败，请重试');
            }
        });
        
        return false;
    });
    </script>
</body>
</html>
