<template>
  <div class="Me">
    <SlideRowList style="width: 100vw;"
                  @first="first"
                  @end="end"
                  v-model:active-index="baseActiveIndex"
    >
      <SlideItem style="overflow:auto;" :style="contentStyle" @scroll="scroll" @click="click">
        <div ref="desc" class="desc">
          <header ref="header">
            <div class="left">
              <img src="../../assets/img/icon/next.svg" alt="">
              <span>切换账号</span>
            </div>
            <div class="right">
              <img src="../../assets/img/icon/more.svg" alt="">
              <img src="../../assets/img/icon/more.svg" alt="" @click.stop="baseActiveIndex = 1">
            </div>
          </header>
          <div class="detail">
            <div class="head">
              <img src="../../assets/img/icon/head-image.jpeg" class="head-image">
              <div class="other-buttons" v-if="false">
                <div class="attention">
                  关注
                </div>
                <div class="more-attention">
                  -
                </div>
              </div>
              <div class="my-buttons ">
                <div class="collect" @click="$nav('/edit-userinfo')">
                  <span class="mr5p">编辑资料</span>
                  <span class="f10p" style="color: darkgray">85%</span>
                </div>
                <div class="add-friend" @click="$nav('/find-acquaintance')">
                  <img src="../../assets/img/icon/add.svg" alt="">
                  <span class="mr5p">熟人</span>
                </div>
              </div>
            </div>
            <div class="description">
              <p class="name f22 mt5p mb5p">ttentau</p>
              <div class="number mb10p">
                <span class="mr5p">私密账号</span>
                <span>抖音号：605128307</span>
                <img src="../../assets/img/icon/qr_code.svg" alt="" @click.stop="$nav('/MyCard')">
              </div>
              <div class="signature f12 mb10p">
                <span>填写个性签名更容易获得别人关注哦</span>
              </div>
              <div class="heat">
                <div class="text"><span class="num">8</span>获赞</div>
                <div class="text"><span class="num">8</span>关注</div>
                <div class="text"><span class="num">8</span>粉丝</div>
              </div>
            </div>
          </div>
        </div>
        <div ref="content" style="margin-bottom: 60px;">
          <Indicator
              name="videoList"
              :fixed="indicatorFixed"
              tabStyleWidth="33%"
              :tabTexts="['作品','私密','喜欢']"
              v-model:active-index="contentIndex">
          </Indicator>
          <SlideRowList
              name="videoList"
              @end="end"
              :indicator-fixed="indicatorFixed"
              v-model:active-index="contentIndex">
            <SlideItem>
              <div ref="tab-content1">
                <Posters></Posters>
                <div class="no-more">暂时没有更多了</div>
              </div>
            </SlideItem>
            <SlideItem>
              <div ref="tab-content2">
                <Posters :list="15"></Posters>
                <div class="no-more">暂时没有更多了</div>
              </div>
            </SlideItem>
            <SlideItem>
              <div ref="tab-content3">
                <Posters :list="5"></Posters>
                <div class="no-more">暂时没有更多了</div>
              </div>
            </SlideItem>
          </SlideRowList>
        </div>
        <Footer v-bind:init-tab="5"/>
      </SlideItem>
      <SlideItem style="min-width: 70vw; overflow:auto;">
        <transition name="fade">
          <div class="ul" v-if="!isMoreFunction">
            <div class="li" @click="$nav('MyCard')">
              <img src="../../assets/img/icon/card-gray.png" alt="">
              <span>我的订单</span>
            </div>
            <div class="li" @click="$nav('MyCard')">
              <img src="../../assets/img/icon/wallet-gray.png" alt="">
              <span>钱包</span>
            </div>
            <div class="line"></div>

            <div class="li" @click="$nav('MyCard')">
              <img src="../../assets/img/icon/card-gray.png" alt="">
              <span>我的收藏</span>
            </div>
            <div class="li" @click="$nav('MyCard')">
              <img src="../../assets/img/icon/card-gray.png" alt="">
              <span>观看历史</span>
            </div>
            <div class="li" @click="$nav('MyCard')">
              <img src="../../assets/img/icon/card-gray.png" alt="">
              <span>我的动态</span>
            </div>
            <div class="li" @click="$nav('MyCard')">
              <img src="../../assets/img/icon/card-gray.png" alt="">
              <span>创作者服务中心</span>
            </div>

            <div class="line"></div>

            <div class="li" @click="$nav('MyCard')">
              <img src="../../assets/img/icon/link-gray.png" alt="">
              <span>小程序</span>
            </div>
            <div class="li" @click="$nav('MyCard')">
              <img src="../../assets/img/icon/card-gray.png" alt="">
              <span>抖音公益</span>
            </div>
            <div class="li" @click="$nav('MyCard')">
              <img src="../../assets/img/icon/umbrella-gray.png" alt="">
              <span>未成年保护工具</span>
            </div>
            <div class="li" @click="$nav('MyCard')">
              <img src="../../assets/img/icon/card-gray.png" alt="">
              <span>我的客服</span>
            </div>
            <div class="li" @click="$nav('MyCard')">
              <img src="../../assets/img/icon/setting-gray.png" alt="">
              <span>设置</span>
            </div>
          </div>
          <div v-else class="more-function">
            <div class="title">生活服务</div>
            <div class="functions">
              <div class="function">
                <img src="../../assets/img/icon/juan-gray.png" alt="">
                <span>卡券</span>
              </div>
              <div class="function">
                <img src="../../assets/img/icon/order-gray.png" alt="">
                <span>免流量</span>
              </div>

              <div class="function" @click="$nav('MyCollect')">
                <img src="../../assets/img/icon/collect-gray.png" alt="">
                <span>我的收藏</span>
              </div>
            </div>
            <div class="title">拓展功能</div>
            <div class="functions">
              <div class="function" @click="$nav('MyCard')">
                <img src="../../assets/img/icon/card-gray.png" alt="">
                <span>我的二维码</span>
              </div>
              <div class="function">
                <img src="../../assets/img/icon/order-gray.png" alt="">
                <span>我的缓存</span>
              </div>
              <div class="function" @click="$nav('MyCard')">
                <img src="../../assets/img/icon/card-gray.png" alt="">
                <span>上热门</span>
              </div>
              <div class="function" @click="$nav('MyCollect')">
                <img src="../../assets/img/icon/collect-gray.png" alt="">
                <span>小店随心推</span>
              </div>
              <div class="function" @click="$nav('MyCollect')">
                <img src="../../assets/img/icon/collect-gray.png" alt="">
                <span>投教园地</span>
              </div>
            </div>
          </div>
        </transition>

        <div class="button-ctn">
          <div class="button" v-if="!isMoreFunction" @click="isMoreFunction = true">
            <img src="../../assets/img/icon/card-gray.png" alt="">
            <span>更多功能</span>
          </div>
          <div class="button" v-if="isMoreFunction" @click="isMoreFunction = false">
            <span>返回</span>
          </div>
        </div>
      </SlideItem>
    </SlideRowList>
  </div>
