<template>
  <div class="app-page log-workbench">
    <div class="page-hero">
      <div class="hero-info">
        <h2>操作日志</h2>
        <p>全操作留痕，不可删除</p>
      </div>
      <el-button type="primary" @click="handleBackup" :loading="backingUp" round>备份数据库</el-button>
    </div>

    <el-table :data="logs" stripe v-loading="loading" style="width:100%" class="data-table">
      <el-table-column prop="id" label="ID" width="70" />
      <el-table-column prop="action" label="操作类型" min-width="120" show-overflow-tooltip />
      <el-table-column prop="entityType" label="实体类型" min-width="120" show-overflow-tooltip />
      <el-table-column prop="entityId" label="实体ID" width="80" />
      <el-table-column prop="operatorId" label="操作人" width="90" />
      <el-table-column prop="ip" label="IP 地址" min-width="140" show-overflow-tooltip />
      <el-table-column prop="createdAt" label="操作时间" min-width="160" />
      <el-table-column label="请求参数" min-width="300" show-overflow-tooltip>
        <template #default="{ row }">
          <code class="log-code">{{ truncate(row.oldDataJson) }}</code>
        </template>
      </el-table-column>
      <el-table-column label="响应数据" min-width="300" show-overflow-tooltip>
        <template #default="{ row }">
          <code class="log-code">{{ truncate(row.newDataJson) }}</code>
        </template>
      </el-table-column>
    </el-table>

    <div style="display:flex;justify-content:flex-end;margin-top:20px">
      <el-pagination
        v-model:current-page="page"
        :total="total"
        :page-size="20"
        layout="total, prev, pager, next"
        @current-change="load"
        background
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { systemApi } from '../../api'
import { ElMessage, ElMessageBox } from 'element-plus'

const logs = ref([])
const page = ref(1)
const total = ref(0)
const loading = ref(false)
const backingUp = ref(false)

onMounted(() => load())

async function load() {
  loading.value = true
  try {
    const result = await systemApi.getLogs({ page: page.value, size: 20 })
    logs.value = result.records || []
    total.value = result.total || 0
  } finally { loading.value = false }
}

function truncate(str) {
  if (!str) return '--'
  return str.length > 120 ? str.slice(0, 120) + '...' : str
}

async function handleBackup() {
  await ElMessageBox.confirm('将复制当前 SQLite 数据库到备份目录。建议在低峰时段执行。', '备份数据库', { type: 'info' })
  backingUp.value = true
  try {
    const result = await systemApi.backupDatabase()
    ElMessage.success('备份已创建：' + result.path)
    await load()
  } finally {
    backingUp.value = false
  }
}
</script>

<style scoped>
.page { width: 100%; }
.page-hero { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 24px; }
.hero-info h2 { font-size: 28px; font-weight: 700; color: var(--color-text); margin: 0; }
.hero-info p { font-size: 14px; color: var(--color-text-secondary); margin: 4px 0 0; }
.data-table { border-radius: 8px; overflow: hidden; }
.data-table :deep(th) { white-space: nowrap; font-weight: 600; color: var(--color-text-secondary); background: var(--color-surface-subtle); }
.log-code { font-size: 11px; color: var(--color-text-secondary); font-family: 'SF Mono', 'Fira Code', monospace; }
</style>
