<template>
  <el-card class="mb-12px" body-class="p-8px!">
    <template #header>
      <div class="flex items-center justify-between">
        <span> 单元格 </span>
        <span>
          <el-tooltip content="添加一行">
            <span>
              <PiRowsPlusBottomLight class="cursor-pointer" size="16px" @click="handleAddRow" />
            </span>
          </el-tooltip>
          <el-tooltip content="添加一列">
            <span>
              <PiColumnsPlusRightLight
                class="cursor-pointer ml-4px"
                size="16px"
                @click="handleAddColumn"
              />
            </span>
          </el-tooltip>
        </span>
      </div>
    </template>
    <el-scrollbar max-height="120px">
      <div class="flex items-center gap-4px mb-12px pr-12px">
        <template v-for="(_, index) in colsWidth">
          <input-number v-model="colsWidth[index]" :min="1" :max="1000" class="flex-1" />
        </template>
      </div>
      <div
        class="flex items-center gap-4px box-border pr-12px mb-4px"
        v-for="(row, rowIndex) in items"
        :key="rowIndex"
      >
        <div class="w-full flex items-center gap-4px relative group/item">
          <div
            v-for="(_, columnIndex) in row"
            :key="`${rowIndex}_${columnIndex}`"
            class="relative flex-1"
          >
            <LanguageInput
              type="textarea"
              :rows="1"
              :autosize="false"
              resize="none"
              class="flex-1"
              v-model="row[columnIndex]"
            />
            <div
              v-if="rowIndex === 0 && columnIndex !== 0"
              class="absolute top--4px right-0 cursor-pointer"
            >
              <LuX size="14px" @click="handleDeleteColumn(columnIndex)" />
            </div>
            <div
              v-if="columnIndex === 0 && rowIndex !== 0"
              class="absolute left--1px top--4px cursor-pointer"
            >
              <LuX size="14px" @click="handleDeleteRow(rowIndex)" />
            </div>
          </div>
        </div>
      </div>
    </el-scrollbar>
  </el-card>
</template>

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

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

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

// 列宽
const colsWidth = computed({
  get() {
    return props.values?.value?.props.columnWidth || []
  },
  set(value) {
    if (props.values?.value?.props.columnWidth) {
      props.values.value.props.columnWidth = value
    }
  }
})

// 列数
const rows = computed({
  get() {
    return props.values?.value?.props.rows || 1
  },
  set(value: number) {
    if (props.values?.value?.props.rows) {
      props.values.value.props.rows = value
    }
  }
})
// 列数
const cols = computed({
  get() {
    return props.values?.value?.props.cols || 1
  },
  set(value: number) {
    if (props.values?.value?.props.cols) {
      props.values.value.props.cols = value
    }
  }
})

/**
 * 添加一行
 */
const handleAddRow = () => {
  items.value.push(new Array(cols.value).fill('cell'))
  rows.value++
}

/**
 * 添加一列
 */
const handleAddColumn = () => {
  items.value.forEach((row, index) => {
    if (index === 0) {
      row.push('header')
    } else {
      row.push('cell')
    }
  })
  cols.value++
  colsWidth.value.push(130)
}

/**
 * 删除一行
 * @param index 索引
 */
const handleDeleteRow = (index: number) => {
  items.value.splice(index, 1)
  rows.value--
}

/**
 * 删除一列
 */
const handleDeleteColumn = (index) => {
  items.value.forEach((row) => {
    row.splice(index, 1)
  })
  cols.value--
  colsWidth.value.splice(index, 1)
}
</script>

<style scoped></style>
