<template>
    <div class="discover">
        <van-sticky ref="top" :offset-top="bar && bar.show ? bar.height : 0">
            <div class="top">
                <div class="top-btn">
                    <div class="btn active">收藏探索</div>
                    <div class="btn" @click="$router.replace('/' + $route.params.companyId + '/creator')">铸造者</div>
                </div>
                <div class="search" @click="$router.push('/' + $route.params.companyId + '/productSearch')">
                    <img src="@assets/svgs/search.svg" alt="" />
                </div>
            </div>
        </van-sticky>

        <swiper pagination class="mySwiper" :autoplay="{ delay: 3500 }" v-if="banners.length > 0">
            <swiper-slide v-for="item in banners" :key="item.id">
                <img :src="item.pic" @click="goNext(item)" />
            </swiper-slide>
        </swiper>

        <van-grid :border="false">
            <van-grid-item text="精选推荐" :to="{ path: '/' + $route.params.companyId + '/productList' }">
                <template v-slot:icon>
                    <img class="grid-img" src="@assets/info_icon_jingxuanxilie.png" />
                </template>
            </van-grid-item>
            <van-grid-item
                text="原创系列"
                :to="{
                    path: '/' + $route.params.companyId + '/productList',
                    query: {
                        type: 'DEFAULT'
                    }
                }"
            >
                <template v-slot:icon>
                    <img class="grid-img" src="@assets/info_icon_yuanchangxilie.png" />
                </template>
            </van-grid-item>
            <van-grid-item
                text="数字盲盒"
                :to="{
                    path: '/' + $route.params.companyId + '/productList',
                    query: {
                        type: 'BLIND_BOX'
                    }
                }"
            >
                <template v-slot:icon>
                    <img class="grid-img" src="@assets/info_icon_manghexilie.png" />
                </template>
            </van-grid-item>
            <van-grid-item text="拍卖系列" @click="wait">
                <template v-slot:icon>
                    <img class="grid-img" src="@assets/info_icon_paimaixilie.png" />
                </template>
            </van-grid-item>
        </van-grid>

        <div class="title">本期推荐</div>

        <van-list class="box-list" v-model:loading="loading" :finished="finished" finished-text="" @load="getList">
            <template v-for="(item, index) in list" :key="item.id">
                <product-info v-model:info="list[index]"></product-info>
            </template>
        </van-list>

        <div class="tabbar-placeholder"></div>
    </div>
</template>

<script>
import { Swiper, SwiperSlide } from 'swiper/vue';

import 'swiper/swiper.min.css';
import 'swiper/swiper-bundle.min.css';

import SwiperCore, { Pagination, Autoplay } from 'swiper';

// install Swiper modules
SwiperCore.use([Pagination, Autoplay]);

import ProductInfo from '../components/product/productInfo.vue';
import banner from '../mixins/banner';

export default {
    name: 'discover',
    inject: ['bar'],
    mixins: [banner],
    components: {
        Swiper,
        SwiperSlide,
        ProductInfo
    },
    data() {
        return {
            stiky: null,
            banners: [],
            list: [],
            loading: false,
            finished: false,
            page: 0,
            empty: false
        };
    },
    mounted() {
        this.getInit();
    },
    methods: {
        getInit() {
            this.$toast.loading({
                message: '加载中...',
                forbidClick: true
            });
            this.getBanner();
        },
        getBanner() {
            this.$http
                .post(
                    '/banner/all',
                    {
                        query: {
                            type: 'DISCOVER'
                        },
                        sort: 'sort,asc;createdAt,desc'
                    },
                    { body: 'json' }
                )
                .then(res => {
                    this.banners = res.content;
                    this.$toast.clear();
                });
        },
        getList() {
            if (this.page === 0) {
                this.list = [];
            }
            this.loading = true;
            this.finished = false;
            this.empty = false;
            this.$http
                .post(
                    '/collection/all',
                    {
                        page: this.page,
                        size: 20,
                        query: {
                            onShelf: true,
                            del: false
                        },
                        sort: 'createdAt,desc'
                    },
                    { body: 'json' }
                )
                .then(res => {
                    this.list = [...this.list, ...res.content];
                    this.empty = res.empty;
                    this.loading = false;
                    this.finished = res.last;
                    if (!this.finished) {
                        this.page = this.page + 1;
                    }
                });
        }
    }
};
</script>

<style lang="less" scoped>
.top {
    display: flex;
    padding: 10px 16px;
    background-color: @bg;
    .top-btn {
        flex-grow: 1;
        .btn {
            font-size: 16px;
            // font-family: ZhenyanGB;
            // font-weight: 400;
            line-height: 26px;
            display: inline-block;
            vertical-align: text-bottom;

            &.active {
                color: @prim;
                font-size: 20px;
                font-weight: bold;
                line-height: 30px;
            }
        }

        .btn + .btn {
            margin-left: 30px;
        }
    }
}

.discover {
    background-color: @bg3;
}

::v-deep(.mySwiper) {
    width: calc(100vw - 32px);
    height: calc(50vw - 16px);
    padding-top: 12px;

    .swiper-pagination {
        bottom: 12px;
    }

    .swiper-pagination-bullet {
        width: 6px;
        height: 2px;
        border-radius: 1px;
        background: #d7d7d7;
    }

    .swiper-pagination-bullet-active {
        background: @prim;
    }
}

.swiper-slide {
    text-align: center;
    font-size: 18px;

    /* Center slide text vertically */
    display: -webkit-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    -webkit-justify-content: center;
    justify-content: center;
    -webkit-box-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
}

.swiper-slide img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 4px;
}
.grid-img {
    display: block;
    width: 38px;
    height: 38px;
}
.van-grid {
    margin-top: 12px;
}
/deep/ .van-grid-item__content {
    padding: 14px 0 16px;
}
/deep/ .van-grid-item__text {
    color: #fff;
    font-size: 13px;
    line-height: 18px;
    margin-top: 4px;
}

.title {
    padding: 16px 20px 8px;
    color: @prim;
    font-size: 18px;
    font-weight: bold;
}

.box-list {
    // display: flex;
    // flex-wrap: wrap;
    padding: 0 8px 100px;
}

/deep/.van-tab {
    color: #fff;
    flex: 0;
    padding: 20px;
    flex-shrink: 0;
    min-width: 74px;

    &.van-tab--active {
        color: @prim;
    }
}

/deep/ .van-tabs__line {
    bottom: 20px;
}
</style>
