<template>
  <el-card class="mb-12px" body-class="p-8px! mb-12px">
    <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="(item, index) in children || []"
        :key="index"
      >
        <div class="w-full flex items-center gap-4px relative group/item">
          <input-number
            controls-position="right"
            :model-value="item.x"
            @change="(val) => setRow(index, val)"
          >
            <template #prefix>
              <span>X</span>
            </template>
          </input-number>
          <input-number
            controls-position="right"
            :model-value="item.y"
            @change="(val) => setColumn(index, val)"
          >
            <template #prefix>
              <span>Y</span>
            </template>
          </input-number>
          <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'

const props = defineProps<{
  values: Ref<{ x: number; y: number }[]>
}>()

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

/*
 * 设置x
 */
const setRow = (index: number, val?: number) => {
  if (typeof val === 'undefined') return

  const record = children.value[index]

  record.x = val
}

/*
 * 设置y
 */
const setColumn = (index: number, val?: number) => {
  if (typeof val === 'undefined') return

  const record = children.value[index]

  record.y = val
}

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

/**
 * 添加一项
 */
const handleAddRow = () => {
  children.value?.push({
    x: 0,
    y: 0
  })
}
</script>

<style scoped></style>
