<template>
    <view class="content">
        <!-- 抽奖 -->
        <image class="luckBack" src="https://card.ysd-bs.com/storage/materials/2022/05/12/luck_back.png"></image>
        <view class="luck">
            <image class="luckTitle" src="https://card.ysd-bs.com/storage/materials/2022/09/23/drawTitle.png" mode="widthFix"></image>
            <view class="drawContent">
                <image class="drawContent-back" src="https://card.ysd-bs.com/storage/materials/2022/05/16/ly-plate-c.png" mode="widthFix"></image>
                <view class="drawContent-number" v-if="isUser">抽奖数<text>{{number >= 0 ? number : '0'}}</text>次</view>
                <image class="drawContent-award" :animation="animationData" bindtransitionend='animationend' :src="picture" mode="widthFix"></image>
                <image class="drawContent-arrow" @click="onExtracting" src="https://card.ysd-bs.com/storage/materials/2022/05/12/playbtn.png" mode="widthFix"></image>
                <image class="drawFooter drawFooter-right" src="https://card.ysd-bs.com/storage/materials/2022/05/12/dial_rigntImg.png" mode="widthFix"></image>
                <image class="drawFooter drawFooter-left" src="https://card.ysd-bs.com/storage/materials/2022/05/12/dial_leftImg.png" mode="widthFix"></image>
            </view>
            <view class="roll">
                <view class="roll-title">
                    - 参与抽奖用户 -
                </view>
                <swiper class="roll-list" interval="3000" autoplay circular vertical bindchange="swiperChange">
                    <swiper-item class="roll-item" catchtouchmove="stopTouchMove" v-for="(item, index) in logsArr" :key="index">
                        <view class="roll-name" v-if="item.user">
                            {{item.user.nickname}}
                        </view>
                        <view class="roll-tel" v-if="item.user">
                            {{item.user.username}}
                        </view>
                        <view class="roll-time">
                            {{item.created_at}}
                        </view>
                    </swiper-item>
                </swiper>
            </view>
            <navigator v-if="isUser" hover-class="none" :url="'logs?id=' + exchageId" class="dial-record">抽奖记录</navigator>
            <view class="dial-btns">
                <view @click="userNav('order?type=paid')" class="dial-btns-go">
                    我的订单
                </view>
                <view @click="userNav('myCoupon')" class="dial-btns-go dial-btns-order">
                    我的优惠券
                </view>
            </view>
            <view class="setupLabel" @click="delStorage" v-if="isUser">
                <view class="setupLabel-name">
                    切换用户
                </view>
            </view>
        </view>
        
        <!-- 弹出 -->
        <view class="popBack" v-if="pointShow"></view>
        <view class="popCont" v-if="pointShow">
            <view class="popTips">
                <image class="luckTips" src="https://card.ysd-bs.com/storage/materials/2022/05/12/luckTips.png" mode="widthFix"></image>
                <view class="Tips-title">
                    恭喜中奖
                </view>
                <view class="Tips-text">
                    获得【{{prize}}】
                </view>
                <view class="Tips-btn">
                    <image class="Tips-btn-image" src="https://card.ysd-bs.com/storage/materials/2022/05/12/luckBtn.png"></image>
                    <view v-if="value == 2" class="Tips-btn-text" @click="exchange(luckId)">去兑换</view>
                    
                    <view v-else class="Tips-btn-text" @click="useGp(luckId)">去使用</view>
                </view>
                <view class="Tips-out">~温馨提示：请您尽快使用哦~</view>
            </view>
            <view class="luckClose">
                <image src="/static/img/luckClose.png" mode="widthFix" @click="luckClosePop"></image>
            </view>
        </view>
    </view>
</template>

