<template>
    <div ref="scrollDiv" class="container" :style="{height: containerHeight}" @scroll="scroll_event">
        <div class="list" :style="{top: listTop}">
            <slot :show_list="getShowList" :show_height="one_height" @chatWithEvent="chatWithHandle"></slot>
            <div :style="{height: barHeight}"></div>
        </div>
    </div>

</template>


<script>
export default{
    data(){
        return{
            start : 0,
            end : this.shownum,
        }
    },
    props:{
        shownum: Number,
        items: Array,
        one_height: Number,
        status:{
            type: Boolean,
            default: false
        }
    },
    mounted(){
    },
    computed:{
        containerHeight(){
            return this.shownum*this.one_height + 'px'
        },
        getShowList(){
            console.log(this.items.slice(this.start,this.end))
            return this.items.slice(this.start,this.end)
        },
        barHeight(){
            return this.one_height * (this.items.length-this.start-this.shownum+1) + 'px'
        },
        listTop(){
            // console.log(this.items,this.shownum,this.one_height)
            return this.one_height * this.start + 'px'
        },
    },
    methods:{
        scroll_event(e){
            const top = e.target.scrollTop
            this.start = Math.floor(top / this.one_height)
            this.end = this.start + this.shownum
        },
        chatWithHandle(){
            console.log(1)
            if(this.status == true){
                this.end = this.items.length
                // console.log(this.end)
                this.start = this.end - this.shownum
                if(this.start < 0 ){
                    this.start = 0
                }else{
                    let scrollElem = this.$refs.scrollDiv
                    scrollElem.scrollTo({top: scrollElem
                        ,behavior: 'smooth'})
                    console.log(1)
                }
            }
        }
    },
    watch:{
        status(){
            console.log(1)
        }
    }
}


</script>
<style scoped>
.container{
    overflow-y: scroll;
    align-items: center;
    background-color: transparent;
    width: 100%;
    height: 100%;
    scrollbar-color:#F3ABB6 #FFFF;
    /* text-align: center; */

    /* height: 100px; */
}
.list{
    position: relative;
    top: 0;
    width: 100%;
}

</style>