<?php
// File: API_KONEKSI/admin/pelanggan/list_pelanggan.php
header('Content-Type: application/json');
include '../../config/koneksi.php';

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    // Gunakan Prepared Statement meskipun tanpa parameter dari user untuk keamanan maksimal
    $query = "SELECT user_id, lengkap_nm, email_tx, whatsapp_no FROM mst_user WHERE role_nm = 'Pelanggan' AND deleted_st = 0 ORDER BY lengkap_nm ASC";
    $stmt = $conn->prepare($query);
    
    if ($stmt->execute()) {
        $result = $stmt->get_result();
        $data = [];

        while ($row = $result->fetch_assoc()) {
            $data[] = $row;
        }

        if (count($data) > 0) {
            echo json_encode(["status" => "sukses", "pesan" => "Data ditemukan", "data" => $data]);
        } else {
            echo json_encode(["status" => "kosong", "pesan" => "Belum ada pelanggan terdaftar", "data" => []]);
        }
    } else {
        echo json_encode(["status" => "gagal", "pesan" => "Gagal mengambil data dari server", "data" => []]);
    }
    $stmt->close();
}
$conn->close();
?>