<?php
// =====================================================
// GET /get-all-salse-query.php
// Response: { data: [ { id, name, username, address, phone, move_charge,
//             final_connection_charge, cable_length, final_cable, cable_code,
//             final_length, sender_id, expected_time, remark, referby,
//             reference, created_at, status } ] }
// =====================================================
require_once 'config.php';

$conn = getConnection();

$result = $conn->query("SELECT id, name, username, address, phone, move_charge,
                                final_connection_charge, cable_length, final_cable,
                                cable_code, final_length, sender_id, expected_time,
                                remark, referby, reference, status
                         FROM marketing ORDER BY id DESC");

$data = [];
while ($row = $result->fetch_assoc()) {
    $data[] = [
        "id"                      => (string)$row['id'],
        "name"                    => $row['name'] ?? '',
        "username"                => $row['username'] ?? 'N/A',
        "address"                 => $row['address'] ?? '',
        "phone"                   => $row['phone'] ?? '',
        "move_charge"             => $row['move_charge'] ?? '',
        "final_connection_charge" => $row['final_connection_charge'] ?? '',
        "cable_length"            => $row['cable_length'] ?? '',
        "final_cable"             => $row['final_cable'] ?? '',
        "cable_code"              => $row['cable_code'] ?? '',
        "final_length"            => $row['final_length'] ?? '',
        "sender_id"               => $row['sender_id'] ?? '',
        "expected_time"           => $row['expected_time'] ?? '',
        "remark"                  => $row['remark'] ?? '',
        "referby"                 => $row['referby'] ?? '',
        "reference"               => $row['reference'] ?? '',
        "created_at"              => '',
        "status"                  => $row['status'] ?? 'pending'
    ];
}

echo json_encode(["data" => $data]);

$conn->close();
