<template>
  <div class="history-list">
    <div v-html="$t('history_title')"></div>
    
    <div v-if="chatStore.historyResults.length === 0">
      <p>{{ $t('history_empty') }}</p>
    </div>
    
    <div
      v-for="item in chatStore.historyResults"
      :key="item.userName"
      class="history-item"
      @click="selectUser(item.userName)"
    >
      {{ item.userName }}
      <small v-if="item.lastMessage">
        - {{ formatTime(item.lastMessage.timestamp) }}
      </small>
    </div>
  </div>
</template>

<script setup>
import { useChatStore } from '../stores/chat';

const chatStore = useChatStore();

function selectUser(userName) {
  chatStore.requestConversation(userName);
}

function formatTime(timestamp) {
  const date = new Date(timestamp);
  return date.toLocaleString('de-DE');
}
</script>

