<template>
    <div class="home">
        <swiper
            pagination
            class="mySwiper"
            :slidesPerView="'auto'"
            :spaceBetween="20"
            :autoplay="{ delay: 3500 }"
            v-if="banners.length > 0"
        >
            <swiper-slide v-for="item in banners" :key="item.id">
                <product-small :info="item"></product-small>
            </swiper-slide>
        </swiper>

        <div class="list">
            <product-large
                v-for="(item, index) in products"
                :key="index"
                v-model:info="products[index]"
            ></product-large>
        </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 banner from '../mixins/banner';
import ProductLarge from '../components/product/productLarge.vue';
import ProductSmall from '../components/product/productSmall.vue';

export default {
    name: 'home',
    inject: ['bar'],
    mixins: [banner],
    components: {
        Swiper,
        SwiperSlide,
        ProductLarge,
        ProductSmall
    },
    data() {
        return {
            banners: [],
            products: []
        };
    },
    mounted() {
        this.getInit();
    },
    methods: {
        getInit() {
            this.$toast.loading({
                message: '加载中...',
                forbidClick: true
            });
            this.getBanner();
            this.getProduct('LIST').then(res => {
                this.products = res;
            });
        },
        getProduct(type = 'BANNER') {
            return this.$http
                .get('/collection/recommend', {
                    type: type
                })
                .then(res => {
                    return Promise.resolve(res);
                });
        },
        getBanner() {
            this.$http
                .post(
                    '/banner/all',
                    {
                        query: {
                            type: 'HOME'
                        },
                        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: 0,
                        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;
                    }
                });
        },
        getMiner() {
            this.$http
                .post(
                    '/user/all',
                    {
                        page: 0,
                        query: { hasRole: 'ROLE_MINTER' },
                        size: 5,
                        sort: 'sales,desc'
                    },
                    { body: 'json' }
                )
                .then(res => {
                    this.miners = res.content;
                    // console.log(this.miners);
                });
        }
    }
};
</script>

<style lang="less" scoped>
.top {
    display: flex;
    padding: 9px 16px;
    background-color: @bg;
    display: flex;
    align-items: center;
    .logo {
        width: 74px;
        height: 26px;
    }

    .search {
        flex-grow: 1;
        background-color: @bg3;
        display: flex;
        align-items: center;
        margin-left: 10px;
        padding: 4px 20px;
        border-radius: 21px;
        img {
            width: 16px;
            height: 16px;
            margin-right: 10px;
        }
        font-size: 14px;
        color: @text3;
        line-height: 24px;
    }
}

.home {
    background-color: @bg3;
}

::v-deep(.mySwiper) {
    // height: 255px;
    padding-top: 16px;
    padding-left: 16px;
    box-sizing: border-box;
    padding-bottom: 35px;

    .swiper-pagination {
        bottom: 13px;
    }

    .swiper-pagination-bullet {
        width: 5px;
        height: 5px;
        border-radius: 5px;
        background: @text3;
        margin: 0 4px;
    }

    .swiper-pagination-bullet-active {
        background: #fff;
    }
}
.swiper-slide {
    width: 255px;
}

.list {
    padding: 12px 0 100px;
}
</style>
