<template>
  <div class="usercenter-container">
    <div class="usercontent w">
      <div class="leftlist">
        <div class="userbaseinfo">
          <ul>
            <li
              @click="pushto('/userinfo/usercenter/townsman')"
              :class="{ tagactive: activelist === 1 }"
            >
              <span><i class="iconfont icon-laoxiang"></i>我的老乡</span
              ><i class="iconfont icon-youjiantou"></i>
            </li>
            <li
              @click="pushto('/userinfo/usercenter/mybill')"
              :class="{ tagactive: activelist === 2 }"
            >
              <span><i class="iconfont icon-gongdan"></i>我的工单</span
              ><i class="iconfont icon-youjiantou"></i>
            </li>
          </ul>
        </div>
        <div class="priceinfo">
          <ul>
            <li
              @click="pushto('/userinfo/usercenter/myintegral')"
              :class="{ tagactive: activelist === 3 }"
            >
              <span><i class="iconfont icon-jifen"></i>我的积分</span
              ><span
                ><span>{{ userpoint >= 0 ? userpoint : "--" }}分</span
                ><i class="iconfont icon-youjiantou"></i
              ></span>
            </li>
            <li
              @click="pushto('/userinfo/usercenter/mycard')"
              :class="{ tagactive: activelist === 4 }"
            >
              <span><i class="iconfont icon-kaquan"></i>我的卡券</span
              ><span
                ><span
                  >{{
                    cardinfo.recordCount ? cardinfo.recordCount : "0"
                  }}张</span
                ><i class="iconfont icon-youjiantou"></i
              ></span>
            </li>
            <li
              @click="pushto('/userinfo/usercenter/mybalance')"
              :class="{ tagactive: activelist === 5 }"
            >
              <span><i class="iconfont icon-yue"></i>我的余额</span
              ><span
                ><span>{{ userbalance >= 0 ? userbalance : "0" }}元</span>
                <i class="iconfont icon-youjiantou"></i
              ></span>
            </li>
          </ul>
        </div>
      </div>
      <div class="rightcontent">
        <router-view
          :userpoint="userpoint"
          :userbalance="userbalance"
        ></router-view>
      </div>
    </div>
  </div>
</template>

<script>
import {
  userBalanceApi,
  getPointApi,
  userCardApi,
} from "../../../api/userinfo";
export default {
  // 组件名称
  name: "usercenter-container",
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data() {
    return {
      activelist: "",
      userbalance: "",
      userpoint: "",
      cardinfo: {},
      // path:,
    };
  },
  // 计算属性
  computed: {
    path() {
      return this.$route.fullPath;
    },
  },
  // 侦听器
  watch: {
    path() {
      this.listactive();
    },
  },
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
   */
  created() {
    this.listactive();
    this.getUserBalanceInfo();
    this.getPoint();
    this.getcardcount();
  },
  /**
   * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
   * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
   */
  mounted() {},
  // 组件方法
  methods: {
    pushto(url) {
      this.$router.push(url);
    },
    listactive() {
      console.log(this.path);
      console.log(1);
      if (this.path.indexOf("/townsman") > -1) {
        this.activelist = 1;
      } else if (this.path.indexOf("/mybill") > -1) {
        this.activelist = 2;
      } else if (this.path.indexOf("/myintegral") > -1) {
        this.activelist = 3;
      } else if (this.path.indexOf("/mycard") > -1) {
        this.activelist = 4;
      } else if (this.path.indexOf("/mybalance") > -1) {
        this.activelist = 5;
      }
    },
    async getUserBalanceInfo() {
      try {
        const { data } = await userBalanceApi();
        this.userbalance = data.data.balance / 100;

        console.log(data);
        console.log(this.userbalance);
      } catch (error) {
        console.log(error);
      }
    },
    async getPoint() {
      try {
        const { data } = await getPointApi();
        console.log(data);
        this.userpoint = data.data.pointTotal;
      } catch (error) {
        console.log(error);
      }
    },
    async getcardcount() {
      try {
        const { data } = await userCardApi();
        console.log(data);
        this.cardinfo = data.data;
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style scoped lang="less">
.usercontent {
  // height: 1000px;
  margin-top: 16px;
  // background-color: pink;
  display: flex;
  .leftlist {
    width: 346px;
    // height: 500px;
    // background-color: skyblue;
    margin-right: 16px;
    .userbaseinfo {
      ul {
        // padding-left: 50px;
        background-color: #fff;
        border-radius: 4px;
        overflow: hidden;
        li {
          position: relative;
          display: flex;
          padding: 16px;
          line-height: 24px;
          justify-content: space-between;
          background-color: #fff;
          // border-bottom: 1px solid #f3f4f4;
          cursor: pointer;
          &:nth-child(1):after {
            position: absolute;
            content: "";
            display: inline-block;
            height: 1px;
            width: 314px;
            bottom: -1px;
            left: 16px;
            background-color: #f3f4f4;
          }
          span {
            font-size: 16px;
            i {
              margin-right: 12px;
              font-size: 20px;
            }
          }
          > i {
            color: #888;
            font-size: 22px;
          }
        }
      }
    }
    .priceinfo {
      ul {
        margin-top: 16px;
        // padding: 0 16px;
        background-color: #fff;
        border-radius: 4px;
        overflow: hidden;
        li {
          position: relative;
          display: flex;
          padding: 16px 16px 16px 16px;

          line-height: 24px;
          justify-content: space-between;
          // border-bottom: 1px solid #f3f4f4;
          cursor: pointer;
          &:nth-child(-n + 2):after {
            position: absolute;
            content: "";
            display: inline-block;
            height: 1px;
            width: 314px;
            bottom: -1px;
            left: 16px;
            background-color: #f3f4f4;
          }
          span:first-of-type {
            font-size: 16px;
            i:first-of-type {
              margin-right: 12px;
              font-size: 20px;
              vertical-align: middle;
            }
          }
          span:last-of-type {
            span {
              color: #999999;
              font-size: 14px;
            }
            i {
              color: #888;
              font-size: 22px;
              vertical-align: middle;
            }
          }
        }
      }
    }
  }
  .rightcontent {
    width: 838px;
    min-height: 600px;
    // height: 600px;
    padding: 0 16px;
    background-color: #fff;
  }
}
</style>
