<template>
  <div class="grade-status">
    <div class="nav">
      <van-icon
        name="arrow-left"
        size="20"
        class="nav-left"
        @click="handlerBack"
      />
      <span class="nav-title">森林状态</span>
    </div>
    <div class="forest-content">
      <div class="forest-level">Lv.{{ userLevel }}{{ levelDes }}</div>
      <div v-for="(item, key, index) in diffrentMap" :key="index">
        <div>
          <div class="nums">
            <span>距离下一级</span>
            <span v-if="key === 'team'">团队用户</span>
            <span v-else-if="key === 'recommend'">直推用户</span>
            <span v-else></span>
            <span>还需增加</span>
            <span v-for="(d, i) in item" :key="i">
              {{ d.value }}
              {{ d.label }}
            </span>
          </div>
        </div>
      </div>

      <!-- <div class="level-progress" v-if="userLevel!=7"> -->
      <div class="level-progress">
        <div class="level-num">Lv.{{ userLevel }}</div>
        <van-progress :percentage="perenct * 100" />
        <div class="level-num">Lv.{{ nextLevel + 1 }}</div>
      </div>
      <!-- <div class="relegation">保级还需增加￥{{ relegationNum }} 业绩</div> -->
    </div>
    <div class="forest-list">
      <div class="forest-item" v-for="(item, index) in list" :key="index">
        <span class="forest-item-num">{{ item.num }}</span>
        <div class="forest-item-label">
          <span>{{ item.label }}</span>
          <van-icon
            v-if="item.hasArrow"
            name="arrow"
            size="16"
            class="nav-left"
            @click="handleUrl(item.urlName)"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getForestStatus } from "@/api/grade";
import { getLevelDes, getNextLevel, diffrentOflevel } from "@/config/grade";
export default {
  name: "Grade",
  data() {
    return {
      userLevel: "",
      levelDes: "白银树",
      directpush: "XXX",
      relegationNum: "21,141",
      currentLevel: "",
      nextLevel: "",
      perenct: 0,
      diffrentMap: {},
      type: ["amount", "recommend", "team"],
      list: [
        {
          num: 0,
          label: "团队详情",
          hasArrow: true,
          urlName: "LeagueNums",
          field: "teamUserCount"
        },
        {
          num: 0,
          label: "我的直推",
          hasArrow: true,
          urlName: "MyStatus",
          field: "recommendUserCount"
        },
        {
          num: 0,
          label: "团队累计总业绩",
          hasArrow: false,
          field: "teamCountDouble"
        },
        {
          num: 0,
          label: "团队当月新增业绩",
          hasArrow: false,
          field: "teamNowCount"
        }
      ]
    };
  },
  mounted() {
    this.getstatus();
  },
  methods: {
    handlerBack() {
      try {
        this.$bridgeToAppFun.navigateBack();
      } catch {
        this.$router.go(-1);
      }
    },
    handleUrl(urlName) {
      this.$router.push({ name: urlName });
    },
    getstatus() {
      const userId = JSON.parse(localStorage.getItem("user")).userId;
      const _this = this;
      getForestStatus(userId)
        .then(res => {
          if (res.code === 0) {
            let resData = res.data;
            _this.handleData(resData);
          } else {
            _this.$toast.fail(res.msg);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    handleData(data) {
      this.perenct = data.perenct;
      this.userLevel = data.userLevel;
      this.levelDes = getLevelDes(data.userLevel);
      let nextLevelObj = getNextLevel(data.userLevel);
      if (!nextLevelObj.isHighest) {
        this.nextLevel = nextLevelObj.nextLevel;
      }
      this.list.forEach(v => {
        v.num = data[v["field"]];
      });
      this.diffrentMap = [];
      if (data.userLevel < 7) {
        let diffrentMap = diffrentOflevel[data.userLevel];
        let desObj = {};
        if (diffrentMap) {
          diffrentMap.forEach(v => {
            v.value = data.differenceMap[v.field];
            if (desObj[v.type]) {
              desObj[v.type].push(v);
            } else {
              desObj[v.type] = [];
              desObj[v.type].push(v);
            }
          });
          this.diffrentMap = desObj;
        }
      }
    }
  }
};
</script>

<style lang="scss" scoped>
$white: #ffffff;
.grade-status {
  width: 100%;
  height: 298px;
  background-image: url("../assets/images/森林状态.png");
  background-size: cover;
  .nav {
    position: relative;
    display: flex;
    align-items: center;
    width: 100%;
    height: 46px;
    color: #fff;
    .nav-left {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 52px;
      height: 100%;
      line-height: 46px;
      text-align: center;
    }
    .nav-title {
      max-width: 60%;
      margin: 0 auto;
      font-weight: bold;
      font-size: 16px;
    }
  }
  .forest-content {
    text-align: center;
    padding-top: 25px;
    .nums {
      font-size: 14px;
      color: $white;
      line-height: 25px;
      margin-bottom: 12px;
    }
    .relegation {
      font-size: 16px;
      color: $white;
      margin-top: 20px;
    }
  }
  .forest-level {
    height: 45px;
    font-family: PingFang-SC-Bold;
    font-size: 24px;
    font-weight: normal;
    font-stretch: normal;
    letter-spacing: 0px;
    color: #ffffff;
  }
  .level-progress {
    box-sizing: border-box;
    width: 100%;
    padding: 0 30px;
    display: flex;
    align-items: center;
    .level-num {
      width: 36px;
      height: 16px;
      line-height: 16px;
      background-image: url("../assets/images/rankbox.png");
      background-size: cover;
      font-family: PingFang-SC-Bold;
      font-size: 11px;
      color: #fc5202;
    }
  }

  .forest-list {
    padding: 0 15px;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
    position: absolute;
    top: 270px;
    .forest-item {
      height: 98px;
      background-color: #ffffff;
      box-shadow: 0px 1px 6px 0px rgba(6, 0, 1, 0.04);
      border-radius: 4px;
      width: calc(50% - 8px);
      margin-bottom: 10px;
      text-align: center;
      padding-top: 15px;
      box-sizing: border-box;
    }
    .forest-item-num {
      height: 19px;
      font-family: DINCondensed-Bold;
      font-size: 26px;
      font-weight: normal;
      font-stretch: normal;
      line-height: 25px;
      letter-spacing: 0px;
      color: #333333;
    }
    .forest-item-label {
      display: flex;
      align-items: center;
      justify-content: center;
      span {
        font-family: PingFang-SC-Medium;
        font-size: 14px;
        font-weight: normal;
        font-stretch: normal;
        letter-spacing: 0px;
        color: #666666;
      }
      .nav-left {
        color: #666;
      }
    }
  }
}
</style>
<style lang="scss">
.van-progress {
  width: calc(100% - 72px);
  height: 4px;
  background-color: rgba(0, 0, 0, 0.2);
  .van-progress__portion {
    background: #f9f9f9;
  }
  .van-progress__pivot {
    display: none;
  }
}
</style>
