<template>
    <van-pull-refresh
        success-text="加载成功"
        success-duration="500"
        class="mine"
        v-model="isLoading"
        @refresh="onRefresh"
    >
        <div class="userInfo" v-if="isLogin">
            <div class="edit-btn" @click="$router.push('/setting')">
                <span>编辑</span>
            </div>
            <div class="userInfo-content">
                <div class="userInfo-top">
                    <van-image
                        radius="100"
                        width="150"
                        height="150"
                        :src="
                            userInfo.avatar
                                ? userInfo.avatar + '?r=' + refreshFlag
                                : require('@assets/img_default_photo.png')
                        "
                        fit="cover"
                        :class="{ activeAvatar: isLogin && userInfo.useCollectionPic }"
                        @click="$router.push('/setting')"
                    />
                    <div class="text">
                        <div class="text1">
                            <span class="van-ellipsis" style="max-width: 120px">{{ userInfo.nickname }}</span>
                        </div>
                        <div class="text2">
                            <span> 用户ID:{{ userInfo.id }} </span>
                            <img @click="copy" src="@assets/svgs/copy_icon.svg" alt="" />
                        </div>
                        <div class="icons" @click="goAuth">
                            <div class="auth-imgs" v-if="authStatus !== '已认证'">
                                <!-- <img src="../assets/renzheng_icon.png" alt="" /> -->
                                <span>未实名认证</span>
                            </div>
                            <div class="auth-imgs authed" @click="goAuth" v-else>
                                <!-- <img src="../assets/yirenzhng-geren-icon.png" alt="" /> -->
                                <span>个人已认证</span>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="sub" :class="{ 'van-multi-ellipsis--l2': !showMore }">
                    {{ userInfo.intro }}
                </div>

                <div class="btns">
                    <div class="collect" @click="$router.push('/store')">
                        <div class="text1">{{ assetNum }}</div>
                        <div class="text2">藏品数</div>
                    </div>
                    <div class="collect" @click="$router.push('/mineFollows')">
                        <div class="text1">{{ userInfo.follows }}</div>
                        <div class="text2">关注</div>
                    </div>
                    <div class="collect" @click="$router.push('/mineFollowers')">
                        <div class="text1">{{ userInfo.followers }}</div>
                        <div class="text2">粉丝</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="userInfo" v-else>
            <div class="userInfo-content">
                <div class="userInfo-top" @click="$router.push('/login')">
                    <van-image
                        round
                        width="150"
                        height="150"
                        :src="require('@assets/img_default_photo.png')"
                        fit="cover"
                        @click="$router.push('/login')"
                    />

                    <div class="text">
                        <div class="text1" @click="$router.push('/login')">点击登录</div>
                        <div class="text2" style="padding-bottom: 16px">立即登录获取精彩服务</div>
                    </div>
                </div>
            </div>
        </div>
        <driver />
        <div class="orderList">
            <div class="order-info" @click="$router.push('/orders?type=DEFAULT')">
                <img src="@assets/info_icon_yishuping.png" alt="" />
                <span>艺术品订单</span>
            </div>
            <div class="order-info" @click="$router.push('/activityOrders')">
                <img src="@assets/info_icon_zhuzaodingdan.png" alt="" />
                <span>铸造订单</span>
            </div>
            <div class="order-info" @click="$router.push('/auctionOrders')">
                <img src="@assets/info_icon_paimaidingdan.png" alt="" />
                <span>拍卖订单</span>
            </div>
        </div>
        <driver />
        <div class="orderList prim">
            <div
                class="menu-info"
                :style="{ width: $store.state.review ? '100%' : '50%' }"
                @click="$router.push('/rank')"
            >
                <img src="@assets/info_icon_top50.png" alt="" />
                <span>TOP50&nbsp;探索官</span>
            </div>
            <div class="menu-info" @click="share" v-if="!$store.state.review">
                <van-badge :dot="shareProduct.openQuota" :offset="[-15, 5]">
                    <img src="@assets/info_icon_yaoqing.png" alt="" />
                </van-badge>
                <span>立即邀请</span>
            </div>
        </div>

        <div class="tabbar-placeholder"></div>
    </van-pull-refresh>
</template>

