Files
syops/frontend/src/features/admin/modules/schedules/SchedulesPage.vue
T
sophie 61693d014d refactor: 重构前端骨架,及admin模块子路由和懒加载
refactor: 优化多次请求,并使用pinia状态管理
2026-06-03 10:30:16 +08:00

1128 lines
73 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* features/admin/modules/schedules/SchedulesPage.vue
* 定时任务管理独立页面。路由:/admin/schedules
*/
<script setup lang="ts">
import { onMounted } from 'vue';
import {
ArrowLeft, ChevronDown, ChevronUp, Download, Edit3, Eye, FlaskConical,
Plus, Power, PowerOff, RefreshCw, Rocket, Settings2, Trash2,
} from 'lucide-vue-next';
import DeleteConfirmDialog from '@/components/DeleteConfirmDialog.vue';
import { useSchedulesAdmin } from './composables/useSchedulesAdmin';
const admin = useSchedulesAdmin();
onMounted(() => { void admin.loadAll(); });
const {
agents, users, schedules, selectedScheduleRuns, scheduleLoading, scheduleRunsLoading,
selectedScheduleId, scheduleRunStatusFilter, scheduleRunStartedAfter, scheduleRunStartedBefore,
scheduleRunColumnDefs, scheduleViewMode, showScheduleCreateModal, scheduleCreateSaving,
scheduleEditingId, scheduleCreateForm, scheduleConfigCollapsed, scheduleCronDesigner,
weeklyDayOptions, scheduleCronPreview, scheduleNextRunPreview, selectedSchedule,
scheduleRunStatusOptions, getScheduleRunStatusBadge, formatDateTime, expandedScheduleRunId,
expandedScheduleRun, stepLabelMap, stepStatusMap, showTestRunDialog, testRunSchedule,
testRunWhere, testRunOrderBy, testRunBaseFetchSql, testRunLimit, testRunLoading, testRunResult,
filteredScheduleRuns, selectedScheduleRunMetrics, selectedScheduleLatestRunAt,
scheduleEnabledCount, scheduleDisabledCount, scheduleRunningCount, scheduleSqlEditForm,
scheduleTestLoading, scheduleTestResult, scheduleNotifyDropdownOpen,
showDeleteConfirmDialog, deleteConfirmTitle, deleteConfirmMessage,
loadSchedules, startScheduleColResize, openScheduleCreateModal, openScheduleEditModal,
openScheduleRunSubmenu, backToScheduleOverview, viewScheduleRuns, resetScheduleRunFilters,
runScheduleNow, setScheduleEnabled, deleteSchedule, downloadScheduleExcel, openTestRunDialog,
handleTestRun, handleTestScheduleSql, getUserName, toggleNotifyUser, removeNotifyUser,
createSchedule, closeDeleteConfirm, confirmDelete,
} = admin;
</script>
<template>
<section class="section-gap animate-fade-in-up">
<div class="admin-panel-lg px-7 py-6 shadow-sm">
<div class="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
<div class="flex items-start gap-4">
<button
v-if="scheduleViewMode === 'workbench'"
class="inline-flex h-14 w-14 items-center justify-center admin-panel text-muted transition hover:bg-surface-soft hover:text-secondary"
@click="backToScheduleOverview"
>
<ArrowLeft class="h-7 w-7" />
</button>
<div class="flex h-14 w-14 items-center justify-center rounded-2xl bg-blue-50 text-blue-600">
<Settings2 class="h-7 w-7" />
</div>
<div>
<h2 class="text-2xl font-bold tracking-tight text-primary">
{{ scheduleViewMode === 'overview' ? $t('admin.schedule.overviewTitle') : $t('admin.schedule.workbenchTitle') }}
</h2>
<p class="mt-1.5 text-sm text-muted">
{{ scheduleViewMode === 'overview' ? $t('admin.schedule.overviewSubtitle') : $t('admin.schedule.workbenchSubtitle') }}
</p>
</div>
</div>
<button
class="inline-flex items-center gap-2 admin-panel px-5 py-2.5 text-sm font-semibold text-secondary transition hover:bg-surface-soft"
@click="loadSchedules"
>
<RefreshCw class="h-5 w-5" />
{{ $t('admin.schedule.refresh') }}
</button>
</div>
</div>
<template v-if="scheduleViewMode === 'overview'">
<div class="admin-panel-lg p-6 shadow-sm">
<div class="mb-5 grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-4">
<div class="admin-stat-tile px-4 py-3">
<div class="text-xl font-bold text-primary">{{ schedules.length }}</div>
<div class="text-sm text-muted">{{ $t('admin.schedule.stats.total') }}</div>
</div>
<div class="admin-stat-tile px-4 py-3">
<div class="text-xl font-bold text-primary">{{ scheduleEnabledCount }}</div>
<div class="text-sm text-muted">{{ $t('admin.schedule.stats.enabled') }}</div>
</div>
<div class="admin-stat-tile px-4 py-3">
<div class="text-xl font-bold text-primary">{{ scheduleDisabledCount }}</div>
<div class="text-sm text-muted">{{ $t('admin.schedule.stats.disabled') }}</div>
</div>
<div class="admin-stat-tile px-4 py-3">
<div class="text-xl font-bold text-primary">{{ scheduleRunningCount }}</div>
<div class="text-sm text-muted">{{ $t('admin.schedule.stats.running') }}</div>
</div>
</div>
<div class="admin-panel p-4">
<div class="mb-3 flex items-center justify-between">
<h4 class="text-lg font-bold text-primary">{{ $t('admin.schedule.sectionOverview') }}</h4>
<span class="text-xs text-muted">{{ $t('admin.schedule.switchByCard') }}</span>
</div>
<div v-if="scheduleLoading" class="py-8 text-center text-sm text-muted">{{ $t('admin.schedule.loading') }}</div>
<div v-else class="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3 auto-rows-fr">
<div class="admin-panel p-4 transition-colors hover:border-brand-300 h-full min-h-[260px] flex flex-col">
<div class="mb-3 flex items-center gap-2">
<div class="flex h-8 w-8 items-center justify-center rounded-xl bg-brand-50 text-brand-600">
<Settings2 class="h-4 w-4" />
</div>
<div class="font-semibold text-primary">{{ $t('admin.schedule.addTitle') }}</div>
</div>
<div class="text-sm text-muted">
{{ $t('admin.schedule.addHint') }}
</div>
<div class="mt-auto pt-3">
<button
@click="openScheduleCreateModal"
class="inline-flex w-full items-center justify-center gap-2 rounded-xl bg-brand-600 px-3 py-2 text-sm font-medium text-white transition-colors hover:bg-brand-700 disabled:cursor-not-allowed disabled:opacity-60"
>
<Plus class="h-4 w-4" />
{{ $t('admin.schedule.addButton') }}
</button>
</div>
</div>
<div
v-for="s in schedules"
:key="s.id"
class="flex h-full min-h-[260px] flex-col admin-panel p-4 transition hover:border-blue-200 hover:bg-surface-soft"
>
<button class="w-full text-left" @click="openScheduleRunSubmenu(s.id)">
<div class="min-h-[96px]">
<div class="flex items-center justify-between gap-3">
<div class="truncate text-base font-semibold text-primary">{{ s.name }}</div>
<span class="inline-flex shrink-0 items-center whitespace-nowrap rounded-full px-2.5 py-1 text-xs font-semibold"
:class="s.enabled ? 'bg-emerald-100 text-emerald-700' : 'bg-surface-strong text-secondary'">
{{ s.enabled ? $t('admin.schedule.enabled') : $t('admin.schedule.disabled') }}
</span>
</div>
<div class="mt-2 text-sm text-muted">{{ $t('admin.schedule.scheduleNo') }}:{{ s.schedule_no }}</div>
<div class="mt-1 text-sm text-muted">Cron:{{ s.cron_expr }}</div>
<div
v-if="s.schedule_type === 'platform_app' && s.platform_api_path"
class="mt-1 text-sm text-muted"
>
{{ $t('admin.schedule.config') }}:{{ s.platform_api_path }}
</div>
<div class="mt-1 text-sm text-muted">{{ $t('admin.schedule.owner') }}:{{ s.owner_username || s.owner_user_id }}</div>
</div>
</button>
<div class="mt-auto pt-3">
<div class="mb-1 text-sm text-muted">
{{ $t('admin.schedule.lastRun') }}:{{ formatDateTime(s.last_run_at) }}
</div>
<div class="mb-3 text-sm text-muted">
{{ $t('admin.schedule.nextRun') }}:{{ formatDateTime(s.next_run_at) }}
</div>
<div class="flex flex-wrap items-center gap-2">
<button
class="inline-flex items-center gap-1 rounded-xl border border-default px-3 py-2 text-xs font-medium text-secondary transition-colors hover:bg-surface-soft"
@click.stop="openScheduleEditModal(s)"
>
<Edit3 class="h-3.5 w-3.5" />
{{ $t('admin.schedule.actions.edit') }}
</button>
<button
v-if="!s.enabled"
class="inline-flex items-center gap-1 rounded-xl border border-emerald-200 px-3 py-2 text-xs font-medium text-emerald-700 transition-colors hover:bg-emerald-50"
@click.stop="setScheduleEnabled(s.id, true)"
>
<Power class="h-3.5 w-3.5" />
{{ $t('admin.schedule.actions.start') }}
</button>
<button
v-else
class="inline-flex items-center gap-1 rounded-xl border border-amber-200 px-3 py-2 text-xs font-medium text-amber-700 transition-colors hover:bg-amber-50"
@click.stop="setScheduleEnabled(s.id, false)"
>
<PowerOff class="h-3.5 w-3.5" />
{{ $t('admin.schedule.actions.pause') }}
</button>
<button
class="inline-flex items-center gap-1 rounded-xl border border-blue-200 px-3 py-2 text-xs font-medium text-blue-700 transition-colors hover:bg-blue-50"
@click.stop="runScheduleNow(s.id)"
>
<Rocket class="h-3.5 w-3.5" />
{{ $t('admin.schedule.actions.run') }}
</button>
<button
v-if="s.platform_api_path && s.data_source_type !== 'external_api'"
class="inline-flex items-center gap-1 rounded-xl border border-violet-200 px-3 py-2 text-xs font-medium text-violet-700 transition-colors hover:bg-violet-50"
@click.stop="openTestRunDialog(s)"
>
<FlaskConical class="h-3.5 w-3.5" />
{{ $t('admin.schedule.actions.test') }}
</button>
<button
class="inline-flex items-center gap-1 rounded-xl border border-rose-200 px-3 py-2 text-xs font-medium text-rose-600 transition-colors hover:bg-rose-50"
@click.stop="deleteSchedule(s)"
>
<Trash2 class="h-3.5 w-3.5" />
{{ $t('admin.schedule.actions.delete') }}
</button>
</div>
</div>
</div>
<div v-if="schedules.length === 0" class="rounded-xl border border-dashed border-default px-4 py-6 text-center text-sm text-muted">
{{ $t('admin.schedule.emptyList') }}
</div>
</div>
</div>
</div>
</template>
<template v-else>
<div class="space-y-6">
<div class="grid grid-cols-2 gap-3 admin-panel-lg p-4 shadow-sm md:grid-cols-4 xl:grid-cols-7">
<div class="col-span-2 admin-stat-tile px-4 py-3 xl:col-span-3">
<div class="truncate text-base font-semibold text-primary">{{ selectedSchedule?.name || $t('admin.schedule.noSelection') }}</div>
<div class="mt-1 text-xs text-muted">{{ $t('admin.schedule.selectedHint', { time: selectedScheduleLatestRunAt }) }}</div>
</div>
<div class="admin-stat-tile px-4 py-3 text-center">
<div class="text-base font-semibold text-primary">{{ selectedScheduleRuns.length }}</div>
<div class="mt-1 text-xs text-muted">{{ $t('admin.schedule.runBatches') }}</div>
</div>
<div class="admin-stat-tile px-4 py-3 text-center">
<div class="text-base font-semibold text-primary">{{ selectedScheduleRunMetrics.total }}</div>
<div class="mt-1 text-xs text-muted">{{ $t('admin.schedule.totalProcessed') }}</div>
</div>
<div class="admin-stat-tile px-4 py-3 text-center">
<div class="text-base font-semibold text-emerald-700">{{ selectedScheduleRunMetrics.success }}</div>
<div class="mt-1 text-xs text-muted">{{ $t('admin.schedule.totalSuccess') }}</div>
</div>
<div class="admin-stat-tile px-4 py-3 text-center">
<div class="text-base font-semibold text-rose-600">{{ selectedScheduleRunMetrics.failed }}</div>
<div class="mt-1 text-xs text-muted">{{ $t('admin.schedule.totalFailed') }}</div>
</div>
</div>
<div class="admin-panel-lg p-6 shadow-sm">
<div class="grid grid-cols-1 gap-6 lg:grid-cols-[320px_minmax(0,1fr)]">
<div class="admin-panel-lg p-6 shadow-sm">
<div class="mb-4 flex items-center justify-between">
<h4 class="text-lg font-semibold text-primary">{{ $t('admin.schedule.listTitle') }}</h4>
<span class="text-xs text-muted">{{ $t('admin.schedule.switchByCard') }}</span>
</div>
<div class="space-y-3">
<button
v-for="s in schedules"
:key="`workbench-${s.id}`"
class="w-full rounded-2xl border px-4 py-4 text-left transition-all"
:class="selectedScheduleId === s.id ? 'border-blue-300 bg-blue-50/60' : 'border-default bg-surface hover:border-blue-200 hover:bg-surface-soft'"
@click="viewScheduleRuns(s.id)"
>
<div class="flex items-start justify-between gap-3">
<div class="min-w-0">
<div class="truncate font-semibold text-primary">{{ s.name }}</div>
<div class="mt-0.5 text-xs text-muted">{{ s.schedule_no }}</div>
<div class="mt-2 flex flex-wrap items-center gap-2">
<button
class="inline-flex items-center justify-center rounded-xl border border-default p-1.5 text-xs font-medium text-secondary transition-colors hover:bg-surface-soft"
:title="$t('admin.schedule.actions.edit')"
@click.stop="openScheduleEditModal(s)"
>
<Edit3 class="h-3 w-3" />
</button>
<button
v-if="!s.enabled"
class="inline-flex items-center justify-center rounded-xl border border-emerald-200 p-1.5 text-xs font-medium text-emerald-700 transition-colors hover:bg-emerald-50"
:title="$t('admin.schedule.actions.start')"
@click.stop="setScheduleEnabled(s.id, true)"
>
<Power class="h-3 w-3" />
</button>
<button
v-else
class="inline-flex items-center justify-center rounded-xl border border-amber-200 p-1.5 text-xs font-medium text-amber-700 transition-colors hover:bg-amber-50"
:title="$t('admin.schedule.actions.pause')"
@click.stop="setScheduleEnabled(s.id, false)"
>
<PowerOff class="h-3 w-3" />
</button>
<button
class="inline-flex items-center justify-center rounded-xl border border-blue-200 p-1.5 text-xs font-medium text-blue-700 transition-colors hover:bg-blue-50"
:title="$t('admin.schedule.actions.run')"
@click.stop="runScheduleNow(s.id)"
>
<Rocket class="h-3 w-3" />
</button>
<button
v-if="s.platform_api_path && s.data_source_type !== 'external_api'"
class="inline-flex items-center justify-center rounded-xl border border-violet-200 p-1.5 text-xs font-medium text-violet-700 transition-colors hover:bg-violet-50"
:title="$t('admin.schedule.actions.test')"
@click.stop="openTestRunDialog(s)"
>
<FlaskConical class="h-3 w-3" />
</button>
<button
class="inline-flex items-center justify-center rounded-xl border border-rose-200 p-1.5 text-xs font-medium text-rose-600 transition-colors hover:bg-rose-50"
:title="$t('admin.schedule.actions.delete')"
@click.stop="deleteSchedule(s)"
>
<Trash2 class="h-3 w-3" />
</button>
</div>
</div>
<span class="inline-flex shrink-0 items-center whitespace-nowrap rounded-full px-2.5 py-1 text-xs font-semibold"
:class="s.enabled ? 'bg-emerald-100 text-emerald-700' : 'bg-surface-strong text-secondary'">
{{ s.enabled ? $t('admin.schedule.enabled') : $t('admin.schedule.disabled') }}
</span>
</div>
</button>
</div>
</div>
<div>
<div v-if="selectedSchedule" class="admin-panel p-4">
<div class="mb-4 flex flex-wrap items-center justify-between gap-3">
<div>
<h3 class="text-lg font-semibold text-primary">{{ $t('admin.schedule.recordsTitle', { name: selectedSchedule.name }) }}</h3>
<p class="mt-1 text-sm text-muted">{{ $t('admin.schedule.recordsSubtitle') }}</p>
</div>
</div>
<div>
<div class="mb-3 flex flex-wrap items-center gap-2 text-xs">
<label class="text-muted">{{ $t('admin.schedule.filterStart') }}</label>
<input type="datetime-local" step="1" v-model="scheduleRunStartedAfter" class="!min-w-0 !w-[170px] !py-1 !px-2 !text-xs rounded-lg border border-default" />
<label class="text-muted">{{ $t('admin.schedule.filterEnd') }}</label>
<input type="datetime-local" step="1" v-model="scheduleRunStartedBefore" class="!min-w-0 !w-[170px] !py-1 !px-2 !text-xs rounded-lg border border-default" />
<select v-model="scheduleRunStatusFilter" class="!min-w-0 !w-[90px] !py-1 !px-2 !text-xs rounded-lg border border-default">
<option v-for="opt in scheduleRunStatusOptions" :key="opt.value" :value="opt.value">
{{ opt.label }}
</option>
</select>
<button
class="inline-flex items-center rounded-lg bg-brand-600 px-2.5 py-1 text-xs font-medium text-white hover:bg-brand-700"
@click="viewScheduleRuns(selectedSchedule.id)"
>
{{ $t('admin.schedule.query') }}
</button>
<button
class="inline-flex items-center rounded-lg border border-default px-2.5 py-1 text-xs font-medium text-secondary hover:bg-surface-soft"
@click="resetScheduleRunFilters(); viewScheduleRuns(selectedSchedule.id)"
>
{{ $t('admin.schedule.reset') }}
</button>
</div>
<div class="admin-table-wrap max-h-[calc(100vh-20rem)] rounded-xl border border-subtle scrollbar-thin">
<table class="admin-table min-w-[1270px] table-fixed text-xs">
<colgroup>
<col
v-for="col in scheduleRunColumnDefs"
:key="col.key"
:style="{ width: `${col.width}px` }"
/>
</colgroup>
<thead class="sticky top-0 z-10 admin-table-sticky-head">
<tr class="text-muted">
<th
v-for="(col, colIndex) in scheduleRunColumnDefs"
:key="col.key"
class="group relative whitespace-nowrap admin-table-sticky-head px-4 py-3 text-xs font-semibold uppercase select-none"
>
<span class="whitespace-nowrap">{{ col.label }}</span>
<span
class="absolute inset-y-0 -right-1 z-10 w-2 cursor-col-resize"
@mousedown.prevent="startScheduleColResize(colIndex, $event)"
:title="$t('admin.schedule.resizeColumn', { label: col.label })"
></span>
</th>
</tr>
</thead>
<tbody>
<tr v-if="scheduleRunsLoading">
<td colspan="10" class="px-4 py-8 text-center text-sm text-muted">{{ $t('admin.schedule.queryingRuns') }}</td>
</tr>
<tr v-else-if="filteredScheduleRuns.length === 0">
<td colspan="10" class="px-4 py-8 text-center text-sm text-muted">{{ $t('admin.schedule.noRunRecords') }}</td>
</tr>
<tr
v-else
v-for="r in filteredScheduleRuns"
:key="r.id"
class="text-secondary transition"
>
<td class="whitespace-nowrap px-4 py-2.5" :title="r.run_no">{{ r.run_no }}</td>
<td class="whitespace-nowrap px-4 py-2.5" :title="formatDateTime(r.started_at)">{{ formatDateTime(r.started_at) }}</td>
<td class="whitespace-nowrap px-4 py-2.5" :title="formatDateTime(r.finished_at)">{{ formatDateTime(r.finished_at) }}</td>
<td class="whitespace-nowrap px-4 py-2.5">
<span class="inline-flex shrink-0 items-center whitespace-nowrap rounded-full px-2.5 py-1 text-xs font-semibold"
:class="getScheduleRunStatusBadge(r.status).cls">
{{ getScheduleRunStatusBadge(r.status).label }}
</span>
<span v-if="r.error_message" class="ml-1 text-xs text-muted" :title="r.error_message">...</span>
</td>
<td class="whitespace-nowrap px-4 py-2.5">{{ r.total_count }}</td>
<td class="whitespace-nowrap px-4 py-2.5">{{ r.success_count }}</td>
<td class="whitespace-nowrap px-4 py-2.5">{{ r.failed_count }}</td>
<td class="whitespace-nowrap px-4 py-2.5">
<span v-if="r.wecom_notified" class="inline-flex shrink-0 items-center gap-1 whitespace-nowrap text-xs text-emerald-600">
<svg class="h-3.5 w-3.5 shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" /></svg>
{{ $t('admin.schedule.notified') }}
</span>
<span v-else-if="r.wecom_notify_error" class="whitespace-nowrap text-xs text-amber-500" :title="r.wecom_notify_error">{{ $t('admin.schedule.notifyError') }}</span>
<span v-else class="text-xs text-muted">-</span>
</td>
<td class="whitespace-nowrap px-4 py-2.5">
<div class="flex items-center">
<a
v-if="r.total_count > 0"
href="#"
@click.prevent="downloadScheduleExcel(r.id)"
class="inline-flex shrink-0 items-center gap-1 whitespace-nowrap rounded-lg border border-default px-2 py-1 text-xs font-medium text-secondary transition-colors hover:bg-surface-soft"
>
<Download class="h-3 w-3 shrink-0" />
{{ $t('admin.schedule.download') }}
</a>
<span v-if="r.total_count === 0" class="text-muted">-</span>
</div>
</td>
<td class="whitespace-nowrap px-4 py-2.5">
<div class="flex items-center">
<button
v-if="(r.step_logs && r.step_logs.length > 0) || r.dify_execution_snapshot"
@click="expandedScheduleRunId = expandedScheduleRunId === r.id ? null : r.id"
class="inline-flex shrink-0 items-center gap-1 whitespace-nowrap rounded-lg border border-blue-200 px-2 py-1 text-xs font-medium text-blue-600 transition-colors hover:bg-blue-50"
>
<Eye class="h-3 w-3 shrink-0" />
{{ $t('admin.schedule.log') }}
<ChevronDown v-if="expandedScheduleRunId !== r.id" class="h-3 w-3 shrink-0" />
<ChevronUp v-else class="h-3 w-3 shrink-0" />
</button>
<span v-else class="text-muted">-</span>
</div>
</td>
</tr>
<tr
v-if="expandedScheduleRun"
:key="'expanded-' + expandedScheduleRunId"
>
<td colspan="10" class="px-4 py-3 bg-surface-soft/60">
<div class="space-y-3">
<div
v-if="expandedScheduleRun.dify_execution_snapshot"
class="rounded-xl border border-indigo-200 bg-indigo-50/50 p-3"
>
<div class="mb-2 flex items-center justify-between">
<h5 class="text-xs font-bold uppercase text-indigo-800">Dify 执行快照(审计)</h5>
<span class="text-[10px] text-indigo-600/80">
{{ expandedScheduleRun.dify_execution_snapshot.captured_at || '-' }}
</span>
</div>
<div v-if="expandedScheduleRun.dify_execution_snapshot.dify_enabled" class="space-y-2 text-xs text-secondary">
<div>
<span class="font-semibold text-muted">Agent:</span>
{{ expandedScheduleRun.dify_execution_snapshot.dify_agent_name || '-' }}
<span class="text-muted">(ID: {{ expandedScheduleRun.dify_execution_snapshot.dify_agent_id ?? '-' }})</span>
</div>
<div v-if="expandedScheduleRun.dify_execution_snapshot.dify_app_id">
<span class="font-semibold text-muted">Dify App ID:</span>
{{ expandedScheduleRun.dify_execution_snapshot.dify_app_id }}
</div>
<div>
<span class="mb-1 block font-semibold text-muted">System Prompt:</span>
<pre
v-if="expandedScheduleRun.dify_execution_snapshot.dify_system_prompt"
class="max-h-[240px] overflow-auto whitespace-pre-wrap rounded-lg border border-indigo-100 bg-surface p-3 font-mono text-[11px] leading-relaxed text-primary"
>{{ expandedScheduleRun.dify_execution_snapshot.dify_system_prompt }}</pre>
<span v-else class="text-muted">(未配置,使用 Dify 编排内默认提示词)</span>
</div>
</div>
<div v-else class="text-xs text-muted">本次执行未启用 Dify Agent</div>
</div>
<div class="space-y-2">
<div class="flex items-center justify-between mb-2">
<h5 class="text-xs font-bold text-secondary uppercase">{{ $t('admin.schedule.runLogs') }}</h5>
<span class="text-xs text-muted">{{ $t('admin.schedule.stepsCount', { count: expandedScheduleRun.step_logs?.length || 0 }) }}</span>
</div>
<div
v-for="(log, li) in expandedScheduleRun.step_logs || []"
:key="li"
class="rounded-xl border p-3"
:class="log.status === 'failed' ? 'border-rose-200 bg-rose-50/40' : 'border-default bg-surface'"
>
<div class="flex items-center gap-2 mb-1">
<span class="inline-flex items-center rounded-full px-2 py-0.5 text-[10px] font-bold"
:class="(stepStatusMap[log.status] || stepStatusMap.success || { cls: 'bg-surface-soft text-secondary' }).cls">
{{ (stepStatusMap[log.status] || { label: log.status }).label }}
</span>
<span class="text-xs font-semibold text-secondary">{{ stepLabelMap[log.step] || log.step }}</span>
<span v-if="log.row_id" class="text-[10px] text-muted">row_id={{ log.row_id }}</span>
<span v-if="log.duration_ms != null" class="text-[10px] text-muted">{{ log.duration_ms }}ms</span>
</div>
<details v-if="log.detail" class="mt-1">
<summary class="cursor-pointer text-[10px] font-semibold text-muted hover:text-secondary">{{ $t('admin.schedule.viewDetail') }}</summary>
<pre class="admin-pre-block">{{ JSON.stringify(log.detail, null, 2) }}</pre>
</details>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div v-else class="rounded-2xl border border-dashed border-default bg-surface px-4 py-10 text-center text-sm text-muted">
{{ $t('admin.schedule.noSelectionHint') }}
</div>
</div>
</div>
</div>
</div>
</template>
</section>
<!-- Test Run Dialog -->
<div v-if="showTestRunDialog" class="admin-modal-overlay items-center">
<div class="admin-modal-panel max-w-4xl max-h-[90vh] overflow-y-auto">
<div class="admin-modal-header sticky top-0 z-10 px-6 py-4 rounded-t-3xl">
<div>
<h3 class="text-lg font-bold text-primary">{{ $t('admin.schedule.testRun.title', { name: testRunSchedule?.name }) }}</h3>
<p class="text-xs text-muted mt-0.5">{{ $t('admin.schedule.testRun.subtitle') }}</p>
</div>
<button @click="showTestRunDialog = false" class="rounded-xl border border-default px-3 py-1.5 text-sm text-muted hover:bg-surface-soft">{{ $t('admin.schedule.testRun.close') }}</button>
</div>
<div class="p-6 space-y-4">
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">基础查询 SQL (base_fetch_sql) — 只读</label>
<textarea
:value="testRunBaseFetchSql"
readonly
class="w-full min-h-[120px] rounded-xl border border-default bg-surface-soft p-3 text-xs leading-relaxed text-muted font-mono resize-y cursor-default"
></textarea>
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">WHERE 条件 (fetch_where_conditions) — 可修改以测试不同筛选条件</label>
<textarea v-model="testRunWhere"
class="w-full min-h-[80px] rounded-xl border border-amber-200 bg-amber-50/30 p-3 text-xs leading-relaxed text-secondary font-mono focus:border-amber-300 focus:outline-none focus:ring-1 focus:ring-amber-200 resize-y"
></textarea>
</div>
<div class="grid grid-cols-2 gap-3">
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">排序 (fetch_order_by)</label>
<input v-model="testRunOrderBy" class="w-full input-field px-3 py-2 text-xs font-mono text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" />
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">限制行数 (limit)</label>
<input type="number" v-model.number="testRunLimit" min="1" max="10" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" />
</div>
</div>
<div class="flex items-center gap-3">
<button
class="inline-flex items-center gap-1.5 rounded-xl bg-violet-600 px-4 py-2 text-sm font-semibold text-white transition hover:bg-violet-700 disabled:cursor-not-allowed disabled:opacity-60"
:disabled="testRunLoading"
@click="handleTestRun"
>
{{ testRunLoading ? $t('admin.schedule.testRun.running') : $t('admin.schedule.testRun.start') }}
</button>
<span v-if="testRunLoading" class="text-xs text-muted">{{ $t('admin.schedule.testRun.waitingHint') }}</span>
</div>
<div v-if="testRunResult" class="space-y-4 mt-2">
<div class="flex items-center gap-4 rounded-xl border border-default bg-surface-soft/50 px-4 py-3">
<div class="text-center">
<div class="text-lg font-bold text-primary">{{ testRunResult.total_count }}</div>
<div class="text-[10px] text-muted">{{ $t('admin.schedule.testRun.total') }}</div>
</div>
<div class="text-center">
<div class="text-lg font-bold text-emerald-600">{{ testRunResult.success_count }}</div>
<div class="text-[10px] text-muted">{{ $t('admin.schedule.testRun.success') }}</div>
</div>
<div class="text-center">
<div class="text-lg font-bold text-rose-600">{{ testRunResult.failed_count }}</div>
<div class="text-[10px] text-muted">{{ $t('admin.schedule.testRun.failed') }}</div>
</div>
<div class="text-center">
<div class="text-lg font-bold text-amber-600">{{ testRunResult.skipped_count }}</div>
<div class="text-[10px] text-muted">{{ $t('admin.schedule.testRun.skipped') }}</div>
</div>
<div class="ml-auto text-xs text-muted">
{{ $t('admin.schedule.testRun.limitSummary', { limit: testRunResult.limit, agentId: testRunResult.dify_agent_id || $t('admin.schedule.testRun.noAgent') }) }}
</div>
</div>
<div
v-if="testRunResult.dify_execution_snapshot"
class="rounded-xl border border-indigo-200 bg-indigo-50/50 p-3"
>
<h4 class="mb-2 text-sm font-bold text-indigo-900">Dify 执行快照(审计)</h4>
<div v-if="testRunResult.dify_execution_snapshot.dify_enabled" class="space-y-2 text-xs text-secondary">
<div>
<span class="font-semibold text-muted">Agent:</span>
{{ testRunResult.dify_execution_snapshot.dify_agent_name || '-' }}
(ID: {{ testRunResult.dify_execution_snapshot.dify_agent_id ?? '-' }})
</div>
<div>
<span class="mb-1 block font-semibold text-muted">System Prompt:</span>
<pre
v-if="testRunResult.dify_execution_snapshot.dify_system_prompt"
class="max-h-[200px] overflow-auto whitespace-pre-wrap rounded-lg border border-indigo-100 bg-surface p-3 font-mono text-[11px] text-primary"
>{{ testRunResult.dify_execution_snapshot.dify_system_prompt }}</pre>
<span v-else class="text-muted">(未配置)</span>
</div>
</div>
<div v-else class="text-xs text-muted">本次测试未启用 Dify</div>
</div>
<div>
<h4 class="text-sm font-bold text-secondary mb-2">{{ $t('admin.schedule.testRun.stepLogs') }}</h4>
<div class="space-y-2">
<div
v-for="(log, li) in testRunResult.step_logs"
:key="li"
class="rounded-xl border p-3"
:class="log.status === 'failed' ? 'border-rose-200 bg-rose-50/40' : 'border-default bg-surface'"
>
<div class="flex items-center gap-2 mb-1">
<span class="inline-flex items-center rounded-full px-2 py-0.5 text-[10px] font-bold"
:class="(stepStatusMap[log.status] || stepStatusMap.success || { cls: 'bg-surface-soft text-secondary' }).cls">
{{ (stepStatusMap[log.status] || { label: log.status }).label }}
</span>
<span class="text-xs font-semibold text-secondary">{{ stepLabelMap[log.step] || log.step }}</span>
<span v-if="log.row_id" class="text-[10px] text-muted">row_id={{ log.row_id }}</span>
<span v-if="log.duration_ms != null" class="text-[10px] text-muted">{{ log.duration_ms }}ms</span>
</div>
<details v-if="log.detail" class="mt-1">
<summary class="cursor-pointer text-[10px] font-semibold text-muted hover:text-secondary">{{ $t('admin.schedule.viewDetail') }}</summary>
<pre class="admin-pre-block">{{ JSON.stringify(log.detail, null, 2) }}</pre>
</details>
</div>
</div>
</div>
<details>
<summary class="cursor-pointer text-xs font-semibold text-muted hover:text-secondary">{{ $t('admin.schedule.testRun.viewSql') }}</summary>
<pre class="admin-pre-block admin-pre-block--sql">{{ testRunResult.sql }}</pre>
</details>
<details v-if="testRunResult.errors && testRunResult.errors.length > 0">
<summary class="cursor-pointer text-xs font-semibold text-rose-600 hover:text-rose-800">{{ $t('admin.schedule.testRun.errors', { count: testRunResult.errors.length }) }}</summary>
<pre class="mt-1 whitespace-pre-wrap rounded-lg bg-rose-50 p-3 text-xs text-rose-700 font-mono">{{ testRunResult.errors.join('\n') }}</pre>
</details>
</div>
</div>
</div>
</div>
<!-- Schedule Create Modal -->
<div v-if="showScheduleCreateModal" class="admin-modal-overlay items-center">
<div class="admin-modal-panel max-w-3xl">
<div class="flex items-center justify-between border-b border-subtle px-8 py-6">
<h3 class="text-2xl font-bold tracking-tight text-primary">{{ scheduleEditingId ? $t('admin.schedule.form.editTitle') : $t('admin.schedule.form.createTitle') }}</h3>
<button
class="rounded-full p-2 text-muted transition hover-surface-soft hover:text-secondary"
@click="showScheduleCreateModal = false"
>
✕
</button>
</div>
<div class="max-h-[72vh] space-y-5 overflow-y-auto px-8 py-6">
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
<div>
<label class="mb-2 block text-sm font-medium text-secondary">{{ $t('admin.schedule.form.name') }}</label>
<input v-model="scheduleCreateForm.name" class="input-field !text-sm !leading-5" :placeholder="$t('admin.schedule.form.namePlaceholder')" />
</div>
<div>
<label class="mb-2 block text-sm font-medium text-secondary">{{ $t('admin.schedule.form.owner') }}</label>
<div class="relative">
<select v-model="scheduleCreateForm.owner_user_id" class="input-field appearance-none pr-10 !text-sm !leading-5" :disabled="users.length === 0">
<option :value="null">{{ users.length === 0 ? $t('admin.schedule.form.noUsers') : $t('admin.schedule.form.selectOwner') }}</option>
<option v-for="u in users" :key="u.id" :value="u.id">{{ u.real_name || u.username }}{{ u.employee_id ? '(' + u.employee_id + ')' : '' }}</option>
</select>
<div class="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3">
<svg class="h-4 w-4 text-muted" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M5.23 7.21a.75.75 0 011.06.02L10 11.168l3.71-3.938a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z" clip-rule="evenodd"/></svg>
</div>
</div>
</div>
</div>
<!-- SQL Config Panel -->
<div class="rounded-2xl border border-default bg-surface-soft/40 p-4">
<div class="mb-3 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
<h4 class="text-base font-bold text-primary shrink-0">{{ $t('admin.schedule.form.dataSourceConfig') }}</h4>
<div class="flex flex-wrap items-center gap-2">
<div
class="segment-control segment-control--fit segment-control--sm shrink-0"
role="radiogroup"
:aria-label="$t('admin.schedule.form.dataSourceConfig')"
>
<button
type="button"
role="radio"
:aria-checked="scheduleCreateForm.data_source_type === 'database'"
class="segment-item"
:class="scheduleCreateForm.data_source_type === 'database' ? 'segment-item--active' : ''"
@click="scheduleCreateForm.data_source_type = 'database'"
>{{ $t('admin.schedule.form.databaseDirect') }}</button>
<button
type="button"
role="radio"
:aria-checked="scheduleCreateForm.data_source_type === 'external_api'"
class="segment-item"
:class="scheduleCreateForm.data_source_type === 'external_api' ? 'segment-item--active' : ''"
@click="scheduleCreateForm.data_source_type = 'external_api'"
>{{ $t('admin.schedule.form.externalApi') }}</button>
</div>
<button
type="button"
class="btn-secondary btn-sm inline-flex shrink-0 items-center gap-1.5 whitespace-nowrap disabled:cursor-not-allowed disabled:opacity-60"
:disabled="scheduleTestLoading || !scheduleEditingId"
@click="handleTestScheduleSql"
>
{{ scheduleTestLoading ? $t('admin.schedule.form.querying') : $t('admin.schedule.form.testQuery') }}
</button>
<button
type="button"
class="btn-secondary btn-sm inline-flex shrink-0 items-center gap-1.5 whitespace-nowrap"
@click="scheduleConfigCollapsed = !scheduleConfigCollapsed"
>
<ChevronDown v-if="scheduleConfigCollapsed" class="h-3.5 w-3.5 shrink-0" />
<ChevronUp v-else class="h-3.5 w-3.5 shrink-0" />
{{ scheduleConfigCollapsed ? $t('admin.schedule.form.expandConfig') : $t('admin.schedule.form.collapseConfig') }}
</button>
</div>
</div>
<div v-show="!scheduleConfigCollapsed" class="space-y-3">
<!-- 数据库直联模式 -->
<template v-if="scheduleCreateForm.data_source_type === 'database'">
<!-- 数据源配置 -->
<details class="rounded-xl border border-default bg-surface" open>
<summary class="cursor-pointer px-4 py-2.5 text-sm font-semibold text-secondary hover:bg-surface-soft rounded-xl">{{ $t('admin.schedule.form.dataSourceSection') }}</summary>
<div class="space-y-3 px-4 pb-4 pt-2">
<div class="grid grid-cols-2 gap-3">
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">数据库类型</label>
<select v-model="scheduleSqlEditForm.ds_db_type" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200">
<option value="sqlserver">SQL Server</option>
<option value="postgresql">PostgreSQL</option>
<option value="mysql">MySQL</option>
</select>
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">主机地址</label>
<input v-model="scheduleSqlEditForm.ds_host" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" placeholder="如 192.168.1.100 或 ${SRM_SQLSERVER_HOST}" />
</div>
</div>
<div class="grid grid-cols-4 gap-3">
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">端口</label>
<input v-model="scheduleSqlEditForm.ds_port" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" placeholder="1433" />
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">数据库</label>
<input v-model="scheduleSqlEditForm.ds_database" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" placeholder="数据库名" />
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">用户名</label>
<input v-model="scheduleSqlEditForm.ds_username" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" placeholder="sa" />
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">密码</label>
<input v-model="scheduleSqlEditForm.ds_password" type="password" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" placeholder="或 ${ENV_VAR}" />
</div>
</div>
<div v-if="scheduleSqlEditForm.ds_db_type === 'sqlserver'" class="grid grid-cols-3 gap-3">
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">ODBC 驱动</label>
<input v-model="scheduleSqlEditForm.ds_driver" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" placeholder="SQL Server" />
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">Encrypt</label>
<select v-model="scheduleSqlEditForm.ds_encrypt" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200">
<option value="">默认 (no)</option>
<option value="yes">yes</option>
<option value="no">no</option>
</select>
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">TrustServerCertificate</label>
<select v-model="scheduleSqlEditForm.ds_trust_cert" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200">
<option value="">默认 (yes)</option>
<option value="yes">yes</option>
<option value="no">no</option>
</select>
</div>
</div>
<p class="text-xs text-muted">值支持 <code class="rounded bg-surface-soft px-1 py-0.5 text-xs">${环境变量名}</code> 格式,运行时自动从环境变量解析(推荐密码使用此方式)</p>
</div>
</details>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">基础查询 SQL (base_fetch_sql) — SELECT + FROM + JOIN 部分,不含 WHERE</label>
<textarea
v-model="scheduleSqlEditForm.base_fetch_sql"
class="w-full min-h-[160px] input-field p-3 text-xs leading-relaxed text-secondary font-mono focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200 resize-y"
></textarea>
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">WHERE 条件 (fetch_where_conditions) — 可修改以测试不同筛选条件</label>
<textarea
v-model="scheduleSqlEditForm.fetch_where_conditions"
class="w-full min-h-[80px] rounded-xl border border-amber-200 bg-amber-50/30 p-3 text-xs leading-relaxed text-secondary font-mono focus:border-amber-300 focus:outline-none focus:ring-1 focus:ring-amber-200 resize-y"
></textarea>
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">排序 (fetch_order_by)</label>
<input v-model="scheduleSqlEditForm.fetch_order_by" class="w-full input-field px-3 py-2 text-xs font-mono text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" />
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">明细 SQL (detail_sql)</label>
<textarea
v-model="scheduleSqlEditForm.detail_sql"
class="w-full min-h-[120px] input-field p-3 text-xs leading-relaxed text-secondary font-mono focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200 resize-y"
></textarea>
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">回写 SQL (writeback_sql)</label>
<textarea
v-model="scheduleSqlEditForm.writeback_sql"
class="w-full min-h-[48px] input-field p-3 text-xs leading-relaxed text-secondary font-mono focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200 resize-y"
></textarea>
</div>
<div class="grid grid-cols-2 gap-3">
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">主键字段 (row_id_key)</label>
<input v-model="scheduleSqlEditForm.row_id_key" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" />
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">日志前缀 (log_prefix)</label>
<input v-model="scheduleSqlEditForm.log_prefix" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" />
</div>
</div>
<div class="hidden">
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">字段顺序 (alias_order) — 逗号分隔</label>
<input v-model="scheduleSqlEditForm.alias_order_str" class="w-full input-field px-3 py-2 text-xs font-mono text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" />
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">明细字段顺序 (detail_alias_order) — 逗号分隔</label>
<input v-model="scheduleSqlEditForm.detail_alias_order_str" class="w-full input-field px-3 py-2 text-xs font-mono text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" />
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">表头标签 (header_labels) — JSON 格式</label>
<textarea
v-model="scheduleSqlEditForm.header_labels_str"
class="w-full min-h-[100px] input-field p-3 text-xs leading-relaxed text-secondary font-mono focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200 resize-y"
></textarea>
</div>
</template>
<!-- 外部API模式 -->
<template v-if="scheduleCreateForm.data_source_type === 'external_api'">
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">API 地址</label>
<input v-model="scheduleSqlEditForm.api_url" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" placeholder="https://api.example.com/endpoint" />
</div>
<div class="grid grid-cols-2 gap-3">
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">请求方法</label>
<select v-model="scheduleSqlEditForm.api_method" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200">
<option value="POST">POST</option>
<option value="GET">GET</option>
<option value="PUT">PUT</option>
</select>
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">超时(秒)</label>
<input type="number" :value="120" class="w-full input-field px-3 py-2 text-sm text-secondary focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200" disabled />
</div>
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">请求头 (Headers) — JSON 格式</label>
<textarea
v-model="scheduleSqlEditForm.api_headers_str"
class="w-full min-h-[60px] input-field p-3 text-xs leading-relaxed text-secondary font-mono focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200 resize-y"
></textarea>
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">请求体模板 (Body)</label>
<textarea
v-model="scheduleSqlEditForm.api_body_template"
class="w-full min-h-[80px] input-field p-3 text-xs leading-relaxed text-secondary font-mono focus:border-blue-300 focus:outline-none focus:ring-1 focus:ring-blue-200 resize-y"
></textarea>
</div>
</template>
</div>
<div v-if="scheduleTestResult" class="mt-4 rounded-2xl border border-emerald-200 bg-emerald-50/40 p-4">
<div class="mb-3 flex items-center justify-between">
<h4 class="text-sm font-bold text-emerald-900">{{ $t('admin.schedule.testRun.queryResult') }}</h4>
<div class="flex items-center gap-3">
<span class="text-xs text-emerald-600">{{ $t('admin.schedule.testRun.resultSummary', { total: scheduleTestResult.total, showing: scheduleTestResult.showing }) }}</span>
<button class="rounded-full p-1 text-emerald-400 transition hover:bg-emerald-100 hover:text-emerald-600" @click="scheduleTestResult = null">✕</button>
</div>
</div>
<div class="mb-2 overflow-x-auto rounded-xl border border-emerald-200 bg-surface">
<table class="w-full text-xs">
<thead class="bg-emerald-50">
<tr>
<th v-for="col in scheduleTestResult.columns" :key="col" class="whitespace-nowrap px-3 py-2 text-left font-semibold text-emerald-800">{{ col }}</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, ri) in scheduleTestResult.rows" :key="ri" class="border-t border-emerald-100">
<td v-for="col in scheduleTestResult.columns" :key="col" class="whitespace-nowrap px-3 py-1.5 text-secondary">{{ row[col] }}</td>
</tr>
</tbody>
</table>
</div>
<details class="mt-2">
<summary class="cursor-pointer text-xs font-semibold text-emerald-700 hover:text-emerald-900">{{ $t('admin.schedule.testRun.viewGeneratedSql') }}</summary>
<pre class="admin-pre-block admin-pre-block--sql">{{ scheduleTestResult.sql }}</pre>
</details>
</div>
</div>
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
<div>
<label class="mb-2 block text-sm font-medium text-secondary">Agent</label>
<div class="relative">
<select v-model="scheduleCreateForm.dify_agent_id" class="input-field appearance-none pr-10 !text-sm !leading-5" :disabled="agents.length === 0">
<option :value="null">{{ agents.length === 0 ? $t('admin.schedule.form.noAgents') : $t('admin.schedule.form.agentDisabled') }}</option>
<option v-for="a in agents" :key="a.id" :value="a.id">{{ a.name }}(ID: {{ a.dify_app_id?.slice(0, 8) || a.id }})</option>
</select>
<div class="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3">
<svg class="h-4 w-4 text-muted" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M5.23 7.21a.75.75 0 011.06.02L10 11.168l3.71-3.938a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z" clip-rule="evenodd"/></svg>
</div>
</div>
</div>
<div>
<label class="mb-2 block text-sm font-medium text-secondary">{{ $t('admin.schedule.form.waitSeconds') }}</label>
<input v-model.number="scheduleCreateForm.wait_seconds" type="number" min="0" class="input-field !text-sm !leading-5" placeholder="300" />
</div>
</div>
<div v-if="scheduleCreateForm.dify_agent_id" class="rounded-2xl border border-indigo-200 bg-indigo-50/40 p-4">
<div class="mb-2">
<label class="block text-sm font-medium text-secondary">Dify System Prompt</label>
<p class="mt-1 text-xs text-muted">仅在启用 Dify Agent 时可配置。保存后将作为 <code class="rounded bg-surface-soft px-1 py-0.5 text-indigo-700">system_prompt</code> 传入 Workflow;编排中 LLM 节点需引用该变量。</p>
</div>
<textarea
v-model="scheduleSqlEditForm.dify_system_prompt"
class="input-field min-h-[140px] p-3 text-sm font-mono resize-y border-indigo-200"
placeholder="例如:你是物料合规判定助手。根据 record 中的检测数据判定是否合格,仅返回 JSON:dify_result(合格/不合格/人工判定)与 dify_message。"
></textarea>
</div>
<div class="rounded-2xl border border-default bg-surface-soft/40 p-4">
<div class="mb-3">
<h4 class="text-base font-bold text-primary">{{ $t('admin.schedule.form.cronTitle') }}</h4>
<p class="text-sm text-muted">{{ $t('admin.schedule.form.cronHint') }}</p>
</div>
<div
class="segment-control segment-control--cols-4 segment-control--sm"
role="radiogroup"
:aria-label="$t('admin.schedule.form.cronTitle')"
>
<button
type="button"
role="radio"
:aria-checked="scheduleCronDesigner.mode === 'every_minutes'"
class="segment-item"
:class="scheduleCronDesigner.mode === 'every_minutes' ? 'segment-item--active' : ''"
@click="scheduleCronDesigner.mode = 'every_minutes'"
>
{{ $t('admin.schedule.form.everyMinutes') }}
</button>
<button
type="button"
role="radio"
:aria-checked="scheduleCronDesigner.mode === 'hourly'"
class="segment-item"
:class="scheduleCronDesigner.mode === 'hourly' ? 'segment-item--active' : ''"
@click="scheduleCronDesigner.mode = 'hourly'"
>
{{ $t('admin.schedule.form.hourly') }}
</button>
<button
type="button"
role="radio"
:aria-checked="scheduleCronDesigner.mode === 'daily'"
class="segment-item"
:class="scheduleCronDesigner.mode === 'daily' ? 'segment-item--active' : ''"
@click="scheduleCronDesigner.mode = 'daily'"
>
{{ $t('admin.schedule.form.daily') }}
</button>
<button
type="button"
role="radio"
:aria-checked="scheduleCronDesigner.mode === 'weekly'"
class="segment-item"
:class="scheduleCronDesigner.mode === 'weekly' ? 'segment-item--active' : ''"
@click="scheduleCronDesigner.mode = 'weekly'"
>
{{ $t('admin.schedule.form.weekly') }}
</button>
</div>
<div class="mt-4 grid grid-cols-1 gap-3 md:grid-cols-3">
<template v-if="scheduleCronDesigner.mode === 'every_minutes'">
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">{{ $t('admin.schedule.form.minuteInterval') }}</label>
<input v-model.number="scheduleCronDesigner.every_minutes" type="number" min="1" max="59" class="input-field !text-sm !leading-5" />
</div>
</template>
<template v-else-if="scheduleCronDesigner.mode === 'hourly'">
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">{{ $t('admin.schedule.form.minuteOfHour') }}</label>
<input v-model.number="scheduleCronDesigner.hourly_minute" type="number" min="0" max="59" class="input-field !text-sm !leading-5" />
</div>
</template>
<template v-else-if="scheduleCronDesigner.mode === 'daily'">
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">{{ $t('admin.schedule.form.hour') }}</label>
<input v-model.number="scheduleCronDesigner.daily_hour" type="number" min="0" max="23" class="input-field !text-sm !leading-5" />
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">{{ $t('admin.schedule.form.minute') }}</label>
<input v-model.number="scheduleCronDesigner.daily_minute" type="number" min="0" max="59" class="input-field !text-sm !leading-5" />
</div>
</template>
<template v-else>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">{{ $t('admin.schedule.form.weekday') }}</label>
<select v-model.number="scheduleCronDesigner.weekly_day" class="input-field !text-sm !leading-5">
<option v-for="d in weeklyDayOptions" :key="d.value" :value="d.value">{{ d.label }}</option>
</select>
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">{{ $t('admin.schedule.form.hour') }}</label>
<input v-model.number="scheduleCronDesigner.weekly_hour" type="number" min="0" max="23" class="input-field !text-sm !leading-5" />
</div>
<div>
<label class="mb-1 block text-xs font-semibold text-muted uppercase">{{ $t('admin.schedule.form.minute') }}</label>
<input v-model.number="scheduleCronDesigner.weekly_minute" type="number" min="0" max="59" class="input-field !text-sm !leading-5" />
</div>
</template>
</div>
<div class="mt-4 rounded-xl border border-dashed border-default bg-surface-soft px-4 py-3 text-xs text-secondary">
{{ $t('admin.schedule.form.cronPreview') }}:<span class="font-mono text-primary">{{ scheduleCronPreview }}</span>
<span v-if="scheduleNextRunPreview" class="ml-4">{{ $t('admin.schedule.form.nextRun') }}:<span class="font-medium text-primary">{{ formatDateTime(scheduleNextRunPreview.toISOString()) }}</span></span>
</div>
</div>
<div class="input-field px-3 py-2.5">
<div class="flex flex-col gap-2 md:flex-row md:items-center">
<label class="inline-flex shrink-0 items-center gap-2 text-sm text-secondary">
<input v-model="scheduleCreateForm.notify_enabled" type="checkbox" class="h-4 w-4 rounded text-blue-600" />
{{ $t('admin.schedule.form.notifyEnabled') }}
</label>
<div v-if="scheduleCreateForm.notify_enabled" class="relative flex-1">
<div
class="min-h-[38px] cursor-pointer input-field px-3 py-2 text-sm flex flex-wrap gap-1.5 items-center focus-within:border-blue-300 focus-within:ring-1 focus-within:ring-blue-200"
@click="scheduleNotifyDropdownOpen = !scheduleNotifyDropdownOpen"
>
<template v-if="scheduleCreateForm.notify_user_ids_json.length > 0">
<span
v-for="uid in scheduleCreateForm.notify_user_ids_json"
:key="uid"
class="inline-flex items-center gap-1 rounded-lg bg-blue-50 px-2 py-0.5 text-xs font-medium text-blue-700"
>
{{ getUserName(uid) }}
<button
class="ml-0.5 text-blue-400 hover:text-blue-600"
@click.stop="removeNotifyUser(uid)"
>✕</button>
</span>
</template>
<span v-else class="text-muted">{{ $t('admin.schedule.form.selectNotifyUsers') }}</span>
</div>
<div
v-if="scheduleNotifyDropdownOpen"
class="admin-dropdown w-full"
>
<div
v-for="u in users"
:key="u.id"
class="flex cursor-pointer items-center gap-2 px-3 py-2 text-sm transition hover:bg-surface-soft"
:class="scheduleCreateForm.notify_user_ids_json.includes(u.id) ? 'bg-blue-50/50' : ''"
@click="toggleNotifyUser(u.id)"
>
<input
type="checkbox"
class="h-3.5 w-3.5 rounded text-blue-600"
:checked="scheduleCreateForm.notify_user_ids_json.includes(u.id)"
@click.stop
@change.stop="toggleNotifyUser(u.id)"
readonly
/>
<span class="text-secondary">{{ u.real_name || u.username }}{{ u.employee_id ? '(' + u.employee_id + ')' : '' }}</span>
</div>
</div>
</div>
</div>
</div>
<div class="rounded-2xl border border-default bg-surface-soft/40 px-4 py-3">
<label class="inline-flex cursor-pointer items-center gap-2 text-sm text-secondary">
<input v-model="scheduleCreateForm.enabled" type="checkbox" class="admin-checkbox" />
{{ $t('admin.schedule.form.enableOnCreate') }}
</label>
</div>
</div>
<div class="admin-modal-footer admin-modal-footer--muted justify-end gap-3 px-8 py-5">
<button
type="button"
class="btn-secondary"
@click="showScheduleCreateModal = false"
>
{{ $t('admin.schedule.form.cancel') }}
</button>
<button
type="button"
class="btn-primary inline-flex items-center gap-2 disabled:cursor-not-allowed disabled:opacity-60"
:disabled="scheduleCreateSaving || !scheduleCreateForm.name || !scheduleCreateForm.owner_user_id"
@click="createSchedule"
>
<Plus class="h-4 w-4 shrink-0" />
{{ scheduleCreateSaving ? $t('admin.schedule.form.saving') : (scheduleEditingId ? $t('admin.schedule.form.saveChanges') : $t('admin.schedule.form.createSchedule')) }}
</button>
</div>
</div>
</div>
<DeleteConfirmDialog
v-model="showDeleteConfirmDialog"
:title="deleteConfirmTitle"
:message="deleteConfirmMessage"
@cancel="closeDeleteConfirm"
@confirm="confirmDelete"
/>
</template>