<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { fetchDemandBelongOssLogs } from '../api/ossLog'
import type { OssLogItem } from '../types/ossLog'

const items = ref<OssLogItem[]>([])
const loading = ref(true)
const error = ref<string | null>(null)

onMounted(async () => {
  try {
    const data = await fetchDemandBelongOssLogs()
    items.value = data.items ?? []
  } catch (e) {
    error.value = e instanceof Error ? e.message : String(e)
  } finally {
    loading.value = false
  }
})

function openHtml(item: OssLogItem) {
  const url = item.oss_path?.trim()
  if (!url) return
  window.open(url, '_blank', 'noopener,noreferrer')
}
</script>

<template>
  <div class="process-page">
    <header class="header">
      <h1>需求归类过程</h1>
      <p class="sub">demand_belong_category_agent · 按创建时间倒序 · 点击行打开可视化 HTML</p>
    </header>

    <div v-if="loading" class="state">正在加载…</div>
    <div v-else-if="error" class="state error">{{ error }}</div>
    <div v-else-if="!items.length" class="state">暂无日志记录</div>
    <div v-else class="table-wrap">
      <table>
        <thead>
          <tr>
            <th class="col-time">创建时间</th>
            <th class="col-name">日志名称</th>
            <th class="col-path">oss_path</th>
          </tr>
        </thead>
        <tbody>
          <tr
            v-for="item in items"
            :key="item.id"
            class="row"
            @click="openHtml(item)"
          >
            <td>{{ item.create_time || '—' }}</td>
            <td>{{ item.log_name || '—' }}</td>
            <td class="path">{{ item.oss_path || '—' }}</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<style scoped>
.process-page {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  gap: 16px;
}

.header h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  color: #0f172a;
}

.sub {
  margin: 6px 0 0;
  font-size: 13px;
  color: #94a3b8;
}

.state {
  padding: 48px;
  text-align: center;
  color: #64748b;
}

.state.error {
  color: #b91c1c;
}

.table-wrap {
  flex: 1;
  min-height: 0;
  overflow: auto;
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.7);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

th,
td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid #eef2f7;
  vertical-align: top;
}

th {
  position: sticky;
  top: 0;
  background: #f8fafc;
  font-size: 12px;
  font-weight: 600;
  color: #64748b;
  z-index: 1;
}

.col-time {
  width: 180px;
}

.col-name {
  width: 280px;
}

.row {
  cursor: pointer;
}

.row:hover td {
  background: #f1f5f9;
}

.path {
  color: #334155;
  word-break: break-all;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 12px;
}
</style>
