<?php
session_start();
header('Content-Type: application/json');

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'company_admin') {
    echo json_encode(['success' => false]);
    exit;
}

require_once '../config/database.php';

$companyId = (int)$_SESSION['company_id'];

try {
    $pending = (int)$pdo->prepare("
        SELECT COUNT(*) FROM hr_leave
        WHERE company_id = ? AND status = 'pending'
    ");
    $pending->execute([$companyId]);
    $count = (int)$pending->fetchColumn();

    echo json_encode([
        'success'      => true,
        'pendingLeave' => $count,
    ]);
} catch (Throwable $e) {
    echo json_encode(['success' => false]);
}