<?php
ob_start();
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/functions.php';

header('Content-Type: application/json');

// 检查登录状态
if (!isLoggedIn()) {
    echo json_encode(['success' => false, 'message' => '请先登录']);
    exit;
}

$user_id = $_SESSION['user_id'];

// 获取参数
$title = trim($_POST['title'] ?? '');
$content = trim($_POST['content'] ?? '');
$priority = $_POST['priority'] ?? 'medium';

if (empty($title) || empty($content)) {
    echo json_encode(['success' => false, 'message' => '请填写完整信息']);
    exit;
}

$title = $db->escape($title);
$content = $db->escape($content);
$db->query("INSERT INTO tickets (user_id, title, content, priority) VALUES ($user_id, '$title', '$content', '$priority')");

echo json_encode(['success' => true, 'message' => '工单提交成功']);
exit;