<template>
  <layout-page>
		<view class="box defaultBgc">
      <scroll-view class="scrollBox" scroll-y="true">
        <view class="content">
          <!-- 钱包 -->
          <view class="wallet">
            <view class="wallet-content">
              <view class="wallet-content-member">
                <view class="iconBox">
                  <view class="iconBox-line">
                    <uni-icons type="vip-filled" size="20" color="#ffbc00"></uni-icons>
                  </view>
                </view>
                <view class="name">
                  <text v-if="useUserStore.isLogin" @tap="toInfo" class="active">
                    {{ useUserStore?.baseInfo?.name || useUserStore?.userInfo?.phone }}
                  </text>
                  <text v-else @tap="handleLogin">点击登录</text>
                </view>
                </view>
              <view class="wallet-content-integral wallet-content-item" @tap="handleTo('integral')">
                <text>{{ balance.point || 0 }}</text>
                <text class="title">积分</text>
              </view>
              <view class="wallet-content-cash wallet-content-item" @tap="handleTo('balance')">
                <text>{{ balance.balance > 0 ? (balance?.balance / 100.0).toFixed(2) : 0 }}</text>
                <text class="title">余额</text>
              </view>
            </view>
          </view>
          <!-- 签到 -->
          <!-- <view class="signIn">
            <view class="signIn-content">
              <view class="signIn-content-items">
                <view class="item" v-for="(sign, i) in SignItems" :key="sign.day">
                  <view class="box" :class="{ active: i < continuousDay }">
                    <uni-icons type="vip" size="30" color="#ffbc00" style="text-align: center;"></uni-icons>
                    <text class="text">+{{ sign.point }}</text>
                  </view>
                  <view class="day">
                    <text>{{ i === todayNumber ? '今' : '第' + sign.day }}天</text>
                  </view>
                </view>
              </view>
              <view class="tips">
                <button
                  :disabled="todaySignIn"
                  :class="{ disabled: todaySignIn }"
                  @tap="handleSignIn"
                >
                  {{ todaySignIn ? '已签到' : '签到'}}
                </button>
              </view>
            </view>
          </view> -->
          <!-- 积分规则、积分兑换 -->
          <view class="ss-m-t-20">
            <uni-section title="积分规则" type="line"  style="background-color: #fff;">
              <uni-list>
                <uni-list-item v-for="(k, i) in integralRules" :key="i" :showArrow="false">
                  <template v-slot:header>
                    <text class="ss-m-t-10">{{ k.title }}</text>
                  </template>
                  <template v-slot:footer>
                    <view class="d-flex align-center" style="width: 130px; text-align: start; position: relative;">
                      <image src="/static/svg/integral.svg" style="width: 30px; height: 30px;"></image>
                      <view class="color-primary ss-m-l-10">{{ k.point }}</view>
                      <uni-tag :inverted="true" :text="k.complete ? '已完成' : '未完成'" :type="k.complete ? 'success' : 'error'" style="position: absolute; right: 0;" />
                    </view>
                  </template>
                </uni-list-item>
              </uni-list>
            </uni-section>
            <view class="ss-m-t-20">
              <uni-section title="积分兑换" type="line" style="background-color: #fff;">
                <view class="goods" >
                  <view class="goods-item" v-for="(item,index) in goodsList" :key="index" @tap.stop="handleClickGoods">
                    <image :src="item.url" class="goods-item-img" mode="widthFix"></image>
                    <view class="goods-item-name">{{ item.name }}</view>
                    <view>消耗积分<text class="goods-item-price">{{ item.point }}</text></view>
                  </view>
                </view>
              </uni-section>
            </view>
          </view>
        </view>
      </scroll-view>
		</view>

    <uni-popup ref="inputDialog" type="dialog">
      <view class="shareQrCodePopupContent">
        <view class="ss-m-b-10">请前往网页版门墩儿商城兑换</view>
        <uni-link href="https://www.menduner.com/mall" text="点击复制网页地址" color="#00897B" fontSize="16" copyTips="已复制，请在电脑端打开"></uni-link>
      </view>
    </uni-popup>
	</layout-page>
</template>

