<template>
    <view class="container activity">
        <tui-skeleton v-if="isRequest" loadingType="2"></tui-skeleton>
        <view class="activity-banner">
            <image class="activity-banner-image" :src="activityBanner" mode=""></image>
        </view>
        <!-- 商品列表 -->
        <view class="container-section tui-skeleton clearfix">
            <view class="product-list" v-for="(pro, index) in productList" :key="index" @click.stop="detail(pro)">
                <view class="product-list-image">
                    <image class="product-image" :src="pro.mainImage" mode=""></image>
                </view>
                <view class="product-list-msg">
                    <view class="product-msg-name">{{ pro.name }}</view>
                    <view class="product-list-tag" v-if="pro.activeStatus === 1">
                        <text class="tag tag-03" v-if="pro.collageStatus === 1">拼团价</text>
                        <!-- <text class="tag tag-01">促销</text> -->
                        <text class="tag tag-02" v-if="pro.collageStatus === 0">活动价</text>
                        <text class="tag tag-02" v-if="pro.couponsLogo">优惠券</text>
                    </view>
                    <view class="product-list-pri">
                        <view class="price">
                            <text>￥{{ pro.price | formatPrice }}</text>
                            <text class="delete">￥{{ pro.price | formatPrice }}</text>
                        </view>
                        <view class="carts" @click.stop="handAddCarts(pro)">
                            <view class="carts-add"> <text class="iconfont icon-gouwuche"></text> </view>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <template v-if="productList.length >= 6">
            <tui-loadmore :index="3" :visible="loadmore"></tui-loadmore>
            <tui-nomore :text="loadingText" :visible="!loadmore" backgroundColor="#ff457b"></tui-nomore>
        </template>
        <!-- 可拖动悬浮按钮 -->
        <cm-drag :cartNum="kindCount" :isDock="true" :existTabBar="true" @btnClick="btnClick"> </cm-drag>
    </view>
</template>

<script>
import wxLogin from '@/services/wxLogin.js'
import { mapGetters, mapActions, mapMutations } from 'vuex'
import cmDrag from '@/components/cm-module/cm-drag/cm-drag'

export default {
    components: {
        cmDrag
    },
    data() {
        return {
            StaticUrl: this.$Static,
            activityBanner: `${this.$Static}banner.png`,
            cartNum: 0,
            listQuery: {
                activityId: 0,
                userId: 0,
                pageNum: 1,
                pageSize: 6
            },
            activityName: '',
            hasNextPage: false,
            productList: [], //商品列表
            isRefresh: false,
            isRequest: true,
            loadmore: false // 正在加载更多
        }
    },
    onLoad(option) {
        // 如果是从分享链接进入的，就通过分享者id查询活动内容
        if (option.type === 'share') {
            this.listQuery.userId = option.userId
        } else {
            this.listQuery.userId = this.userId
        }
        this.listQuery.activityId = option.activityId
        this.activityName = option.name
        this.getProductActivityDetails()
    },
    computed: {
        ...mapGetters(['hasLogin', 'userId', 'kindCount']),
        loadingText() {
            return this.hasNextPage ? '上拉加载更多' : '没有更多了'
        }
    },
    methods: {
        ...mapActions('cart', ['addToCart']),
        // 获取活动信息
        getProductActivityDetails() {
            this.loadmore = true
            this.ProductService.GetProductActivityDetails(this.listQuery)
                .then(response => {
                    const data = response.data
                    this.activityBanner = data.image
                    this.hasNextPage = data.pageInfo.hasNextPage
                    if (this.isRefresh) {
                        this.productList = data.pageInfo.list
                    } else {
                        this.productList = [...this.productList, ...data.pageInfo.list]
                    }
                    this.isRequest = false
                    this.loadmore = false
                })
                .catch(error => {
                    this.$util.msg(error.msg, 2000)
                })
                .finally(() => {
                    if (this.isRefresh) {
                        setTimeout(() => {
                            uni.stopPullDownRefresh()
                        }, 500)
                        this.isRefresh = false
                    }
                })
        },
        detail(pro) {
            this.$api.navigateTo(`/pages/goods/product-detail?productId=${pro.productId}&jumpState=2`)
        },
        handAddCarts(pro) {
            this.addToCart({ productId: pro.productId, heUserId: this.userId })
        },
        btnClick() {
            this.$api.navigateTo('/pages/goods/cart')
        }
    },
    onPullDownRefresh() {
        //下拉刷新
        this.isRefresh = true
        this.listQuery.pageNum = 1
        this.getProductActivityDetails()
    },
    onReachBottom() {
        if (this.hasNextPage) {
            this.listQuery.pageNum++
            this.getProductActivityDetails()
        }
    },
    onShareAppMessage(res) {
        // 加密参数
        const shareData = {
            type: 0,
            inviteUserId: this.userId,
            activityId: this.listQuery.activityId,
            userId: this.userId,
        }
        // 加密
        const state_str = encodeURIComponent(this.$crypto.encrypt(shareData))
        return {
            title: `${this.activityName}`,
            path: `/pages/tabBar/index/index?state_str=${state_str}`,
            imageUrl: 'https://static.caimei365.com/app/mini-hehe/icon/icon-index-share.jpg'
        }
    }
}
</script>