<script>
import { mapState } from 'vuex';
import { Capacitor } from '@capacitor/core';
import { StatusBar, Style } from '@capacitor/status-bar';
export default {
    data() {
        return {
            org: false,
            showMore: false,
            assetNum: 0,
            shareProduct: {
                openQuota: false
            },
            shareMoney: 0,
            refreshFlag: Number(localStorage.getItem('refreshFlag') || '0'),
            enableWallet: false,
            faceAuth: false,
            publishShow: false
        };
    },
    computed: {
        ...mapState(['userInfo'])
    },
    created() {
        this.getInit();
    },
    methods: {
        getInit(refresh) {
            if (this.isLogin) {
                this.$http.get('/sysConfig/get/publishShow').then(res => {
                    if (res.value === true || res.value === '1') {
                        this.publishShow = true;
                    }
                });
                this.$http
                    .post(
                        '/asset/all',
                        {
                            query: {
                                page: 0,
                                size: 1,
                                userId: this.$store.state.userInfo.id,
                                status: 'NORMAL,TRADING,GIFTING,MINTING'
                            }
                        },
                        { body: 'json' }
                    )
                    .then(res => {
                        this.assetNum = Number(res.totalElements);
                    });

                this.getProduct();
                this.$http.get('/sysConfig/get/share_lvzhoushi_num,enable_wallet,enable_face_auth').then(res => {
                    if (res['share_lvzhoushi_num'] && res['share_lvzhoushi_num'].value) {
                        this.shareMoney = Number(res['share_lvzhoushi_num'].value);
                    }
                    this.enableWallet = res['enable_wallet'].value === '1';
                    this.faceAuth = res['enable_face_auth'].value === '1';
                });
            }

            if (window.isNativePlatform) {
                StatusBar.setStyle({ style: Style.Light });
            }

            return this.$store.dispatch('getUserInfo', refresh);
        },
        copy() {
            this.$copyText(this.userInfo.id).then(
                e => {
                    this.$toast.success('复制成功');
                    console.log(e);
                },
                e => {
                    this.$toast('复制失败');
                    console.log(e);
                }
            );
        },
        goAuth() {
            if (this.authStatus === '认证中' || this.authStatus === '认证失败') {
                this.$router.push('/waiting');
            } else if (this.authStatus === '未认证') {
                if (this.faceAuth) {
                    this.$router.push('/faceAuth');
                } else {
                    this.$router.push('/verified');
                }
            }
        },
        getProduct() {
            return this.$http
                .get('/sysConfig/get/share_collection_id')
                .then(res => {
                    if (res.value) {
                        return this.$http.get('collection/get/' + res.value);
                    } else {
                        return Promise.reject('活动未开始');
                    }
                })
                .catch(e => {
                    return Promise.reject('活动未开始');
                })
                .then(res => {
                    this.shareProduct = res;
                    return Promise.resolve();
                });
        },
        share() {
            this.checkLogin().then(() => {
                this.getProduct()
                    .then(() => {
                        return new Promise((resolve, reject) => {
                            this.$nextTick(() => {
                                if (this.$refs.level.money <= this.shareMoney) {
                                    this.$dialog.alert({
                                        title: '提示',
                                        message: '绿洲核大于' + this.shareMoney + '的用户才能参加该活动'
                                    });
                                    resolve();
                                } else if (this.shareProduct.openQuota) {
                                    this.$router.push(
                                        `/productTasks?id=${this.shareProduct.id}&invitor=${this.$store.state.userInfo.id}`
                                    );
                                    resolve();
                                } else {
                                    reject('活动藏品分享已关闭');
                                }
                            });
                        });
                    })
                    .catch(e => {
                        if (e) {
                            this.$toast(e);
                        }
                    });
            });
        },
        onRefresh() {
            this.refreshFlag++;
            localStorage.setItem('refreshFlag', this.refreshFlag);
            return this.getInit(true)
                .then(() => {
                    this.isLoading = false;
                })
                .catch(() => {
                    this.isLoading = false;
                });
        },
        changeShow(isPublicShow) {
            this.$dialog
                .confirm({
                    title: isPublicShow ? '关闭隐私' : '开启隐私',
                    message: isPublicShow ? '关闭后，他人将看到你的主页信息' : '开启后，他人将无法查看你的主页信息'
                })
                .then(() => {
                    this.updateUser({ isPublicShow: isPublicShow });
                });
        }
    }
};
</script>

<style lang="less" scoped>
.mine {
    background-color: #272b2e;
    padding-top: 0 !important;
    position: relative;
    min-height: 100vh;
}

