<template>
    <table class="table is-stripped is-fullwidth">
        <thead>
            <a-row :item="labels" :columns="columns" :orderable="orderable"
                    @move="$emit('colmove', $event)">
                <template v-if="$slots['header-head']" v-slot:head="data">
                    <slot name="header-head" v-bind="data"/>
                </template>
                <template v-if="$slots['header-tail']" v-slot:tail="data">
                    <slot name="header-tail" v-bind="data"/>
                </template>
            </a-row>
        </thead>
        <tbody>
            <slot name="head"/>
            <template v-for="(item,row) in items" :key="row">
                <!-- data-index comes from AList component drag & drop -->
                <a-row :item="item" :cell="{row}" :columns="columns" :data-index="row"
                        :data-row="row"
                        :draggable="orderable"
                        @dragstart="onDragStart" @dragover="onDragOver" @drop="onDrop"
                        @cell="onCellEvent(row, $event)">
                    <template v-for="[name,slot] of rowSlots" :key="slot" v-slot:[slot]="data">
                        <template v-if="slot == 'head' || slot == 'tail'">
                            <slot :name="name" v-bind="data"/>
                        </template>
                        <template v-else>
                            <div>
                                <slot :name="name" v-bind="data"/>
                            </div>
                        </template>
                    </template>
                </a-row>
            </template>
            <slot name="tail"/>
        </tbody>
    </table>
</template>
<script>
import AList from './AList.vue'
import ARow from './ARow.vue'

const Component = {
    extends: AList,
    components: { ARow },
    emit: ['cell', 'colmove'],

    props: {
        ...AList.props,
        columns: Array,
        labels: Object,
        allowCreate: Boolean,
    },

    data() {
        return {
            ...super.data,
            extraItem: new this.set.model(),
        }
    },

    computed: {
        rowCells() {
            const cells = []
            for(var row in this.items)
                cells.push({row})
        },

        rowSlots() {
            return Object.keys(this.$slots).filter(x => x.startsWith('row-'))
                                           .map(x => [x, x.slice(4)])
        },
    },

    methods: {
        /**
         * React on 'cell' event, re-emitting it with additional values:
         * - `set`: data set
         * - `row`: row index
         *
         * @param {Number} row: row index
         * @param {} data: cell's event data
         */
        onCellEvent(row, event) {
            if(event.name == 'focus')
                this.focus(event.data, event.cell)
            this.$emit('cell', {
                ...event, row,
                set: this.set
            })
        },

        /**
         * Return row component at provided index
         */
        getRow(row) {
            const els = this.$el.querySelectorAll('tr')
            for(var el of els)
                if(el.__row && row == Number(el.dataset.row))
                    return el.__row
        },

         /**
         * Focus on a cell
         */
        focus(row, col, from=null) {
            if(from)
                row += from.row
            row = this.getRow(row)
            row && row.focus(col, from)
        },
    },
}
Component.props.itemTag.default = 'tr'
Component.props.listTag.default = 'tbody'

export default Component
</script>
