<template>
    <div class="detail">
        <div class="detail-top">
            <img src="../../assets/png-bg-shangping.png" class="bg-img" />

            <div class="detail-info">
                <swiper pagination class="mySwiper" v-if="banners.length > 0">
                    <swiper-slide v-for="(item, index) in banners" :key="index">
                        <!-- <img :src="item" /> -->

                        <video
                            class="swiper-video"
                            v-if="isVideo(item)"
                            :src="item.url"
                            :poster="getImg(changeImgs([item]), '', 1200)"
                            controls="controls"
                        >
                            您的浏览器不支持 video 标签。
                        </video>
                        <van-image
                            v-else
                            @click="preview(index, changeImgs(banners))"
                            :src="getImg(item.url, '', 1200)"
                            width="calc(100vw - 134px)"
                            height="calc(100vw - 134px)"
                            fit="scale-down"
                        />
                    </swiper-slide>
                </swiper>

                <div class="share-content">
                    <like-button :isLike="info.liked" @click="likeProduct">
                        {{ info.likes }}
                    </like-button>

                    <div class="share-icon" @click="share">
                        <img src="../../assets/svgs/icon-fenxiang.svg" alt="" />
                        <span>分享</span>
                    </div>
                </div>
            </div>
        </div>

        <div class="info">
            <div class="info-title">
                <div class="detail-name">
                    <span>{{ info.name }}</span>
                    <img src="../../assets/svgs/png-biaotikuang-01.svg" alt="" />
                    <img src="../../assets/svgs/png-biaotikuang-02.svg" alt="" />
                    <img src="../../assets/svgs/png-biaotikuang-03.svg" alt="" />
                    <img src="../../assets/svgs/png-biaotikuang-04.svg" alt="" />
                </div>
            </div>
            <div class="name" v-if="info.salable && startTime">
                <div class="name1">首发抢购倒计时</div>
                <div class="name2">{{ startTime }}</div>
            </div>

            <div class="price-line" v-if="info.salable">
                <div class="price"><i class="font_family icon-icon_jiage"></i>{{ info.price }}</div>
                <div class="price-sub" v-if="info.originalPrice">¥{{ info.originalPrice || 0 }}</div>
                <div class="sub" v-if="info.royalties">
                    含 <span>{{ info.royalties }}%</span> 版税
                </div>
                <div class="flex1"></div>
                <div class="text" v-if="info.salable && info.total > 0">
                    <span>已售 {{ info.sale }}</span>
                    <span>剩余 {{ info.stock }}</span>
                </div>
            </div>
            <!-- <div class="title">{{ info.name }}</div> -->
            <div class="info-bottom">
                <span class="text1" v-if="info.number"> 编号 {{ info.number }} </span>
                <!-- <van-button
          v-if="info.type !== 'DEFAULT'"
          type="primary"
          plain
          size="mini"
          >选择其他编号</van-button
        > -->
                <div class="flex1"></div>
                <!-- <like-button :isLike="info.liked" @click="likeProduct">
                    {{ info.likes }}
                </like-button> -->
            </div>
        </div>

        <driver />
        <van-cell
            value="进入主页"
            is-link
            class="creator"
            :to="{
                path: '/creatorDetail',
                query: {
                    id: info.minterId
                }
            }"
        >
            <template #icon>
                <van-image width="40" height="40" :src="info.minterAvatar" fit="cover" radius="100" />
            </template>
            <template #title>
                <div class="text1">{{ info.minter }}</div>
                <div class="text2">铸造者</div>
            </template>
        </van-cell>

        <driver />
        <div class="top">
            <div class="top1">
                <div class="text1">类型</div>
                <div v-if="info.category" class="text2">{{ info.category }}</div>
                <div v-else class="text5">铸造者未设置</div>
            </div>
            <div class="top1">
                <div class="text1">悄悄话</div>
                <div class="text2 text3" v-if="init2">
                    <img class="img" :src="init2.icon[0]" alt="" />
                    <div class="text4" :class="{ prim: init2.opened }">
                        {{ init2.opened ? '已打开' : '未打开' }}
                    </div>
                </div>
                <div class="text5" v-else>铸造者未设置</div>
            </div>
        </div>
        <div class="titleIcon">持有特权<span>（灰色表示已使用）</span></div>
        <div class="prive">
            <div v-if="init">
                <div v-for="(item, index) in init" :key="index">
                    <div class="prive1">
                        <img class="img" :src="item.icon[0]" alt="" />
                        <div class="prive2">{{ item.name }}</div>
                    </div>
                </div>
            </div>
        </div>
        <div v-if="!init" class="conName">铸造者未设置</div>
        <div class="conName" v-else>铸造者未设置</div>
        <!-- </template> -->
        <div class="goods">
            <div class="goods-info">
                <div class="page-title">商品特性</div>
                <div class="specific-list" v-if="properties.length > 0">
                    <div class="specific-item" v-for="(item, index) in properties" :key="index">
                        <div class="text1">{{ item.name }}</div>
                        <div class="text2">{{ item.value }}</div>
                    </div>
                </div>
                <div v-else class="textName">铸造者未设置</div>
            </div>
            <div class="goods-info">
                <div class="page-title">链上信息</div>
                <div class="page-text" v-if="info.txHash || info.blockNumber || info.tokenId">
                    Hash地址：{{ info.txHash }}<br />
                    区块高度： {{ info.blockNumber }}<br />
                    令牌ID： {{ info.tokenId }}
                </div>
                <div v-else class="textName">铸造者未设置</div>
            </div>
            <div class="goods-info">
                <div class="page-title">盲盒详情</div>
                <swiper v-if="boxs.length > 0" :slidesPerView="'auto'" :spaceBetween="20" class="detail-swiper">
                    <swiper-slide v-for="(item, index) in boxs" :key="index">
                        <van-image
                            width="100%"
                            height="170"
                            :src="getImg(item, '', 1000)"
                            fit="cover"
                            radius="12"
                            @click="preview(index, boxs)"
                        />
                    </swiper-slide>
                </swiper>
                <div v-else class="textName">铸造者未设置</div>
            </div>

            <div class="goods-info">
                <div class="page-title">作品描述</div>
                <div v-if="info.detail" class="page-text page-detail" v-html="info.detail"></div>
                <div v-else class="textName">铸造者未设置</div>
            </div>
        </div>
        <div class="btn van-safe-area-bottom" v-if="info.isAppointment">
            <div class="btns">
                <van-button @click="appointment" type="primary" block round>
                    {{ info.appointment ? '已预约' : '一键预约' }}
                </van-button>
            </div>
        </div>

        <div class="btn van-safe-area-bottom" ref="btn" v-if="isBuy">
            <div class="btns">
                <van-button type="primary" block round @click="buy">立即购买</van-button>
            </div>
        </div>

        <div class="btn van-safe-area-bottom" v-if="!info.salable">
            <div class="btns status-text">仅展示</div>
        </div>

        <!-- <driver /> -->

        <!-- <van-collapse v-model="activeName" accordion>
      <van-collapse-item title="交易记录" name="1">
        <van-cell title="单元格" value="内容"> </van-cell>
      </van-collapse-item>
    </van-collapse> -->

        <driver />

        <post ref="post" :info="info" noButton />
    </div>
