<template>
    <view>
        <view class="gradeWrap">
            <view style="margin: 0 50rpx;display: flex;align-items: center;">
                <view @click="gouserHome">
                    <img style='width:55px;height:55px;border-radius: 50%' v-if='avatar'
                        :src="avatar && avatar.indexOf('http') == 0 ? avatar : base + avatar" alt="">
                    <img v-else style='width:55px;height:55px;border-radius: 50%'
                        src="../../../static/crowd/defaultAva.jpg" alt="">
                </view>
                <p v-if='phoneuserinfo.companyName' style='margin-bottom:5px;font-size:17px;font-weight:600;'>
                    {{ phoneuserinfo.companyName }}
                </p>
                <p v-else style='margin-bottom:5px;font-size:17px;font-weight:600;margin-left: 10rpx;'>{{ nickName }}
                </p>
            </view>
            <view class="gradeSwiperWrap">
                <swiper class="gradeSwiper" previous-margin="30rpx" :duration="swipeDuration" next-margin="30rpx"
                    :current="currentImgIndex" @change="changeswiper">
                    <swiper-item v-for="(index) in scoreArray.length - 1" :key="index">
                        <view class="swiper-item gradeSwiperItem">
                            <view class="bothSide">
                                <view>
                                    <view class="currentState">{{ currentGrade(scoreArray[index].minScore, index, true)
                                        }}</view>
                                    <view class="currentLevel">Lv. {{ index }}</view>
                                </view>
                                <view @click="toToggle('gradeTask')"
                                    v-show="currentGrade(scoreArray[index].minScore, index, true) == '当前等级'"
                                    class="toGrow">去升级
                                </view>
                            </view>
                            <view class="grailLayout">
                                <view>Lv. {{ index }}</view>
                                <view style="font-size: 24rpx;color: #666666;">已获得{{ myScore }}成长值</view>
                                <view>Lv. {{ index + 1 }}</view>
                            </view>
                            <view class="splitLine">
                                <view class="progress" :style="{ width: progressWidth(index) }"></view>
                            </view>
                            <view class="grailLayout">
                                <view>{{ scoreArray[index].minScore }}</view>
                                <view
                                    v-if="'currentGrow1' == stateMap[currentGrade(scoreArray[index].minScore, index, true)]"
                                    class="currentGrow1">{{ currentGrade(scoreArray[index].minScore, index, false) }}
                                </view>
                                <view
                                    v-if="'currentGrow2' == stateMap[currentGrade(scoreArray[index].minScore, index, true)]"
                                    class="currentGrow2">{{ currentGrade(scoreArray[index].minScore, index, false) }}
                                </view>
                                <view
                                    v-if="'currentGrow3' == stateMap[currentGrade(scoreArray[index].minScore, index, true)]"
                                    class="currentGrow3">{{ currentGrade(scoreArray[index].minScore, index, false) }}
                                </view>
                                <view>{{ index == scoreArray.length - 1 ? scoreArray[scoreArray.length - 1].minScore :
                                    scoreArray[index + 1]['minScore'] }}
                                </view>
                            </view>
                        </view>
                    </swiper-item>
                </swiper>
            </view>
            <view class="newRight"><img src="http://image.bjkeyware.com/static/index/myRight.png" alt=""> 新增权益</view>
            <view class="rightInfo">新增竞标 <span :style="{ color: levelColor[level] }">{{ level }}</span> 的众包任务的资格</view>
        </view>

        <view class="rulesGroup">
            <view @click="toToggle('growInfo')">
                <view>成长值明细</view><img src="http://image.bjkeyware.com/static/index/xia.png" alt="">
            </view>
            <view @click="toToggle('gradeRules')">
                <view>等级规则</view><img src="http://image.bjkeyware.com/static/index/xia.png" alt="">
            </view>
        </view>

    </view>
</template>

<script>
const levelColor = {
    'V1': '#6740E5',
    'V2': '#E86914',
    'V3': '#9752FF',
}

import { scoreStrategies, getLevel } from '@/utils/userlevel';
const scoreArray = JSON.parse(JSON.stringify(scoreStrategies)).reverse()

export default {
    data() {
        return {
            scoreArray: Object.freeze(scoreArray),
            myScore: '0',
            avatar: '',
            phoneuserinfo: '',
            nickName: '',
            currentImgIndex: 0,
            swipeDuration:0,
            stateMap: Object.freeze({
                '未达成': 'currentGrow3',
                '已达成': 'currentGrow1',
                '当前等级': 'currentGrow2'
            }),
            levelColor: Object.freeze(levelColor)
        }
    },
    methods: {
        toToggle(url) {
            uni.navigateTo({
                url: `./${url}`
            })
        },
        changeswiper(e) {
            this.currentImgIndex = e.detail.current
        }
    },
    onShow() {

    },
    onLoad() {
        this.http.quickGet('/getInfo', true).then(res => {
            const { growthValue } = res.data.user
            this.myScore = growthValue || 0
            this.currentImgIndex = getLevel(this.myScore).level
            this.swipeDuration = 500
        })
        this.phoneuserinfo = uni.getStorageSync('wxUserInfo');
        this.avatar = uni.getStorageSync('avatar');
        this.nickName = uni.getStorageSync('nickName');
    },
    computed: {
        level() {
            const { _, equity } = getLevel(this.myScore)
            return equity
        },
        currentGrade() {
            return (min, index, tag) => {
                // if (index == this.scoreArray.length - 1) {
                //     return '未达成'
                // }
                const max = this.scoreArray[index + 1]['minScore']
                if (this.myScore >= max) {
                    return '已达成'
                } else if (this.myScore <= max && this.myScore >= min) {
                    let result = ''
                    result = tag ? '当前等级' : `还差${max - this.myScore}可升级`
                    return result
                } else {
                    return '未达成'
                }
            }
        },
        progressWidth() {
            return (index) => {
                const max = this.scoreArray[index + 1]['minScore']
                if (this.myScore > max) {
                    return '100%'
                } else if (this.myScore < this.scoreArray[index]['minScore']) {
                    return '0%'
                } else {
                    return (((this.myScore - this.scoreArray[index]['minScore']) / (max - this.scoreArray[index]['minScore'])) * 100).toFixed(2) + "%"
                }
            }
        }
    }
}
</script>

