Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

65.5 KB
Raw Blame
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onMounted, reactive, ref, watch } from "vue";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { useI18n } from "vue-i18n";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Switch } from "@/components/ui/switch";
import { Check, ChevronDown, ChevronRight, DatabaseBackup, Download, FileInput, FolderOpen, Loader2, Pencil, Play, Plus, RotateCcw, Search, Square, Trash2 } from "@lucide/vue";
import * as api from "@/lib/backend/api";
import { useScheduledDatabaseBackups } from "@/composables/useScheduledDatabaseBackups";
import DatabaseBackupConfigFields from "@/components/backup/DatabaseBackupConfigFields.vue";
import { useToast } from "@/composables/useToast";
import { translateBackendError } from "@/i18n/backend-errors";
import { generateDatabaseExportId } from "@/lib/export/databaseExport";
import {
  DEFAULT_DATABASE_BACKUP_RUN_DIRECTORY_PATTERN,
  DEFAULT_DATABASE_BACKUP_FILE_NAME_PATTERN,
  databaseBackupFileNamePatternHasRunId,
  databaseBackupFileNamePatternIsValid,
  databaseBackupFilePath,
  databaseBackupRunDirectory,
  databaseBackupRunDirectoryPatternIsValid,
  nextDatabaseBackupRunAt,
  normalizeDatabaseBackupRetention,
  normalizeDatabaseBackupTablePatterns,
  supportsNavicatBackupFormat,
  supportsScheduledDatabaseBackup,
  type DatabaseBackupExecutionConfig,
  type DatabaseBackupFile,
  type DatabaseBackupRun,
  type DatabaseBackupSchedule,
} from "@/lib/backup/scheduledDatabaseBackup";
import { databaseBackupTableSelectionScopeKey, normalizeDatabaseBackupTableTargets, type DatabaseBackupTableSelectionState } from "@/lib/backup/scheduledDatabaseBackup";
import { getLastBackupDirectory, setLastBackupDirectory } from "@/lib/export/exportPath";
import { useConnectionStore } from "@/stores/connectionStore";
import { useSettingsStore } from "@/stores/settingsStore";
import { fetchNamespaceOptionsForConnection } from "@/composables/useDatabaseOptions";

const { t, locale } = useI18n();
const { toast } = useToast();
const connectionStore = useConnectionStore();
const settingsStore = useSettingsStore();
const {
  schedules,
  runs,
  activeScheduleIds,
  activeRunIds,
  cancellingRunIds,
  activeRuns,
  heartbeat,
  destinationRoot,
  error: backupError,
  saveSchedule,
  setScheduleEnabled,
  deleteSchedule,
  deleteRuns,
  importBackupFiles,
  renameRun,
  runSchedule,
  runOneShot,
  runOneShotBatch,
  cancelRun,
} = useScheduledDatabaseBackups();
const desktop = isTauriRuntime();
const backgroundEnabled = ref(false);
const backgroundBusy = ref(false);
onMounted(async () => {
  try {
    backgroundEnabled.value = (await api.databaseBackupBackground()).enabled;
  } catch (error) {
    toast(String(error), 5000);
  }
});
async function changeBackground(enabled: boolean) {
  backgroundBusy.value = true;
  try {
    backgroundEnabled.value = (await api.databaseBackupBackground(enabled)).enabled;
  } catch (error) {
    toast(String(error), 5000);
  } finally {
    backgroundBusy.value = false;
  }
}
async function changeScheduleEnabled(id: string, enabled: boolean) {
  try {
    await setScheduleEnabled(id, enabled);
  } catch (error) {
    toast(String(error), 5000);
  }
}

const scheduleDialogOpen = ref(false);
const oneShotDialogOpen = ref(false);
const deleteScheduleDialogOpen = ref(false);
const deleteRunDialogOpen = ref(false);
const renameRunDialogOpen = ref(false);
const editingScheduleId = ref("");
const pendingDeleteSchedule = ref<DatabaseBackupSchedule | null>(null);
const pendingDeleteRunIds = ref<string[]>([]);
const pendingRenameRun = ref<DatabaseBackupRun | null>(null);
const renameRunName = ref("");
const loadingDatabases = ref(false);
const databaseLoadError = ref("");
const saving = ref(false);
const oneShotStarting = ref(false);
const databaseOptions = ref<string[]>([]);
const allDatabases = ref(true);
const selectedDatabases = ref<string[]>([]);
const tablePatternsInput = ref("");
const scheduleTableSelectionState = ref<DatabaseBackupTableSelectionState>({ scopeKey: "", ready: false });
const oneShotTableSelectionState = ref<DatabaseBackupTableSelectionState>({ scopeKey: "", ready: false });
function tableScopeReady(config: DatabaseBackupExecutionConfig, state: DatabaseBackupTableSelectionState): boolean {
  if (config.tableFilterMode !== "selected") return config.tableFilterMode === "all" || normalizeDatabaseBackupTablePatterns(tablePatternsInput.value).length > 0;
  const targets = config.selectedTables ?? [];
  return (
    !allDatabases.value &&
    selectedDatabases.value.length > 0 &&
    targets.length > 0 &&
    targets.every((target) => selectedDatabases.value.includes(target.database)) &&
    state.ready &&
    state.scopeKey === databaseBackupTableSelectionScopeKey(config.connectionId, connectionStore.getConfig(config.connectionId)?.db_type, selectedDatabases.value)
  );
}
function backupTablePayload(config: DatabaseBackupExecutionConfig) {
  return {
    tablePatterns: config.tableFilterMode === "include" || config.tableFilterMode === "exclude" ? normalizeDatabaseBackupTablePatterns(tablePatternsInput.value) : [],
    // Override any inactive selection carried by the draft before JSON serialization.
    selectedTables: config.tableFilterMode === "selected" ? normalizeDatabaseBackupTableTargets(config.selectedTables) : undefined,
  };
}
const expandedRunIds = reactive(new Set<string>());
const selectedRunIds = reactive(new Set<string>());
const historyConnectionId = ref("");
const historyConnectionPickerOpen = ref(false);
const historyConnectionSearch = ref("");
const historyBackupMethod = ref<"all" | ReturnType<typeof runBackupMethod>>("all");
const historyStatus = ref<"all" | DatabaseBackupRun["status"]>("all");

const sqlConnections = computed(() => connectionStore.connections.filter((connection) => supportsScheduledDatabaseBackup(connection.db_type)));
const canCreateSchedule = computed(() => sqlConnections.value.length > 0);
const sortedRuns = computed(() => [...runs.value].sort((left, right) => Date.parse(right.startedAt) - Date.parse(left.startedAt)));
const historyConnections = computed(() => {
  const connectionById = new Map<string, string>();
  for (const run of sortedRuns.value) {
    // Imported backup files are not tied to a saved connection.
    if (run.connectionId && !connectionById.has(run.connectionId)) connectionById.set(run.connectionId, run.connectionName || connectionName(run.connectionId));
  }
  return [...connectionById].map(([id, name]) => ({ id, name })).sort((left, right) => left.name.localeCompare(right.name, locale.value));
});
const filteredHistoryConnections = computed(() => {
  const query = historyConnectionSearch.value.trim().toLocaleLowerCase();
  if (!query) return historyConnections.value;
  return historyConnections.value.filter((connection) => connection.name.toLocaleLowerCase().includes(query));
});
const filteredRuns = computed(() =>
  sortedRuns.value.filter((run) => {
    if (historyConnectionId.value && run.connectionId !== historyConnectionId.value) return false;
    if (historyBackupMethod.value !== "all" && historyBackupMethod.value !== runBackupMethod(run)) return false;
    return historyStatus.value === "all" || historyStatus.value === run.status;
  }),
);
const selectableFilteredRuns = computed(() => filteredRuns.value.filter((run) => !activeRunIds.has(run.id)));
const selectedRuns = computed(() => sortedRuns.value.filter((run) => selectedRunIds.has(run.id) && !activeRunIds.has(run.id)));
const allSelectableRunsSelected = computed(() => selectableFilteredRuns.value.length > 0 && selectableFilteredRuns.value.every((run) => selectedRunIds.has(run.id)));
const selectedHistoryConnectionName = computed(() => historyConnections.value.find((connection) => connection.id === historyConnectionId.value)?.name || t("databaseBackup.allConnections"));
const weekdays = computed(() => [
  { value: 0, label: t("databaseBackup.weekdays.sunday") },
  { value: 1, label: t("databaseBackup.weekdays.monday") },
  { value: 2, label: t("databaseBackup.weekdays.tuesday") },
  { value: 3, label: t("databaseBackup.weekdays.wednesday") },
  { value: 4, label: t("databaseBackup.weekdays.thursday") },
  { value: 5, label: t("databaseBackup.weekdays.friday") },
  { value: 6, label: t("databaseBackup.weekdays.saturday") },
]);

