<?php
header('Content-Type: application/json; charset=utf-8'); // Set content type to UTF-8 for proper encoding
include('../connection.php'); // Include database connection

// Ensure the connection is using UTF-8
mysqli_set_charset($conn, "utf8mb4");

// Prepare the SQL statement to get all tickets
$stmt = $conn->prepare("SELECT * FROM ticket ORDER BY id DESC");
$stmt->execute();
$result = $stmt->get_result();

$response = [];
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        // Prepare ticket data
        $response[] = [
            'id' => $row['id'],
            'problem' => $row['problem'],
            'assign' => $row['assign'],
            'location' => $row['location'],
            'created_at' => $row['created_at'],
            'status' => $row['status'],
            'end_time' => $row['end_time'],
            'recharge_id' => $row['recharge_id'],
            'user_id' => $row['user_id'],
            'name' => $row['name'],
            'phone' => $row['phone'],
            'priority' => $row['priroty'],
            'create_by' => $row['create_by'],
            'remark' => $row['remark'],
            'check_count' => $row['check_count'],
        ];
    }
} else {
    // If no tickets found, prepare the error response
    $response = [
        'status' => 'error',
        'message' => 'No tickets found',
    ];
}

// Encode the response to JSON and ensure the Bangla characters are properly displayed
echo json_encode($response, JSON_UNESCAPED_UNICODE);

// Close the database connection
$stmt->close();
?>
