
import { computed } from 'vue'
import { useUserStore } from "@/stores/modules/user";
import { useBaseStore } from "@/stores/modules/base";

interface LevelProps {
  amount: number
  lv: number
}

export default function () {
  const baseStore = useBaseStore();
  const userStore = useUserStore();
  /**当前vip等级对应奖励 */
  const levelProp = computed(() => {
    let prop = userStore.vipInfo.rows && userStore.vipInfo.rows.filter((item: any) => item.lv === userStore.vipInfo.myLv)
    return prop && prop.length > 0 ? prop[0] : {
      dailyAward: [],
      award: [],
      monthlyAward: [],
      weeklyAward: [],
    }
  })
  /**当前vip等级 */
  const currentLevel = computed(() => {
    let level: LevelProps = { amount: 0, lv: 1 };
    if (baseStore.globalConfig.platformConfig) {
      console.log(userStore.userInfo.vipLevelType === 1 ? userStore.userInfo.totalPay : userStore.userInfo.totalBet);
      console.log(baseStore.globalConfig.platformConfig.vip_config);


      let v = baseStore.globalConfig.platformConfig.vip_config.filter(
        (i: any) => i.amount <= (userStore.userInfo.vipLevelType === 1 ? userStore.userInfo.totalPay : (userStore.userInfo.totalBet || 0))
      );
      if (v.length) {
        level = v[0];
      }
    }
    return level;
  })
  /**下级vip等级 */
  const nextLevel = computed(() => {
    let level: LevelProps = { amount: 0, lv: 2 };
    if (!currentLevel.value.lv) {
      level.lv = 1
      return level
    }

    if (baseStore.globalConfig.platformConfig) {
      let v = baseStore.globalConfig.platformConfig.vip_config.filter(
        (i: any) => i.amount > (userStore.userInfo.vipLevelType === 1 ? userStore.userInfo.totalPay : userStore.userInfo.totalBet || 0)
      );
      console.log('====================================');
      console.log(v);
      console.log('====================================');
      if (v.length) {
        level = v[v.length - 1];
      } else {
        level = baseStore.globalConfig.platformConfig.vip_config[0]
      }
    }
    return level;
  })
  /**到达下级vip等级进度 */
  const levelSpeed = computed(() => {
    const current = userStore.userInfo.vipLevelType === 1 ? userStore.userInfo.totalPay : userStore.userInfo.totalBet || 0
    const next = nextLevel.value.amount
    if (!(current / next)) {
      return
    }
    return ((current / next) * 100) > 100 ? '100%' : ((current / next) * 100).toFixed(2) + '%';
  })
  /**差多少充值下一级 */
  const poorRecharge = computed(() => {
    const current = userStore.userInfo.vipLevelType === 1 ? userStore.userInfo.totalPay : userStore.userInfo.totalBet || 0
    const next = nextLevel.value.amount
    if (!current && baseStore.globalConfig.platformConfig) {
      let v0 = baseStore.globalConfig.platformConfig.vip_config.find((item: any) => item.lv == 1)
      let v1 = baseStore.globalConfig.platformConfig.vip_config.find((item: any) => item.lv == 2)
      return v1.amount - v0.amount;
    }
    console.log(current,next);
    
    return current > next ? 0 : next - current;
  })

  return {
    currentLevel,
    nextLevel,
    levelSpeed,
    poorRecharge,
    levelProp
  }
}