<template>
    <el-dialog v-model="dialogVisible" title="列配置" width="600px" :before-close="handleClose">
        <div class="column-config-container">
            <div class="draggable-container">
                <div v-for="(item, index) in columns" :key="item.id" class="draggable-item" draggable="false"
                    @dragstart="dragStart($event, index)" @dragover.prevent="dragOver($event, index)"
                    @drop="drop($event, index)">
                    <div class="draggable-item-content">
                        <div class="draggable-item-label">{{ item.label }}</div>
                        <el-checkbox style="transform: scale(1.5);" label="" v-model="item.checked"></el-checkbox>
                    </div>
                </div>
                <div v-if="draggedIndex !== null && dropIndex !== null" class="drop-placeholder"
                    :class="{ 'drop-highlight': dropIndex === index }" v-for="(item, index) in columns" :key="item.id"
                    @dragover.prevent="dragOver($event, index)" @drop="drop($event, index)"></div>
            </div>
        </div>
        <template #footer>
            <div>
                <el-button type="primary" @click="handleSave">保 存</el-button>
                <el-button @click="handleClose">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { ref, defineEmits, defineProps, onMounted } from 'vue';
import { ElDialog, ElMessage } from 'element-plus';

const emits = defineEmits(['close', 'ok']);
const props = defineProps({
    /** 所有列数据 */
    columns: {
        type: Array,
        default: () => []
    },
    /** 列配置数组 ,格式例子:['userName', 'userAge'] */
    cfgData: {
        type: Array,
        default: () => []
    }
})
const dialogVisible = ref(true);

const columns = ref([]);

const handleSave = () => {
    let arr = columns.value.filter(item => {
        return !!item.checked
    }).map(item => item.prop)
    emits('ok', arr);
    emits('close')
};

let draggedIndex = null;
let dropIndex = null;

const dragStart = (event, index) => {
    return
    draggedIndex = index;
    event.dataTransfer.effectAllowed = 'move';
    event.dataTransfer.dropEffect = 'move';
};

const dragOver = (event, index) => {
    return
    dropIndex = index;
};

const drop = (event, index) => {
    return
    if (draggedIndex !== null && draggedIndex !== index) {
        const [removed] = columns.value.splice(draggedIndex, 1);
        columns.value.splice(index, 0, removed);
    }
    draggedIndex = null;
    dropIndex = null;
};

const handleClose = (done) => {
    emits('close');
};

const init = () => {
    // const cfgKeyArr = props.cfgData(item=>{
    //     if(item.prop) return item.prop
    //     if(typeof item === 'string') return item
    //     return item
    // })
    
    columns.value = props.columns.map(item => {
        return {
            label: item.title,
            prop: item.prop,
            checked: props.cfgData.includes(item.prop)
        }
    })
};
onMounted(() => {
    init()
})
</script>

<style scoped lang="scss">
.column-config-container {
    min-height: 600px;
    padding: 20px 0;
    display: flex;
    justify-content: center;
    align-content: center;
    overflow-y: auto;
}

.draggable-container {
    display: flex;
    flex-direction: column;
    gap: 10px;
    transition: all 0.3s ease;
}

.draggable-item {
    width: 540px;
    padding: 8px 18px;
    background-color: #f0f0f0;
    border: 1px solid #ccc;
    border-radius: 4px;
    //   cursor: move;
    transition: transform 0.3s ease;

    .draggable-item-content {
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 16px;
        font-weight: bolder;
    }
}

// .draggable-item:active {
//     transform: scale(1.05);
// }

.draggable-item:hover {
    background-color: #e0e0e0;
}

.drop-placeholder {
    width: 540px;
    height: 10px;
    background-color: transparent;
    transition: background-color 0.3s ease;
}

.drop-highlight {
    background-color: #e0f7fa;
    height: 40px;
    /* Increase height to match draggable-item */
    margin-top: -20px;
    /* Adjust margin to align with draggable-item */
    z-index: 1;
    position: relative;
}
</style>