<template>
  <el-card class="mb-12px" body-class="p-8px!">
    <template #header>
      <div class="flex items-center justify-between">
        <span>标签</span>
        <LuPlus class="cursor-pointer" size="16px" @click="handleAddRow" />
      </div>
    </template>
    <el-scrollbar max-height="120px">
      <div class="box-border pr-12px mb-4px" v-for="(_, index) in children || []" :key="index">
        <div class="w-full flex items-center gap-4px relative group/item">
          <el-radio v-model="activeIndex" :value="index" class="mr-0!" />
          <el-input
            spellcheck="false"
            v-model="children[index].name"
            readonly
            @click="handleEdit(children[index], index)"
          />
          <div class="cursor-pointer" @click="handleDeleteItem(index)">
            <LuTrash2 size="14px" />
          </div>
        </div>
      </div>
    </el-scrollbar>
  </el-card>
  <el-dialog
    draggable
    append-to-body
    title="编辑选项"
    width="440px"
    v-model="open"
    close-on-click-modal
  >
    <el-form ref="form" :model="formData" :rules="rules" hide-required-asterisk>
      <el-form-item label="名称" prop="name">
        <LanguageInput v-model="formData.name" />
      </el-form-item>
      <el-form-item label="文本" prop="text">
        <LanguageInput type="textarea" :rows="2" v-model="formData.text" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed, type Ref } from 'vue'
import { LuPlus, LuTrash2 } from 'vue-icons-plus/lu'
import { FormInstance } from 'element-plus'
import { klona } from 'klona'
import LanguageInput from '../components/LanguageInput.vue'

const props = defineProps<{
  values: Ref<any>
}>()

const open = ref(false)
const form = ref<FormInstance>()
const formData = ref({
  name: '',
  text: ''
})
const rules = {
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
}

// 子项
const children = computed({
  get() {
    return (props.values?.value?.children || []) as any[]
  },
  set(list: any[]) {
    if (props.values?.value?.children) {
      props.values.value.children = list
    }
  }
})

// 当前激活
const activeIndex = computed({
  get() {
    return props.values?.value?.props?.activeIndex
  },
  set(index: number) {
    if (props.values?.value?.props) {
      props.values.value.props.activeIndex = index
    }
  }
})

/**
 * 删除一项
 * @param index 索引
 */
const handleDeleteItem = (index: number) => {
  children.value.splice(index, 1)
}

/**
 * 添加一项
 */
const handleAddRow = () => {
  children.value?.push({
    name: 'Tab',
    type: 'tab',
    children: []
  })
}

let tempIndex
const handleEdit = (record, idx: number | string) => {
  tempIndex = idx
  open.value = true
  formData.value = klona(record)
}

const handleSubmit = () => {
  form.value?.validate().then(() => {
    children.value[tempIndex] = klona(formData.value)
    open.value = false
  })
}
</script>

<style scoped></style>