</template>

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

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

import SwiperCore, { Pagination } from 'swiper';
import Post from '../../components/Post.vue';
import { ImagePreview } from 'vant';

// install Swiper modules
SwiperCore.use([Pagination]);
import product from '../../mixins/product';

export default {
    components: {
        Swiper,
        SwiperSlide,
        Post
    },
    mixins: [product],
    data() {
        return {
            activeName: '1',
            info: {},
            liked: false,
            btn: null,
            blindBoxItems: [],
            init: [],
            init2: null
        };
    },
    computed: {
        banners() {
            return this.info.pic || [];
        },
        properties() {
            return this.info.properties || [];
        },
        isBuy() {
            return this.info.stock && this.info.onShelf && this.info.salable;
        },
        boxs() {
            let list = [...this.blindBoxItems];
            return list.map(item => {
                return this.changeImgs(item.pic);
            });
        }
    },
    mounted() {
        this.getProduct();
    },
    methods: {
        preview(index = 0, list = []) {
            ImagePreview({
                images: [...list].map(item => {
                    return item;
                }),
                startPosition: index
            });
        },
        appointment() {
            if (this.info.appointment) {
                return;
            }
            this.$http
                .post('/collection/appointment?id=' + this.info.id)
                .then(res => {
                    this.getProduct();
                    this.$toast.success('预约成功');
                })
                .catch(e => {
                    if (e.error) {
                        this.$toast.warning(e.error);
                    }
                });
        },
        getProduct() {
            this.$toast.loading({
                message: '加载中...',
                forbidClick: true
            });
            this.$http
                .get('/collection/get/' + this.$route.query.id)
                .then(res => {
                    this.info = res;
                    if (res.privileges) {
                        this.init2 = res.privileges.find(item => {
                            return item.name === '悄悄话';
                        });
                    }
                    if (res.privileges) {
                        this.init = res.privileges.filter(item => {
                            return item.name !== '悄悄话';
                        });
                    }
                    this.getTime(res.startTime);
                    this.$nextTick(() => {
                        if (this.isBuy) {
                            this.btn = this.$refs.btn;
                        }
                    });
                    this.$toast.clear();

                    if (res.type === 'BLIND_BOX') {
                        return this.$http.post(
                            '/blindBoxItem/all',
                            {
                                query: {
                                    blindBoxId: res.id
                                }
                            },
                            { body: 'json' }
                        );
                    } else {
                        return Promise.resolve();
                    }
                })
                .then(res => {
                    if (res) {
                        this.blindBoxItems = res.content;
                    }
                });
        },
        likeProduct() {
            if (!this.info.liked) {
                this.$http.get(`/collection/${this.info.id}/like`).then(() => {
                    this.getProduct();
                    this.$toast.success('收藏成功');
                });
            } else {
                this.$http.get(`/collection/${this.info.id}/unlike`).then(() => {
                    this.getProduct();
                    this.$toast.success('取消收藏');
                });
            }
        },
        buy() {
            this.checkLogin().then(() => {
                this.$router.push({
                    path: '/submit',
                    query: {
                        id: this.$route.query.id
                    }
                });
            });
        },
        share() {
            this.$refs.post.init();
        }
    }
};
</script>

