<template>
  <div class="sidebar-container">
    <div class="sidebarTop">
      <div class="sidebarHeader">
        <img
          :src="
            userInfo.headPath ? userInfo.headPath : userInfo.countryCircularIcon
          "
          alt=""
        />
      </div>
      <div class="nameCountry">
        <div class="username">{{ userInfo.memberName }}</div>
        <img :src="userInfo.countrySquareIcon" alt="" />
      </div>
      <!-- 如果是0表示没有奖衔 只显示等级徽章!-->
      <div class="gradeClass">
        <img
          @error="defaultSrc"
          v-if="userInfo.gradeIcon || defaultSrc == true"
          @click="goRouter(1)"
          :src="userInfo.gradeIcon"
          alt=""
        />
      </div>
      <div class="honor_wrapper">
        <div class="honor1">
          <div class="honor-title-wrapper">
            <div>荣誉奖衔</div>
            <div class="honor-title">{{ userInfo.pkAwardsVal }}</div>
          </div>
        </div>
      </div>
    </div>

    <!--! 用户卡片信息 -->
    <div class="awardscard">
      <div class="user-cards" @click="clickTap">
        <div class="user-cards-left">
          <div class="progress-wrapper">
            <div class="progress-wrapper__label">昨日业绩</div>
            <div
              :class="
                awards.aNewPv != 0 && awards.bNewPv != 0
                  ? 'schedule1'
                  : 'schedule'
              "
            >
              <div
                class="current-schedule"
                :style="{
                  width: yesPercent(awards.aNewPv, awards.bNewPv),
                }"
              ></div>
              <div class="cha">
                左区 {{ awards.aNewPv }}万/右区 {{ awards.bNewPv }}万
              </div>
            </div>
          </div>
          <div class="progress-wrapper">
            <div class="progress-wrapper__label">当月业绩</div>
            <div
              :class="
                awards.aMonthPv != 0 && awards.bMonthPv != 0
                  ? 'schedule1'
                  : 'schedule'
              "
            >
              <div
                class="current-schedule"
                :style="{
                  width: yesPercent(awards.aMonthPv, awards.bMonthPv),
                }"
              ></div>
              <div class="cha">
                左区 {{ awards.aMonthPv }}万/右区 {{ awards.bMonthPv }}万
              </div>
            </div>
          </div>
          <div class="progress-wrapper">
            <div class="progress-wrapper__label">历史业绩</div>
            <div
              :class="
                awards.aSumPv != 0 && awards.bSumPv != 0
                  ? 'schedule1'
                  : 'schedule'
              "
            >
              <div
                class="current-schedule"
                :style="{
                  width: yesPercent(awards.aSumPv, awards.bSumPv),
                }"
              ></div>
              <div class="cha">
                左区 {{ awards.aSumPv }}万/右区 {{ awards.bSumPv }}万
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 会员有效!-->
    <div
      class="create-time"
      v-if="userInfo.pkCountry == 1 && userInfo.expireDate"
    >
      有效期：{{ userInfo.expireDate }}
    </div>
    <!-- 注册时间!-->
    <div class="create-time">注册时间：{{ userInfo.registerTime }}</div>
    <div class="countDown">
      <div class="divs">{{ userDay }}</div>
      <div class="margin-s">天</div>
      <div class="divs">{{ userHr }}</div>
      <div class="margin-s">时</div>
      <div class="divs">{{ userMin }}</div>
      <div class="margin-s">分</div>
      <div class="divs">{{ userSec }}</div>
      <div class="margin-s">秒</div>
    </div>
  </div>
</template>

