<?php
// Include database connection
require_once('connectionSC.php');

// Cek koneksi database
if ($conn->connect_error) {
    die(json_encode([
        "status" => "error",
        "message" => "Error in connecting to Database: " . $conn->connect_error
    ]));
}

// Query untuk mengambil daftar cabang
$query = "SELECT cabang FROM admin GROUP BY cabang ORDER BY cabang DESC";

$result = $conn->query($query);

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

if ($result && $result->num_rows > 0) {
    $branches = [];
    while ($row = $result->fetch_assoc()) {
        $cabang = $row['cabang'];
        
        // Ganti "Semua" dengan "Semua Cabang"
        if (strtoupper($cabang) === 'SEMUA') {
            $cabang = 'SEMUA CABANG';
        }

        $branches[] = $cabang;
    }

    echo json_encode([
        "status" => "success",
        "branches" => $branches
    ]);
} else {
    echo json_encode([
        "status" => "empty",
        "message" => "No branches found."
    ]);
}

// Menutup koneksi
$conn->close();
?>