<script>
    import { index, result } from '@/apis/interfaces/draw'
    export default {
        data() {
            return {
                isUser          : false,  // 用户是否登录
                number          : '',   //抽奖次数
                logsArr         : '',   //获奖列表
                luckId          : '',   //抽奖列表id
                pointShow       : false, //抽奖弹出
                prize           : '',   //获得奖项标题
                value           : '',   //获得奖项类型
                animationData   : {},   //旋转动画
                picture         : '',    //抽奖转盘图片
                isState         : false, //抽奖状态
                exchageId       : '',    //抽奖id
                backGround      : '',
                itemArr         : []
            }
        },
        
        // 生命周期函数--监听页面加载
        onLoad(options) {
            // 初始化动画信息
            const animation = uni.createAnimation({
                duration        : 2000,
                timingFunction  : 'ease-in-out'
            })
            this.animation = animation
        },
    
        // 生命周期函数--监听页面显示
        onShow() {
            // 存储环境-用户抽奖活动
            getApp().globalData.envType = 'drawEnv'
            if(uni.getStorageSync("token")) {
                this.isUser = true
                // 获取抽奖默认信息（抽奖次数）
            }
            this.drawShow();
        },
        methods: {
            // 抽奖默认信息（抽奖次数）
            drawShow(){
                index().then(res=>{
                    this.countLengtht= res.lottery.items_count
                    this.number      = res.lottery.number
                    this.background  = res.lottery.background
                    this.picture     = res.lottery.picture
                    this.exchageId   = res.lottery.exchage_id
                    this.itemArr     = res.lottery.items
                    this.logsArr     = res.logs
                })
            },
            
            // 抽奖
            onExtracting(){
                if(uni.getStorageSync("token")) {
                    result(this.exchageId).then(res=>{
                        let runNum      = 3  // 旋转圈数
                        // 检查抽奖状态
                        if(this.isState) return
                        if(this.number <= 0){
                            uni.showToast({
                                title: '今日抽奖次数已用完，请明天再来吧~',
                                icon : 'none'
                            })
                            return
                        }
                        
                        // 更新抽奖状态
                        this.isState = true
                        this.prize   = res.log.exchange_item.title
                        this.value   = res.log.exchange_item.type.value
                        this.luckId  = res.log.exchage_log_id
                        
                        //旋转角度
                        this.runDeg = this.runDeg || 0;
                        this.runDeg = this.runDeg + (360 - this.runDeg % 360) + (360 * runNum - (res.log.exchange_item.order - 1) * Math.round(360 / this.countLengtht))
                        
                        // 动画
                        this.animation.rotate(this.runDeg).step()
                        this.animationData = this.animation.export()
                    
                        // 提示用户中奖信息
                        setTimeout(()=>{
                            this.isState = false
                            this.pointShow = true
                        },3000)
                    }).catch(err => {
                        uni.showToast({
                            title: err.message,
                            icon: 'none'
                        })
                    })
                    return
                }
                // 去登录
                uni.navigateTo({
                    url: '/pages/draw/signin'
                })
            },
        
            // 动画结束弹出
            animationend(){
                setTimeout(()=>{
                    this.pointShow = true
                },1000)
            },
        
            // 关闭弹出
            luckClosePop() {
                uni.showModal({
                    title   : '提示',
                    content : '是否放弃此权益',
                    success : res=> {
                        if (res.confirm) {
                            this.pointShow = false
                        }
                        
                        // 获取抽奖默认信息（抽奖次数）
                        this.drawShow();
                    }
                })
            },
            
            // 兑换乐心三件套
            exchange(id) {
                uni.navigateTo({
                    url: "choose?id=" + id
                })
                this.pointShow = false
            },
            
            // 使用权益券
            useGp(id){
                uni.navigateTo({
                    url: "right?luck_id=" + id
                })
                this.pointShow = false
            },
            
            // 处理未登录时的转跳
            userNav(url){
                let pageUrl = url
                if(uni.getStorageSync("token")) {
                    uni.navigateTo({
                        url: pageUrl
                    })
                    return
                }
                // 去登录
                uni.navigateTo({
                    url: '/pages/draw/signin'
                })
            },
            
            // 退出登录
            delStorage() {
                uni.clearStorage()
                uni.navigateTo({
                    url:'/pages/draw/signin'
                })
            },
            
            // 禁止滚动
            stopTouchMove() {
                return false
            }
        }
    }
</script>