<style lang="scss">
page {
    height: auto !important;
    background-color: $color-system;
}
.activity-banner {
    width: 100%;
    height: 600rpx;
    float: left;
    margin-bottom: 64rpx;
    .activity-banner-image {
        width: 100%;
        height: 100%;
        display: block;
    }
}
.container-section {
    width: 100%;
    height: auto;
    box-sizing: border-box;
    padding: 0 24rpx;
    .product-list {
        width: 339rpx;
        height: 532rpx;
        float: left;
        margin-right: 24rpx;
        margin-bottom: 24rpx;
        background-color: #ffffff;
        border-radius: 16rpx;
        &:nth-child(2n) {
            margin-right: 0;
        }
        .product-list-image {
            width: 100%;
            height: 339rpx;
            float: left;
            position: relative;
            .product-image {
                width: 100%;
                height: 100%;
                display: block;
                border-radius: 16rpx 16rpx 0 0;
            }
            .product-icon {
                width: 68rpx;
                height: 55rpx;
                display: block;
                position: absolute;
                top: 0;
                left: 34rpx;
            }
        }
        .product-list-msg {
            width: 100%;
            height: 193rpx;
            box-sizing: border-box;
            padding: 16rpx 24rpx;
            float: left;
            position: relative;
            .product-msg-name {
                width: 100%;
                height: 72rpx;
                line-height: 35rpx;
                text-overflow: ellipsis;
                overflow: hidden;
                display: -webkit-box;
                -webkit-line-clamp: 2;
                line-clamp: 2;
                -webkit-box-orient: vertical;
                font-size: $font-size-26;
                color: #333333;
                text-align: justify;
                float: left;
            }
            .product-list-tag {
                position: relative;
                z-index: 9;
                width: 100%;
                height: 30rpx;
                margin-top: 8rpx;
                float: left;
                .tag {
                    display: inline-block;
                    height: 32rpx;
                    font-size: 22rpx;
                    line-height: 30rpx;
                    text-align: center;
                    color: #f83c6c;
                    float: left;
                    margin-right: 10rpx;
                    &.tag-02 {
                        width: 80rpx;
                        background: url(https://static.caimei365.com/app/mini-hehe/icon/icon-active.png) top center
                            no-repeat;
                        background-size: contain;
                    }
                    &.tag-01 {
                        width: 56rpx;
                        color: #fff;
                        background-color: #f83c6c;
                        border-radius: 4rpx;
                    }
                    &.tag-03 {
                        width: 80rpx;
                        background: linear-gradient(270deg, #ff457b 0%, #b03bb8 51%, #6431f2 100%);
                        color: #fff;
                        border-radius: 4rpx;
                    }
                }
            }
            .product-list-pri {
                width: 100%;
                height: 44rpx;
                position: absolute;
                bottom: 16rpx;
                left: 0;
                box-sizing: border-box;
                padding: 0 24rpx;
                .price {
                    font-size: $font-size-26;
                    color: #f83c6c;
                    font-weight: bold;
                    line-height: 44rpx;
                    
                    .delete{
                        font-size: 20rpx;
                        color: #999999;
                        text-decoration: line-through;
                        font-weight: normal;
                        margin-left: 10rpx;
                    }
                }
                .carts {
                    position: absolute;
                    right: 24rpx;
                    bottom: 0;
                    .carts-add {
                        width: 44rpx;
                        height: 44rpx;
                        text-align: center;
                        line-height: 44rpx;
                        background-color: #ff457b;
                        border-radius: 50%;
                        .iconfont {
                            font-size: 32rpx;
                            color: #ffffff;
                        }
                    }
                }
            }
        }
    }
}
</style>
