<?php
session_start();
require_once '../config.php';

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

if (!isset($_SESSION['opd_patient'])) {
    echo json_encode(['count' => 0]);
    exit;
}

$patient_id = $_SESSION['opd_patient'];

$query = "SELECT COUNT(*) as count FROM appointments 
          WHERE patient_id = '$patient_id' 
          AND status IN ('pending', 'confirmed')";
$result = mysqli_query($conn, $query);

if ($result && $row = mysqli_fetch_assoc($result)) {
    echo json_encode(['count' => (int)$row['count']]);
} else {
    echo json_encode(['count' => 0]);
}
?>