<template>
    <view class="share-buy-detail">
        <tui-skeleton v-if="isRequest" :loadingType="3" :isLoading="true"></tui-skeleton>
        <!-- 拼单信息 && 立即拼单 -->
        <view class="group-message">
            <share-buy-group-message :countDownTime="countDownTime" :hasEndTip="true">
                <template v-slot:head>
                    <view class="message">
                        还差
                        <text v-text="collageData.needNum"></text>
                        人参团，赶快邀请好友来拼单吧！
                    </view>
                </template>
                <template v-slot:foot>
                    <tui-button type="base" width="600rpx" height="90rpx" shape="circle" :size="30" @click="onShare">
                        <text>邀请好友拼单</text>
                    </tui-button>
                    <view class="line"></view>
                    <tui-button
                        type="base"
                        width="600rpx"
                        height="90rpx"
                        shape="circle"
                        :plain="true"
                        :size="30"
                        @click="handleToHome"
                    >
                        <text>去首页逛逛</text>
                    </tui-button>
                </template>
            </share-buy-group-message>
        </view>
        <!-- 商品基本信息 参与拼单用户信息-->
        <view class="member-info">
            <share-buy-member-info
                :memberNum="collageData.memberNum"
                :existNum="collageData.existNum"
                :shopInfo="collageData"
            ></share-buy-member-info>
        </view>

        <!-- 分享 -->
        <cm-share-popup ref="sharePopup" :data="posterData" type="product"></cm-share-popup>
    </view>
</template>

<script>
import { fetchCollageOrderDetail } from '@/services/api/order.js'
import { countDown } from '@/common/utils.js'
import { mapGetters } from 'vuex'
export default {
    data() {
        return {
            isRequest: true,
            collageId: '',
            collageData: {},
            // 海报生成数据
            posterData: {
                porductName: '',
                productPrice: '',
                productOriginPrice: '',
                productImage: '',
                qrCodeImage: '' // 商品详情二维码
            },
            timer: null,
            countDownTime: {}
        }
    },
    computed: {
        ...mapGetters(['userId'])
    },
    // 分享到微信用户
    onShareAppMessage() {
        const shareData = {
            type: 4,
            collageId: this.collageData.collageId,
            initiatorId: this.userId
        }

        const state_str = encodeURIComponent(this.$crypto.encrypt(shareData))
        console.log(state_str)
        return {
            title: '哈喽，赶快来参团吧！这件商品拼团买更便宜~',
            path: `/pages/index/index?state_str=${state_str}`,
            imageUrl: this.collageData.productImage
        }
    },
    onLoad(options) {
        this.collageId = options.collageId
    },
    onShow() {
        this.fetchOrderDetails()
    },
    methods: {
        // 获取拼单详情
        async fetchOrderDetails() {
            try {
                const res = await fetchCollageOrderDetail({
                    collageId: this.collageId,
                    userId: this.userId
                })
                this.collageData = res.data
                const { memberNum, existNum, productName, productImage, price } = this.collageData
                // 验证拼单是否成功
                if (memberNum === existNum) {
                    uni.setStorageSync('SHARE_BUY_COLLAGE_DATA', this.collageData)
                    setTimeout(() => {
                        this.$router.redirectTo('share-buy/share-buy-success')
                    }, 500)
                    return
                }
                // 设置分享海报商品信息
                this.posterData.porductName = productName
                this.posterData.productImage = productImage
                this.posterData.productPrice = price
                this.countDown()
                this.isRequest = false
            } catch (e) {
                console.log(e)
            }
        },
        // 去首页
        handleToHome() {
            this.$router.switchTab('home')
        },
        // 分享
        async onShare() {
            this.$refs.sharePopup.open()
        },
        // 倒计时
        countDown() {
            const startTime = Date.now()
            const endTime = new Date(this.collageData.endTime).getTime()
            this.timer && clearTimeout(this.timer)
            countDown(endTime, startTime, {}, item => {
                this.countDownTime = item.conttainer
                this.timer = item.t
            })
        }
    }
}
</script>

<style lang="scss" scoped>
.share-buy-detail {
    .line {
        height: 24rpx;
    }

    .member-info {
        margin-top: 24rpx;
    }

    .message {
        font-size: 30rpx;
        color: #333333;
        text {
            color: #ff457b;
        }
    }
}
</style>