</template>
<script>
import Posters from '../../components/Posters'
import Footer from "../../components/Footer";
import Indicator from '../../components/Indicator'

export default {
  name: "Me",
  components: {Posters, Footer, Indicator},
  data() {
    return {
      serviceEl: {},
      serviceHeight: 0,
      contentIndex: 0,
      baseActiveIndex: 0,
      desc: null,
      tabContents: [],
      indicatorHeight: 42,
      indicatorFixed: false,
      refs: {
        header: null,
        headerHeight: 0,
      },
      isMoreFunction: false,
    }
  },
  watch: {
    contentIndex(newVal, oldVal) {
      this.changeIndex(newVal, oldVal)
    },
  },
  computed: {
    bodyHeight() {
      return this.$store.state.bodyHeight
    },
    contentStyle() {
      return {
        opacity: this.baseActiveIndex === 1 ? 0.5 : 1
      }
    }
  },
  mounted() {
    setTimeout(() => {
      this.refs.header = this.$refs.header
      this.refs.headerHeight = this.$refs.header.offsetHeight
      this.changeIndex(0, null)
    })
  },
  methods: {
    click(e) {
      if (this.baseActiveIndex === 0) return
      if (this.baseActiveIndex === 1) {
        this.baseActiveIndex = 0
        this.$stopPropagation(e)
      }
    },
    scroll() {
      let top = this.tabContents[this.contentIndex].getBoundingClientRect().top
      this.indicatorFixed = top < this.indicatorHeight;
    },
    changeIndex(newVal, oldVal) {
      let desc = this.$refs.desc
      // console.log(desc.clientHeight)
      let content = this.$refs['content']
      if (!this.tabContents.length) {
        this.tabContents = [this.$refs['tab-content1'], this.$refs['tab-content2'], this.$refs['tab-content3']]
      }
      // debugger
      let moveDistance = 0
      if (oldVal !== null) {
        moveDistance = this.bodyHeight - this.tabContents[oldVal].getBoundingClientRect().top
      } else {
        moveDistance = 0
      }
      // console.log('moveDistance', moveDistance)
      if (moveDistance > this.tabContents[newVal].clientHeight) {
        if (moveDistance + this.indicatorHeight > this.bodyHeight) {
          content.style.height = this.bodyHeight + 'px'
        } else {
          content.style.height = moveDistance + this.indicatorHeight + 'px'
        }
      } else {
        if (this.bodyHeight - desc.clientHeight > this.tabContents[this.contentIndex].clientHeight + this.indicatorHeight) {
          content.style.height = this.bodyHeight - desc.clientHeight + this.indicatorHeight + 'px'
        } else {
          return content.style.height = this.tabContents[this.contentIndex].clientHeight + this.indicatorHeight + 'px'
          // if (this.tabContents[this.contentIndex].clientHeight + this.indicatorHeight > this.bodyHeight) {
          //   content.style.height = this.bodyHeight + 'px'
          // } else {
          //   content.style.height = this.tabContents[this.contentIndex].clientHeight + this.indicatorHeight + 'px'
          // }
        }
      }
    },
    first(e) {
      if (this.baseActiveIndex !== 0) return
      if (this.refs.header.getBoundingClientRect().top !== 0) return
      this.refs.header.style.transition = 'all 0s'
      this.refs.header.style.height = this.refs.headerHeight + (e / 3) + 'px'
    },
    end() {
      if (this.baseActiveIndex !== 0) return
      this.refs.header.style.transition = 'all .3s'
      this.refs.header.style.height = this.refs.headerHeight + 'px'
    }
  }
}
</script>