function handleHistoryScrollOutside(event: Event) {
  if (!historyConnectionPickerOpen.value) return;
  const target = event.target;
  if (target instanceof HTMLElement && target.closest?.('[data-slot="popover-content"]')) return;
  historyConnectionPickerOpen.value = false;
}

watch(historyConnectionPickerOpen, (open) => {
  if (!open) {
    window.removeEventListener("scroll", handleHistoryScrollOutside, { capture: true });
    historyConnectionSearch.value = "";
    return;
  }
  window.addEventListener("scroll", handleHistoryScrollOutside, { capture: true, passive: true });
});

onBeforeUnmount(() => {
  window.removeEventListener("scroll", handleHistoryScrollOutside, { capture: true });
});

watch(scheduleDialogOpen, async (open) => {
  if (open) {
    await nextTick();
    const dialogs = document.querySelectorAll<HTMLElement>(".dbx-backup-dialog");
    for (const dialog of dialogs) dialog.scrollTop = 0;
  }
});

watch(oneShotDialogOpen, async (open) => {
  if (open) {
    await nextTick();
    const dialogs = document.querySelectorAll<HTMLElement>(".dbx-backup-dialog");
    for (const dialog of dialogs) dialog.scrollTop = 0;
  }
});

/**
 * 解析新建备份时的默认「备份目录」。
 * 优先级:服务端根目录(Web 端) > 上次备份目录 > 设置中的「首选导出路径」。
 * Desktop 端后端 root 恒为 null(见 src-tauri/background_backup.rs),
 * 此前默认值恒为空串,导致每次新建备份都要重新选择目录(issue #11317)。
 */
function defaultBackupDestinationDirectory(): string {
  if (destinationRoot.value) return destinationRoot.value;
  const remembered = getLastBackupDirectory();
  if (remembered) return remembered;
  return settingsStore.editorSettings.preferredExportPath?.trim() || "";
}

function newBackupConfig(connectionId = sqlConnections.value[0]?.id ?? ""): DatabaseBackupExecutionConfig {
  return {
    connectionId,
    databases: [],
    tableFilterMode: "all",
    tablePatterns: [],
    destinationDirectory: defaultBackupDestinationDirectory(),
    includeStructure: true,
    includeData: true,
    includeObjects: true,
    dropTableIfExists: false,
    outputCompression: "none",
    fileNamePattern: DEFAULT_DATABASE_BACKUP_FILE_NAME_PATTERN,
  };
}

function newScheduleDraft(connectionId = sqlConnections.value[0]?.id ?? ""): DatabaseBackupSchedule {
  const now = new Date();
  const draft: DatabaseBackupSchedule = {
    ...newBackupConfig(connectionId),
    id: generateDatabaseExportId(),
    name: t("databaseBackup.defaultScheduleName"),
    enabled: true,
    frequency: "daily",
    intervalHours: 6,
    timeOfDay: "02:00",
    timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
    weekday: 1,
    retentionCount: 10,
    runDirectoryPattern: DEFAULT_DATABASE_BACKUP_RUN_DIRECTORY_PATTERN,
    createdAt: now.toISOString(),
    updatedAt: now.toISOString(),
    nextRunAt: "",
  };
  draft.nextRunAt = nextDatabaseBackupRunAt(draft, now).toISOString();
  return draft;
}

const draft = ref<DatabaseBackupSchedule>(newScheduleDraft());
const previousPositiveRetention = ref(10);

function togglePermanentRetention(forever: boolean) {
  if (forever) {
    if (draft.value.retentionCount > 0) {
      previousPositiveRetention.value = draft.value.retentionCount;
    }
    draft.value.retentionCount = 0;
  } else {
    draft.value.retentionCount = previousPositiveRetention.value > 0 ? previousPositiveRetention.value : 10;
  }
}
const oneShotDraft = ref<DatabaseBackupExecutionConfig>(newBackupConfig());
const activeDraft = computed<DatabaseBackupExecutionConfig>(() => (oneShotDialogOpen.value ? oneShotDraft.value : draft.value));
const activeOneShotRun = computed(() => activeRuns.value.find((run) => run.source === "one-shot"));
// A one-shot backup may target several connections; each one is queued as its own run, which the backend executes in turn.
const oneShotConnectionIds = ref<string[]>([]);
const oneShotBatchRunning = ref(false);
const oneShotBatchRunIds = reactive(new Set<string>());
let oneShotBatchCancelRequested = false;
const multipleOneShotConnections = computed(() => oneShotConnectionIds.value.length > 1);
// Cancel acts on the runs this dialog started: every queued run of a batch, or the active one-shot run otherwise.
const cancellableOneShotRuns = computed(() => (oneShotBatchRunning.value ? activeRuns.value.filter((run) => oneShotBatchRunIds.has(run.id)) : activeOneShotRun.value ? [activeOneShotRun.value] : []));
type BackupDialogKind = "schedule" | "one-shot";
let databaseLoadGeneration = 0;

function databaseLoadIsCurrent(generation: number, dialog: BackupDialogKind, targetDraft: DatabaseBackupExecutionConfig, connectionId: string): boolean {
  if (generation !== databaseLoadGeneration) return false;
  if (dialog === "one-shot") return oneShotDialogOpen.value && oneShotDraft.value === targetDraft && targetDraft.connectionId === connectionId;
  return scheduleDialogOpen.value && draft.value === targetDraft && targetDraft.connectionId === connectionId;
}

// `.nb3` is a MySQL/PostgreSQL-only format; keep a saved or newly selected value from being submitted for other connections.
const scheduleNavicatFormatSupported = computed(() => supportsNavicatBackupFormat([connectionStore.getConfig(draft.value.connectionId)?.db_type]));
const oneShotNavicatFormatSupported = computed(() => supportsNavicatBackupFormat(oneShotConnectionIds.value.map((connectionId) => connectionStore.getConfig(connectionId)?.db_type)));

const canSave = computed(() => {
  const hasContent = draft.value.includeStructure || draft.value.includeData || draft.value.includeObjects;
  const hasDatabaseScope = allDatabases.value || selectedDatabases.value.length > 0;
  const hasTableScope = tableScopeReady(draft.value, scheduleTableSelectionState.value);
  return (
    !!draft.value.name.trim() &&
    !!draft.value.connectionId &&
    !!draft.value.destinationDirectory.trim() &&
    databaseBackupRunDirectoryPatternIsValid(draft.value.runDirectoryPattern || "") &&
    databaseBackupFileNamePatternIsValid(draft.value.fileNamePattern || "") &&
    (draft.value.outputCompression !== "nb3" || scheduleNavicatFormatSupported.value) &&
    hasContent &&
    hasDatabaseScope &&
    hasTableScope &&
    !saving.value &&
    !loadingDatabases.value
  );
});
const nextRunPreview = ref(new Date());
const previewError = ref("");
const previewZone = ref(draft.value.timeZone);
watch(
  () => [draft.value.frequency, draft.value.intervalHours, draft.value.timeOfDay, draft.value.weekday, draft.value.timeZone],
  async (_, __, onCleanup) => {
    let current = true;
    onCleanup(() => {
      current = false;
    });
    try {
      const time = await api.databaseBackupCommand<string>({ action: "preview", schedule: draft.value });
      if (current) {
        nextRunPreview.value = new Date(time);
        previewZone.value = draft.value.timeZone;
        previewError.value = "";
      }
    } catch (error) {
      if (current) previewError.value = String(error);
    }
  },
  { immediate: true },
);
const scheduleOutputPathPreview = computed(() => {
  if (!draft.value.destinationDirectory.trim() || !databaseBackupRunDirectoryPatternIsValid(draft.value.runDirectoryPattern || "") || !databaseBackupFileNamePatternIsValid(draft.value.fileNamePattern || "")) return "";
  const directory = databaseBackupRunDirectory(draft.value.destinationDirectory, draft.value.runDirectoryPattern || "", draft.value.name.trim() || t("databaseBackup.defaultScheduleName"), nextRunPreview.value, "preview01", previewZone.value);
  return databaseBackupFilePath(directory, draft.value.name.trim() || t("databaseBackup.defaultScheduleName"), "database", nextRunPreview.value, "preview01", draft.value.outputCompression, draft.value.fileNamePattern, previewZone.value);
});
const oneShotOutputPathPreview = computed(() => {
  if (!oneShotDraft.value.destinationDirectory.trim() || !databaseBackupFileNamePatternIsValid(oneShotDraft.value.fileNamePattern || "")) return "";
  return databaseBackupFilePath(oneShotDraft.value.destinationDirectory, t("databaseBackup.oneShotName"), "database", new Date(), "preview01", oneShotDraft.value.outputCompression, oneShotDraft.value.fileNamePattern);
});
const canStartOneShot = computed(() => {
  const multiple = multipleOneShotConnections.value;
  const hasContent = oneShotDraft.value.includeStructure || oneShotDraft.value.includeData || oneShotDraft.value.includeObjects;
  // Database names and exact table lists are per connection, so several connections always back up all databases.
  const hasDatabaseScope = multiple || allDatabases.value || selectedDatabases.value.length > 0;
  const hasTableScope = tableScopeReady(oneShotDraft.value, oneShotTableSelectionState.value);
  // Runs started together share a timestamp, so only {runId} keeps their files apart.
  const hasUniqueFileNames = !multiple || databaseBackupFileNamePatternHasRunId(oneShotDraft.value.fileNamePattern);
  const hasSupportedFormat = oneShotDraft.value.outputCompression !== "nb3" || oneShotNavicatFormatSupported.value;
  return (
    !!oneShotDraft.value.connectionId &&
    !!oneShotDraft.value.destinationDirectory.trim() &&
    databaseBackupFileNamePatternIsValid(oneShotDraft.value.fileNamePattern || "") &&
    hasContent &&
    hasDatabaseScope &&
    hasTableScope &&
    hasUniqueFileNames &&
    hasSupportedFormat &&
    !oneShotStarting.value &&
    !loadingDatabases.value
  );
});

