<template>
    <view class="container activity">
        <view class="activity-banner"><image class="activity-banner-image" :src="activityBanner"></image></view>
        <!-- 商品列表 -->
        <view class="container-section 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 tags" v-if="pro.activeStatus === 1">
                        <!-- 拼团价  活动价  限时特价  券后价   -->
                        <view class="tag pt" v-if="pro.collageStatus > 0">拼团价</view>
                        <view class="tag hd" v-else-if="pro.activeStatus > 0">活动价</view>
                        <view class="tag other" v-else-if="pro.discountStatus > 0">限时特价</view>
                        <view class="tag other" v-else-if="pro.couponStatus > 1">{{ pro.couponInfo }}</view>
                        <view class="tag other" v-else-if="pro.couponStatus > 0">券后价</view>
                    </view>
                    <view class="product-list-pri">
                        <view class="price">
                            <text>￥{{ pro.price | priceFormat }}</text>
                            <text class="delete">￥{{ pro.price | priceFormat }}</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>
        <cm-loadmore
            :hasMore="hasNextPage"
            :isLoading="isLoading"
            :visiable="visiable"
            backgroundColor="#ff457b"
        ></cm-loadmore>
        <!-- 可拖动悬浮按钮 -->
        <cm-drag :cartNum="kindCount" :isDock="true" :existTabBar="true" @btnClick="btnClick"></cm-drag>

        <!-- 分享弹窗 -->
        <cm-share-popup ref="sharePopup" :data="posterData" type="activity"></cm-share-popup>

        <!-- 返回顶部 -->
        <tui-scroll-top
            :scrollTop="scrollTop"
            :isShare="true"
            :bottom="60"
            :customShare="true"
            @share="onShare"
        ></tui-scroll-top>
    </view>
</template>

<script>
import { fetchProductList } from '@/services/api/goods.js'
import { debounce, queryStringify } from '@/common/utils.js'
import { makeQuery } from '@/pages/views/goods/config/config.js'
import { mapGetters, mapActions } from 'vuex'

export default {
    data() {
        return {
            isRefresh: true,
            isLoading: false, // 正在加载更多
            activityBanner: '',
            listQuery: makeQuery(),
            activityName: '',
            productList: [], //商品列表
            total: 0,
            hasNextPage: false,
            posterData: {},
            scrollTop: 0,
            type: 'other'
        }
    },
    onLoad(option) {
        this.listQuery.activityId = option.activityId
        this.listQuery.userId = this.userId
        if (option.type === 'share') {
            this.type = option.type
            this.listQuery.userId = option.dealerUserId
        }
        this.listQuery.listType = 6
        this.activityName = option.name
        this.fetchProductList()
        this.$store.dispatch('cart/fetchCartKindCount')
    },
    computed: {
        ...mapGetters(['userId', 'kindCount']),
        visiable() {
            return this.productList.length > this.listQuery.pageSize
        }
    },
    onPageScroll(e) {
        this.scrollTop = e.scrollTop
    },
    onShareAppMessage() {
        // 加密
        const state_str = encodeURIComponent(
            this.$crypto.encrypt({
                type: 3,
                activityId: this.listQuery.activityId,
                dealerUserId: this.userId
            })
        )
        return {
            title: this.activityName,
            path: `/pages/index/index?state_str=${state_str}`,
            imageUrl: this.activityBanner
        }
    },
    methods: {
        ...mapActions('cart', ['addToCart']),
        // 分享事件
        onShare() {
            this.posterData = {
                query: queryStringify({
                    type: 3,
                    activityId: this.listQuery.activityId,
                    dealerUserId: this.userId
                }),
                productImage: this.activityBanner,
                porductName: this.activityName
            }
            this.$refs.sharePopup.open()
        },
        // 获取商品列表
        fetchProductList: debounce(async function() {
            try {
                if (this.listQuery.sortType === 1) {
                    this.listQuery.productIds = this.productList.map(item => item.productId).join(',')
                } else {
                    this.listQuery.productIds = ''
                }
                const { data } = await fetchProductList(this.listQuery)
                // 活动banner
                this.activityBanner = data.activityBanner
                // 产品相关
                this.productList = [...this.productList, ...data.pageInfo.results]
                this.hasNextPage = data.pageInfo.hasNextPage
                this.total = data.pageInfo.totalRecord
                // 页面状态
                this.listQuery.pageNum++
                this.isRequest = false
                this.isLoading = false
            } catch (e) {
                console.log('获取商品列表失败')
            }
        }, 200),

        // 查单详情
        detail(pro) {
            this.$router.navigateTo(`goods/goods-detail?productId=${pro.productId}&jumpState=2`)
        },

        // 加入购物车
        handAddCarts(pro) {
            this.addToCart({ skuId: pro.skuId, heUserId: this.userId })
        },

        // 跳转购物车
        btnClick() {
            console.log('购物车')
            this.$router.navigateTo('cart/cart')
        }
    }
}
</script>

<style lang="scss" scoped>
page {
    height: auto !important;
    background-color: #f83c6c;
}
.activity-banner {
    width: 750rpx;
    height: 600rpx;
    float: left;
    margin-bottom: 24rpx;
    .activity-banner-image {
        width: 100%;
        height: 100%;
        display: block;
    }
}

.share-button {
    @extend .cm-flex-center;
    position: fixed;
    right: 30rpx;
    bottom: 120rpx;
    flex-direction: column;
    z-index: 98;
    width: 96rpx;
    height: 96rpx;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.2);
    .label {
        font-size: 20rpx;
        color: #fff;
    }
    .iconfont {
        color: #fff;
    }
}

.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: 26rpx;
                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: 26rpx;
                    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>
