<?php
// =====================================================
// POST /add_ticket.php
// Params: problem, assign, location, created_at, status, end_time,
//         recharge_id, name, phone, user_id, priority, remark, create_by, isAdmin
// Response: { ticket_id, message }
// =====================================================
require_once 'config.php';

$conn = getConnection();

$problem    = $_POST['problem']    ?? '';
$assign     = $_POST['assign']     ?? '';
$location   = $_POST['location']   ?? '';
$created_at = $_POST['created_at'] ?? '';
$status     = $_POST['status']     ?? '';
$end_time   = $_POST['end_time']   ?? '';
$recharge_id = $_POST['recharge_id'] ?? '';
$name       = $_POST['name']       ?? '';
$phone      = $_POST['phone']      ?? '';
$user_id    = $_POST['user_id']    ?? '';
$priority   = $_POST['priority']   ?? '';
$remark     = $_POST['remark']     ?? '';
$create_by  = $_POST['create_by']  ?? '';
$isAdmin    = $_POST['isAdmin']    ?? '';

$stmt = $conn->prepare("INSERT INTO ticket (problem, assign, location, created_at, status, end_time, recharge_id, user_id, name, phone, priroty, create_by, remark) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssssssssss", $problem, $assign, $location, $created_at, $status, $end_time, $recharge_id, $user_id, $name, $phone, $priority, $create_by, $remark);

if ($stmt->execute()) {
    $ticket_id = $conn->insert_id;
    echo json_encode([
        "ticket_id" => (string)$ticket_id,
        "message"   => "Ticket added successfully"
    ]);
} else {
    echo json_encode([
        "ticket_id" => "",
        "message"   => "Failed to add ticket"
    ]);
}

$stmt->close();
$conn->close();