function connectionName(connectionId: string): string {
  return connectionStore.getConfig(connectionId)?.name || t("databaseBackup.missingConnection");
}

function selectHistoryConnection(connectionId: string) {
  historyConnectionId.value = connectionId;
  historyConnectionPickerOpen.value = false;
  historyConnectionSearch.value = "";
}

function runBackupMethod(run: DatabaseBackupRun): "manual" | "scheduled" | "one-shot" | "imported" {
  if (run.source === "one-shot" || run.source === "imported") return run.source;
  return run.trigger;
}

function runBackupMethodLabel(run: DatabaseBackupRun): string {
  const method = runBackupMethod(run);
  if (method === "one-shot") return t("databaseBackup.oneShotTrigger");
  if (method === "imported") return t("databaseBackup.importedTrigger");
  return method === "scheduled" ? t("databaseBackup.scheduledTrigger") : t("databaseBackup.manualTrigger");
}

function formatDate(value?: string): string {
  if (!value || !Number.isFinite(Date.parse(value))) return t("databaseBackup.never");
  return new Intl.DateTimeFormat(locale.value, { dateStyle: "medium", timeStyle: "short" }).format(new Date(value));
}

function frequencyLabel(schedule: DatabaseBackupSchedule): string {
  if (schedule.frequency === "hourly") return t("databaseBackup.everyHours", { count: schedule.intervalHours });
  if (schedule.frequency === "daily") return t("databaseBackup.dailyAt", { time: schedule.timeOfDay });
  return t("databaseBackup.weeklyAt", {
    weekday: weekdays.value.find((item) => item.value === schedule.weekday)?.label ?? "",
    time: schedule.timeOfDay,
  });
}

function databaseScopeLabel(schedule: DatabaseBackupSchedule): string {
  if (schedule.databases.length === 0) return t("databaseBackup.allDatabases");
  if (schedule.databases.length === 1) return schedule.databases[0]!;
  return t("databaseBackup.databaseCount", { count: schedule.databases.length });
}

function tableScopeLabel(schedule: DatabaseBackupSchedule): string {
  if (schedule.tableFilterMode === "include") return t("databaseBackup.includedTablePatterns", { count: schedule.tablePatterns.length });
  if (schedule.tableFilterMode === "exclude") return t("databaseBackup.excludedTablePatterns", { count: schedule.tablePatterns.length });
  return "";
}

function runStatusLabel(status: DatabaseBackupRun["status"]): string {
  return t(`databaseBackup.status.${status}`);
}

function displayedRunStatusLabel(run: DatabaseBackupRun): string {
  return cancellingRunIds.has(run.id) ? t("databaseBackup.cancelling") : runStatusLabel(run.status);
}

function runStatusVariant(status: DatabaseBackupRun["status"]): "default" | "secondary" | "destructive" | "outline" {
  if (status === "success") return "default";
  if (status === "failed") return "destructive";
  if (status === "running") return "secondary";
  return "outline";
}

function activeRunForSchedule(scheduleId: string): DatabaseBackupRun | undefined {
  return activeRuns.value.find((run) => run.scheduleId === scheduleId);
}

function scheduleCancellationRequested(scheduleId: string): boolean {
  const run = activeRunForSchedule(scheduleId);
  return !!run && cancellingRunIds.has(run.id);
}

function oneShotCancellationRequested(): boolean {
  const runs = cancellableOneShotRuns.value;
  return runs.length > 0 && runs.every((run) => cancellingRunIds.has(run.id));
}

async function loadDatabases(dialog: BackupDialogKind, targetDraft: DatabaseBackupExecutionConfig, preserveSelection: boolean) {
  const generation = ++databaseLoadGeneration;
  const connectionId = targetDraft.connectionId;
  databaseOptions.value = [];
  databaseLoadError.value = "";
  if (!preserveSelection) {
    selectedDatabases.value = [];
    allDatabases.value = true;
    targetDraft.tableFilterMode = "all";
    targetDraft.tablePatterns = [];
    targetDraft.selectedTables = [];
    tablePatternsInput.value = "";
  }
  if (!connectionId) {
    loadingDatabases.value = false;
    return;
  }
  loadingDatabases.value = true;
  try {
    const loadNames = async () => {
      await connectionStore.ensureConnected(connectionId);
      const config = connectionStore.getConfig(connectionId);
      return config?.db_type === "dameng" ? await fetchNamespaceOptionsForConnection(connectionId, config) : (await api.listDatabases(connectionId)).map((database) => database.name);
    };
    let names: string[];
    try {
      names = await loadNames();
    } catch (error) {
      // A connection can be closed by MySQL after the store's recent health
      // check. Mark it stale and retry the read once through a fresh pool.
      if (!connectionStore.recordConnectionLostError(connectionId, error)) throw error;
      names = await loadNames();
    }
    if (!databaseLoadIsCurrent(generation, dialog, targetDraft, connectionId)) return;
    databaseOptions.value = names;
    if (preserveSelection) {
      const selected = new Set(selectedDatabases.value);
      selectedDatabases.value = names.filter((database) => selected.has(database));
    }
  } catch {
    if (databaseLoadIsCurrent(generation, dialog, targetDraft, connectionId)) {
      databaseLoadError.value = t("databaseBackup.databaseListLoadFailed", { name: connectionName(connectionId) });
    }
  } finally {
    if (generation === databaseLoadGeneration) loadingDatabases.value = false;
  }
}

function resetDatabaseScope(targetDraft: DatabaseBackupExecutionConfig) {
  scheduleTableSelectionState.value = { scopeKey: "", ready: false };
  oneShotTableSelectionState.value = { scopeKey: "", ready: false };
  databaseOptions.value = [];
  databaseLoadError.value = "";
  allDatabases.value = true;
  selectedDatabases.value = [];
  targetDraft.tableFilterMode = "all";
  targetDraft.tablePatterns = [];
  targetDraft.selectedTables = [];
  tablePatternsInput.value = "";
}

async function openCreateSchedule() {
  oneShotDialogOpen.value = false;
  editingScheduleId.value = "";
  const nextDraft = newScheduleDraft();
  draft.value = nextDraft;
  previousPositiveRetention.value = nextDraft.retentionCount > 0 ? nextDraft.retentionCount : 10;
  resetDatabaseScope(nextDraft);
  scheduleDialogOpen.value = true;
}

async function openEditSchedule(schedule: DatabaseBackupSchedule) {
  scheduleTableSelectionState.value = { scopeKey: "", ready: false };
  oneShotDialogOpen.value = false;
  editingScheduleId.value = schedule.id;
  draft.value = { ...schedule, databases: [...schedule.databases], tablePatterns: [...schedule.tablePatterns] };
  previousPositiveRetention.value = schedule.retentionCount > 0 ? schedule.retentionCount : 10;
  allDatabases.value = schedule.databases.length === 0;
  selectedDatabases.value = [...schedule.databases];
  tablePatternsInput.value = schedule.tablePatterns.join(", ");
  databaseLoadError.value = "";
  scheduleDialogOpen.value = true;
  if (!allDatabases.value) await loadDatabases("schedule", draft.value, true);
}