<style scoped lang='scss'>
@import "../../assets/scss/index";

.fade-enter-active,
.fade-leave-active {
  transition: all 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  transform: translateY(10px);
  opacity: 0;
}

.Me {
  background: $main-bg;
  height: 100%;
  width: 100%;

  .no-more {
    font-size: 1.4rem;
    padding: 10px;
    color: gray;
    text-align: center;
  }

  .desc {
    header {
      color: white;
      height: 150px;
      /* background-image: url('./imgs/header2.jpg'); */
      background-image: url('data:image/jpeg;base64, ');
      background-size: cover;
      background-position: center;
      background-repeat: no-repeat;
      display: flex;
      justify-content: space-between;
      /*background: url("../../assets/img/icon/top-bg.jpg");*/
      padding: 20px;
      box-sizing: border-box;

      .left {
        font-size: 1.2rem;
        height: 26px;
        display: flex;
        padding-right: 13px;
        padding-left: 5px;
        align-items: center;
        border-radius: 20px;
        background: rgba(82, 80, 80, 0.5);

        img {
          padding: 4px;
          height: 16px;
          width: 16px;
        }
      }

      .right {
        img {
          margin-left: 20px;
          border-radius: 50%;
          background: rgba(82, 80, 80, 0.5);
          padding: 4px;
          height: 16px;
          width: 16px;
        }
      }
    }

    .detail {
      background: $main-bg;
      padding: 0 20px;

      .head {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        box-sizing: border-box;
        transform: translateY(-20px);

        .head-image {
          background: black;
          padding: 2px;
          border-radius: 50%;
          width: 80px;
          height: 80px;
        }

        .my-buttons {
          div {
            font-size: 1.2rem;
            display: flex;
            align-items: center;
            justify-content: center;
            float: left;
            border-radius: 2px;
            background: $second-btn-color;
            margin-left: 5px;
            padding: 0 10px;
            height: 30px;
            color: white;

            &:nth-child(1) {
              padding: 0 25px;
            }

            img {
              padding: 0 6px;
              height: 16px;
            }
          }

        }
      }

      .description {
        font-size: 1.2rem;
        color: white;
        transform: translateY(-20px);

        .number {
          color: $second-text-color;
          padding-bottom: 10px;
          border-bottom: 1px solid $line-color;
          display: flex;
          align-items: center;

          img {
            width: 18px;
            margin-left: 5px;
          }

        }

        .heat {
          color: darkgray;
          display: flex;
          align-items: center;

          .num {
            color: white;
            font-size: 1.8rem;
            font-weight: bold;
            margin-right: 3px;
          }

          .text {
            font-size: 1.4rem;
            margin-right: 10px;
          }
        }
      }
    }
  }

  .ul {
    font-size: 1.2rem;
    width: 100%;
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0;
    margin: 0;
    position: absolute;
    color: white;
    background: $main-bg;

    .line {
      height: 1px;
      background: #cccccc;
      opacity: .1;
      margin: 5px 20px;
    }

    .li {
      box-sizing: border-box;
      background: $main-bg;
      width: 100%;
      padding: 1rem 2rem;
      list-style: none;
      display: flex;
      align-items: center;
      line-height: 4rem;

      &:nth-last-child(1) {
        margin-bottom: 90px;
      }

      &:active {
        background: #454b65;
      }

      img {
        height: 20px;
        width: 20px;
        margin-right: 15px;
      }
    }
  }

  .more-function {
    box-sizing: border-box;
    font-size: 1.2rem;
    width: 100%;
    position: absolute;
    background: $main-bg;

    .title {
      padding: 20px;
      color: gray;
    }

    .functions {
      display: grid;
      grid-template-columns: 33.33% 33.33%  33.33%;

      color: white;

      .function {
        margin: 15px 0;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        img {
          height: 25px;
          width: 25px;
          margin-bottom: 15px;
        }
      }
    }
  }

  .button-ctn {
    font-size: 1.4rem;
    z-index: 99999;
    width: 70vw;
    position: absolute;
    bottom: 0;
    background: $main-bg;

    .button {
      box-sizing: border-box;
      width: calc(100% - 4rem);
      background: $second-btn-color;
      margin: 2rem;
      height: 4.5rem;
      border-radius: 3px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: white;

      img {
        height: 25px;
        width: 25px;
        margin-right: 15px;
      }
    }
  }

}
</style>
