import { browser, getDevelopment } from '/src/helpers/utils'
import { userInfo } from '../App'
import { Dialog } from 'vant'
import { postMessage } from '/src/helpers/native-message'
import request from '/src/helpers/request'

/** 去会员中心 */
const gotoMemberCenter = () => {
  const isDev = getDevelopment()
  const url = isDev
    ? `https://ponline.colexiu.com/orchestra-student/#/memberCenter`
    : `${location.origin}/orchestra-student/#/memberCenter`
  postMessage({
    api: 'back',
  })
  postMessage({
    api: 'openWebView',
    content: {
      url: url,
      orientation: 1,
    },
  })
}

/**
 * 效验是否开通会员
 */
export const useFee = async () => {
  const browserInfo = browser()
  // 学生端验证
  if (browserInfo.isStudent) {
    let student: any = ''
    try {
      const res: any = await request.get('/student/member')
      student = res?.data
    } catch (error) {}
    if (!student) return;
    //学生有待激活会员
    if (!student.membershipDays && student.purchaseMemberRecord) {
      Dialog.alert({
        title: '温馨提示',
        message: '您的团练宝暂未激活，请激活后使用',
        className: 'noVip',
      }).then((val) => {
        gotoMemberCenter()
      })
      return
    }
    //学生没有会员
    if (!student.vipMember) {
      Dialog.alert({
        title: '温馨提示',
        message: '您暂未开通团练宝，请开通后使用',
        className: 'noVip',
      }).then(() => {
        gotoMemberCenter()
      })
      return
    }
  }
}
