<template>
	<div class="model-log-container">
		<div class="header">
			<h1>{{ t('pages.modelLog.title') }}</h1>
			<div class="header-actions">
				<el-button type="primary" @click="refreshLogs">
					<el-icon>
						<RefreshRight />
					</el-icon>
					{{ t('common.refresh') }}
				</el-button>
			</div>
		</div>

		<div class="stats-cards">
			<div class="stat-card">
				<div class="stat-icon success">
					<SvgIcon name="check-circle" size="30" />
				</div>
				<div class="stat-info">
					<div class="stat-value">2,345</div>
					<div class="stat-label">{{ t('pages.modelLog.stats.successCalls') }}</div>
				</div>
			</div>
			<div class="stat-card">
				<div class="stat-icon error">
					<SvgIcon name="x-circle" size="30" />
				</div>
				<div class="stat-info">
					<div class="stat-value">12</div>
					<div class="stat-label">{{ t('pages.modelLog.stats.failedCalls') }}</div>
				</div>
			</div>
			<div class="stat-card">
				<div class="stat-icon">
					<SvgIcon name="zap" size="30" />
				</div>
				<div class="stat-info">
					<div class="stat-value">1.2s</div>
					<div class="stat-label">{{ t('pages.modelLog.stats.avgResponse') }}</div>
				</div>
			</div>
			<div class="stat-card">
				<div class="stat-icon">
					<SvgIcon name="dollar-sign" size="30" />
				</div>
				<div class="stat-info">
					<div class="stat-value">$45.67</div>
					<div class="stat-label">{{ t('pages.modelLog.stats.todayCost') }}</div>
				</div>
			</div>
		</div>

		<div class="logs-table">
			<el-table :data="modelLogs" stripe style="width: 100%">
				<el-table-column prop="timestamp" :label="t('pages.modelLog.table.timestamp')" />
				<el-table-column prop="model" :label="t('pages.modelLog.table.model')" />
				<el-table-column prop="workflowName" :label="t('pages.modelLog.table.workflow')" />
				<el-table-column :label="t('pages.modelLog.table.status')">
					<template #default="scope">
						<el-tag :type="scope.row.status === 'success' ? 'success' : 'danger'">
							{{ scope.row.status === 'success' ? t('common.status.success') : t('common.status.failed') }}
						</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="tokens" :label="t('pages.modelLog.table.tokens')" />
				<el-table-column prop="cost" :label="t('pages.modelLog.table.cost')" />
				<el-table-column prop="responseTime" :label="t('pages.modelLog.table.responseTime')" />
				<el-table-column :label="t('pages.modelLog.table.actions')">
					<template #default="scope">
						<el-button size="small" @click="viewDetails(scope.row)">{{ t('common.view') }}</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<!-- 详情对话框 -->
		<el-dialog v-model="dialogVisible" :title="t('pages.modelLog.dialog.title')" width="800px">
			<div v-if="selectedLog" class="log-detail">
				<div class="detail-section">
					<h3>{{ t('pages.modelLog.dialog.basicInfo') }}</h3>
					<div class="detail-grid">
						<div class="detail-item">
							<span class="label">{{ t('pages.modelLog.dialog.time') }}:</span>
							<span class="value">{{ selectedLog.timestamp }}</span>
						</div>
						<div class="detail-item">
							<span class="label">{{ t('pages.modelLog.dialog.model') }}:</span>
							<span class="value">{{ selectedLog.model }}</span>
						</div>
						<div class="detail-item">
							<span class="label">{{ t('pages.modelLog.dialog.workflow') }}:</span>
							<span class="value">{{ selectedLog.workflowName }}</span>
						</div>
						<div class="detail-item">
							<span class="label">{{ t('pages.modelLog.dialog.status') }}:</span>
							<el-tag :type="selectedLog.status === 'success' ? 'success' : 'danger'">
								{{ selectedLog.status === 'success' ? t('common.status.success') : t('common.status.failed') }}
							</el-tag>
						</div>
					</div>
				</div>

				<div class="detail-section">
					<h3>{{ t('pages.modelLog.dialog.request') }}</h3>
					<div class="code-block">
						<pre>{{ selectedLog.prompt }}</pre>
					</div>
				</div>

				<div class="detail-section">
					<h3>{{ t('pages.modelLog.dialog.response') }}</h3>
					<div class="code-block">
						<pre>{{ selectedLog.response }}</pre>
					</div>
				</div>

				<div class="detail-section">
					<h3>{{ t('pages.modelLog.dialog.usage') }}</h3>
					<div class="detail-grid">
						<div class="detail-item">
							<span class="label">{{ t('pages.modelLog.dialog.inputTokens') }}:</span>
							<span class="value">{{ selectedLog.inputTokens }}</span>
						</div>
						<div class="detail-item">
							<span class="label">{{ t('pages.modelLog.dialog.outputTokens') }}:</span>
							<span class="value">{{ selectedLog.outputTokens }}</span>
						</div>
						<div class="detail-item">
							<span class="label">{{ t('pages.modelLog.dialog.total') }}:</span>
							<span class="value">{{ selectedLog.tokens }}</span>
						</div>
						<div class="detail-item">
							<span class="label">{{ t('pages.modelLog.dialog.cost') }}:</span>
							<span class="value">{{ selectedLog.cost }}</span>
						</div>
					</div>
				</div>
			</div>
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
import { RefreshRight } from '@element-plus/icons-vue'
import { computed, ref } from 'vue'
import { useI18n } from '@/composables/useI18n'

