/**
*@author:Guoxiaohuan
*@date:2020.06.02
*@info：楼层列表
*/
<template>
    <div class='floor-box'>
        <div class='floor-list'>
            <div class='icon-top' v-if='floorsArr.length>showNumber'>
                <!-- @click='changeFloor(1,currIndex)' -->
                <!-- @mousedown='mousedown' -->
                <!-- @mouseup='mouseup' -->
                <!-- v-repeat-click='increase' -->
                <img v-if='parseInt(marginTop) !== 0' v-repeat-click='increase' src='@/assets/imgs/iconBlackTop.png' alt />
                <img class='disabled' v-else src='@/assets/imgs/iconLightTop.png' alt />
            </div>
            <div class='floor-out'>
                <!--  放开marginTop样式  -->
                <div class='floor-center' :style='{ marginTop : marginTop }'>
                    <div
                        class='floor-item'
                        :class='item.seq == currentFloorId?"isActive":""'
                        @click='tabFloor(item,index)'
                        v-for='(item,index) in floorsArr'
                        :key='index'
                    >{{item.code}}</div>
                </div>
            </div>
            <div class='icon-bottom' v-if='floorsArr.length>showNumber'>
                <!-- v-repeat-click='decrease' -->
                <img v-if='parseInt(marginTop) !== marginTopMax' v-repeat-click='decrease' src='@/assets/imgs/iconBlackBottom.png' alt />
                <img class='disabled' v-else src='@/assets/imgs/iconLightBottom.png' alt />
            </div>
        </div>
    </div>
</template>
<script>
import store from '../store'
import { mapGetters } from 'vuex'
import RepeatClick from '@/directives/repeat-click'