async function changeConnection(connectionId: string) {
  const dialog: BackupDialogKind = oneShotDialogOpen.value ? "one-shot" : "schedule";
  const targetDraft = dialog === "one-shot" ? oneShotDraft.value : draft.value;
  const wasSelectingSpecificDatabases = !allDatabases.value;
  targetDraft.connectionId = connectionId;
  databaseOptions.value = [];
  databaseLoadError.value = "";
  selectedDatabases.value = [];
  targetDraft.tableFilterMode = "all";
  targetDraft.tablePatterns = [];
  targetDraft.selectedTables = [];
  tablePatternsInput.value = "";
  if (!wasSelectingSpecificDatabases) {
    allDatabases.value = true;
    return;
  }
  allDatabases.value = false;
  await loadDatabases(dialog, targetDraft, true);
}

function toggleOneShotConnection(connectionId: string) {
  const current = oneShotConnectionIds.value;
  const selected = new Set(current);
  if (selected.has(connectionId)) selected.delete(connectionId);
  else selected.add(connectionId);
  // Keep the picker order stable and never leave the form without a connection.
  const next = sqlConnections.value.map((connection) => connection.id).filter((id) => selected.has(id));
  if (next.length === 0) return;
  const targetDraft = oneShotDraft.value;
  if (next.length > 1 && current.length <= 1) {
    // Invalidate any in-flight database list load and drop the per-connection scope.
    databaseLoadGeneration++;
    loadingDatabases.value = false;
    databaseOptions.value = [];
    databaseLoadError.value = "";
    allDatabases.value = true;
    selectedDatabases.value = [];
    if (targetDraft.tableFilterMode === "selected") targetDraft.tableFilterMode = "all";
    targetDraft.selectedTables = [];
    oneShotTableSelectionState.value = { scopeKey: "", ready: false };
  }
  oneShotConnectionIds.value = next;
  targetDraft.connectionId = next[0]!;
}

async function setAllDatabases(value: boolean) {
  allDatabases.value = value;
  databaseLoadError.value = "";
  if (value) {
    selectedDatabases.value = [];
    return;
  }
  const dialog: BackupDialogKind = oneShotDialogOpen.value ? "one-shot" : "schedule";
  const targetDraft = dialog === "one-shot" ? oneShotDraft.value : draft.value;
  await loadDatabases(dialog, targetDraft, true);
}

function toggleDatabase(database: string) {
  const selected = new Set(selectedDatabases.value);
  if (selected.has(database)) selected.delete(database);
  else selected.add(database);
  selectedDatabases.value = databaseOptions.value.filter((item) => selected.has(item));
}

function updateSelectedDatabases(databases: string[]) {
  const selected = new Set(databases);
  selectedDatabases.value = databaseOptions.value.filter((item) => selected.has(item));
}

async function chooseDestination() {
  const { open } = await import("@tauri-apps/plugin-dialog");
  // 传入当前目录,让系统选择框直接定位到上次使用的位置(issue #11317)
  const selected = await open({ directory: true, multiple: false, defaultPath: activeDraft.value.destinationDirectory || undefined, title: t("databaseBackup.selectDestination") });
  if (typeof selected === "string") {
    activeDraft.value.destinationDirectory = selected;
    // 记住本次选择,作为下次新建备份的默认目录
    setLastBackupDirectory(selected);
  }
}

async function submitSchedule() {
  if (!canSave.value || previewError.value) return;
  saving.value = true;
  try {
    if (desktop) await api.recordDatabaseExportDestination(draft.value.destinationDirectory);
    if (desktop) setLastBackupDirectory(draft.value.destinationDirectory);
    await saveSchedule({
      ...draft.value,
      retentionCount: normalizeDatabaseBackupRetention(draft.value.retentionCount),
      databases: allDatabases.value ? [] : [...selectedDatabases.value],
      ...backupTablePayload(draft.value),
    });
    scheduleDialogOpen.value = false;
    toast(t(editingScheduleId.value ? "databaseBackup.scheduleUpdated" : "databaseBackup.scheduleCreated"), 2500);
  } catch (error: any) {
    toast(error?.message || String(error), 5000);
  } finally {
    saving.value = false;
  }
}

async function openOneShotBackup() {
  oneShotTableSelectionState.value = { scopeKey: "", ready: false };
  scheduleDialogOpen.value = false;
  const nextDraft = newBackupConfig();
  oneShotDraft.value = nextDraft;
  oneShotConnectionIds.value = nextDraft.connectionId ? [nextDraft.connectionId] : [];
  resetDatabaseScope(nextDraft);
  oneShotDialogOpen.value = true;
}

function toastRunResult(run: DatabaseBackupRun) {
  if (run.status === "success") toast(t("databaseBackup.runSuccess", { count: run.files.length }), 3000);
  else if (run.status === "cancelled") toast(t("databaseBackup.runCancelled"), 3000);
  else toast(t("databaseBackup.runFailed", { error: run.error ? translateBackendError(t, run.error) : t("databaseBackup.unknownError") }), 5000);
}

async function startOneShotBackup() {
  if (!canStartOneShot.value) return;
  oneShotStarting.value = true;
  try {
    if (desktop) await api.recordDatabaseExportDestination(oneShotDraft.value.destinationDirectory);
    if (desktop) setLastBackupDirectory(oneShotDraft.value.destinationDirectory);
    if (multipleOneShotConnections.value) {
      await startOneShotBatch();
      return;
    }
    const run = await runOneShot(
      {
        ...oneShotDraft.value,
        databases: allDatabases.value ? [] : [...selectedDatabases.value],
        ...backupTablePayload(oneShotDraft.value),
      },
      t("databaseBackup.oneShotName"),
    );
    if (!run) return;
    oneShotDialogOpen.value = false;
    toastRunResult(run);
  } catch (error: any) {
    toast(translateBackendError(t, error), 5000);
  } finally {
    oneShotStarting.value = false;
  }
}

async function startOneShotBatch() {
  oneShotBatchRunning.value = true;
  oneShotBatchCancelRequested = false;
  try {
    const configs = oneShotConnectionIds.value.map((connectionId) => ({
      ...oneShotDraft.value,
      connectionId,
      databases: [],
      ...backupTablePayload(oneShotDraft.value),
    }));
    const { runs: finished, enqueueErrors } = await runOneShotBatch(configs, t("databaseBackup.oneShotName"), (run) => {
      oneShotBatchRunIds.add(run.id);
      // Cancel was requested while later connections were still being queued.
      if (oneShotBatchCancelRequested) void cancelRun(run.id).catch(() => {});
    });
    if (finished.length === 0 && enqueueErrors.length > 0) {
      toast(translateBackendError(t, enqueueErrors[0]!.error), 5000);
      return;
    }
    oneShotDialogOpen.value = false;
    const count = (status: DatabaseBackupRun["status"]) => finished.filter((run) => run.status === status).length;
    toast(t("databaseBackup.batchRunSummary", { success: count("success"), failed: count("failed") + enqueueErrors.length, cancelled: count("cancelled") }), 5000);
  } finally {
    oneShotBatchRunning.value = false;
    oneShotBatchRunIds.clear();
  }
}

async function cancelActiveOneShotBackup() {
  oneShotBatchCancelRequested = true;
  const runs = cancellableOneShotRuns.value.filter((run) => !cancellingRunIds.has(run.id));
  const results = await Promise.all(runs.map((run) => cancelRun(run.id)));
  if (results.some(Boolean)) toast(t("databaseBackup.cancelRequested"), 2500);
}

async function requestCancelRun(runId: string) {
  if (await cancelRun(runId)) toast(t("databaseBackup.cancelRequested"), 2500);
}

async function confirmLegacyDestination(schedule: DatabaseBackupSchedule): Promise<DatabaseBackupSchedule | null> {
  if (!desktop) return schedule;
  if (!(await api.databaseExportDestinationNeedsConfirmation(schedule.destinationDirectory))) return schedule;

  const { open } = await import("@tauri-apps/plugin-dialog");
  const selected = await open({
    directory: true,
    multiple: false,
    defaultPath: schedule.destinationDirectory,
    title: t("databaseBackup.selectDestination"),
  });
  if (typeof selected !== "string") return null;

  await api.recordDatabaseExportDestination(selected);
  if (selected === schedule.destinationDirectory) return schedule;
  return saveSchedule({ ...schedule, destinationDirectory: selected });
}

async function runNow(schedule: DatabaseBackupSchedule) {
  try {
    const confirmedSchedule = await confirmLegacyDestination(schedule);
    if (!confirmedSchedule) return;
    const run = await runSchedule(confirmedSchedule.id);
    if (!run) return;
    if (run.status === "success") toast(t("databaseBackup.runSuccess", { count: run.files.length }), 3000);
    else if (run.status === "cancelled") toast(t("databaseBackup.runCancelled"), 3000);
    else toast(t("databaseBackup.runFailed", { error: run.error ? translateBackendError(t, run.error) : t("databaseBackup.unknownError") }), 5000);
  } catch (error: any) {
    toast(error?.message || String(error), 5000);
  }
}

