<template>
  <div style="width:100%">
    <vxe-button status="primary" @click="addEvent">新增（顶部）</vxe-button>
    <vxe-button status="primary" @click="pushEvent">新增（尾部）</vxe-button>
    <vxe-button status="primary" @click="insertEvent">新增（第三行插入）</vxe-button>
    <vxe-button status="success" @click="getInsertEvent">获取新增的数据</vxe-button>
    <vxe-grid ref="gridRef" v-bind="gridOptions"></vxe-grid>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { VxeGridProps, VxeGridInstance } from 'vxe-table'

const gridRef = ref<VxeGridInstance<any>>()
const gridOptions = reactive<VxeGridProps<any>>({
  border: true,
  showOverflow: true,
  keepSource: true,
  height:"400",
  width:"100%",
  editConfig: {
    trigger: 'click',
    mode: 'cell',
    showStatus: true
  },
  scrollX: {
    enabled: true
  },
  columns: [],
  data: []
})

const addEvent = async () => {
  const $grid = gridRef.value
  if ($grid) {
    const record = {}
    const { row: newRow } = await $grid.insert(record)
    $grid.setEditRow(newRow)
  }
}

const pushEvent = async () => {
  const $grid = gridRef.value
  if ($grid) {
    const record = {}
    const { row: newRow } = await $grid.insertAt(record, -1)
    $grid.setEditRow(newRow)
  }
}

const insertEvent = async () => {
  const $grid = gridRef.value
  if ($grid) {
    const record = { }
    const { row: newRow } = await $grid.insertAt(record, 2)
    $grid.setEditRow(newRow)
  }
}

const getInsertEvent = () => {
  const $grid = gridRef.value
  if ($grid) {
    const insertRecords = $grid.getInsertRecords()
  }
}


const iniTable = async (columns=[],datas=[]) => {
  gridOptions.data = datas
  gridOptions.columns = columns
}

defineExpose({
  iniTable,
});
</script>