export default {
    directives: {
        repeatClick: RepeatClick
    },
    data() {
        return {
            floorId: 'F1',
            showT: true,
            showB: true,
            num: 0,
            floorMapIdName: '',
            floor: {
                code: 'F1',
                gcode: '1F',
                gname: 'f1',
                name: '第1层',
                seq: 100
            },
            currentFloorId: null,
            marginTop: 0,
            marginTopMax: 0,
            startTime: '',
            endTime: '',
            showNumber: 8, //需要暂时的楼层数   //TODO:
            height: 39, //一个楼层的高度
            currIndex: 0 //当前楼层在 楼层数组中的下标,上下箭头使用
        }
    },
    props: {
        floorsArr: {
            type: Array,
            default: () => {
                ;[]
            }
        }
    },
    computed: {
        ...mapGetters(['legendTable'])
    },
    mounted() {
        window.vm = this
        this.init()
    },
    methods: {
        mousedown() {
            this.startTime = new Date()
        },
        mouseup() {
            this.endTime = new Date()
            if (this.endTime - this.startTime < 200) {
                console.log(11111111)
                this.changeFloor(1, this.currIndex)
                return false
            }
        },
        increase() {
            console.log('increase')
            let marginTop = parseInt(this.marginTop)
            marginTop < 0 && this.changeFloor(1, this.currIndex)
        },
        decrease() {
            console.log('decrease')
            let marginTop = Math.abs(parseInt(this.marginTop)),
                marginTopMax = Math.abs(parseInt(this.marginTopMax))
            marginTop < marginTopMax && this.changeFloor(-1, this.currIndex)
        },
        init() {
            // console.log('init')
            if (!this.floorsArr.length) {
                return false
            }
            this.floorIdArr = []
            this.floorsArr.map(item => {
                this.floorIdArr.push(item.seq)
            })
            this.currentFloorId = Number(this.$cookie.get('currentFloorId') || 100)
            // bug fix 修复 fenbuPic 弹窗中,楼层不能选中的问题
            // 如果 floorIdArr 不在 floorIdArr数组中, 使用 floorIdArr[0], 而不使用 cookie
            let index = this.floorIdArr.findIndex(item => item === this.currentFloorId)
            if (index === -1) {
                this.currentFloorId = this.floorIdArr[0]
            }
            this.marginTopMax = -(this.floorIdArr.length - this.showNumber) * this.height
            this.changeFloor(0, index)
        },
        /**
         * @name changeFloor
         * @param {Number} flag 1:向上滚动楼层, -1向下滚动 , 0:进入页面初始化时,执行位置处理
         * @description 点击图例下方的,上下切换按钮
         */
        changeFloor(flag, index) {
            console.log('changeFloor')
            const len = this.floorIdArr.length
            // let index = this.floorIdArr.findIndex(item => item === this.currentFloorId)
            this.currIndex = index
            // 点击上箭头
            if (flag === 1) {
                index--
                this.currIndex = index
            } else if (flag === -1) {
                //点击下箭头
                index++
                this.currIndex = index
            }
            // FEAT:  点击上下箭头,不触发cookie更改 , 只有初始化时,才进行选中的楼层 状态更新
            if (flag === 0) {
                this.handleCookie()
            }
            this.handlePosition(flag, index, len)
        },
        handleCookie() {
            // return true
            let currentFloor = this.floorsArr.filter(item => item.seq == this.currentFloorId)[0]
            if (currentFloor) {
                this.$cookie.set('floorNow', currentFloor.code || '', 3)
                this.$cookie.set('floorMapId', currentFloor.gname, 3)
                this.$cookie.set('currentFloorId', currentFloor.seq, 3)
                this.floorId = this.$cookie.get('floorNow') || currentFloor.code
                this.floorMapIdName = this.$cookie.get('floorMapId') || currentFloor.gname
                store.commit('SETCURRENTFLOOR', currentFloor)
                this.$emit('emitFloor', currentFloor)
            }
        },
        /**
         * @name tabFloor
         * @param {Object} item 选中的楼层信息
         * @param {Number} index 楼层信息在floorsArr数组中的位置
         */
        tabFloor(item, index) {
            this.currentFloorId = this.floorIdArr[index]
            this.handleCookie()
            this.handlePosition(2, index, this.floorIdArr.length)
            this.viewLengend()
        },
        viewLengend() {
            if (this.legendTable.length > 0) {
                this.$store.commit('SETSHOWVIEW', 1)
            } else {
                this.$store.commit('SETSHOWVIEW', 0)
            }
        },
        /**
         * @description 楼层位置动画处理
         * @param flag 1:向上滚动楼层, -1向下滚动 , 0:进入页面初始化时,执行位置处理 2:直接点击楼层
         * @param index 楼层 在floorIdArr中的下标
         * @param len floorIdArr长度
         */
        handlePosition(flag, index, len) {
            // 取出当前 marginTop
            let marginTop = parseInt(this.marginTop)
            switch (flag) {
                // 初始化进入页面,位置处理
                case 0:
                // 直接点击楼层,滚动楼层
                case 2:
                    // 将 marginTop 设置为对应的index 应滚动的距离
                    marginTop = -index * this.height
                    // marginTop 过大时,取最大值marginTopMax
                    if (Math.abs(marginTop) >= Math.abs(this.marginTopMax)) {
                        marginTop = parseInt(this.marginTopMax)
                    }
                    // marginTop＞0时,取0,防止楼层上边出现空白
                    marginTop = marginTop >= 0 ? 0 : marginTop
                    // index为0,marginTop设置为0
                    index == 0 && (marginTop = 0)
                    // index为最后一个,设置为最大marginTopMax
                    index == len - 1 && (marginTop = parseInt(this.marginTopMax))
                    this.marginTop = marginTop + 'px'
                    break
                //  1:向上滚动楼层
                case 1:
                    this.marginTop = marginTop + this.height + 'px'
                    break
                // -1向下滚动楼层
                case -1:
                    this.marginTop = marginTop + this.height * -1 + 'px'
                    break
                default:
                    break
            }
        }
    }
}
</script>
<style lang="less" scoped>
.floor-box {
    position: fixed;
    right: 32px;
    top: 172px;
    z-index: 99;
    .floor-list {
        width: 44px;
        // height: 212px;
        background: rgba(255, 255, 255, 1);
        box-shadow: 0px 2px 15px 0px rgba(31, 36, 41, 0.08);
        border-radius: 2px;
        position: relative;
        padding: 6px 4px;
        text-align: center;
        .floor-out {
            height: 300px; //TODO:
            overflow: hidden;
            position: relative;
            overflow-y: auto;
            &::-webkit-scrollbar {
                display: none;
            }

            .floor-center {
                transition: all linear 0.5s;
                .floor-item {
                    line-height: 28px;
                    height: 28px;
                    cursor: pointer;
                    position: relative;
                    &::after {
                        position: absolute;
                        left: 50%;
                        margin-left: -20%;
                        bottom: -6px;
                        content: '';
                        width: 14px;
                        height: 1px;
                        background: rgba(195, 199, 203, 1);
                        border: 0px solid rgba(228, 229, 231, 1);
                    }
                    & + .floor-item {
                        margin-top: 10px;
                    }
                }
            }
        }

        .icon-top {
            cursor: pointer;
            height: 18px;
            img {
                width: 18px;
                height: 100%;
                margin-top: -10px;
            }
        }
        .icon-bottom {
            cursor: pointer;
            height: 18px;
            img {
                width: 18px;
                height: 100%;
                margin-top: -10px;
            }
        }
        .isActive {
            border-radius: 4px;
            color: #025baa;
            background: #e1f2ff;
        }
    }
    .disabled {
        cursor: not-allowed !important;
    }
}
</style>