<style lang="scss" scoped>
.gradeWrap {
    background-color: #e3e6fe;
    border-bottom-left-radius: 60rpx;
    border-bottom-right-radius: 60rpx;

    padding: 20rpx 0;

    .gradeSwiperWrap {

        .gradeSwiper {
            height: 300rpx;
            box-sizing: border-box;

            .gradeSwiperItem {
                padding: 22rpx 0rpx;
                box-shadow: 0rpx 2rpx 18rpx 0rpx rgba(30, 66, 111, 0.15);
                border-radius: 10rpx;
                margin: 0 10rpx;
                background: url('http://image.bjkeyware.com/static/index/gradeBg.png') no-repeat 0 0;
                background-size: 100% 100%;

            }

            .bothSide {
                display: flex;
                justify-content: space-between;
                font-family: PingFang SC;
                position: relative;

                .toGrow {
                    width: 130rpx;
                    height: 46rpx;
                    background: linear-gradient(90deg, #5EA6FD, #1A81F9);
                    border-radius: 23rpx;
                    text-align: center;
                    font-weight: 500;
                    font-size: 26rpx;
                    color: #FFFFFF;
                    line-height: 46rpx;
                    position: absolute;
                    bottom: 10rpx;
                    right: 30rpx;
                }

                .currentState {
                    width: 150rpx;
                    line-height: 46rpx;
                    font-weight: 500;
                    font-size: 26rpx;
                    color: #333333;
                    background: #EAF1FD;
                    text-align: center;
                    border-radius: 0rpx 23rpx 23rpx 0rpx;
                }

                .currentLevel {
                    margin: 30rpx 40rpx;
                    height: 28rpx;
                    font-weight: 800;
                    font-size: 36rpx;
                    color: #000000;
                    line-height: 38rpx;
                }
            }

            .splitLine {
                margin: 10rpx auto;
                width: 572rpx;
                height: 10rpx;
                // background: #98C5FB;
                background: #FFFFFF;
                border-radius: 5rpx;
                position: relative;

                .progress {
                    position: absolute;
                    height: 10rpx;
                    width: 15%;
                    background: #98C5FB;
                    border-radius: 5rpx;
                }
            }

            .grailLayout {
                margin-top: 10rpx;
                display: flex;
                justify-content: space-between;
                margin: 0 40rpx;

                font-family: PingFang SC;
                font-weight: 500;
                font-size: 26rpx;
                color: #333333;
                line-height: 38rpx;

                .currentGrow1 {
                    background: url('http://image.bjkeyware.com/static/index/currentState1.png') no-repeat 0 0;
                    min-width: 10rpx;
                    padding: 0 20rpx;
                    height: 50rpx;
                    line-height: 58rpx;
                    background-size: 100% 100%;
                    text-align: center;
                    font-family: PingFang SC;
                    font-weight: 500;
                    font-size: 24rpx;
                    color: #FFFFFF;
                }

                .currentGrow2 {
                    background: url('http://image.bjkeyware.com/static/index/currentState2.png') no-repeat 0 0;
                    min-width: 10rpx;
                    padding: 0 20rpx;
                    height: 50rpx;
                    line-height: 58rpx;
                    background-size: 100% 100%;
                    text-align: center;
                    font-family: PingFang SC;
                    font-weight: 500;
                    font-size: 24rpx;
                    color: #FFFFFF;
                }

                .currentGrow3 {
                    background: url('http://image.bjkeyware.com/static/index/currentState3.png') no-repeat 0 0;
                    min-width: 10rpx;
                    padding: 0 20rpx;
                    height: 50rpx;
                    line-height: 58rpx;
                    background-size: 100% 100%;
                    text-align: center;
                    font-family: PingFang SC;
                    font-weight: 500;
                    font-size: 24rpx;
                    color: #FFFFFF;
                }

                &:last-child {
                    border: none;
                }
            }
        }
    }

    .newRight {
        height: 29rpx;
        font-family: PingFang SC;
        font-weight: bold;
        font-size: 30rpx;
        color: #000000;
        line-height: 38rpx;
        margin: 30rpx 50rpx 20rpx 50rpx;
        display: flex;
        align-items: center;

        img {
            width: 41rpx;
            height: 37rpx;
        }
    }

    .rightInfo {
        margin: 0rpx 50rpx;
        padding-bottom: 20rpx;
        font-family: PingFang SC;
        font-weight: 500;
        font-size: 24rpx;
        color: #4D4D4D;
    }
}

.rulesGroup {
    width: 683rpx;
    height: 180rpx;
    margin: 30rpx auto;
    background: #FFFFFF;
    border-radius: 10rpx;
    padding: 0 40rpx;

    img {
        width: 13rpx;
        height: 21rpx;
    }

    >view {
        height: 50%;
        line-height: 90rpx;
        border-bottom: 1px solid #EDEDED;
        ;
        display: flex;
        justify-content: space-between;
        align-items: center;

        &:last-child {
            border: none;
        }
    }
}
</style>