<script>
import * as sid from "@/api/sidebaruserinfo.js";
import { mapGetters } from "vuex";
import user from "@/store/modules/user";
export default {
  name: "sidebarUserInfo",
  computed: {
    user() {
      return user;
    },
    ...mapGetters(["userInfo"]),
  },
  data() {
    return {
      countTime: 11183423,
      userDay: 0,
      userHr: 0,
      awards: {},
      userMin: 0,
      userSec: 0,
      _interval: "",
      toLiveBtn: "",
      isHandImg: true,
    };
  },
  created() {
    this.countdown();
    this.getUserAwards();
  },

  beforeDestroy() {
    clearInterval(this._interval);
  },
  methods: {
    clickTap() {
      this.$router.push({
        path: "/personal",
        query: {
          id: 5,
        },
      });
    },

    defaultSrc() {
      this.isHandImg = false;
    },
    yesPercent(left, right) {
      left = parseFloat(left);
      right = parseFloat(right);
      if (isNaN(left) || isNaN(right)) {
        return "-";
      }
      return right + left == 0
        ? "0%"
        : Math.round((left / (right + left)) * 10000) / 100.0 + "%";
    },

    // 倒计时事件
    countdown() {
      const that = this;
      that._interval = setInterval(() => {
        let time = new Date(this.userInfo.registerTime).getTime() / 1000;
        let time1 = new Date().getTime() / 1000;
        let time2 = parseInt(time1 - time);
        that.countTime = time2;
        that.countTime++;
        let day = parseInt(that.countTime / 60 / 60 / 24);
        let hr = parseInt((that.countTime / 60 / 60) % 24);
        let min = parseInt((that.countTime / 60) % 60);
        let sec = parseInt(that.countTime % 60);
        day = day > 9 ? day : "0" + day;
        hr = hr > 9 ? hr : "0" + hr;
        min = min > 9 ? min : "0" + min;
        sec = sec > 9 ? sec : "0" + sec;
        // that.toLiveBtn = `${day}天${hr}时${min}分${sec}秒`
        that.userDay = day;
        that.userHr = hr;
        that.userMin = min;
        that.userSec = sec;
      }, 1000);
    },

    //获取用户真实奖衔
    getUserAwards() {
      sid.getUserAwards().then((res) => {
        this.awards = res.data;
      });
    },
    goRouter(index) {
      //1等级2奖衔
      this.$router.push({
        path: "/roadtoGrowth",
        query: { index: index },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.avatar {
  img {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    margin-top: 10px;
    padding: 0 15px;
  }
}
.user-name {
  color: #333;
  font-size: 18px;
  //width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

::v-deep .el-input--suffix .el-input__inner {
  background: #fff !important;
}
::v-deep .el-input--suffix .el-input__inner {
  background: #fff !important;
}
.seamless-warp {
  //height: 196px;
  overflow: hidden;
  line-height: 14px;
  text-align: left;
  font-size: 12px;
  .item {
    margin: 0;
    text-align: center;
    margin-left: -40px;
    li {
      list-style: none;
    }
    .title {
      font-size: 12px;
      line-height: 20px;
    }
  }
}

.sidebar-container {
  width: 380px;
  padding: 20px 0;
  background: #ffffff;
  box-shadow: 5px 5px 20px 0px rgba(233, 233, 233, 0.5);
  border-radius: 10px 10px 10px 10px;
  opacity: 1;
  margin-right: 20px;
  .sidebarTop {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    .sidebarHeader {
      img {
        width: 80px;
        height: 80px;
        border-radius: 50%;
      }
    }
    .nameCountry {
      margin-top: 20px;
      display: flex;
      align-content: center;
      align-items: center;
      .username {
        font-size: 22px;
        margin-right: 12px;
      }
      img {
        width: 39px;
        height: 26px;
      }
    }
    .gradeClass {
      display: flex;
      width: 200px;
      margin-top: 20px;
      align-items: center;
      justify-content: space-around;
      img {
        width: 55px;
        //height: 45px;
        cursor: pointer;
      }
    }
  }
  .create-time {
    margin: 10px auto 10px auto;
    color: #666;
    font-size: 14px;
    text-align: center;
  }
  .countDown {
    display: flex;
    padding: 0 40px;
    line-height: 40px;
    font-size: 14px;
    color: #666;
    .margin-s {
      margin: 0 10px;
    }
    .divs {
      padding: 0 10px;
      //width: 40px;
      height: 40px;
      border-radius: 8px 8px 8px 8px;
      opacity: 1;
      border: 1px solid #979797;
      color: #c8161d;
      font-size: 18px;
      text-align: center;
      line-height: 40px;
    }
  }

  .awardscard {
    width: 346px;
    margin: 0 auto;
    margin-top: 10px;
    background: url("~@/assets/images/id-card-bg.png") no-repeat;
    background-size: 100% 100%;
    //background: linear-gradient(180deg, #f8d869 0%, #f48b1f 100%);
    border-radius: 10px;
    position: relative;
    .user-cards {
      cursor: pointer;

      padding: 16px 10px;
      // position: absolute;
      z-index: 1;
      // display: flex;
      .user-card-bg {
        position: absolute;
        right: 0;
        top: 0;
        img {
          width: 145px;
          height: 145px;
        }
      }
      .user-cards-left {
        .schedule {
          // width: 149px;
          flex: 1;
          height: 16px;
          background: rgba(255, 255, 255, 0.3);
          border-radius: 10px 10px 10px 10px;
          margin: 5px;
          position: relative;
        }
        .schedule1 {
          // width: 149px;
          flex: 1;
          height: 16px;
          background: #fff;
          border-radius: 10px 10px 10px 10px;
          margin: 5px;
          position: relative;
        }
        .current-schedule {
          position: absolute;
          width: 30%;
          background: #fea43c;
          height: 16px;
          border-radius: 10px 10px 10px 10px;
        }
        .progress-wrapper {
          display: flex;
          align-items: center;
          margin-top: 5px;
        }
        .progress-wrapper:first-child {
          margin-top: 0px;
        }
        .progress-wrapper__label {
          margin-right: 10px;
          font-size: 12px;
          color: #fff;
        }
        .cha {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          font-size: 12px;
          white-space: nowrap;
        }
        .state {
          font-size: 10px;
          color: #fff;
          margin: 10px 0 0 0px;
          span {
            font-size: 14px;
            font-weight: 600;
          }
        }
      }
    }
  }
}

.honor_wrapper {
  width: 100%;
  .honor1 {
    width: 100%;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 10px 0 0px 0;

    .honor-title-wrapper {
      flex: 1;
    }
    .honor-title {
      font-weight: 600;
      font-size: 18px;
    }
  }
}
</style>