<script setup>
import { ref, watch } from 'vue'
import layoutPage from '@/layout'
import {
	// getRewardSignInConfigList,
	// getRewardSignInRecordSummary,
  // createRewardSignInRecord,
  getAccountBalance,
  getUserAccount,
} from '@/api/sign'
import { getTaskList } from '@/api/integral'
import { onShow, onLoad, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { userStore } from '@/store/user'
import { showAuthModal } from '@/hooks/useModal'
import { showNecessaryInfoPopup } from '@/utils/request'

const inputDialog = ref()
const useUserStore = userStore()
// 设置自定义tabBar选中值

// const SignItems = ref([])
// 积分规则
const integralRules = ref([])
const getTask = async () => {
  const { data } = await getTaskList({ mark: '推荐任务', type: 0 })
  integralRules.value = data
}
// 商品列表
const goodsList = [
  { name: '房券-高端酒店房券', point: 12000, url: 'https://minio.menduner.com/dev/menduner/hotalRoomVoucher.png' },
  { name: '门墩儿酒店英语学习年卡', point: 8000, url: 'https://minio.menduner.com/dev/menduner/englishCourses.jpg' },
  { name: '红酒-经典年份葡萄酒', point: 5000, url: 'https://minio.menduner.com/dev/menduner/redWine.png' },
  { name: '瑞幸咖啡券-瑞幸咖啡精致享受券', point: 2000, url: 'https://minio.menduner.com/dev/menduner/coffee.png' },
  { name: '减压捏捏乐', point: 500, url: 'https://minio.menduner.com/dev/menduner/pinchMusic.png' }
]

onLoad(() => {
  wx.showShareMenu({
    withShareTicket: true,
    menus: ['shareAppMessage', 'shareTimeline']
  })
  onShareAppMessage(() => {
    return {
      title: '门墩儿 专注顶尖招聘',
      path: '/pages/index/position',
      imageUrl: 'https://minio.menduner.com/dev/menduner/miniProgram/share-poster.jpg'
    }
  })
  onShareTimeline(() => {
    return {
      title: '门墩儿 专注顶尖招聘',
      path: '/pages/index/position',
      imageUrl: 'https://minio.menduner.com/dev/menduner/miniProgram/share-poster.jpg'
    }
  })
})

const handleClickGoods = () => {
  // uni.showToast({
  //   icon: 'none',
  //   title: '请前往网页版门墩儿商城兑换'
  // })
  inputDialog.value.open()
}

// 连续签到天数
// const continuousDay = ref(0)
// 今天有无签到
// const todaySignIn = ref(false)

// 今天
// const todayNumber = ref()

const balance = ref({})

watch([() => useUserStore.refreshToken, () => useUserStore.isLogin], () => {
  if (useUserStore.isLogin) {
    // getSummary()
    getBalance()
  }
})

onShow(() => {
  const currentPage = getCurrentPages()[0];  // 获取当前页面实例
  const currentTabBar = currentPage?.getTabBar?.();
  // 设置当前tab页的下标index
  currentTabBar?.setData({ selected: 2 });
  init()
})

function init () {
  // 获取签到列表
  // getSignIn()
  // 获取签到信息
  // getSummary()
  // 获取余额积分
  getBalance()
  // 获取积分规则列表
  getTask()
}

// 获取积分余额
async function getBalance() {
  const { data } = await getAccountBalance()
  const { data: _data } = await getUserAccount()
  balance.value = {
    ...data,
    point: _data?.point
  }
}


// 获取个人签到统计
// async function getSummary() {
//   const { data } = await getRewardSignInRecordSummary()
//   if (!data) return
//   continuousDay.value = data.continuousDay // 连续签到第n天
//   todaySignIn.value = data.todaySignIn // 今天有无签到
//   todayNumber.value = todaySignIn.value ? continuousDay.value - 1 : continuousDay.value
// }
// 获取签到列表
// async function getSignIn () {
//   try {
//     const { data } = await getRewardSignInConfigList()
//     SignItems.value = data
//   } catch (error) {
    
//   }
// }
// 签到
// async function handleSignIn () {
//   uni.showLoading({
//     title: '正在签到'
//   })
//   try {
//     const { code } = await createRewardSignInRecord()
//     if (code !== 0) {
//       return
//     }
//     setTimeout(async () => {
//       const { code: _code } = await getSummary()
//       if (_code !== 0) {
//         return
//       }
//       uni.showToast({
//         title: '签到成功',
//         icon: 'success',
//         mask: true
//       })
//       // 更新积分
//       getBalance()
//     }, 1000)
//   } catch (error) {
    
//   } finally {
//     uni.hideLoading()
//   }
// }

function handleLogin () {
  if (!useUserStore.isLogin) {
		showAuthModal()
		return
	}
}

function toInfo () {
  uni.navigateTo({
		url: '/pagesA/info/index'
	})
}

function handleTo (page) {
  if (!useUserStore.isLogin) {
		showAuthModal()
		return
  }
  if (showNecessaryInfoPopup()) {
		uni.showToast({
			title: '请先完善基本信息',
			icon: 'none'
		})
		showAuthModal('necessaryInfo')
		return
	}
  uni.navigateTo({
    url: `/pagesA/${page}/index`
  })
}
</script>

<style scoped lang="scss">
.shareQrCodePopupContent {
	width: 75vw;
	padding: 40rpx;
	margin-bottom: 20rpx;
	text-align: center;
	background-color: #fff;
}
.box {
  overflow: hidden;
  height: 100vh;
  padding: 20rpx 0 0 0;
  box-sizing: border-box;
  .scrollBox{
    height: 100%;
    
    padding-bottom: 120rpx;
    box-sizing: border-box;
  }
  .content {
    padding-bottom: 24rpx;
    box-sizing: border-box;
  }
  .wallet {
    box-sizing: border-box;
    height: 200rpx;
    margin-bottom: 20rpx;
    &-content {
      display: flex;
      height: 100%;
      border-radius: 10rpx;
      background: #FFF;
      
      &-member {
        width: 50%;
        display: flex;
        align-items: center;
        position: relative;
        &:after {
          content: '';
          position: absolute;
          width: 4rpx;
          height: 30%;
          right: 0;
          top: 35%;
          background: #00897B;
        }
        .iconBox {
          width: 100rpx;
          height: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          &-line {
            border: 4rpx solid #ffbc00;
            border-radius: 180rpx;
            padding: 10rpx;
          }
        }
        .name {
          flex: 1;
          .actvie {
            color: #ffbc00;
          }
          
        }
      }
      &-item {
        width: 25%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        .title {
          font-size: .85em;
        }
      }
      &-integral {
      }
      &-cash {
        
      }
    }
  }
  .signIn {
    // padding: 0 20rpx;
    box-sizing: border-box;
    &-content {
      border-radius: 10rpx;
      background: #FFF;
      &-items {
        padding: 20rpx 20rpx;
        display: flex;
        height: 220rpx;
        box-sizing: border-box;
        .item {
          height: 140rpx;
          width: 100%;
          align-items: center;
          justify-content: center;
          padding: 0 10rpx;
          .box {
            height: 100%;
            width: 100%;
            display: flex;
            flex-direction: column;
            background: #f2f4f7;
            border-radius: 10rpx;
            &.active {
              color: #fff;
              background: rgba(16,137,123, .66);
            }
            .text {
              font-size: .75em;
              text-align: center;
            }
          }
          .day {
            margin-top: 10rpx;
            height: 60rpx;
            text-align: center;
            font-size: .5em;
          }
        }
      }
      .tips {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        height: 80rpx;
        font-size: .75em;
        color: #999;
        &-light {
          color: #00897B;
          padding: 0 10rpx;
        }
        button {
          width: 180rpx;
          height: 60rpx;
          font-size: 24rpx;
          margin: 0 10rpx;
          border: unset;
          background-color: #00897B;
          color: #FFF;
          &.disabled {
            // background-color: #a7a7a7 !important;
            opacity: .5;
          }
        }
      }
      
    }
  }
  .goods {
    padding: 0 15upx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
    &-item {
      background: #FFFFFF;
      width: 48%;
      margin: 10upx 0;
      box-sizing: border-box;
      &-img {
        width: 100%;
        height: 250upx;
        display: block;
        margin: auto;
      }
      &-price {
        padding-top: 10upx;
        color: #00897B;
        font-size: 32upx;
      }
      &-name {
        width: 100%;
        white-space: nowrap;
        font-size: 28upx;
        line-height: 50upx;
        padding-bottom: 10upx;
        padding-top: 10upx;
        overflow:hidden; 
        text-overflow:ellipsis;
        -webkit-box-orient:vertical;
        -webkit-line-clamp:2; 
      }
    }
  }
}
:deep(.uni-section .uni-section-header__decoration) {
  background-color: #00897B !important;
}
</style>