function requestDeleteSchedule(schedule: DatabaseBackupSchedule) {
  pendingDeleteSchedule.value = schedule;
  deleteScheduleDialogOpen.value = true;
}

async function confirmDeleteSchedule() {
  if (!pendingDeleteSchedule.value) return;
  try {
    if (!(await deleteSchedule(pendingDeleteSchedule.value.id))) {
      toast(t("databaseBackup.cannotDeleteRunningSchedule"), 3000);
      return;
    }
    deleteScheduleDialogOpen.value = false;
    pendingDeleteSchedule.value = null;
  } catch (error) {
    toast(String(error), 5000);
  }
}

function toggleRunSelected(runId: string, selected: boolean) {
  if (selected) selectedRunIds.add(runId);
  else selectedRunIds.delete(runId);
}

function toggleAllFilteredRuns(selected: boolean) {
  for (const run of selectableFilteredRuns.value) toggleRunSelected(run.id, selected);
}

function requestDeleteRuns(runIds: readonly string[]) {
  const ids = runIds.filter((runId) => !activeRunIds.has(runId));
  if (ids.length === 0) return;
  pendingDeleteRunIds.value = ids;
  deleteRunDialogOpen.value = true;
}

function requestDeleteRun(run: DatabaseBackupRun) {
  requestDeleteRuns([run.id]);
}

function requestDeleteSelectedRuns() {
  requestDeleteRuns(selectedRuns.value.map((run) => run.id));
}

function requestRenameRun(run: DatabaseBackupRun) {
  if (activeRunIds.has(run.id)) return;
  pendingRenameRun.value = run;
  renameRunName.value = run.displayName || run.scheduleName;
  renameRunDialogOpen.value = true;
}

async function confirmRenameRun() {
  const run = pendingRenameRun.value;
  if (!run || !renameRunName.value.trim()) return;
  try {
    if (!(await renameRun(run.id, renameRunName.value))) return;
    renameRunDialogOpen.value = false;
    pendingRenameRun.value = null;
    toast(t("databaseBackup.backupRenamed"), 2500);
  } catch (error) {
    toast(String(error), 5000);
  }
}

async function confirmDeleteRuns() {
  const runIds = pendingDeleteRunIds.value;
  if (runIds.length === 0) return;
  try {
    await deleteRuns(runIds);
    for (const runId of runIds) {
      selectedRunIds.delete(runId);
      expandedRunIds.delete(runId);
    }
    deleteRunDialogOpen.value = false;
    pendingDeleteRunIds.value = [];
    toast(runIds.length === 1 ? t("databaseBackup.backupDeleted") : t("databaseBackup.backupsDeleted", { count: runIds.length }), 2500);
  } catch (error: any) {
    toast(error?.message || String(error), 5000);
  }
}

function toggleRunExpanded(runId: string) {
  if (expandedRunIds.has(runId)) expandedRunIds.delete(runId);
  else expandedRunIds.add(runId);
}

async function revealBackup(file: DatabaseBackupFile) {
  try {
    if (!desktop) {
      const run = runs.value.find((item) => item.files.includes(file));
      if (run) await api.downloadDatabaseBackupFile(run.id, run.files.indexOf(file));
      return;
    }
    await api.revealPathInFileManager(file.filePath);
  } catch (error: any) {
    toast(translateBackendError(t, error), 5000);
  }
}

const BACKUP_FILE_FILTERS = [{ name: "Database backup", extensions: ["sql", "gz", "nb3"] }];

async function pickBackupFiles(multiple: boolean): Promise<string[]> {
  const { open } = await import("@tauri-apps/plugin-dialog");
  const selected = await open({ multiple, filters: BACKUP_FILE_FILTERS });
  return Array.isArray(selected) ? selected : selected ? [selected] : [];
}

/** Restores a backup file created anywhere, without adding it to the history. */
async function restoreFromFile() {
  try {
    // The web SQL file dialog uploads the file itself; the desktop picks a local path first.
    const [filePath] = desktop ? await pickBackupFiles(false) : [undefined];
    if (desktop && !filePath) return;
    connectionStore.sqlFileSource = { connectionId: "", database: "", ...(filePath ? { filePath } : {}) };
  } catch (error) {
    toast(translateBackendError(t, error), 5000);
  }
}

async function importBackups() {
  try {
    const files = await pickBackupFiles(true);
    if (files.length === 0) return;
    const imported = await importBackupFiles(files);
    toast(t("databaseBackup.backupsImported", { count: imported.length }), 2500);
  } catch (error) {
    toast(translateBackendError(t, error), 5000);
  }
}

const pendingDeleteMessage = computed(() => {
  const pending = pendingDeleteRunIds.value.map((runId) => runs.value.find((run) => run.id === runId)).filter((run): run is DatabaseBackupRun => !!run);
  const owned = pending.filter((run) => run.source !== "imported");
  if (owned.length === 0) return t("databaseBackup.deleteImportedConfirm", { count: pending.length });
  const files = owned.reduce((total, run) => total + run.files.length, 0);
  const message = pending.length === 1 ? t("databaseBackup.deleteBackupConfirm", { count: files }) : t("databaseBackup.deleteBackupsConfirm", { count: pending.length, files });
  return owned.length === pending.length ? message : `${message} ${t("databaseBackup.importedFilesKept")}`;
});

async function restoreBackup(run: DatabaseBackupRun, file: DatabaseBackupFile) {
  try {
    const prepared = desktop ? file.filePath : await api.prepareDatabaseBackupRestore(run.id, run.files.indexOf(file));
    connectionStore.sqlFileSource = {
      connectionId: run.connectionId,
      database: file.database,
      ...(typeof prepared === "string" ? { filePath: prepared } : { preview: prepared }),
    };
  } catch (error) {
    toast(String(error), 5000);
  }
}
</script>

