<template>
    <view
        class="mb-order"
        :style="{
            borderRadius: borderRadius,
        }"
    >
        <view
            class="mb-order-list"
            :style="{
                'transform': 'translateY(' + (-currentIndex * 42) + 'px)',
                '-webkit-transform': 'translateY(' + (-currentIndex * 42) + 'px)',
                'transition': showTrans ? 'all .7s cubic-bezier(0.2, 0, 0.2, 1)' : 'none',
                '-webkit-transition': showTrans ? 'all .7s cubic-bezier(0.2, 0, 0.2, 1)' : 'none',
            }"    
        >
            <view
                v-for="(item, index) in orderlist"
                :key="index"
                class="mb-order-i c-font-normal c-color-333"
                :class="{'mb-order-i-after': !item.btnText}"
                :style="{
                    opacity: index === currentIndex ? 1 : 0,
                    'transition': showTrans ? 'all .7s cubic-bezier(0.2, 0, 0.2, 1)' : 'none',
                    '-webkit-transition': showTrans ? 'all .7s cubic-bezier(0.2, 0, 0.2, 1)' : 'none',
                }"
                @tap.stop="onJump(item)"
            >
                <image :src="item.icon" class="mb-order-img c-gap-right"/>
                <view class="mb-order-text c-line-clamp1">{{ item.title }}</view>
                <view v-if="item.btnText" class="mb-order-btn">{{ item.btnText }}</view>
            </view>
        </view>
    </view>
</template>

<script>
/**
 * @author chenshuo04
 * @file 订单信息
 * @categories component
 * @tags component
 */
import {navigate} from '../../utils/navigate';
import {request} from '../../utils/request';
import * as conf from '../../utils/conf';
import {ubcSend} from '../../utils/log';

export default {
    config: {
        component: true
    },
    props: {
        /**
         * 订单list
         */
        orderlist: {
            type: Array,
            default: () => {
                return [];
            },
            required: true
        },
        /**
         * 圆角
         */
        borderRadius: {
            type: String,
            default: '22px'
        },
        // ubc日志必传字段
        page: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            currentIndex: 0,
            timer: 4000,
            showTrans: false
        };
    },
    watch: {
        currentIndex(newVal) {
            this.carouselLoop(newVal);
        }
    },
    created() {
        this.carouselLoop(0);
    },
    methods: {
        onJump(data) {
            if (data.clickUrl) {
                ubcSend({
                    page: this.page,
                    type: 'clk',
                    value: 'order-bubble-click',
                    ext: {
                        pd: 'brand'
                    }
                });
                request({
                    url: conf.goApiHost + data.clickUrl,
                    isNeedLogin: true,
                    isFirstScreen: false
                }).then(res => { });
            }
            let urlType = 0;
            if (data.h5Url) {
                urlType = 2;
            }
            navigate({url: data.h5Url || data.msUrl, urlType, extra: {action: 'order-bubble-jump'}});
        },
        carouselLoop(newVal, isFirst) {
            if (newVal === this.orderlist.length - 1) {
                setTimeout(() => {
                    this.currentIndex = 0;
                    this.showTrans = false;
                }, this.timer / 2);
            }
            else {
                setTimeout(() => {
                    this.currentIndex += 1;
                    this.showTrans = true;
                }, this.timer);
            }
        }
    }
};
</script>
<style lang="less" scoped>
.mb-order {
    background: #FFFFFF;
    box-shadow: 0 2px 10px 0 rgba(0,0,0,0.05);
    border: 1px solid #F1F1F1;
    height: 42px;
    overflow: hidden;
    animation: show-in 0.3s;
    &-i {
        position: relative;
        display: flex;
        align-items: center;
        height: 42px;
        padding: 0 8px;
    }
    &-i-after {
        &::after {
            content: ' ';
            position: absolute;
            right: 15px;
            width: 0;
            height: 0;
            border-top: 5px solid transparent;
            border-bottom: 5px solid transparent;
            border-left: 6px solid #bbb;
        }
    }
    &-img {
        width: 30px;
        min-width: 30px;
        height: 30px;
        border-radius: 30px;
    }
    &-text {
        width: calc(100% - 60px);
    }
    &-btn {
        width: 76px;
        min-width: 76px;
        height: 27px;
        line-height: 27px;
        border-radius: 15px;
        font-size: 13px;
        background: #eFAA32;
        color: #fff;
        text-align: center;
    }
}
@keyframes show-in {
    0% {
        opacity: 0;
        height: 0;
    }
    100% {
        opacity: 1;
        height: 44px;
    }
}
</style>
