<template>
	<div ref="scFormTableRef" class="sc-form-table">
		<el-table ref="tableRef" :data="data" border stripe>
			<el-table-column type="index" width="50" fixed="left">
				<template #header>
					<el-button v-if="!hideAdd" type="primary" :icon="Plus" size="small" circle @click="rowAdd"></el-button>
				</template>
				<template #default="scope">
					<div :class="['sc-form-table-handle', { 'sc-form-table-handle-delete': !hideDelete }]">
						<span>{{ scope.$index + 1 }}</span>
						<el-button v-if="!hideDelete" type="danger" :icon="Delete" size="small" plain circle @click="rowDel(scope.row, scope.$index)"></el-button>
					</div>
				</template>
			</el-table-column>
			<el-table-column v-if="dragSort" label="" width="50">
				<template #default>
					<div class="move" style="cursor: move"><DCaret style="width: 1em; height: 1em" /></div>
				</template>
			</el-table-column>
			<slot></slot>
			<template #empty>
				{{ placeholder }}
			</template>
		</el-table>
	</div>
</template>

<script setup>
import Sortable from 'sortablejs'
import { Plus, Delete } from '@element-plus/icons-vue'
import { nextTick, onMounted, ref, watch } from 'vue'

const myProps = defineProps({
	modelValue: { type: Array, default: () => [] },
	addTemplate: { type: Object, default: () => {} },
	placeholder: { type: String, default: '暂无数据' },
	dragSort: { type: Boolean, default: false },
	hideAdd: { type: Boolean, default: false },
	hideDelete: { type: Boolean, default: false }
})

const emit = defineEmits(['update:modelValue'])

const data = ref([])
const tableRef = ref(null)
const scFormTableRef = ref(null)

const rowDrop = () => {
	const tbody = tableRef.value.$el.querySelector('.el-table__body-wrapper tbody')
	Sortable.create(tbody, {
		handle: '.move',
		animation: 300,
		ghostClass: 'ghost',
		onEnd({ newIndex, oldIndex }) {
			data.value.splice(newIndex, 0, data.value.splice(oldIndex, 1)[0])
			const newArray = data.value.slice(0)
			const tmpHeight = scFormTableRef.value.offsetHeight
			scFormTableRef.value.style.setProperty('height', tmpHeight + 'px')
			data.value = []
			nextTick(() => {
				data.value = newArray
				nextTick(() => {
					scFormTableRef.value.style.removeProperty('height')
				})
			})
		}
	})
}

const rowAdd = () => {
	const temp = JSON.parse(JSON.stringify(myProps.addTemplate))
	data.value.push(temp)
}
const rowDel = (row, index) => {
	data.value.splice(index, 1)
}
//插入行
const pushRow = row => {
	const temp = row || JSON.parse(JSON.stringify(myProps.addTemplate))
	data.value.push(temp)
}
//根据index删除
const deleteRow = index => {
	data.value.splice(index, 1)
}

watch(
	() => myProps.modelValue,
	(newQuestion, oldQuestion) => {
		// data.value = myProps.modelValue
		data.value = newQuestion
	}
)

watch(
	data,
	(newQuestion, oldQuestion) => {
		emit('update:modelValue', newQuestion)
	},
	{ deep: true }
)

onMounted(() => {
	data.value = myProps.modelValue
	if (myProps.dragSort) {
		rowDrop()
	}
})
</script>

<style scoped>
.sc-form-table {
	width: 100%;
}
.sc-form-table .sc-form-table-handle {
	text-align: center;
}
.sc-form-table .sc-form-table-handle span {
	display: inline-block;
}
.sc-form-table .sc-form-table-handle button {
	display: none;
}
.sc-form-table .hover-row .sc-form-table-handle-delete span {
	display: none;
}
.sc-form-table .hover-row .sc-form-table-handle-delete button {
	display: inline-block;
}
.sc-form-table .move {
	text-align: center;
	font-size: 14px;
	margin-top: 3px;
}
</style>
