<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="flex items-center gap-4px 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">
          <LanguageInput v-model="children[index].text" />
          <div class="cursor-pointer" @click="handleDeleteItem(index)">
            <LuTrash2 size="14px" />
          </div>
        </div>
      </div>
    </el-scrollbar>
  </el-card>
</template>

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

const props = defineProps<{
  values: Ref<{ text: string }[]>
}>()

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

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

/**
 * 添加一项
 */
const handleAddRow = () => {
  children.value?.push({
    text: 'title'
  })
}
</script>

<style scoped></style>
