<?php
// factors/contractors.php — Военные подрядчики
$api_url = 'https://' . $_SERVER['HTTP_HOST'] . '/api/latest.php';
$cache_file = __DIR__ . '/cache/contractors.json';
$cache_ttl = 300;

if (file_exists($cache_file) && (time() - filemtime($cache_file)) < $cache_ttl) {
    $data = json_decode(file_get_contents($cache_file), true);
} else {
    $raw = @file_get_contents($api_url);
    $data = $raw ? json_decode($raw, true) : ['components' => ['contractors' => ['raw' => 1, 'score' => 2.0]]];
    if (!is_dir(dirname($cache_file))) mkdir(dirname($cache_file), 0755, true);
    file_put_contents($cache_file, json_encode($data));
}

$factor = $data['components']['contractors'] ?? ['raw' => 1, 'score' => 2.0];
$current_level = $factor['raw'];
$current_score = $factor['score'];
?>
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>Военные подрядчики</title>
    <style>
        body { font-family: Arial; background: #0f172a; color: #e2e8f0; padding: 2rem; }
        .card { background: #1e293b; padding: 1.5rem; border-radius: 12px; max-width: 600px; margin: auto; }
        .value { font-size: 2.5rem; font-weight: 700; color: #f59e0b; }
    </style>
</head>
<body>
    <div class="card">
        <h2>Военные подрядчики</h2>
        <p>Текущее значение:</p>
        <div class="value"><?= $current_level ?>/5</div>
        <p>Вклад в индекс: <strong><?= $current_score ?></strong> из 10</p>
    </div>
</body>
</html>