<template>
	<view class="logs-page">
		<view class="tab-bar">
			<view
				v-for="t in tabs"
				:key="t.key"
				:class="['tab-item', activeTab === t.key ? 'tab-active' : '']"
				@click="activeTab = t.key"
			>
				<text class="tab-text">{{ t.label }}</text>
				<text class="tab-count">({{ getCount(t.key) }})</text>
			</view>
		</view>

		<scroll-view class="log-list" scroll-y>
			<view class="log-empty" v-if="!filteredLogs.length">
				<text>暂无记录</text>
			</view>
			<view class="log-card" v-for="(log, idx) in filteredLogs" :key="idx" @click="toggleDetail(idx)">
				<view class="log-title-row">
					<text class="log-time">{{ log.time }}</text>
					<text :class="['log-tag', log.ok ? 'tag-ok' : 'tag-fail']">{{ log.ok ? '成功' : '失败' }}</text>
				</view>
				<text class="log-summary">
					{{ getTypeLabel(log) }} · 上传 {{ log.recordCount ?? log.smsCount ?? 0 }} 条 | 状态码 {{ log.statusCode || '-' }}
				</text>
				<view class="log-detail" v-if="expandIdx === idx">
					<view class="detail-section">
						<text class="detail-label">记录类型</text>
						<text class="detail-value">{{ getTypeLabel(log) }}</text>
					</view>
					<view class="detail-section">
						<text class="detail-label">上传接口</text>
						<text class="detail-value" selectable>{{ log.apiUrl || '-' }}</text>
					</view>
					<view class="detail-section">
						<text class="detail-label">请求参数</text>
						<scroll-view class="detail-code-scroll" scroll-y scroll-x>
							<text class="detail-code" selectable>{{ formatJson(log.requestData) }}</text>
						</scroll-view>
					</view>
					<view class="detail-section">
						<text class="detail-label">请求响应</text>
						<scroll-view class="detail-code-scroll" scroll-y scroll-x>
							<text class="detail-code" selectable>{{ formatJson(log.responseData) }}</text>
						</scroll-view>
					</view>
					<view class="detail-section" v-if="log.message">
						<text class="detail-label">消息</text>
						<text class="detail-value">{{ log.message }}</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			activeTab: 'all',
			expandIdx: -1,
			logs: [],
			tabs: [
				{ key: 'all', label: '全部' },
				{ key: 'success', label: '成功' },
				{ key: 'fail', label: '失败' }
			]
		};
	},
	computed: {
		filteredLogs() {
			if (this.activeTab === 'all') return this.logs;
			const isOk = this.activeTab === 'success';
			return this.logs.filter(item => item.ok === isOk);
		}
	},
	onLoad(opts) {
		if (opts.tab) this.activeTab = opts.tab;
		this.logs = uni.getStorageSync('sms_upload_logs') || [];
	},
	methods: {
		getCount(key) {
			if (key === 'all') return this.logs.length;
			const isOk = key === 'success';
			return this.logs.filter(item => item.ok === isOk).length;
		},
		getTypeLabel(log) {
			if (log.recordTypeLabel) return log.recordTypeLabel;
			if (log.recordType === 'call') return '来电';
			return '短信';
		},
		toggleDetail(idx) {
			this.expandIdx = this.expandIdx === idx ? -1 : idx;
		},
		formatJson(str) {
			if (!str) return '-';
			try {
				const data = typeof str === 'string' ? JSON.parse(str) : str;
				return JSON.stringify(data, null, 2);
			} catch (e) {
				return str;
			}
		}
	}
};
</script>

<style>
.logs-page {
	display: flex;
	flex-direction: column;
	min-height: 100vh;
	background-color: #f6f7fb;
}

.tab-bar {
	display: flex;
	flex-direction: row;
	background-color: #ffffff;
	border-bottom: 1rpx solid #e5e7eb;
	position: sticky;
	top: 0;
	z-index: 10;
}

.tab-item {
	flex: 1;
	display: flex;
	flex-direction: row;
	justify-content: center;
	align-items: center;
	padding: 24rpx 0;
	border-bottom: 4rpx solid transparent;
}

.tab-active {
	border-bottom-color: #2563eb;
}

.tab-active .tab-text,
.tab-active .tab-count {
	color: #2563eb;
	font-weight: 600;
}

.tab-text {
	font-size: 28rpx;
	color: #6b7280;
}

.tab-count {
	font-size: 22rpx;
	color: #9ca3af;
	margin-left: 6rpx;
}

.log-list {
	flex: 1;
	width: 100%;
	padding: 20rpx 24rpx;
	box-sizing: border-box;
}

.log-empty {
	padding: 80rpx 0;
	text-align: center;
	color: #9ca3af;
	font-size: 28rpx;
}

.log-card {
	background-color: #ffffff;
	border-radius: 16rpx;
	padding: 24rpx;
	margin-bottom: 16rpx;
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
	box-sizing: border-box;
	overflow: hidden;
}

.log-title-row {
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 8rpx;
}

.log-time {
	font-size: 28rpx;
	font-weight: 600;
	color: #1f2937;
}

.log-tag {
	font-size: 22rpx;
	padding: 4rpx 16rpx;
	border-radius: 8rpx;
}

.tag-ok {
	color: #16a34a;
	background-color: #f0fdf4;
	border: 1rpx solid #bbf7d0;
}

.tag-fail {
	color: #dc2626;
	background-color: #fef2f2;
	border: 1rpx solid #fca5a5;
}


.log-summary {
	font-size: 24rpx;
	color: #6b7280;
}

.log-detail {
	margin-top: 20rpx;
	padding-top: 16rpx;
	border-top: 1rpx solid #f0f0f0;
}

.detail-section {
	margin-bottom: 16rpx;
}

.detail-label {
	font-size: 22rpx;
	color: #9ca3af;
	margin-bottom: 6rpx;
	font-weight: 600;
}

.detail-value {
	font-size: 24rpx;
	color: #374151;
	word-break: break-all;
}

.detail-code-scroll {
	max-height: 400rpx;
	background-color: #f9fafb;
	border-radius: 8rpx;
	padding: 12rpx;
	border: 1rpx solid #e5e7eb;
}

.detail-code {
	font-size: 22rpx;
	color: #374151;
	font-family: monospace;
	word-break: break-all;
	white-space: pre-wrap;
}
</style>