.top {
    display: flex;
    align-items: center;
    padding: 36px 16px 30px;
    border-bottom: 1px solid @bg2;
    position: relative;

    .van-image {
        border: 5px solid @bg;
    }

    .text {
        margin-left: 12px;
        position: relative;
        z-index: 2;

        .text1 {
            font-size: @font3;
            font-weight: bold;
            color: @text0;
            line-height: 24px;
            img {
                width: 18px;
                height: 18px;
                display: inline-block;
                margin-left: 6px;
            }
        }

        .text2 {
            font-size: @font2;
            color: #949699;
            line-height: 24px;
        }
    }

    .top-bg {
        position: absolute;
        top: -83px;
        left: 0;
        right: 0;
        width: 100%;
        display: block;
        height: auto;
        z-index: 0;
    }
}

/deep/ .title {
    padding: 12px 12px 6px;
    border-bottom: 1px solid @bg3;
    .van-cell__title {
        span {
            font-size: @font3;
            line-height: 28px;
            color: @text0;
            font-weight: bold;
        }
    }

    .van-cell__value {
        span {
            font-size: @font1;
            line-height: 28px;
        }
    }

    .van-icon {
        line-height: 28px;
    }
}
.orderList {
    .flex();
    // padding: 0 12px;
    .order-info {
        padding: 16px 0;
        position: relative;
        width: 33%;
        box-sizing: border-box;
        .flex-col();
        align-items: center;
        justify-content: center;

        .order-info-box {
            .flex();
            width: 132px;
        }
        img {
            width: 28px;
            height: 28px;
            // margin-right: 8px;
        }
        span {
            font-size: 14px;
            font-weight: bold;
            color: #ffffff;
            line-height: 24px;
            margin-top: 4px;
        }
    }
    .order-info + .order-info {
        // margin-left: 20px;
        &::before {
            content: '';
            width: 1px;
            background-color: #373b3e;
            position: absolute;
            left: -0.5px;
            top: 0;
            bottom: 0;
        }
    }

    // /deep/.van-badge__wrapper {
    //     width: 50%;
    //     .order-info {
    //         width: 100%;
    //     }
    // }

    &.prim {
        span {
            font-weight: normal;
            color: #43ce00;
            font-family: 'ZhenyanGB';
        }
    }
}
.orderList {
    .flex();
    // padding: 0 12px;
    .menu-info {
        padding: 16px 0;
        position: relative;
        width: 50%;
        box-sizing: border-box;
        .flex();
        justify-content: center;

        .menu-info-box {
            .flex();
            width: 132px;
        }
        img {
            width: 28px;
            height: 28px;
            margin-right: 8px;
        }
        span {
            font-size: 14px;
            font-weight: bold;
            color: #ffffff;
            line-height: 24px;
        }
    }
    .menu-info + .menu-info {
        // margin-left: 20px;
        &::before {
            content: '';
            width: 1px;
            background-color: #373b3e;
            position: absolute;
            left: -0.5px;
            top: 0;
            bottom: 0;
        }
    }

    // /deep/.van-badge__wrapper {
    //     width: 50%;
    //     .order-info {
    //         width: 100%;
    //     }
    // }

    &.prim {
        span {
            font-weight: normal;
            color: #43ce00;
            font-family: 'ZhenyanGB';
        }
    }
}
.grid-img {
    display: block;
}

.driver {
    background-color: #373b3e;
}

/deep/ .van-grid-item {
    .van-grid-item__text {
        font-size: @font1;
        color: @text0;
        line-height: 18px;
        margin-top: 4px;
        white-space: nowrap;
    }
    .van-grid-item__content {
        padding: 4.2vw 2.1vw;
    }
}

/deep/.menu {
    .van-cell {
        padding: 22px 20px 24px;

        &::after {
            left: 52px;
            right: 16px;
        }
    }

    .van-cell__title {
        span {
            font-weight: bold;
        }
    }
}

