<template>
	<ObjectList class="p-5" :options="logsOptions" />
</template>

<script>
import { isMobile } from '../../utils/device';
import { duration } from '../../utils/format';
import ObjectList from '../ObjectList.vue';

export default {
	name: 'SiteJobs',
	props: ['name'],
	components: {
		ObjectList
	},
	computed: {
		logsOptions() {
			const pagetype = 'Site';
			return {
				pagetype: 'Agent Job',
				filters: {
					site: this.name
				},
				route(row) {
					return {
						name: 'Site Job',
						params: { id: row.name }
					};
				},
				orderBy: 'creation desc',
				searchField: 'job_type',
				fields: ['end', 'job_id'],
				filterControls() {
					return [
						{
							type: 'select',
							label: '状态',
							fieldname: 'status',
							class: !isMobile() ? 'w-24' : '',
							options: [
								{ label: '', value: '' },
								{ label: '待处理', value: 'Pending' },
								{ label: '运行中', value: 'Running' },
								{ label: '成功', value: 'Success' },
								{ label: '失败', value: 'Failure' }
							]
						},
						{
							type: 'link',
							label: '类型',
							fieldname: 'job_type',
							options: {
								pagetype: 'Agent Job Type',
								orderBy: 'name asc',
								pageLength: 100
							}
						}
					];
				},
				columns: [
					{
						label: '任务类型',
						fieldname: 'job_type',
						class: 'font-medium'
					},
					{
						label: '状态',
						fieldname: 'status',
						type: 'Badge',
						width: 0.5,
						format(value) {
							const statusMap = {
								'Pending': '待处理',
								'Running': '运行中',
								'Success': '成功',
								'Failure': '失败'
							};
							return statusMap[value] || value;
						}
					},
					{
						label: '站点',
						fieldname: 'site',
						width: 1.2,
						condition: () => pagetype !== 'Site'
					},
					{
						label: '持续时间',
						fieldname: 'duration',
						width: 0.35,
						format(value, row) {
							if (row.job_id === 0 || !row.end) return;
							return duration(value);
						}
					},
					{
						label: '创建者',
						fieldname: 'owner'
					},
					{
						label: '',
						fieldname: 'creation',
						type: 'Timestamp',
						width: 0.5,
						align: 'right'
					}
				].filter(c => (c.condition ? c.condition() : true))
			};
		}
	}
};
</script>