<template>
  <!-- ================================================================== -->
  <!-- 系统日志页面 — 配置变更历史记录                                      -->
  <!-- ================================================================== -->
  <div class="system-logs">
    <!-- 日志列表 -->
    <el-table :data="logs" v-loading="loading" stripe style="width: 100%">
      <el-table-column prop="changed_at" label="时间" width="170">
        <template #default="{ row }">
          {{ formatTime(row.changed_at) }}
        </template>
      </el-table-column>
      <el-table-column prop="config_key" label="配置键" width="250" show-overflow-tooltip />
      <el-table-column prop="changed_by_name" label="操作人" width="100">
        <template #default="{ row }">
          {{ row.changed_by_name || '—' }}
        </template>
      </el-table-column>
      <el-table-column prop="old_value" label="变更前" min-width="200" show-overflow-tooltip>
        <template #default="{ row }">
          <span class="old-value">{{ row.old_value || '—' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="new_value" label="变更后" min-width="200" show-overflow-tooltip>
        <template #default="{ row }">
          <span class="new-value">{{ row.new_value || '—' }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-bar">
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.pageSize"
        :total="pagination.total"
        :page-sizes="[20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="loadLogs"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
// =============================================================================
// 系统日志页面逻辑
// =============================================================================
import { ref, reactive, onMounted } from 'vue'
import { getSystemLogs } from '@/api/admin'
import { ElMessage } from 'element-plus'

// ---------- 状态 ----------
const loading = ref(false)
const logs = ref<any[]>([])

const pagination = reactive({
  page: 1,
  pageSize: 50,
  total: 0,
})

// ---------- 数据加载 ----------
async function loadLogs() {
  loading.value = true
  try {
    const { data } = await getSystemLogs({
      page: pagination.page,
      page_size: pagination.pageSize,
    })
    if (data.code === 0) {
      logs.value = data.data.items || []
      pagination.total = data.data.total || 0
    }
  } catch (err: any) {
    ElMessage.error('加载系统日志失败')
  } finally {
    loading.value = false
  }
}

// ---------- 事件处理 ----------
function handleSizeChange() {
  pagination.page = 1
  loadLogs()
}

// ---------- 工具函数 ----------
function formatTime(iso: string): string {
  if (!iso) return '—'
  const d = new Date(iso)
  return d.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
  })
}

// ---------- 生命周期 ----------
onMounted(() => loadLogs())
</script>

<style scoped>
.system-logs {
  padding: 0;
}

.pagination-bar {
  display: flex;
  justify-content: flex-end;
  margin-top: 16px;
}

.old-value {
  color: #f56c6c;
  text-decoration: line-through;
}

.new-value {
  color: #67c23a;
  font-weight: 500;
}
</style>