<style lang="less" scoped>
.detail {
    padding-bottom: 100px;
}
.prive {
    display: flex;
    align-items: center;
    width: 100%;
    flex-wrap: wrap;
    .prive1 {
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 16px 15px 0;
        width: 94px;
        height: 32px;
        color: #000;
        background: linear-gradient(48deg, #fdfb60 0%, #ff8f3e 100%);
        border-radius: 4px;
        &.prim {
            background: #202122;
            color: @text3;
        }
        .img {
            text-align: center;
            width: 18px;
            height: 18px;
            margin-right: 4px;
        }
        .prive2 {
            text-align: center;
        }
    }
}
.info {
    // height: 164px;
    background-color: @bg;
    border-radius: 20px 20px 0 0;
    transform: translateY(-16px);
    position: relative;
    z-index: 2;
    padding: 16px 16px 0;
    box-sizing: border-box;
    .name {
        text-align: center;
        .name1 {
            font-size: 14px;
            font-weight: 400;
            color: #ffffff;
            line-height: 24px;
        }
        .name2 {
            font-size: @font2;
            font-weight: 400;
            color: #fdfb60;
            line-height: 24px;
            background: linear-gradient(135deg, #fdfb60 0%, #ff8f3e 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
    }
    .price {
        font-size: 36px;
        font-family: OSP;
        color: #fdfb60;
        line-height: 36px;
        transform: translateY(3px);

        .font_family {
            font-size: 10px;
            line-height: 24px;
            vertical-align: middle;
        }
    }

    .price-line {
        display: flex;
        align-items: flex-end;
        .sub {
            flex-grow: 1;
            margin-left: 5px;
            font-size: @font2;
            color: #949699;
            line-height: 16px;
            span {
                color: #fff;
            }
        }

        .price-sub {
            font-size: @font2;
            color: @text3;
            text-decoration: line-through;
            margin-left: 10px;
        }

        .text {
            font-size: @font2;
            color: @text3;
            line-height: 16px;
            span {
                margin-left: 10px;
            }
        }
    }

    .title {
        font-size: 20px;
        font-weight: bold;
        color: #ffffff;
        line-height: 28px;
        margin-top: 12px;
    }

    .info-bottom {
        display: flex;
        position: relative;
        margin-top: 4px;
        height: 24px;
        .text1 {
            font-size: @font2;
            color: #949699;
            line-height: 24px;
        }
        .van-button {
            font-size: @font2;
            color: #fdfb60;
            line-height: 24px;
            background: linear-gradient(135deg, #fdfb60 0%, #ff8f3e 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            border-width: 0px;
            margin-left: 10px;
        }
        .like {
            position: absolute;
            right: 0;
        }
    }
}

/deep/.creator {
    align-items: center;
    padding: 24px 16px 24px;
    .van-cell__title {
        margin-left: 12px;
        .text1 {
            font-size: @font2;
            line-height: 24px;
        }
        .text2 {
            font-size: @font2;
            color: @text3;
            line-height: 22px;
        }
    }

    .van-cell__value {
        font-size: @font2;
    }
}
.conName {
    font-size: @font2;
    font-weight: 400;
    color: #939599;
    line-height: 24px;
    padding: 10px 0 0 16px;
}
.titleIcon {
    font-size: @font2;
    font-weight: bold;
    color: #ffffff;
    line-height: 24px;
    padding: 30px 0 0 16px;
}
span {
    font-size: @font2;
    font-weight: 400;
    color: #939599;
    line-height: 24px;
}
.top {
    padding: 20px 16px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .top1 {
        display: flex;
        align-items: center;
        .text1 {
            font-size: @font2;
            font-weight: bold;
            color: #ffffff;
            line-height: 28px;
        }
        .text2 {
            width: 94px;
            height: 32px;
            border: 1px solid yellow;
            border-radius: 4px;
            margin-left: 14px;
            line-height: 32px;
            text-align: center;
            color: #ffffff;
            &.text3 {
                display: flex;
                align-items: center;
                justify-content: center;
            }
            .img {
                width: 18px;
                height: 18px;
                margin-right: 4px;
            }
            &.prim {
                color: @text3;
                border: 1px solid #303133;
                background: #202122;
            }
        }
        .text4 {
            color: #fdfb60;
            &.prim {
                color: @text3;
            }
        }
        .text5 {
            font-size: @font2;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #939599;
            line-height: 24px;
            padding-left: 10px;
        }
    }
}
.buy {
    width: 100%;
    display: block;
    height: 52px;
    background: linear-gradient(135deg, @prim 0%, @warn 100%);
    border-radius: 8px;
    border-width: 0;
    color: #000;
    &:hover {
        background: linear-gradient(135deg, darken(@prim, 10%), darken(@warn, 10%));
    }

    &.used {
        background: linear-gradient(135deg, darken(@prim, 50%), darken(@warn, 50%));
        color: @text3;
    }
}
/deep/ .mySwiper {
    width: calc(100vw - 120px);
    height: calc(100vw - 120px);
    z-index: 1;

    .swiper-pagination {
        bottom: 22px;
    }

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

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

    .swiper-slide img {
        display: block;
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: 4px;
    }
}
.goods {
    padding: 30px 16px 20px;

    .page-title {
        font-size: @font2;
        font-weight: bold;
        color: #ffffff;
        line-height: 24px;
        &:not(:first-child) {
            padding-top: 16px;
        }
    }
}

.specific-list {
    padding: 10px 0 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    overflow-x: auto;
}
.textName {
    font-size: @font2;
    font-weight: 400;
    color: #939599;
    line-height: 24px;
    padding-top: 10px;
}
.specific-item {
    // width: 94px;
    // height: 62px;
    border-radius: 4px;
    padding: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    position: relative;
    background: linear-gradient(135deg, @prim, @warn);
    margin-right: 20px;
    box-sizing: border-box;
    min-width: 94px;

    &::after {
        content: '';
        position: absolute;
        top: 1px;
        left: 1px;
        right: 1px;
        bottom: 1px;
        background-color: @bg;
        border-radius: 4px;
        z-index: 0;
    }

    .text1 {
        font-size: @font2;
        color: @text3;
        line-height: 24px;
        z-index: 1;
        white-space: nowrap;
    }

    .text2 {
        font-size: @font2;
        color: #ffffff;
        line-height: 24px;
        z-index: 1;
        white-space: nowrap;
    }
}

.page-text {
    font-size: @font2;
    color: #ffffff;
    line-height: 28px;
    margin-top: 10px;
}

.btn {
    position: fixed;
    z-index: 20;
    bottom: 0;
    left: 0;
    right: 0;
    background: #202122ee;
    .btns {
        padding: 6px 42px;
    }
    .van-button {
        background: linear-gradient(135deg, #fdfb60 0%, #ff8f3e 100%);
        color: #333230;
        font-size: @font2;
        border-width: 0px;
    }
}
.goods-info {
    padding: 0 0 30px;
}
.detail-swiper {
    height: 170px;
    margin-top: 10px;
}

.swiper-slide {
    width: 35.2vw;
}

/deep/.page-detail {
    img {
        display: block;
        max-width: 100%;
        height: auto;
    }
}
.swiper-video {
    width: calc(100vw - 134px);
    height: calc(100vw - 134px);
    border: 2px solid #2f2f2f;
    border-radius: 8px;
    overflow: hidden;
    background-color: @bg;
}

.detail-top {
    position: relative;
    .detail-info {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        display: flex;
        flex-direction: column;
        align-items: center;
    }
    .bg-img {
        display: block;
        width: 100vw;
    }
    .mySwiper {
        border-radius: 8px;
        border: 2px solid #ffffff;
        .swiper-slide {
            display: flex;
            align-items: center;
            justify-content: center;
            /deep/ .van-image {
                border: 2px solid #2f2f2f;
                border-radius: 8px;
                overflow: hidden;
                background-color: @bg;
            }
        }
    }

    .name {
        font-size: @font4;
        font-weight: bold;
        color: #ffffff;
        line-height: 30px;
        padding: 10px 25px;
        position: relative;

        img {
            position: absolute;
            &:nth-child(2),
            &:nth-child(5) {
                width: 45px;
                display: block;
            }

            &:nth-child(3),
            &:nth-child(4) {
                width: calc(100% - 65px);
                height: 10px;
            }
            &:nth-child(2) {
                top: 0;
                left: 0;
            }
            &:nth-child(3) {
                top: -0.8px;
                left: 45px;
            }
            &:nth-child(4) {
                bottom: 0.5px;
                right: 45px;
            }
            &:nth-child(5) {
                bottom: 0;
                right: 0;
            }
        }
    }
}

.share-content {
    display: flex;
    margin-top: 20px;
}
.share-icon {
    margin-left: 30px;
    img {
        width: 18px;
        height: 18px;
        display: inline-block;
        vertical-align: middle;
    }
    span {
        font-size: @font1;
        color: #949699;
        line-height: 24px;
        vertical-align: middle;
        margin-left: 2px;
    }
}
.info {
    .info-title {
        display: flex;
        flex-direction: column;
        align-items: center;
    }
}

.detail-name {
    font-size: @font4;
    font-weight: bold;
    color: #ffffff;
    line-height: 30px;
    padding: 10px 25px;
    position: relative;

    img {
        position: absolute;
        &:nth-child(2),
        &:nth-child(5) {
            width: 45px;
            display: block;
        }

        &:nth-child(3),
        &:nth-child(4) {
            width: calc(100% - 65px);
            height: 10px;
        }
        &:nth-child(2) {
            top: 0;
            left: 0;
        }
        &:nth-child(3) {
            top: -0.8px;
            left: 45px;
        }
        &:nth-child(4) {
            bottom: 0.5px;
            right: 45px;
        }
        &:nth-child(5) {
            bottom: 0;
            right: 0;
        }
    }
}

.status-text {
    font-size: @font4;
    color: #939599;
    line-height: 28px;
    text-align: center;
    padding: 14px 50px !important;
    border-top: 1px solid #313233;
}
</style>
