<template>
  <div class="detail-container">
    <div v-if="loading" class="loading">로딩 중...</div>

    <template v-else-if="sample">
      <h1 class="detail-title">{{ sample.title }}</h1>
      <div class="detail-meta">
        <span>작성자: {{ sample.author }}</span>
        <span>{{ formatDate(sample.created_at) }}</span>
      </div>
      <hr class="divider" />
      <p class="detail-content">{{ sample.content }}</p>
      <div class="detail-actions">
        <router-link to="/samples" class="btn btn-secondary">목록</router-link>
        <router-link :to="`/samples/${sample.id}/edit`" class="btn btn-primary">수정</router-link>
        <button class="btn btn-danger" @click="remove">삭제</button>
      </div>
    </template>

    <div v-else class="error">데이터를 찾을 수 없습니다.</div>
  </div>
</template>

<script>
export default {
  name: "SampleDetailPage",
  data() {
    return {
      sample: null,
      loading: true,
    };
  },
  methods: {
    formatDate(dateStr) {
      const d = new Date(dateStr);
      const y = d.getFullYear();
      const m = String(d.getMonth() + 1).padStart(2, "0");
      const day = String(d.getDate()).padStart(2, "0");
      const h = String(d.getHours()).padStart(2, "0");
      const min = String(d.getMinutes()).padStart(2, "0");
      return `${y}-${m}-${day} ${h}:${min}`;
    },
    async remove() {
      if (!confirm("정말 삭제하시겠습니까?")) return;
      const res = await fetch(`/api/samples/${this.$route.params.id}`, {
        method: "DELETE",
      });
      if (!res.ok) {
        alert("삭제에 실패했습니다.");
        return;
      }
      this.$router.push("/samples");
    },
  },
  async mounted() {
    try {
      const res = await fetch(`/api/samples/${this.$route.params.id}`);
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      this.sample = await res.json();
    } catch (e) {
      this.sample = null;
    } finally {
      this.loading = false;
    }
  },
};
</script>

<style scoped>
.detail-container {
  width: 100%;
  max-width: 700px;
  margin: 0 auto;
  background: #fff;
  border-radius: 12px;
  padding: 2rem;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}
.detail-title {
  font-size: 1.5rem;
  margin-bottom: 0.5rem;
  color: #333;
}
.detail-meta {
  display: flex;
  gap: 1rem;
  font-size: 0.85rem;
  color: #888;
  margin-bottom: 1rem;
}
.divider {
  border: none;
  border-top: 1px solid #eee;
  margin-bottom: 1rem;
}
.detail-content {
  font-size: 1rem;
  line-height: 1.7;
  color: #444;
  margin-bottom: 2rem;
  white-space: pre-wrap;
}
.detail-actions {
  display: flex;
  gap: 0.5rem;
}
.btn {
  padding: 0.5rem 1.2rem;
  border-radius: 8px;
  text-decoration: none;
  font-size: 0.95rem;
  border: none;
  cursor: pointer;
  transition: opacity 0.2s;
}
.btn:hover {
  opacity: 0.8;
}
.btn-secondary {
  background: #6c757d;
  color: #fff;
}
.btn-primary {
  background: #667eea;
  color: #fff;
}
.btn-danger {
  background: #e74c3c;
  color: #fff;
}
.loading,
.error {
  text-align: center;
  padding: 2rem;
  font-size: 1rem;
}
.error {
  color: #e74c3c;
}
</style>
