<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  element: { type: Object, required: true },
  saving: { type: Boolean, default: false },
})

const emit = defineEmits(['save'])

const edit = ref({ title: '', description: '', examples: [], hints: [] })

watch(() => props.element, load, { immediate: true })

function load() {
  edit.value = {
    title: props.element.title,
    description: props.element.description,
    examples: [...props.element.examples],
    hints: [...props.element.hints],
  }
}

function save() {
  if (props.saving) return
  emit('save', {
    title: edit.value.title,
    description: edit.value.description,
    examples: edit.value.examples.filter((s) => s.trim()),
    hints: edit.value.hints.filter((s) => s.trim()),
  })
}
</script>

<template>
  <div class="el-edit">
    <button class="edit-save" :disabled="saving" @click="save">
      {{ saving ? 'Saving…' : 'Save' }}
    </button>

    <label>Title</label>
    <input v-model="edit.title" placeholder="Title" />

    <label>Description</label>
    <textarea v-model="edit.description" placeholder="Description"></textarea>

    <label>Examples</label>
    <div v-for="(ex, i) in edit.examples" :key="'ex' + i" class="edit-row">
      <textarea v-model="edit.examples[i]" placeholder="Example"></textarea>
      <button class="edit-del" title="Remove" @click="edit.examples.splice(i, 1)">×</button>
    </div>
    <button class="edit-add" @click="edit.examples.push('')">+ Example</button>

    <label>Hints</label>
    <div v-for="(h, i) in edit.hints" :key="'hi' + i" class="edit-row">
      <textarea v-model="edit.hints[i]" placeholder="Hint"></textarea>
      <button class="edit-del" title="Remove" @click="edit.hints.splice(i, 1)">×</button>
    </div>
    <button class="edit-add" @click="edit.hints.push('')">+ Hint</button>
  </div>
</template>

<style scoped>
.el-edit {
  flex: 1;
  overflow-y: auto;
  padding: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.el-edit label {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-faint);
  margin-top: 0.5rem;
}

.el-edit input,
.el-edit textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  font-size: 0.85rem;
  font-family: inherit;
  background: var(--panel);
  color: var(--text);
  outline: none;
}

.el-edit textarea {
  resize: vertical;
  min-height: 120px;
  overflow: auto;
  line-height: 1.4;
}

.el-edit input:focus,
.el-edit textarea:focus {
  border-color: var(--accent);
}

.edit-row {
  display: flex;
  gap: 6px;
  align-items: flex-start;
}

.edit-row textarea {
  flex: 1;
}

.edit-del {
  flex-shrink: 0;
  width: 30px;
  align-self: stretch;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: none;
  color: var(--danger);
  font-size: 1rem;
  cursor: pointer;
}

.edit-add {
  align-self: flex-start;
  padding: 5px 10px;
  border: 1px dashed var(--border-strong);
  border-radius: 8px;
  background: none;
  color: var(--text-muted);
  font-size: 0.78rem;
  cursor: pointer;
}

.edit-add:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.edit-save {
  position: sticky;
  top: 0;
  z-index: 1;
  margin-bottom: 0.3rem;
  padding: 9px 10px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.edit-save:disabled {
  opacity: 0.5;
  cursor: wait;
}
</style>
