<template>
    <swiper class='swiper' ref='mySwiper' :options='swiperOption' v-show='swiperList.length'>
        <swiper-slide v-for='(item,index) in swiperData' :key='index'>
            <div
                class='card'
                :class='{"active":detail.active}'
                @click='handleClick(detail)'
                v-for='(detail,dIndex) in item'
                :key='dIndex'
            >{{detail.drawernum}}</div>
        </swiper-slide>
        <div class='swiper-pagination' slot='pagination'></div>
        <div class='swiper-button-prev' slot='button-prev'></div>
        <div class='swiper-button-next' slot='button-next'></div>
        <!-- 如果需要滚动条 -->
        <!-- <div class='swiper-scrollbar'></div> -->
    </swiper>
</template>

<script>
import Vue from 'vue'
import { Swiper, SwiperSlide, directive } from 'vue-awesome-swiper'
import 'swiper/swiper-bundle.css'
import { cloneDeep } from 'lodash'
let swiperVm = null
export default {
    components: {
        Swiper,
        SwiperSlide,
    },
    directives: {
        swiper: directive,
    },
    props: {
        list: {
            type: Array,
            default: () => {
                return []
            },
        },
    },
    computed: {
        swiper() {
            return this.$refs.mySwiper.$swiper
        },
    },
    data() {
        return {
            swiperList: [],
            swiperData: [],
            swiperOption: {
                observer: true,
                observeParents: true,
                pagination: {
                    el: '.swiper-pagination',
                    clickable: false,
                    type: 'fraction',
                },
                navigation: {
                    nextEl: '.swiper-button-next',
                    prevEl: '.swiper-button-prev',
                },
                // on: {
                //     click: function (event) {
                //         const { realIndex } = this
                //         // 这里有坑，需要注意的是：this 指向的是 swpier 实例，而不是当前的 vue， 因此借助 vm，来调用 methods 里的方法
                //         // console.log(this) // -> Swiper
                //         // 当前活动块的索引，与activeIndex不同的是，在loop模式下不会将 复制的块 的数量计算在内。
                //         swiperVm.handleTap(event.target.innerText)
                //     },
                // },
                // autoplay: {
                //     //自动播放
                //     delay: 1500,
                //     disableOnInteraction: false,
                // },
                // speed: 1500, //播放速度
            },
        }
    },
    created() {
        swiperVm = this
        this.swiperList = cloneDeep(this.list)
        let swiperData = []
        swiperData = this.group(this.swiperList, 15)
        this.swiperData = swiperData
    },
    mounted() {
        console.log(this.swiper)
    },
    methods: {
        handleClick(detail) {
            // console.log(detail)
            let currentInfo = {}
            this.swiperList.map((item, index) => {
                item.active = false
                if (detail.drawernum === item.drawernum) {
                    item.active = true
                    currentInfo = cloneDeep(item)
                }
            })
            this.$emit('itemClick', currentInfo)
        },
        // 数组拆分为 15个的二维数组
        group(array, subGroupLength) {
            let index = 0
            let newArray = []
            while (index < array.length) {
                newArray.push(array.slice(index, (index += subGroupLength)))
            }
            return newArray
        },
    },
}
</script>

<style lang="less" scoped>
.swiper {
    height: 210px;
    width: 100%;
    .swiper-slide {
        display: flex;
        flex-wrap: wrap;
        text-align: center;
        color: #333333;
        font-size: 14px;
        font-weight: 400;
    }
}
/deep/ .swiper-wrapper {
    height: auto;
    max-height: 190px;
}
.card {
    width: calc(33.33333% - 5.333334px);
    margin-bottom: 8px;
    height: 30px;
    line-height: 30px;
    background: #ebeded;
}
.card:not(:nth-child(3n)) {
    margin-right: 8px;
}
.swiper-pagination {
    position: absolute;
    bottom: 0;
    display: flex;
    justify-content: center;
    align-items: flex-end;
    flex-wrap: wrap;
    color: #025baa;
    font-size: 14px;
}
.active {
    background: #025baa !important;
    color: #fff !important;
}

.swiper-button-prev,
.swiper-button-next {
    top: initial !important;
    bottom: 0;
    width: 15px !important;
    height: 12px !important;
}
.swiper-button-prev {
    left: 110px;
}
.swiper-button-next {
    right: 110px;
}
.swiper-button-prev:after,
.swiper-button-next:after {
    font-size: 14px !important;
}
</style>