<?php
session_start();
include "conn.php";

// Admin boleh lihat semua, selain admin hanya customer_id sendiri
if (in_array($_SESSION['role'], ['finance', 'admin', 'cashier'])) { 
    $sql = "SELECT COUNT(*) as total FROM invoice WHERE payment_status != 'Paid' AND payment_status != 'Failed'"; 
    $stmt = $con->prepare($sql);
} else {
    $sql = "SELECT COUNT(*) as total FROM invoice WHERE payment_status != 'Paid' AND payment_status != 'Failed' AND customer_id=?"; 
    $stmt = $con->prepare($sql);
    $stmt->bind_param("i", $_SESSION['aidi']);
}

$stmt->execute();
$res = $stmt->get_result()->fetch_assoc();
echo json_encode(['count' => $res['total'] ?? 0]);

