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

const router = useRouter()
const store = useSchemasStore()

function repeatLabel(schema) {
  if (!schema.repeat) return 'Einmalig'
  if (schema.repeat.daily) return 'Täglich'
  if (schema.repeat.weekly) return 'Wöchentlich'
  if (schema.repeat['2week']) return '2-Wöchentlich'
  if (schema.repeat['4week']) return '4-Wöchentlich'
  if (schema.repeat.monthly) return 'Monatlich'
  if (schema.repeat.days) return 'Mehrere Tage'
  return ''
}

async function onDelete(id) {
  await store.remove(id)
}

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

<template>
  <div class="schema-all-view">
    <div class="actions">
      <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.schemas.length === 0" class="hint">Lädt…</p>
    <p v-else-if="store.error" class="error">{{ store.error }}</p>
    <p v-else-if="store.schemas.length === 0" class="hint">Keine Schemas.</p>

    <ul v-else class="schema-list">
      <li
        v-for="schema in store.schemas"
        :key="schema.id"
        class="schema-item"
        :class="{ inactive: schema.status === 'inactive' }"
      >
        <div class="schema-info">
          <span class="schema-name">{{ schema.name }}</span>
          <span class="schema-repeat">{{ repeatLabel(schema) }}</span>
        </div>
        <div class="schema-actions">
          <button class="icon-btn" @click="router.push(`/schemas/${schema.id}`)" title="Bearbeiten">
            <Icon name="pencil" />
          </button>
          <button class="icon-btn" @click="onDelete(schema.id)" title="Löschen">
            <Icon name="trash" />
          </button>
        </div>
      </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);
}

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

.schema-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}

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

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

.schema-item.inactive {
  opacity: 0.5;
  font-style: italic;
}

.schema-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.schema-repeat {
  font-size: 0.85rem;
  color: var(--text-muted);
}

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

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

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