<style lang="scss" scoped>
    page {
        background-color: #e4353a;
    }
    
    .luckBack {
        position: fixed;
        width: 100vw;
        height: 100%;
        left: 0;
        top: 0;
    }
    
    .luck {
        position: fixed;
        width: 100%;
        overflow: hidden;
        height: 100%;
        z-index: 9;
        left: 0;
        top: 0;
        padding: 80rpx 40rpx 20rpx;
        box-sizing: border-box;
        overflow-y: scroll;
        text-align: center;
    }
    
    .luckTitle {
        width: 90%;
    }
    
    .drawFooter{
        position: absolute;
        z-index: 9;
    }
    
    .drawFooter-right {
        width: 48%;
        bottom: -70rpx;
        right: -16%;
    }
    
    .drawFooter-left {
        width: 60%;
        bottom: -30rpx;
        left: -18%;
    }
    
    /* 转盘 */
    .drawContent{
        margin: 60rpx auto 40rpx;
        width: 100%;
        padding-top: 100%;
        position: relative;
    }
    
    .drawContent-arrow,
    .drawContent-back,
    .drawContent-award{
        position: absolute;
    }
    
    .drawContent-back{
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }
    
    .drawContent-award{
        width: 76%;
        height: 76%;
        top: 5.5%;
        left: 13.5%;
    }
    
    .drawContent-arrow{
        width: 26%;
        height: 26%;
        top: 28%;
        left: 39%;
    }
    
    .drawContent-number {
        bottom: 38rpx;
        color: #ffffff;
        left: 0;
        width: 100%;
        position: absolute;
        text-align: center;
        padding-left: 20rpx;
        box-sizing: border-box;
        font-size: 28rpx;
    }
    
    .drawContent-number text {
        font-weight: 600;
        padding: 0 5rpx;
    }
    
    .dial-btns {
        padding-bottom: 40rpx;
        display: flex;
        .dial-btns-go {
            flex: 2;
            text-align: center;
            box-sizing: border-box;
            background-color: #ffeace;
            border: 2rpx solid transparent;
            color: #5a0000;
            margin-top: 40rpx;
            border-radius: 50rpx;
            height: 80rpx;
            line-height: 80rpx;
            margin: 50rpx 15rpx 0;
        }
        .dial-btns-order {
            background-color: #f3524a;
            color: #ffeace;
            border-color: #ffeace;
        }
    }
    
    /* 弹出 */
    .popBack {
        background-color: rgba(0, 0, 0, .7);
        position: fixed;
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;
        z-index: 99;
    }
    
    .popCont {
        display: -webkit-box;
    	-webkit-box-orient: vertical;
    	-webkit-box-pack: center;
    	left: 0;
    	right: 0;
    	top: 0;
    	bottom: 0;
        position: fixed;
        width: 100%;
        height: 100%;
        z-index: 100;
        padding: 140rpx 100rpx 0;
        box-sizing: border-box;
    }
    
    .popTips{
        color: #ffffff;
        width: 100%;
        padding: 140rpx 30rpx 30rpx;
        box-sizing: border-box;
        background-color: #de3935;
        border-radius: 20rpx;
        text-align: center;
        color: #ffe3ba;
        position: relative;
    }
    
    .luckTips {
        position: absolute;
        top: -120rpx;
        z-index: 101;
        width: 60%;
        left: 25%;
    }
    
    .Tips-title {
        font-size: 60rpx;
        margin-bottom: 60rpx;
    }
    
    .Tips-text {
        font-size: 34rpx;
    }
    
    .Tips-btn {
        margin: 100rpx 0 20rpx;
        position: relative;
        padding-top: 14%;
    }
    
    .Tips-btn-image {
        position: absolute;
        width: 70%;
        height: 100%;
        left: 15%;
        top: 0;
    }
    
    .Tips-btn-text {
        font-size: 30rpx;
        text-align: center;
        position: absolute;
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;
        color: #c35b34;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        font-weight: 600;
    	-webkit-box-pack: center;
    }
    
    .Tips-out {
        font-size: 28rpx;
        line-height: 90rpx;
    }
    
    .luckClose {
        width: 100%;
        text-align: center;
        margin-top: 40rpx;
    }
    
    .luckClose image {
        width: 64rpx;
        opacity: .5;
    }
    
    /* 抽奖规则 */
    .rulesBack {
        background-color: rgba(0, 0, 0, .7);
        position: fixed;
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;
        z-index: 99;
        display: none;
    }
    
    .rulesCont {
    	-webkit-box-orient: vertical;
    	-webkit-box-pack: center;
    	left: 0;
    	right: 0;
    	top: 25vh;
    	bottom: 0;
        position: fixed;
        width: 100%;
        height: 50vh;
        z-index: 100;
        display: none;
    }
    .rulesBack.active {
        display: block;
    }
    
    .rulesCont.active {
        display: -webkit-box;
    }
    
    .luckRules {
        position: absolute;
        left: 10%;
        top: 0;
        width: 80%;
        box-sizing: border-box;
        z-index: 100;
    }
    
    .textRules {
        position: absolute;
        left: 10%;
        top: 0;
        width: 80%;
        z-index: 101;
        height: 100%;
        padding: 60rpx 80rpx;
        color: #ffffff;
        line-height: 58rpx;
        box-sizing: border-box;
    }
    
    .rulesClose-title {
        width: 60%;
        display: block;
        margin: 20rpx auto 40rpx;
    }
    
    .textRules-text {
        height: 70%;
        overflow: hidden;
        overflow-y: scroll;
    }
    
    .rulesClose {
        position: absolute;
        z-index: 102;
        right: 50rpx;
        top: 60rpx;
        width: 54rpx;
        opacity: .8;
    }
    
    .roll {
        height: 160rpx;
        overflow: hidden;
        background-color: #f5675a;
        border: 4rpx solid #ffeace;
        border-radius: 20rpx;
        padding: 20rpx;
        box-sizing: border-box;
        margin: 60rpx 15rpx 0;
        width: calc(100% - 30rpx);
        .roll-title {
            color: #ffeace;
            font-size: 34rpx;
        }
        .roll-list {
            height: 90rpx;
        }
        .roll-item {
            height: 90rpx;
            display: flex;
            line-height: 90rpx;
            color: #fff0dd;
            font-size: 28rpx;
            view {
                flex: 3;
            }
            .roll-name {
                text-align: left;
            }
            .roll-time {
                text-align: right;
            }
        }
    }
    
    // 中间记录
    .dial-record {
        position: absolute;
        top: 20%;
        right: 0;
        background-color: rgba($color: #ffdba2, $alpha: .9);
        border-radius: 30rpx 0 0 30rpx;
        line-height: 58rpx;
        font-size: 30rpx;
        color: #b80501;
        padding: 0 15rpx 0 20rpx;
    }
    
    .setupLabel {
        width: 100%;
        text-align: center;
        line-height: 90rpx;
        color: #FFFFFF;
        opacity: .8;
    }
</style>