.search-icon {
    width: 28px;
    height: 28px;
    // margin-right: 10px;
    display: block;
    /deep/.van-icon__image {
        width: 28px;
        height: 28px;
        display: block;
    }
}
.userInfo {
    padding-top: 52vw;
    // border-bottom: 1px solid @bg2;
    position: relative;
    margin-bottom: -103px;

    .top-img {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 1;
        &::after {
            content: '';
            position: absolute;
            left: 0;
            bottom: 0;
            height: 90px;
            right: 0;
            z-index: 1;
            background: linear-gradient(180deg, rgba(39, 43, 46, 0) 0%, #272b2e 100%);
        }
    }
}
.userInfo-content {
    padding: 0 16px;
    z-index: 2;
    position: relative;
    transform: translateY(-103px);
    .sub {
        font-size: 12px;
        color: @text3;
        line-height: 17px;
        padding: 2px 0 0;
        text-align: center;
    }

    .sub-right {
        .flex();
        margin-top: 4px;
        justify-content: center;
        img {
            width: 18px;
            height: 18px;
            display: block;
            transition: transform ease-in-out 0.3s;

            &.subAll {
                transform: rotate(180deg);
            }
        }
    }

    .sub-right > div {
        font-size: 12px;
        color: @text0;
        line-height: 17px;
        text-decoration: underline;
    }

    .btns {
        display: flex;
        // padding-bottom: 16px;
        align-items: center;

        .collect {
            width: 33%;
            .flex-col();
            align-items: center;
            padding: 16px 0;
            .text1 {
                font-size: 18px;
                color: #fff;
                line-height: 24px;
                font-weight: bold;
            }
            .text2 {
                font-size: 12px;
                color: @text3;
                line-height: 17px;
                margin-top: 2px;
            }
        }
    }
}
.userInfo-top {
    display: flex;
    align-items: center;
    transform: translateX(-5px);
    position: relative;
    flex-direction: column;
    .van-image {
        // border: 5px solid @bg;
        flex-shrink: 0;
        border-radius: 100px;
    }

    .text {
        margin: 0 10px 0 12px;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        align-items: center;
        .text1 {
            font-size: 24px;
            font-weight: bold;
            color: @bg;
            line-height: 24px;
            margin-top: 15px;
            .flex();
            // img {
            //     width: 18px;
            //     height: 18px;
            //     display: inline-block;
            //     // margin-left: 5px;
            //     vertical-align: middle;
            // }
        }
        .text2 {
            font-size: 14px;
            color: #949699;
            line-height: 24px;
            display: flex;
            align-items: center;
            margin-top: 5px;
            img {
                display: block;
                margin-left: 6px;
            }
        }

        .icons {
            .flex();
            .auth-imgs {
                .flex();
                transform: translateX(15px);
                img {
                    width: 28px;
                    height: 28px;
                    position: relative;
                    z-index: 1;
                }
                span {
                    font-size: 12px;
                    color: @text3;
                    line-height: 18px;
                    background: #373b3e;
                    border-radius: 2px;
                    padding: 0 4px 0 10px;
                    transform: scale(0.8) translateX(-24px);
                    position: relative;
                    z-index: 0;
                }

                &.authed {
                    span {
                        color: #ff8f3e;
                        background-color: #ffefe8;
                        position: relative;
                    }
                }
            }
        }
    }

    .renzhen-img {
        position: absolute;
        left: 60px;
        bottom: 5px;
        width: 20px;
        height: 20px;
    }
}
.menus {
    // margin: 0 16px;
    // background-color: @bg;
    // border-radius: 8px;
    // overflow: hidden;
    &.van-grid {
        margin: 0;
    }

    /deep/.van-grid-item__text {
        color: #fff;
    }

    /deep/.van-grid-item__content:after {
        border-color: #373b3e;
    }

    /deep/&.van-hairline--top:after {
        border-color: #373b3e;
    }
}

/deep/.auth {
    color: @text3!important;
    padding: 0 10px;
    margin-left: 6px;
    .van-button__content {
        .flex();
        .van-icon__image {
            display: block;
        }
        font-weight: normal;
    }
    height: 24px;
}

.edit {
    color: @text3;
}

.card {
    box-shadow: 0px 2px 6px 0px rgba(200, 201, 204, 0.3);
    border-radius: 12px;
    margin: 16px;
}

.edit-btn {
    position: absolute;
    right: 16px;
    top: 16px;
    top: calc(var(--safe-top) + 16px);
    .flex();
    z-index: 20;
    img {
        width: 18px;
        height: 18px;
    }
    span {
        font-size: 12px;
        color: #ffffff;
        line-height: 12px;
        margin-left: 4px;
    }
    width: 72px;
    height: 24px;
    background: #272b2e;
    border-radius: 4px;
    justify-content: center;
}

.yinsi {
    position: absolute;
    background: #3ab200;
    border-radius: 4px;
    top: calc(var(--safe-top) + 16px);
    left: 16px;
    z-index: 3;
    padding: 0 11px;
    height: 24px;
    .flex();
    img {
        width: 18px;
        height: 18px;
    }
    span {
        font-size: 12px;
        color: #ffffff;
        line-height: 24px;
        margin-left: 3px;
    }

    &.not {
        background: #ff4f50;
    }
}
</style>
