<template>
  <v-dialog :model-value="modelValue" max-width="520" @update:model-value="$emit('update:modelValue', $event)">
    <v-card border>
      <v-card-title>推送失败诊断 — {{ targetName }}</v-card-title>
      <v-card-text>
        <v-progress-linear v-if="loading" indeterminate class="mb-3" />
        <template v-else-if="result">
          <v-list density="compact">
            <v-list-item title="SSH 连接">
              <template #append>
                <v-icon :color="result.ssh_ok ? 'success' : 'error'">
                  {{ result.ssh_ok ? 'mdi-check' : 'mdi-close' }}
                </v-icon>
              </template>
            </v-list-item>
            <v-list-item v-if="result.disk_avail" :title="`磁盘可用: ${result.disk_avail}`" />
            <v-list-item
              v-if="result.disk_used_pct != null"
              :title="`磁盘使用率: ${result.disk_used_pct}%`"
            />
            <v-list-item title="目标路径存在">
              <template #append>
                <v-icon :color="result.path_exists ? 'success' : 'error'">
                  {{ result.path_exists ? 'mdi-check' : 'mdi-close' }}
                </v-icon>
              </template>
            </v-list-item>
            <v-list-item v-if="result.path_perms" :subtitle="result.path_perms" title="权限" />
            <v-list-item title="目标路径可写">
              <template #append>
                <v-icon :color="result.path_writable ? 'success' : 'error'">
                  {{ result.path_writable ? 'mdi-check' : 'mdi-close' }}
                </v-icon>
              </template>
            </v-list-item>
          </v-list>
          <v-alert
            v-for="(err, i) in result.errors"
            :key="i"
            type="error"
            density="compact"
            variant="tonal"
            class="mt-2"
          >
            {{ formatPushErrorMessage(err) }}
          </v-alert>
        </template>
      </v-card-text>
      <v-card-actions>
        <v-spacer />
        <v-btn variant="text" @click="$emit('update:modelValue', false)">关闭</v-btn>
      </v-card-actions>
    </v-card>
  </v-dialog>
</template>

<script setup lang="ts">
import { formatPushErrorMessage } from '@/composables/push/labels'
import type { DiagnoseResult } from '@/composables/push/types'

defineProps<{
  modelValue: boolean
  loading: boolean
  result: DiagnoseResult | null
  targetName?: string
}>()

defineEmits<{
  'update:modelValue': [value: boolean]
}>()
</script>
