<script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useTasksStore } from '../stores/tasks'
import Icon from '../components/Icon.vue'

const router = useRouter()
const store = useTasksStore()

function formatDate(iso) {
  return new Date(iso).toLocaleDateString('de-DE')
}

async function onDelete(id) {
  if (!confirm('Task wirklich löschen?')) return
  await store.remove(id)
}

onMounted(() => {
  store.fetchAll()
})
</script>

<template>
  <div class="task-all-view">
    <div class="actions">
      <button class="icon-btn" @click="router.push('/schemas')" title="Schemas">
        <Icon name="calendar" />
      </button>
      <button class="icon-btn" @click="router.push('/schemas/create')" title="Neues Schema">
        <Icon name="plus" />
      </button>
      <button class="icon-btn" @click="store.fetchAll()" title="Neu laden">
        <Icon name="refresh" />
      </button>
    </div>

    <p v-if="store.loading && store.tasks.length === 0" class="hint">Lädt…</p>
    <p v-else-if="store.error" class="error">{{ store.error }}</p>
    <p v-else-if="store.tasks.length === 0" class="hint">Keine Tasks.</p>

    <ul v-else class="task-list">
      <li
        v-for="task in store.tasks"
        :key="task.id"
        class="task-item"
        :class="{
          done: task.status === 'done',
          inactive: task.status === 'inactive',
          past: task.status === 'past',
        }"
        @click="store.toggle(task.id)"
      >
        <span class="task-name">{{ task.name }}</span>
        <span v-if="task.date" class="task-date">{{ formatDate(task.date) }}</span>
        <span class="row-actions">
          <button
            class="icon-btn"
            @click.stop="router.push(`/tasks/${task.id}`)"
            title="Bearbeiten"
          >
            <Icon name="pencil" :size="16" />
          </button>
          <button
            class="icon-btn danger"
            @click.stop="onDelete(task.id)"
            title="Löschen"
          >
            <Icon name="trash" :size="16" />
          </button>
        </span>
      </li>
    </ul>
  </div>
</template>

<style scoped>
.actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 0.375rem;
  color: var(--text);
  cursor: pointer;
}

.icon-btn:hover {
  background: var(--breadcrumb-bg);
}

.icon-btn.danger:hover {
  border-color: #dc2626;
  color: #dc2626;
}

.task-list {
  list-style: none;
  padding: 0;
  margin: 0;
  border: 1px solid var(--border);
  border-radius: 0.375rem;
}

.task-item {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

.task-item:last-child {
  border-bottom: none;
}

.task-item:hover {
  background: var(--breadcrumb-bg);
}

.task-name {
  flex: 1;
}

.task-item.done .task-name {
  text-decoration: line-through;
  color: var(--text-muted);
}

.task-item.inactive .task-name {
  color: var(--text-muted);
  font-style: italic;
}

.task-item.past {
  opacity: 0.5;
}

.task-date {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-right: 0.5rem;
}

.row-actions {
  display: inline-flex;
  gap: 0.25rem;
}

.row-actions .icon-btn {
  width: 2rem;
  height: 2rem;
}

.hint {
  color: var(--text-muted);
}

.error {
  color: #dc2626;
}
</style>