const { t, tm } = useI18n()
const dialogVisible = ref(false)
const selectedLog = ref<any>(null)

const modelLogs = computed(
	() =>
		(tm('pages.modelLog.logs') as Array<Record<string, unknown>>) || []
)

const refreshLogs = () => {
	console.log('Refreshing model logs...')
}

const viewDetails = (log: any) => {
	selectedLog.value = log
	dialogVisible.value = true
}
</script>

<style lang="less" scoped>
.model-log-container {
	max-width: 1400px;
	margin: 0 auto;
	padding: 20px;

	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 24px;

		h1 {
			font-size: 28px;
			font-weight: 600;
			color: var(--text-primary);
			display: flex;
			gap: 12px;
		}
	}

	.stats-cards {
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
		gap: 20px;
		margin-bottom: 24px;

		.stat-card {
			background: var(--bg-base);
			border-radius: 12px;
			padding: 24px;
			box-shadow: var(--shadow-sm);
			display: flex;
			align-items: center;
			gap: 20px;

			.stat-icon {
				width: 56px;
				height: 56px;
				border-radius: 12px;
				background: linear-gradient(135deg, var(--el-color-primary) 0%, #764ba2 100%);
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;
				font-size: 28px;

				&.success {
					background: linear-gradient(135deg, #10b981 0%, #059669 100%);
				}

				&.error {
					background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
				}
			}

			.stat-info {
				.stat-value {
					font-size: 28px;
					font-weight: 700;
					color: var(--text-primary);
					line-height: 1;
					margin-bottom: 8px;
				}

				.stat-label {
					font-size: 13px;
					color: var(--text-secondary);
				}
			}
		}
	}

	.logs-table {
		background: var(--bg-base);
		border-radius: 12px;
		padding: 20px;
		box-shadow: var(--shadow-sm);
	}

	.log-detail {
		.detail-section {
			margin-bottom: 24px;

			&:last-child {
				margin-bottom: 0;
			}

			h3 {
				font-size: 16px;
				font-weight: 600;
				color: #333;
				margin-bottom: 16px;
			}

			.detail-grid {
				display: grid;
				grid-template-columns: repeat(2, 1fr);
				gap: 16px;

				.detail-item {
					display: flex;
					align-items: center;
					gap: 8px;

					.label {
						font-size: 14px;
						color: var(--text-primary);
						font-weight: 500;
					}

					.value {
						font-size: 14px;
						color: var(--text-primary);
					}
				}
			}

			.code-block {
				background: var(--bg-container);
				border-radius: 8px;
				padding: 16px;
				max-height: 300px;
				overflow-y: auto;

				pre {
					margin: 0;
					font-size: 13px;
					color: var(--text-primary);
					line-height: 1.6;
					white-space: pre-wrap;
					word-wrap: break-word;
				}
			}
		}
	}
}
</style>
