<template>
	<div class="p-5">
		<div class="flex items-center space-x-2">
			<Button
				:route="{
					name:
						object.pagetype === 'Site'
							? '站点日志'
							: `${object.pagetype} 详细日志`
				}"
			>
				<template #icon>
					<i-lucide-arrow-left class="inline-block h-4 w-4" />
				</template>
			</Button>
			<h2 class="text-lg font-medium text-gray-900">{{ logName }}</h2>
			<div class="!ml-auto flex gap-2">
				<Button
					:route="{
						name: '日志浏览器',
						params: {
							mode: object.pagetype === 'Site' ? 'site' : 'bench',
							docName: name,
							logId: logName
						}
					}"
				>
					<template #prefix>
						<i-lucide-sparkle class="h-4 w-4" />
					</template>
					在日志浏览器中查看
				</Button>
				<Button
					@click="$resources.log.reload()"
					:loading="$resources.log.loading"
				>
					<template #icon>
						<i-lucide-refresh-ccw class="h-4 w-4" />
					</template>
				</Button>
			</div>
		</div>

		<div class="mt-3">
			<div class="mt-8 space-y-4">
				<div
					class="overflow-auto rounded border border-gray-100 bg-gray-900 px-2.5 py-2 text-sm text-gray-200"
				>
					<pre>{{
						$resources.log.loading ? '加载中...' : log || '无输出'
					}}</pre>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import { FeatherIcon } from 'jingrow-ui';
import { getObject } from '../objects';
import { unreachable } from '../objects/common';

export default {
	name: 'LogPage',
	props: ['name', 'logName', 'objectType'],
	components: { FeatherIcon },
	resources: {
		log() {
			const url = this.forSite ? 'jcloud.api.site.log' : 'jcloud.api.bench.log';
			const params = { log: this.logName, name: this.name };
			if (!this.forSite) {
				params.name = `bench-${this.name?.split('-')[1]}`;
				params.bench = this.name;
			}

			return {
				url,
				params,
				auto: true,
				transform(log) {
					return log[this.logName];
				},
				onSuccess() {
					this.lastLoaded = Date.now();
				}
			};
		}
	},
	computed: {
		forSite() {
			if (this.objectType === 'Site') return true;
			if (this.objectType === 'Bench') return false;
			throw unreachable;
		},
		object() {
			return getObject(this.objectType);
		},
		log() {
			return this.$resources.log.data;
		}
	}
};
</script>