<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"></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(){
            if((this.items.length-this.start-this.shownum+1) <= 0)
                return 0 + 'px'
            else
                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

            // e.target.scrollTop = e.target.scrollHeight
            // this.end = this.items.length
            // this.start = this.end - this.shownum
        },
        chatWithHandle(){
            if(this.status == true){
                let scrollElem = this.$refs.scrollDiv
                scrollElem.scrollTop = scrollElem.scrollHeight 
                console.log(scrollElem.scrollHeight,scrollElem.scrollTop)
                this.end = this.items.length
                this.start = this.end - this.shownum
                if(this.start<0)
                    this.start = 0
            }
        }
    },
    expose: ['chatWithHandle'],
    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>