<script setup>
import { ref, watch } from 'vue'
import { useProjectsStore } from '../stores/projects'
import { useModalsStore } from '../stores/modals'
import { svnApi } from '../api/client'

const store = useProjectsStore()
const modals = useModalsStore()
const logs = ref([])
const loading = ref(false)

watch(() => modals.logOpen, async (open) => {
  if (open && store.currentId) {
    loading.value = true
    try {
      const id = store.currentId?.value ?? store.currentId
      if (!id) return
      const res = await svnApi.log(id, 50)
      logs.value = res.data || []
    } catch (e) {
      logs.value = []
    } finally {
      loading.value = false
    }
  }
})
</script>

<template>
  <div v-if="modals.logOpen" class="modal-overlay" @click.self="modals.closeLog()">
    <div class="modal modal-lg">
      <div class="modal-header">
        <h3>提交日志</h3>
        <button class="modal-close" @click="modals.closeLog()">&times;</button>
      </div>
      <div class="modal-body">
        <div v-if="loading" class="loading">加载中...</div>
        <div v-else class="log-list">
          <div v-for="log in logs" :key="log.revision" class="log-item">
            <div class="log-meta">
              <span class="log-rev">r{{ log.revision }}</span>
              <span class="log-author">{{ log.author }}</span>
              <span class="log-date">{{ log.date }}</span>
            </div>
            <div class="log-message">{{ log.message || '(无消息)' }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 1000; }
.modal { background: var(--bg-primary); border: 1px solid var(--border-color); border-radius: 8px; min-width: 400px; max-height: 80vh; display: flex; flex-direction: column; }
.modal-lg { width: 600px; }
.modal-header { display: flex; justify-content: space-between; align-items: center; padding: 1rem; border-bottom: 1px solid var(--border-color); }
.modal-header h3 { margin: 0; font-size: 1rem; }
.modal-close { background: none; border: none; color: var(--text-secondary); font-size: 1.5rem; cursor: pointer; }
.modal-body { padding: 1rem; overflow-y: auto; }
.loading { color: var(--text-secondary); }
.log-list { display: flex; flex-direction: column; gap: 0.75rem; }
.log-item { padding: 0.5rem; background: var(--bg-secondary); border-radius: 4px; }
.log-meta { font-size: 0.75rem; color: var(--text-secondary); margin-bottom: 0.25rem; }
.log-rev { font-weight: 600; margin-right: 0.5rem; }
.log-message { font-size: 0.875rem; white-space: pre-wrap; word-break: break-word; }
</style>