<template>
  <div class="flex flex-col gap-6">
    <div class="flex flex-wrap items-center justify-between gap-3">
      <div class="min-w-0">
        <h3 class="text-base font-semibold">{{ t("databaseBackup.schedules") }}</h3>
        <p class="mt-1 text-sm text-muted-foreground">{{ t(desktop ? "databaseBackup.backgroundLoginScope" : "databaseBackup.serverRuntime") }}</p>
        <p v-if="!canCreateSchedule" class="mt-1 text-xs text-muted-foreground">{{ t("databaseBackup.noSupportedConnections") }}</p>
      </div>
      <div class="flex flex-wrap items-center justify-end gap-2">
        <Button data-backup-restore-from-file variant="outline" size="sm" :title="t('databaseBackup.restoreFromFileHint')" @click="restoreFromFile">
          <RotateCcw class="mr-2 h-4 w-4" />
          {{ t("databaseBackup.restoreFromFile") }}
        </Button>
        <Button v-if="desktop" data-backup-import variant="outline" size="sm" :title="t('databaseBackup.importBackupHint')" @click="importBackups">
          <FileInput class="mr-2 h-4 w-4" />
          {{ t("databaseBackup.importBackup") }}
        </Button>
        <Button variant="outline" size="sm" :disabled="!canCreateSchedule" :title="canCreateSchedule ? t('databaseBackup.oneShotBackup') : t('databaseBackup.noSupportedConnections')" @click="openOneShotBackup">
          <Play class="mr-2 h-4 w-4" />
          {{ t("databaseBackup.oneShotBackup") }}
        </Button>
        <Button size="sm" :disabled="!canCreateSchedule" :title="canCreateSchedule ? t('databaseBackup.addSchedule') : t('databaseBackup.noSupportedConnections')" @click="openCreateSchedule">
          <Plus class="mr-2 h-4 w-4" />
          {{ t("databaseBackup.addSchedule") }}
        </Button>
      </div>
    </div>

    <div class="flex flex-wrap items-center justify-between gap-3 border-b pb-4">
      <div class="min-w-0 space-y-1">
        <Label>{{ t("databaseBackup.backgroundWorker") }}</Label>
        <p class="text-xs text-muted-foreground">{{ heartbeat ? t("databaseBackup.workerLastSeen", { time: formatDate(heartbeat) }) : t("databaseBackup.workerStarting") }}</p>
        <p v-if="destinationRoot" class="break-all text-xs text-muted-foreground">{{ t("databaseBackup.serverDestination", { path: destinationRoot }) }}</p>
      </div>
      <Switch v-if="desktop" :model-value="backgroundEnabled" :disabled="backgroundBusy" :aria-label="t('databaseBackup.backgroundWorker')" @update:model-value="changeBackground" />
    </div>
    <p v-if="backupError" class="break-words text-sm text-destructive">{{ backupError }}</p>
    <div class="overflow-hidden rounded-md border border-border/70">
      <div v-if="schedules.length === 0" class="flex min-h-44 flex-col items-center justify-center gap-3 px-4 py-8 text-center text-muted-foreground">
        <DatabaseBackup class="h-8 w-8 opacity-60" />
        <div>
          <div class="text-sm font-medium text-foreground">{{ t("databaseBackup.noSchedules") }}</div>
          <p class="mt-1 text-sm">{{ t("databaseBackup.noSchedulesHint") }}</p>
        </div>
        <div class="flex flex-wrap justify-center gap-2">
          <Button variant="outline" size="sm" :disabled="!canCreateSchedule" @click="openOneShotBackup"><Play class="mr-2 h-4 w-4" />{{ t("databaseBackup.oneShotBackup") }}</Button>
          <Button size="sm" :disabled="!canCreateSchedule" @click="openCreateSchedule"><Plus class="mr-2 h-4 w-4" />{{ t("databaseBackup.addSchedule") }}</Button>
        </div>
      </div>
      <div v-for="schedule in schedules" :key="schedule.id" class="grid gap-3 border-b border-border/70 px-4 py-3 last:border-b-0 md:grid-cols-[minmax(0,1fr)_auto] md:items-center">
        <div class="min-w-0">
          <div class="flex min-w-0 flex-wrap items-center gap-2">
            <span class="truncate text-sm font-medium">{{ schedule.name }}</span>
            <Badge variant="outline" class="font-normal">{{ connectionName(schedule.connectionId) }}</Badge>
            <Badge v-if="activeScheduleIds.has(schedule.id)" variant="secondary" class="font-normal">{{ scheduleCancellationRequested(schedule.id) ? t("databaseBackup.cancelling") : t("databaseBackup.status.running") }}</Badge>
          </div>
          <div class="mt-1 flex flex-wrap gap-x-4 gap-y-1 text-xs text-muted-foreground">
            <span>{{ frequencyLabel(schedule) }}</span>
            <span>{{ databaseScopeLabel(schedule) }}</span>
            <span v-if="schedule.tableFilterMode !== 'all'">{{ tableScopeLabel(schedule) }}</span>
            <span>{{ t("databaseBackup.nextRun", { time: formatDate(schedule.nextRunAt) }) }}</span>
            <span>{{ schedule.retentionCount === 0 ? t("databaseBackup.keepForever") : t("databaseBackup.keepRuns", { count: schedule.retentionCount }) }}</span>
          </div>
        </div>
        <div class="flex items-center justify-end gap-1">
          <Switch :model-value="schedule.enabled" :disabled="activeScheduleIds.has(schedule.id)" :title="schedule.enabled ? t('databaseBackup.disable') : t('databaseBackup.enable')" @update:model-value="(value: boolean) => changeScheduleEnabled(schedule.id, value)" />
          <Button
            v-if="activeRunForSchedule(schedule.id)"
            variant="ghost"
            size="icon"
            class="h-8 w-8"
            :disabled="scheduleCancellationRequested(schedule.id)"
            :title="scheduleCancellationRequested(schedule.id) ? t('databaseBackup.cancelling') : t('databaseBackup.cancel')"
            @click="requestCancelRun(activeRunForSchedule(schedule.id)!.id)"
          >
            <Loader2 v-if="scheduleCancellationRequested(schedule.id)" class="h-4 w-4 animate-spin" />
            <Square v-else class="h-4 w-4" />
          </Button>
          <Button v-else variant="ghost" size="icon" class="h-8 w-8" :title="t('databaseBackup.runNow')" @click="runNow(schedule)">
            <Play class="h-4 w-4" />
          </Button>
          <Button variant="ghost" size="icon" class="h-8 w-8" :disabled="activeScheduleIds.has(schedule.id)" :title="t('databaseBackup.edit')" @click="openEditSchedule(schedule)">
            <Pencil class="h-4 w-4" />
          </Button>
          <Button variant="ghost" size="icon" class="h-8 w-8 text-muted-foreground hover:text-destructive" :disabled="activeScheduleIds.has(schedule.id)" :title="t('databaseBackup.delete')" @click="requestDeleteSchedule(schedule)">
            <Trash2 class="h-4 w-4" />
          </Button>
        </div>
      </div>
    </div>

    <div class="flex flex-col gap-3">
      <div class="flex flex-wrap items-center justify-between gap-3">
        <h3 class="text-base font-semibold">{{ t("databaseBackup.history") }}</h3>
        <div class="flex flex-wrap items-center justify-end gap-2">
          <Popover v-model:open="historyConnectionPickerOpen">
            <PopoverTrigger as-child>
              <Button data-backup-history-connection-picker type="button" variant="outline" role="combobox" :aria-expanded="historyConnectionPickerOpen" class="min-w-52 justify-between font-normal">
                <span class="truncate">{{ selectedHistoryConnectionName }}</span>
                <ChevronDown class="ml-2 h-4 w-4 shrink-0 opacity-50" />
              </Button>
            </PopoverTrigger>
            <PopoverContent align="end" :hide-when-detached="true" class="w-[var(--reka-popover-trigger-width)] p-1">
              <div class="relative">
                <Search class="pointer-events-none absolute left-2.5 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
                <Input v-model="historyConnectionSearch" data-backup-history-connection-search class="h-9 pl-8" :aria-label="t('databaseBackup.searchHistoryConnections')" :placeholder="t('databaseBackup.searchHistoryConnections')" />
              </div>
              <div class="max-h-60 overflow-y-auto py-1">
                <button type="button" class="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-left text-sm hover:bg-accent hover:text-accent-foreground focus-visible:bg-accent focus-visible:text-accent-foreground focus-visible:outline-none" @click="selectHistoryConnection('')">
                  <Check class="h-4 w-4 shrink-0" :class="historyConnectionId ? 'opacity-0' : 'opacity-100'" />
                  <span class="min-w-0 flex-1 truncate">{{ t("databaseBackup.allConnections") }}</span>
                </button>
                <button
                  v-for="connection in filteredHistoryConnections"
                  :key="connection.id"
                  type="button"
                  class="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-left text-sm hover:bg-accent hover:text-accent-foreground focus-visible:bg-accent focus-visible:text-accent-foreground focus-visible:outline-none"
                  @click="selectHistoryConnection(connection.id)"
                >
                  <Check class="h-4 w-4 shrink-0" :class="connection.id === historyConnectionId ? 'opacity-100' : 'opacity-0'" />
                  <span class="min-w-0 flex-1 truncate">{{ connection.name }}</span>
                </button>
                <div v-if="filteredHistoryConnections.length === 0" class="px-2 py-2 text-sm text-muted-foreground">{{ t("databaseBackup.noMatchingConnections") }}</div>
              </div>
            </PopoverContent>
          </Popover>
          <Select v-model="historyBackupMethod">
            <SelectTrigger class="w-36" :aria-label="t('databaseBackup.backupMethod')"><SelectValue /></SelectTrigger>
            <SelectContent>
              <SelectItem value="all">{{ t("databaseBackup.allBackupMethods") }}</SelectItem>
              <SelectItem value="manual">{{ t("databaseBackup.manualTrigger") }}</SelectItem>
              <SelectItem value="scheduled">{{ t("databaseBackup.scheduledTrigger") }}</SelectItem>
              <SelectItem value="one-shot">{{ t("databaseBackup.oneShotTrigger") }}</SelectItem>
              <SelectItem value="imported">{{ t("databaseBackup.importedTrigger") }}</SelectItem>
            </SelectContent>
          </Select>
          <Select v-model="historyStatus">
            <SelectTrigger class="w-32" :aria-label="t('databaseBackup.backupStatus')"><SelectValue /></SelectTrigger>
            <SelectContent>
              <SelectItem value="all">{{ t("databaseBackup.allStatuses") }}</SelectItem>
              <SelectItem value="running">{{ t("databaseBackup.status.running") }}</SelectItem>
              <SelectItem value="success">{{ t("databaseBackup.status.success") }}</SelectItem>
              <SelectItem value="failed">{{ t("databaseBackup.status.failed") }}</SelectItem>
              <SelectItem value="cancelled">{{ t("databaseBackup.status.cancelled") }}</SelectItem>
            </SelectContent>
          </Select>
        </div>
      </div>
      <div v-if="selectableFilteredRuns.length > 0" class="flex flex-wrap items-center gap-3 rounded-md border border-border/70 bg-muted/20 px-3 py-2">
        <label class="flex cursor-pointer items-center gap-2 text-sm">
          <input data-backup-history-select-all type="checkbox" class="h-4 w-4 rounded border-border accent-primary" :checked="allSelectableRunsSelected" @change="toggleAllFilteredRuns(($event.target as HTMLInputElement).checked)" />
          {{ t("databaseBackup.selectAllFiltered") }}
        </label>
        <span v-if="selectedRuns.length > 0" class="text-sm text-muted-foreground">{{ t("databaseBackup.selectedRuns", { count: selectedRuns.length }) }}</span>
        <Button v-if="selectedRuns.length > 0" data-backup-delete-selected variant="destructive" size="sm" @click="requestDeleteSelectedRuns">
          <Trash2 class="mr-2 h-4 w-4" />
          {{ t("databaseBackup.deleteSelected") }}
        </Button>
      </div>
      <div class="overflow-hidden rounded-md border border-border/70">
        <div v-if="filteredRuns.length === 0" class="px-4 py-8 text-center text-sm text-muted-foreground">{{ historyConnectionId || historyBackupMethod !== "all" || historyStatus !== "all" ? t("databaseBackup.noFilteredHistory") : t("databaseBackup.noHistory") }}</div>
        <template v-for="run in filteredRuns" :key="run.id">
          <div class="grid gap-2 border-b border-border/70 px-3 py-3 last:border-b-0 md:grid-cols-[auto_auto_minmax(0,1fr)_auto] md:items-center">
            <input
              data-backup-history-select
              type="checkbox"
              class="h-4 w-4 rounded border-border accent-primary"
              :checked="selectedRunIds.has(run.id)"
              :disabled="activeRunIds.has(run.id)"
              :aria-label="t('databaseBackup.selectRun', { name: run.displayName || run.scheduleName })"
              @change="toggleRunSelected(run.id, ($event.target as HTMLInputElement).checked)"
            />
            <Button variant="ghost" size="icon" class="h-7 w-7" :disabled="run.files.length === 0" :title="t('databaseBackup.showFiles')" @click="toggleRunExpanded(run.id)">
              <ChevronDown v-if="expandedRunIds.has(run.id)" class="h-4 w-4" />
              <ChevronRight v-else class="h-4 w-4" />
            </Button>
            <div class="min-w-0">
              <div class="flex min-w-0 flex-wrap items-center gap-2">
                <span class="truncate text-sm font-medium">{{ run.displayName || run.scheduleName }}</span>
                <Badge :variant="runStatusVariant(run.status)" class="font-normal">{{ displayedRunStatusLabel(run) }}</Badge>
                <Badge variant="outline" class="font-normal">{{ runBackupMethodLabel(run) }}</Badge>
              </div>
              <div class="mt-1 flex flex-wrap gap-x-4 gap-y-1 text-xs text-muted-foreground">
                <span>{{ run.source === "imported" ? t("databaseBackup.externalFile") : run.connectionName || connectionName(run.connectionId) }}</span>
                <span>{{ formatDate(run.startedAt) }}</span>
                <span>{{ t("databaseBackup.fileCount", { count: run.files.length }) }}</span>
                <span v-if="run.error" class="break-all text-destructive">{{ translateBackendError(t, run.error) }}</span>
              </div>
              <div v-if="activeRunIds.has(run.id)" class="mt-2 flex items-center gap-2">
                <div class="h-1.5 min-w-0 flex-1 overflow-hidden rounded-full bg-muted" role="progressbar" :aria-label="t('databaseBackup.progress')" aria-valuemin="0" aria-valuemax="100" :aria-valuenow="run.progressPercent ?? 0">
                  <div class="h-full rounded-full bg-primary transition-[width] duration-300" :style="{ width: `${run.progressPercent ?? 0}%` }" />
                </div>
                <span class="w-9 shrink-0 text-right text-xs tabular-nums text-muted-foreground">{{ run.progressPercent ?? 0 }}%</span>
              </div>
            </div>
            <div class="flex items-center justify-end gap-1">
              <Button
                v-if="activeRunIds.has(run.id) && run.source === 'one-shot'"
                variant="ghost"
                size="icon"
                class="h-8 w-8"
                :disabled="cancellingRunIds.has(run.id)"
                :title="cancellingRunIds.has(run.id) ? t('databaseBackup.cancelling') : t('databaseBackup.cancel')"
                @click="requestCancelRun(run.id)"
              >
                <Loader2 v-if="cancellingRunIds.has(run.id)" class="h-4 w-4 animate-spin" />
                <Square v-else class="h-4 w-4" />
              </Button>
              <Loader2 v-else-if="activeRunIds.has(run.id)" class="mr-2 h-4 w-4 animate-spin text-primary" />
              <Button variant="ghost" size="icon" class="h-8 w-8" :disabled="activeRunIds.has(run.id)" :title="t('databaseBackup.renameBackup')" @click="requestRenameRun(run)">
                <Pencil class="h-4 w-4" />
              </Button>
              <Button v-if="run.files[0]" variant="ghost" size="icon" class="h-8 w-8" :title="t(desktop ? 'databaseBackup.revealFile' : 'common.download')" @click="revealBackup(run.files[0])">
                <component :is="desktop ? FolderOpen : Download" class="h-4 w-4" />
              </Button>
              <Button variant="ghost" size="icon" class="h-8 w-8 text-muted-foreground hover:text-destructive" :disabled="activeRunIds.has(run.id)" :title="t('databaseBackup.deleteBackup')" @click="requestDeleteRun(run)">
                <Trash2 class="h-4 w-4" />
              </Button>
            </div>
          </div>
          <div v-if="expandedRunIds.has(run.id) && run.files.length > 0" class="border-b border-border/70 bg-muted/20 px-4 py-2 last:border-b-0">
            <div v-for="file in run.files" :key="file.filePath" class="grid gap-2 border-b border-border/50 py-2 last:border-b-0 sm:grid-cols-[minmax(0,1fr)_auto] sm:items-center">
              <div class="min-w-0">
                <div class="truncate text-xs font-medium">{{ file.displayName }}</div>
                <div class="truncate text-xs text-muted-foreground" :title="file.filePath">{{ file.filePath }}</div>
              </div>
              <div class="flex items-center justify-end gap-1">
                <Button variant="ghost" size="icon" class="h-7 w-7" :title="t(desktop ? 'databaseBackup.revealFile' : 'common.download')" @click="revealBackup(file)">
                  <component :is="desktop ? FolderOpen : Download" class="h-3.5 w-3.5" />
                </Button>
                <Button variant="outline" size="sm" class="h-7" :disabled="run.status !== 'success'" @click="restoreBackup(run, file)">
                  <RotateCcw class="mr-1.5 h-3.5 w-3.5" />
                  {{ t("databaseBackup.restore") }}
                </Button>
              </div>
            </div>
          </div>
        </template>
      </div>
    </div>
  </div>

  <Dialog v-model:open="scheduleDialogOpen">
    <DialogContent class="dbx-backup-dialog dbx-form-dialog dbx-form-dialog--lg max-h-[min(760px,calc(var(--dbx-viewport-height)-32px))] max-w-[min(720px,calc(100vw-32px))] overflow-x-hidden overflow-y-auto pr-8 [scrollbar-gutter:stable]">
      <DialogHeader>
        <DialogTitle>{{ editingScheduleId ? t("databaseBackup.editSchedule") : t("databaseBackup.addSchedule") }}</DialogTitle>
      </DialogHeader>

      <div class="backup-schedule-form grid gap-5 py-1">
        <div class="space-y-2">
          <Label>{{ t("databaseBackup.scheduleName") }}</Label>
          <Input v-model="draft.name" />
        </div>

        <DatabaseBackupConfigFields
          :draft="draft"
          :connections="sqlConnections"
          :all-databases="allDatabases"
          :selected-databases="selectedDatabases"
          :database-options="databaseOptions"
          :table-patterns-input="tablePatternsInput"
          :loading-databases="loadingDatabases"
          :database-load-error="databaseLoadError"
          :run-directory-pattern="draft.runDirectoryPattern"
          :run-directory-pattern-valid="databaseBackupRunDirectoryPatternIsValid(draft.runDirectoryPattern || '')"
          :output-path-preview="scheduleOutputPathPreview"
          @change-connection="changeConnection"
          @choose-destination="chooseDestination"
          @toggle-database="toggleDatabase"
          @update:selected-databases="updateSelectedDatabases"
          @update:all-databases="setAllDatabases"
          @update:table-patterns-input="(value: string) => (tablePatternsInput = value)"
          @table-selection-state="(state) => (scheduleTableSelectionState = state)"
          @update:run-directory-pattern="(value: string) => (draft.runDirectoryPattern = value)"
        />

        <div class="grid gap-4 sm:grid-cols-3">
          <div class="space-y-2">
            <Label>{{ t("databaseBackup.frequency") }}</Label>
            <Select v-model="draft.frequency">
              <SelectTrigger><SelectValue /></SelectTrigger>
              <SelectContent>
                <SelectItem value="hourly">{{ t("databaseBackup.frequencyHourly") }}</SelectItem>
                <SelectItem value="daily">{{ t("databaseBackup.frequencyDaily") }}</SelectItem>
                <SelectItem value="weekly">{{ t("databaseBackup.frequencyWeekly") }}</SelectItem>
              </SelectContent>
            </Select>
          </div>
          <div v-if="draft.frequency === 'hourly'" class="space-y-2">
            <Label>{{ t("databaseBackup.intervalHours") }}</Label>
            <Input v-model.number="draft.intervalHours" type="number" min="1" max="168" />
          </div>
          <div v-else class="space-y-2">
            <Label>{{ t("databaseBackup.time") }}</Label>
            <Input v-model="draft.timeOfDay" type="time" />
          </div>
          <div v-if="draft.frequency === 'weekly'" class="space-y-2">
            <Label>{{ t("databaseBackup.weekday") }}</Label>
            <Select :model-value="String(draft.weekday)" @update:model-value="(value: any) => (draft.weekday = Number(value))">
              <SelectTrigger><SelectValue /></SelectTrigger>
              <SelectContent>
                <SelectItem v-for="weekday in weekdays" :key="weekday.value" :value="String(weekday.value)">{{ weekday.label }}</SelectItem>
              </SelectContent>
            </Select>
          </div>
          <div class="space-y-2">
            <div class="flex items-center justify-between">
              <Label>{{ t("databaseBackup.retention") }}</Label>
              <label class="flex cursor-pointer items-center gap-1.5 text-xs text-muted-foreground">
                <input data-backup-retention-forever type="checkbox" class="h-3.5 w-3.5 rounded border-border accent-primary" :checked="draft.retentionCount === 0" @change="togglePermanentRetention(($event.target as HTMLInputElement).checked)" />
                {{ t("databaseBackup.keepForever") }}
              </label>
            </div>
            <Input v-if="draft.retentionCount !== 0" v-model.number="draft.retentionCount" type="number" min="1" max="1000" />
            <Input v-else disabled :model-value="t('databaseBackup.keepForever')" class="cursor-not-allowed bg-muted text-muted-foreground" />
          </div>
        </div>
        <div class="space-y-2">
          <Label>{{ t("databaseBackup.timeZone") }}</Label>
          <Input v-model="draft.timeZone" :aria-invalid="!!previewError" />
          <p v-if="previewError" class="text-xs text-destructive">{{ previewError }}</p>
          <div v-else class="text-xs text-muted-foreground">{{ t("databaseBackup.nextRunPreview", { time: formatDate(nextRunPreview.toISOString()) }) }}</div>
        </div>

        <div class="flex items-center justify-between gap-4 border-t border-border/70 pt-4">
          <div>
            <Label>{{ t("databaseBackup.enabled") }}</Label>
            <div class="mt-1 text-xs text-muted-foreground">{{ t("databaseBackup.savedTimeZoneHint") }}</div>
          </div>
          <Switch v-model="draft.enabled" />
        </div>
      </div>

      <DialogFooter>
        <Button variant="outline" @click="scheduleDialogOpen = false">{{ t("common.cancel") }}</Button>
        <Button :disabled="!canSave" @click="submitSchedule">
          <Loader2 v-if="saving" class="mr-2 h-4 w-4 animate-spin" />
          {{ t("common.save") }}
        </Button>
      </DialogFooter>
    </DialogContent>
  </Dialog>

  <Dialog v-model:open="oneShotDialogOpen">
    <DialogContent class="dbx-backup-dialog dbx-form-dialog dbx-form-dialog--lg max-h-[min(760px,calc(var(--dbx-viewport-height)-32px))] max-w-[min(720px,calc(100vw-32px))] overflow-x-hidden overflow-y-auto pr-8 [scrollbar-gutter:stable]">
      <DialogHeader>
        <DialogTitle>{{ t("databaseBackup.oneShotBackup") }}</DialogTitle>
        <p class="text-sm text-muted-foreground">{{ t("databaseBackup.oneShotDescription") }}</p>
      </DialogHeader>

      <DatabaseBackupConfigFields
        :draft="oneShotDraft"
        :connections="sqlConnections"
        multi-select-connections
        :selected-connection-ids="oneShotConnectionIds"
        :all-databases="allDatabases"
        :selected-databases="selectedDatabases"
        :database-options="databaseOptions"
        :table-patterns-input="tablePatternsInput"
        :loading-databases="loadingDatabases"
        :database-load-error="databaseLoadError"
        :output-path-preview="oneShotOutputPathPreview"
        @toggle-connection="toggleOneShotConnection"
        @choose-destination="chooseDestination"
        @toggle-database="toggleDatabase"
        @update:selected-databases="updateSelectedDatabases"
        @update:all-databases="setAllDatabases"
        @update:table-patterns-input="(value: string) => (tablePatternsInput = value)"
        @table-selection-state="(state) => (oneShotTableSelectionState = state)"
      />

      <DialogFooter>
        <Button variant="outline" @click="oneShotDialogOpen = false">{{ oneShotStarting ? t("common.close") : t("common.cancel") }}</Button>
        <Button v-if="oneShotStarting" variant="destructive" :disabled="cancellableOneShotRuns.length === 0 || oneShotCancellationRequested()" :title="oneShotCancellationRequested() ? t('databaseBackup.cancelling') : t('databaseBackup.cancel')" @click="cancelActiveOneShotBackup">
          <Loader2 v-if="oneShotCancellationRequested()" class="mr-2 h-4 w-4 animate-spin" />
          <Square v-else class="mr-2 h-4 w-4" />
          {{ oneShotCancellationRequested() ? t("databaseBackup.cancelling") : t("databaseBackup.cancel") }}
        </Button>
        <Button v-else :disabled="!canStartOneShot" @click="startOneShotBackup">
          {{ t("databaseBackup.startBackup") }}
        </Button>
      </DialogFooter>
    </DialogContent>
  </Dialog>

  <Dialog v-model:open="deleteScheduleDialogOpen">
    <DialogContent class="max-w-md">
      <DialogHeader
        ><DialogTitle>{{ t("databaseBackup.deleteSchedule") }}</DialogTitle></DialogHeader
      >
      <p class="text-sm text-muted-foreground">{{ t("databaseBackup.deleteScheduleConfirm", { name: pendingDeleteSchedule?.name || "" }) }}</p>
      <DialogFooter>
        <Button variant="outline" @click="deleteScheduleDialogOpen = false">{{ t("common.cancel") }}</Button>
        <Button variant="destructive" @click="confirmDeleteSchedule">{{ t("databaseBackup.delete") }}</Button>
      </DialogFooter>
    </DialogContent>
  </Dialog>

  <Dialog v-model:open="deleteRunDialogOpen">
    <DialogContent class="max-w-md">
      <DialogHeader
        ><DialogTitle>{{ t("databaseBackup.deleteBackup") }}</DialogTitle></DialogHeader
      >
      <p data-backup-delete-message class="text-sm text-muted-foreground">{{ pendingDeleteMessage }}</p>
      <DialogFooter>
        <Button variant="outline" @click="deleteRunDialogOpen = false">{{ t("common.cancel") }}</Button>
        <Button variant="destructive" @click="confirmDeleteRuns">{{ t("databaseBackup.delete") }}</Button>
      </DialogFooter>
    </DialogContent>
  </Dialog>

  <Dialog v-model:open="renameRunDialogOpen">
    <DialogContent class="max-w-md">
      <DialogHeader
        ><DialogTitle>{{ t("databaseBackup.renameBackup") }}</DialogTitle></DialogHeader
      >
      <div class="space-y-2">
        <Label>{{ t("databaseBackup.backupName") }}</Label>
        <Input v-model="renameRunName" autofocus @keyup.enter="confirmRenameRun" />
      </div>
      <DialogFooter>
        <Button variant="outline" @click="renameRunDialogOpen = false">{{ t("common.cancel") }}</Button>
        <Button :disabled="!renameRunName.trim()" @click="confirmRenameRun">{{ t("common.save") }}</Button>
      </DialogFooter>
    </DialogContent>
  </Dialog>
</template>

<style scoped>
.backup-schedule-form {
  grid-template-columns: minmax(0, 1fr);
  width: 100%;
  min-width: 0;
  max-width: 100%;
}
</style>