import {computed, defineComponent, getCurrentInstance, h} from 'vue'

export default defineComponent({
    name: 'SdColumn',
    props: {
        prop: String,
        label: String,
        width: String,
        type: String
    },
    setup(props, {slots}) {
        const renderCell = (data, index) => {
            return slots.default({
                $index: index,
                row: data
            })
        }

        const editCell = (data, index) => {
            return slots.edit({
                $index: index,
                row: data
            })
        }
        const instance = getCurrentInstance()
        let data = {
            label: props.label,
            prop: props.prop,
            width: props.width,
            renderCell: renderCell,
            slots: slots.default,
            editSlots: slots.edit,
            editCell: editCell
        }
        const owner = computed(() => {
            let parent = instance.parent as any
            while (parent && !parent.tableId) {
                parent = parent.parent
            }
            return parent
        })
        owner.value.store.column.push(data)
    },
    render() {
        return h('div', null, {default: this.$slots.default, edit: this.$slots.edit})
    },
})
