<?php
// 添加自动加载
spl_autoload_register(function ($class) {
    error_log("尝试自动加载类: " . $class);
    
    // 定义可能的路径
    $paths = [
        __DIR__ . '/includes/exchanges/',
        __DIR__ . '/includes/',
        'includes/exchanges/',
        'includes/'
    ];
    
    foreach ($paths as $path) {
        $file = $path . $class . '.php';
        error_log("尝试加载文件: " . $file);
        
        if (file_exists($file)) {
            error_log("找到文件: " . $file);
            require_once $file;
            return;
        }
    }
    
    error_log("未找到类文件: " . $class);
});

require_once 'config/config.php';
require_once 'config/database.php';
require_once 'config/functions.php';
require_once 'config/encryption.php';
require_once 'config/session.php';
require_once 'config/logger.php';
require_once 'config/auth.php';

// 需要登录才能访问
requireLogin();

$page_title = '持仓明细';
$db = Database::getInstance();
$user_id = $_SESSION['user_id'];

// 获取 API ID
$api_id = isset($_GET['api_id']) ? intval($_GET['api_id']) : 0;

if (!$api_id) {
    $_SESSION['flash'] = ['type' => 'danger', 'message' => '无效的API ID'];
    header('Location: api_settings.php');
    exit;
}

try {
    // 获取 API 信息
    $stmt = $db->prepare("SELECT * FROM exchange_apis WHERE id = ? AND user_id = ?");
    $stmt->execute([$api_id, $user_id]);
    $api = $stmt->get_result()->fetch_assoc();
    
    if (!$api) {
        throw new Exception('未找到API配置或无权访问');
    }

    // 获取持仓信息
    $positions = [];
    $stmt = $db->prepare("SELECT * FROM positions WHERE api_id = ? AND user_id = ?");
    $stmt->execute([$api_id, $user_id]);
    $positions = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);

} catch (Exception $e) {
    $_SESSION['flash'] = ['type' => 'danger', 'message' => $e->getMessage()];
    header('Location: api_settings.php');
    exit;
}

include 'templates/header.php';
?>

<div class="container">
    <h2>持仓明细</h2>
    <p><a href="api_settings.php">返回API列表</a></p>
    
    <!-- API 信息 -->
    <!--
    <div>
        <p>交易所：<?php //echo htmlspecialchars($api['exchange']); ?></p>
        <p>API Key：<?php //echo substr($api['trade_api_key'], 0, 8) . '...'; ?></p>
        <p>状态：<?php //echo $api['status'] === 'active' ? '正常' : '禁用'; ?></p>
        <p>添加时间：<?php //echo date('Y-m-d H:i:s', strtotime($api['created_at'])); ?></p>
    </div>
    -->
    
    <!-- 持仓列表 -->
    <!--
    <table class="table">
        <thead>
            <tr>
                <th>交易对</th>
                <th>方向</th>
                <th>数量</th>
                <th>开仓均价</th>
                <th>当前价格</th>
                <th>未实现盈亏</th>
                <th>杠杆倍数</th>
                <th>更新时间</th>
                <th data-sort="margin">保证金</th>
                <th data-sort="marginRatio">保金占比</th>
            </tr>
        </thead>
        <tbody>
            <?php //if (empty($positions)): ?>
            <tr>
                <td colspan="8" style="text-align: center;">暂无持仓</td>
            </tr>
            <?php //else: ?>
            <?php //foreach ($positions as $position): ?>
            <tr>
                <td><?php //echo htmlspecialchars($position['symbol']); ?></td>
                <td><?php //echo $position['side'] === 'long' ? '多' : '空'; ?></td>
                <td><?php //echo number_format($position['size'], 4); ?></td>
                <td><?php //echo number_format($position['entry_price'], 4); ?></td>
                <td><?php //echo number_format($position['mark_price'], 4); ?></td>
                <td><?php //echo number_format($position['unrealized_pnl'], 2); ?> USDT</td>
                <td><?php //echo $position['leverage']; ?>x</td>
                <td><?php //echo date('Y-m-d H:i:s', strtotime($position['updated_at'])); ?></td>
                <td class="number-column"><?php //echo number_format($position['isolatedMargin'], 4); ?></td>
                <td class="number-column"><?php //echo number_format($position['marginRatio'], 2); ?>%</td>
            </tr>
            <?php //endforeach; ?>
            <?php //endif; ?>
        </tbody>
    </table>
    -->
</div>

<?php include 'templates/footer.php'; ?> 