<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 box-border pr-12px items-center gap-4px mb-4px"
        v-for="(_, index) in modelValue || []"
        :key="index"
      >
        <div class="w-full relative group/item">
          <LanguageInput v-model="modelValue[index]" />
          <div class="absolute top--7px right--4px cursor-pointer" @click="handleDeleteItem(index)">
            <LuX size="14px" />
          </div>
        </div>
      </div>
    </el-scrollbar>
  </el-card>
</template>

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

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

const modelValue = computed({
  get() {
    return props.values?.value
  },
  set(val) {
    props.values.value = val
  }
})

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

/**
 * 添加一项
 */
const handleAddRow = () => {
  modelValue.value?.push('option')
}
</script>

<style scoped></style>
