<template>
  <div class="my-status">
    <div class="userInfo">
      <img src="@/assets/images/白银树.png" alt="等级背景图" />
      <span class="level-label">我的等级</span>
      <span class="level"
        >Lv.{{ userRecommendInfo.userlevel
        }}{{ userRecommendInfo.userlevel | userLevel }}</span
      >
      <div class="userAvatar">
        <div class="avatar-box">
          <img class="avatar" :src="avatar" alt="头像" />
          <img class="level-img" src="@/assets/images/等级展示框.png" alt />
          <span class="avatar-level">Lv.{{ userRecommendInfo.userlevel }}</span>
        </div>
      </div>
    </div>
    <div class="status">
      <van-tabs
        v-model="activeTab"
        swipeable
        sticky
        color="#28b537"
        @click="changeTab"
      >
        <van-tab
          v-for="item in Tabs"
          :key="`tba-${item.id}`"
          :title="`${item.name}(${item.num})`"
        >
          <base-refresh-scroll @downLoad="onDownLoad" @upRefresh="onUpRefresh">
            <div slot="content">
              <div v-for="item in inviteeArr" :key="item.id">
                <invitee-item :invitee-item="item" />
              </div>
            </div>
          </base-refresh-scroll>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>

<script>
import { recomendInfo, recommendDetail } from "@/api/myStatus";
import BaseRefreshScroll from "../../components/BaseRefreshScroll.vue";
import InviteeItem from "./components/invitee";
export default {
  name: "MyStatus",
  components: {
    InviteeItem,
    BaseRefreshScroll
  },
  filters: {
    userLevel(key) {
      let levelName = "";
      switch (key) {
        case 0:
          levelName = "普通用户";
          break;
        case 1:
          levelName = "幼苗";
          break;
        case 2:
          levelName = "青铜树";
          break;
        case 3:
          levelName = "白银树";
          break;
        case 4:
          levelName = "黄金树";
          break;
        case 5:
          levelName = "农场主";
          break;
        case 6:
          levelName = "森林之星";
          break;
        case 7:
          levelName = "西田森合伙人";
          break;
        default:
          levelName = "";
          break;
      }
      return levelName;
    }
  },
  data() {
    return {
      avatar: '',
      inviteeArr: [],
      userRecommendInfo: {},
      activeTab: "",
      Tabs: [
        {
          id: 0,
          name: "普通用户",
          num: 0,
          keyName: "normalUserNum"
        },
        {
          id: 1,
          name: "幼苗",
          num: 0,
          keyName: "seedlingNum"
        },
        {
          id: 2,
          name: "青铜树",
          num: 0,
          keyName: "bronzeTreeNum"
        },
        {
          id: 3,
          name: "白银树",
          num: 0,
          keyName: "silverTreeNum"
        },
        {
          id: 4,
          name: "黄金树",
          num: 0,
          keyName: "goldTreeNum"
        },
        {
          id: 5,
          name: "农场主",
          num: 0,
          keyName: "farmerNum"
        },
        {
          id: 6,
          name: "森林之星",
          num: 0,
          keyName: "forestStartNum"
        },
        {
          id: 7,
          name: "西田森合伙人",
          num: 0,
          keyName: "partnerNum"
        }
      ]
    };
  },
  created() {
    this.getRecommendInfo();
    this.getRecommendDetail(0);
  },
  mounted() {
    const userAvatar = JSON.parse(localStorage.getItem('user')).headImage;
    this.avatar = process.env.VUE_APP_BASE_URL + userAvatar
  },
  methods: {
    changeTab(name) {
      this.getRecommendDetail(name);
    },
    onDownLoad(val) {
      console.log("下拉加载", val);
    },
    onUpRefresh(val) {
      console.log("上拉刷新", val);
    },
    getRecommendInfo() {
      const params = {
        userId:JSON.parase(localStorage.getItem('user')).userId
      };
      recomendInfo(params).then(res => {
        if (res.code === 0) {
          this.userRecommendInfo = res.data;
          for (const key in res.data.myRecommed) {
            this.Tabs.forEach(el => {
              if (el.keyName === key) {
                el.num = res.data.myRecommed[key];
              }
            });
          }
        }
      });
    },
    getRecommendDetail(level) {
      const params = {
        userId:JSON.parase(localStorage.getItem('user')).userId,
        userlevel: level
      };
      recommendDetail(params).then(res => {
        if (res.code === 0) {
          this.inviteeArr = res.data;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.userInfo {
  box-sizing: border-box;
  position: relative;
  width: 364px;
  height: 119px;
  margin: 0 auto 20px;
  padding: 7px 10px 12px;
  background-color: #ffffff;
  border-radius: 4px;

  img {
    width: 345px;
    height: 100px;
  }
  .level-label {
    position: absolute;
    top: 35px;
    left: 30px;
    font-size: 12px;
  }
  .level {
    position: absolute;
    top: 56px;
    left: 31px;
    font-size: 22px;
    color: #ffffff;
  }
  .userAvatar {
    position: absolute;
    top: 26px;
    right: 43px;
    .avatar-box {
      position: relative;
      width: 60px;
      height: 60px;
      .avatar {
        width: 57px;
        height: 57px;
        border: 2px solid #ffffff;
        border-radius: 50%;
      }
      .level-img {
        position: absolute;
        top: 50px;
        right: 0px;
        width: 59px;
        height: 16px;
      }
      .avatar-level {
        position: absolute;
        top: 51px;
        right: 20px;
        font-size: 12px;
        color: #ff9600;
      }
    }
  }
}
::v-deep .van-tabs__content {
  position: relative;
  height: 100vh;
  background-color: #ffffff;
}
::v-deep .van-tab__pane {
  padding-top: 10px;
}
</style>
