<template>
  <form class="post-form" @submit.prevent="handleSubmit">
    <div class="form-header">
      <h2 class="form-title">{{ isEdit ? 'Edit post' : 'New post' }}</h2>
      <p class="form-hint">Fill in <em>Seeking</em> or <em>Offering</em> (or both).</p>
    </div>

    <div class="field">
      <label for="post-title">Title</label>
      <input
        id="post-title"
        v-model="form.title"
        type="text"
        maxlength="120"
        placeholder="Short summary"
      >
    </div>

    <div class="field-row">
      <div class="field">
        <label for="post-seeking">Seeking <span class="opt">(optional)</span></label>
        <textarea
          id="post-seeking"
          v-model="form.seeking"
          rows="2"
          maxlength="500"
          placeholder="What are you looking for?"
        />
      </div>

      <div class="field">
        <label for="post-offering">Offering <span class="opt">(optional)</span></label>
        <textarea
          id="post-offering"
          v-model="form.offering"
          rows="2"
          maxlength="500"
          placeholder="What can you offer?"
        />
      </div>
    </div>

    <div class="field">
      <label for="post-note">Note <span class="opt">(optional)</span></label>
      <textarea
        id="post-note"
        v-model="form.note"
        rows="2"
        maxlength="300"
        placeholder="Anything else to add?"
      />
    </div>

    <div v-if="tags.length" class="field">
      <label>Tags</label>
      <div class="pill-grid">
        <button
          v-for="tag in tags"
          :key="tag.slug"
          type="button"
          class="pill"
          :class="{ selected: form.tags.includes(tag.slug) }"
          @click="toggleTag(tag.slug)"
        >{{ tag.label || tag.name || tag.slug }}</button>
      </div>
    </div>

    <p v-if="error" class="form-error">{{ error }}</p>

    <div class="form-actions">
      <button type="button" class="btn" @click="$emit('cancel')">Cancel</button>
      <button type="submit" class="btn btn-primary">
        {{ isEdit ? 'Save changes' : 'Post' }}
      </button>
    </div>
  </form>
</template>

<script setup>
const props = defineProps({
  post: { type: Object, default: null },
  tags: { type: Array, default: () => [] },
})

const emit = defineEmits(['submit', 'cancel'])

const isEdit = computed(() => !!props.post)

const form = reactive({
  title: props.post?.title || '',
  seeking: props.post?.seeking || '',
  offering: props.post?.offering || '',
  note: props.post?.note || '',
  tags: Array.isArray(props.post?.tags) ? [...props.post.tags] : [],
})

const error = ref('')

watch(() => props.post, (p) => {
  form.title = p?.title || ''
  form.seeking = p?.seeking || ''
  form.offering = p?.offering || ''
  form.note = p?.note || ''
  form.tags = Array.isArray(p?.tags) ? [...p.tags] : []
}, { immediate: false })

function toggleTag(slug) {
  const idx = form.tags.indexOf(slug)
  if (idx === -1) form.tags.push(slug)
  else form.tags.splice(idx, 1)
}

function handleSubmit() {
  error.value = ''
  const title = form.title.trim()
  const seeking = form.seeking.trim()
  const offering = form.offering.trim()

  if (!title) {
    error.value = 'Title is required.'
    return
  }
  if (!seeking && !offering) {
    error.value = 'Add at least one of Seeking or Offering.'
    return
  }

  emit('submit', {
    title,
    seeking,
    offering,
    note: form.note.trim(),
    tags: [...form.tags],
  })
}
</script>

<style scoped>
.post-form {
  border: 1px dashed var(--border);
  padding: 14px 16px;
  background: transparent;
}

.form-header {
  margin-bottom: 10px;
}
.form-title {
  font-family: "Brygada 1918", serif;
  font-size: 15px;
  font-weight: 500;
  color: var(--text-bright);
}
.form-hint {
  font-size: 11px;
  color: var(--text-faint);
  font-family: "Commit Mono", monospace;
  margin-top: 2px;
}
.form-hint em {
  color: var(--text-dim);
  font-style: normal;
}

.field {
  margin-bottom: 8px;
  flex: 1;
  min-width: 0;
}
.field-row {
  display: flex;
  gap: 12px;
}
.field label {
  display: block;
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin-bottom: 3px;
}
.field label .opt {
  color: var(--text-faint);
  text-transform: none;
  letter-spacing: 0;
  font-size: 9px;
  margin-left: 4px;
  opacity: 0.7;
}
.field input,
.field textarea {
  width: 100%;
  padding: 4px 8px;
  font-family: "Commit Mono", monospace;
  font-size: 12px;
  color: var(--text-bright);
  background: var(--input-bg);
  border: 1px solid var(--border);
  outline: none;
  resize: vertical;
}
.field input:focus,
.field textarea:focus {
  border-color: var(--candle);
}

.pill-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.pill {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border: 1px dashed var(--border);
  background: transparent;
  color: var(--text-faint);
  font-size: 10px;
  font-family: "Commit Mono", monospace;
  cursor: pointer;
  user-select: none;
  transition: all 0.12s;
  white-space: nowrap;
}
.pill:hover {
  color: var(--text-dim);
  border-color: var(--border-d);
}
.pill.selected {
  background: var(--surface);
  color: var(--text-bright);
  border-color: var(--candle);
  border-style: solid;
}
.pill:focus-visible {
  outline: 2px dashed var(--candle);
  outline-offset: 2px;
}

.form-error {
  font-size: 11px;
  color: var(--ember);
  margin: 8px 0;
  padding: 6px 10px;
  border: 1px dashed var(--ember);
  background: var(--ember-bg);
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
}

@media (max-width: 640px) {
  .field-row {
    flex-direction: column;
    gap: 0;
  }
}
</style>
