<script setup>
  import { useAuthUser } from '@/composables/useAuthUser'
  import { onLoad, onShow } from '@dcloudio/uni-app'
  import { navigateTo } from '@/utils/router'
  import { useUI } from '@/utils/use-ui'
  import { formatNumberWithWan } from '../../utils'
  import { getUserPayPwd } from '@/api/my-index'
  import { useUserStore } from '../../stores/user'
  import { ref } from 'vue'

  const bottomList = [
    {
      name: '我的钱包',
      icon: 'wallet',
      url: '/pages/my-index/wallet/index'
    },
    {
      name: '邀请好友',
      icon: 'user-code',
      url: '/pages/my-index/wallet/invite'
    },
    {
      name: '我的拼团伙伴',
      icon: 'team',
      url: '/pages/my-index/my-team'
    },
    // { name: '群创建直播', icon: 'videocam', url: '' },
    {
      name: '直播记录',
      icon: 'meeting',
      url: '/pages/my-index/meeting-record/index'
    },
    {
      name: '我的朋友圈',
      icon: 'circle',
      url: '/pages/discover/dynamic/dynamic'
    },
    // {
    //   name: '我的收藏',
    //   icon: 'collection',
    //   url: '/pages/my-index/collection/index'
    // },
    {
      name: '在线客服',
      icon: 'customer',
      url: '/pages/my-index/customer-service/index'
    },
    {
      name: '系统设置',
      icon: 'system',
      url: '/pages/my-index/set-up/index'
    }
  ]

  const { getIntegral, refreshUserInfo } = useUserStore()
  const { showDialog } = useUI()
  const { userInfo, integralData } = useAuthUser()

  /** 点击列表跳转 */
  const onGo = async item => {
    if (item.url === '/pages/my-index/wallet/index') {
      const res = await getUserPayPwd()
      if (res?.data) {
        navigateTo(item.url)
      } else {
        const show = await showDialog('提示', '请先设置支付密码')
        if (show) {
          navigateTo('/pages/my-index/wallet/edit-password', { type: 0 })
        }
      }
    } else {
      navigateTo(item.url, item.icon === 'circle' ? { type: 1 } : null)
    }
  }

  const paging = ref(null)
  const onRefresh = async () => {
    await refreshUserInfo()
    paging.value.complete()
  }

  onLoad(() => {
    // 获取用户信息
    console.log(userInfo.value, '===获取用户信息')
  })
  onShow(() => {
    getIntegral(false)
  })
</script>

<template>
  <z-paging
    ref="paging"
    :paging-style="{ 'background-color': '#f9f9f9' }"
    refresher-only
    @onRefresh="onRefresh"
  >
    <view class="my-index">
      <view
        class="top-info"
        @click="navigateTo('/pages/my-index/personal-center/index')"
      >
        <view class="left-box">
          <image
            v-if="userInfo?.avatar"
            :src="userInfo?.avatar"
            mode="aspectFill"
            class="avatar"
          ></image>
          <uni-icons v-else type="contact-filled" size="70"></uni-icons>
          <view class="nickname">
            <text class="name">{{ userInfo?.userName || '' }}</text>
            <text class="name">
              ID: {{ userInfo?.invitationCode || '' }}
            </text>
          </view>
        </view>
        <image
          src="/static/images/public/right-arrow.png"
          mode="heightFix"
          class="right-box"
        ></image>
      </view>
      <!-- 卡片列表 -->
      <view class="card-list">
        <view class="top-box">
          <view class="left-name">
            <text>账户积分</text>
            <text>{{ integralData }}</text>
          </view>
          <view class="right-btn">
            <button
              @click="
                showDialog('提示', '联系客服或者联系上级分享人', false)
              "
            >
              充值
            </button>
            <button @click="navigateTo('/pages/my-index/withdraw')">
              提现
            </button>
          </view>
        </view>
        <!-- 入口列表 -->
        <view
          v-for="(item, index) in bottomList"
          :key="index"
          class="item-box"
          @click="item.url && onGo(item)"
        >
          <view class="item-name">
            <uni-icons
              v-if="item.icon === 'videocam'"
              type="videocam"
              size="64rpx"
              color="#1b38b9"
            ></uni-icons>
            <image
              v-else
              :src="`/static/images/my-index/${item.icon}.png`"
              mode="heightFix"
              class="icon"
            ></image>
            <text class="text-box">{{ item.name }}</text>
          </view>
          <image
            src="/static/images/public/right-arrow.png"
            mode="heightFix"
            class="right-box"
          ></image>
        </view>
      </view>
    </view>
  </z-paging>
</template>

<style lang="scss" scoped>
  page {
    background-color: #f7f7f7;
  }

  .my-index {
    padding: 32rpx 26rpx;
    .right-box {
      height: 32rpx;
    }
    .top-info {
      padding: 32rpx 46rpx;
      background: #ffffff;
      border-radius: 16rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left-box {
        display: flex;
        align-items: center;
        .avatar {
          width: 128rpx;
          height: 128rpx;
          border-radius: 128rpx;
          margin-right: 32rpx;
        }
        .nickname {
          display: flex;
          flex-direction: column;
          .name {
            font-family:
              PingFang SC,
              PingFang SC;
            font-weight: bold;
            font-size: 32rpx;
            color: #333333;
            text-align: left;
            font-style: normal;
            text-transform: none;
            &:last-child {
              font-weight: 500;
              font-size: 24rpx;
              color: #999999;
              margin-top: 16rpx;
            }
          }
        }
      }
    }

    .card-list {
      margin-top: 28rpx;
      background: #ffffff;
      border-radius: 16rpx;
      overflow: hidden;
      .top-box {
        padding: 12rpx 36rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background: linear-gradient(180deg, #00d993 0%, #00d9c5 100%);
        position: relative;
        &::before {
          content: '';
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background-image: url('/static/images/my-index/my-card-bg.png');
          background-size: 30%;
          background-position: center;
          background-repeat: no-repeat;
          z-index: 1;
          pointer-events: none;
        }
        .left-name {
          display: flex;
          flex-direction: column;
          text {
            font-family:
              PingFang SC,
              PingFang SC;
            font-weight: 500;
            font-size: 28rpx;
            color: #ffffff;
            text-align: center;
            font-style: normal;
            text-transform: none;
            &:last-child {
              font-weight: bold;
              font-size: 40rpx;
              margin-top: 10rpx;
            }
          }
        }
        .right-btn {
          display: flex;
          button {
            width: 128rpx;
            height: 64rpx;
            line-height: 64rpx;
            border-radius: 100rpx 100rpx 100rpx 100rpx;
            font-family:
              PingFang SC,
              PingFang SC;
            font-weight: 500;
            font-size: 28rpx;
            color: #ffffff;
            text-align: center;
            font-style: normal;
            text-transform: none;
            background: transparent;
            border: 2rpx solid #ffffff;
            &::after {
              border: none;
            }
            &:last-child {
              margin-left: 16rpx;
              background: #ffffff;
              color: #00d993;
              border: none;
            }
          }
        }
      }
      .item-box {
        padding: 23rpx 32rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .item-name {
          display: flex;
          justify-content: space-between;
          align-items: center;
          .icon {
            height: 64rpx;
          }
          .text-box {
            margin-left: 16rpx;
            font-family:
              PingFang SC,
              PingFang SC;
            font-weight: 500;
            font-size: 32rpx;
            color: #333333;
            font-style: normal;
            text-transform: none;
          }
        }
      }
    }
  }
</style>
