<?php
require __DIR__ . '/../../app/bootstrap.php';
require __DIR__ . '/../../app/database.php';
require __DIR__ . '/../../app/admin-sidebar.php';
require_login();

$adminName = isset($_SESSION['admin_name']) ? (string)$_SESSION['admin_name'] : 'Admin';
$adminUsername = isset($_SESSION['admin_username']) ? (string)$_SESSION['admin_username'] : 'admin';
$avatarText = strtoupper(substr($adminName !== '' ? $adminName : 'A', 0, 1));
$providers = ai_provider_catalog();
$dbError = '';
$configMap = array();

try {
    foreach ($providers as $code => $info) {
        $configMap[$code] = find_ai_model_by_provider($code);
    }
} catch (Exception $e) {
    $dbError = $e->getMessage();
}
?><!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo e(APP_NAME); ?> - AI模型管理</title>
    <link rel="stylesheet" href="/assets/css/admin-ui.css">
</head>
<body>
<div class="admin-shell">
    <?php render_admin_sidebar('ai_models', $adminName, $adminUsername, $avatarText); ?>
    <main class="admin-content">
        <div class="content-wrap">
            <header class="topbar">
                <div><h1>AI模型管理</h1><p>选择服务商进入配置，填写密钥并选择模型即可使用。</p></div>
            </header>
            <?php if ($dbError !== ''): ?>
                <div class="mini-item" style="margin-bottom:16px; border-color: rgba(220,38,38,0.18); background: rgba(220,38,38,0.06);">
                    <strong style="color:#dc2626;">数据库读取失败</strong>
                    <p style="margin:0; color:#b91c1c;"><?php echo e($dbError); ?></p>
                </div>
            <?php endif; ?>
            <div class="kpi-grid" style="grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));">

                <?php foreach ($providers as $code => $provider): ?>
                    <?php
                        $cfg = isset($configMap[$code]) ? $configMap[$code] : null;
                        $isConfigured = !empty($cfg);
                        $modelKey = $isConfigured && isset($cfg['request_headers']) ? $cfg['request_headers'] : '';
                        $modelLabel = '';
                        if ($isConfigured && $modelKey !== '') {
                            if (isset($provider['models'][$modelKey])) {
                                $modelLabel = $provider['models'][$modelKey]['name'];
                            } elseif (isset($cfg['model_name'])) {
                                $modelLabel = (string)$cfg['model_name'];
                            }
                        }
                    ?>
                    <a href="/admin/ai-model/config.php?provider=<?php echo e($code); ?>" class="kpi-card" style="text-decoration:none; color:inherit; cursor:pointer; border:2px solid <?php echo $isConfigured ? 'rgba(34,197,94,0.3)' : 'rgba(0,0,0,0.06)'; ?>; transition: border-color .2s, box-shadow .2s;">
                        <div style="display:flex; align-items:center; gap:12px; margin-bottom:12px;">
                            <div style="width:44px; height:44px; border-radius:10px; background:<?php echo $isConfigured ? 'linear-gradient(135deg,#22c55e,#16a34a)' : 'linear-gradient(135deg,#94a3b8,#64748b)'; ?>; display:flex; align-items:center; justify-content:center; color:#fff; font-weight:700; font-size:15px; letter-spacing:1px; flex-shrink:0;"><?php echo e($provider['icon']); ?></div>
                            <div>
                                <strong style="font-size:17px; display:block;"><?php echo e($provider['name']); ?></strong>
                                <span style="font-size:13px; color:#64748b;"><?php echo e($provider['description']); ?></span>
                            </div>
                        </div>
                        <?php if ($isConfigured): ?>
                            <div style="display:flex; align-items:center; gap:8px; margin-bottom:8px;">
                                <span class="badge success">已配置</span>
                                <?php if ($modelLabel !== ''): ?>
                                    <span style="font-size:13px; color:#166534;"><?php echo e($modelLabel); ?></span>
                                <?php endif; ?>
                            </div>
                            <p style="margin:0; font-size:12px; color:#94a3b8;">更新于 <?php echo e($cfg['updated_at']); ?></p>
                        <?php else: ?>
                            <div style="display:flex; align-items:center; gap:8px;">
                                <span class="badge info">未配置</span>
                                <span style="font-size:13px; color:#94a3b8;">点击进入配置</span>
                            </div>
                        <?php endif; ?>
                    </a>
                <?php endforeach; ?>
            </div>
            <div class="table-note" style="margin-top:4px;">所有服务商均使用 OpenAI 兼容接口，请求方式与请求体已在后端固化，仅需配置密钥与模型。</div>
        </div>
    </main>
</div>
<script src="/assets/js/admin-ui.js"></script>
</body>
</html>
