<?php
// api/add_funds.php - 國庫撥款專用
header("Content-Type: text/html; charset=utf-8");
require_once 'db.php';

$user_id = 8; // 鎖定統帥 ID
$amount = 999999;

try {
    // 1. 真實寫入資料庫
    $stmt = $pdo->prepare("UPDATE users SET coins = ? WHERE id = ?");
    $stmt->execute([$amount, $user_id]);
    
    // 2. 為了確保瀏覽器同步，我們順便輸出一段更新腳本
    echo "
    <div style='background:#121212; color:#ffd700; padding:40px; text-align:center;'>
        <h1>💰 國庫撥款完成！</h1>
        <p>統帥 (ID: $user_id) 的真實餘額已更新為 $amount Z幣。</p>
        <p>正在同步前端顯示...</p>
    </div>
    <script>
        // 更新瀏覽器記憶
        let user = JSON.parse(localStorage.getItem('zoo_user')) || {};
        user.coins = $amount;
        user.user_id = $user_id; // 確保 ID 正確
        localStorage.setItem('zoo_user', JSON.stringify(user));
        
        // 1秒後跳轉去發布任務驗收
        setTimeout(() => { window.location.href = '../projects.html'; }, 1000);
    </script>
    ";

} catch (Exception $e) {
    echo "錯誤：" . $e->getMessage();
}
?>