import { postMessage } from '@/helpers/native-message'
import { browser } from '@/helpers/utils'
import { state } from '@/state'
import { showDialog } from 'vant'

const gotoMemberCenter = () => {
  postMessage({ api: 'back' })
  const browserInfo = browser()
  if (browserInfo.isApp) {
    const url = window.location.origin + location.pathname + `#/memberCenter`
    postMessage({
      api: 'openWebView',
      content: {
        url: url,
        orientation: 1
      }
    })
    return
  }
  location.href = location.origin + location.pathname + `#/memberCenter`
}

/**验证是否是会员 */
export const handleCheckVip = () => {
  // 学员端验证
  if (state.platformType == 'STUDENT') {
    //学员有待激活会员
    if (!state.user.data.membershipDays && state.user.data.purchaseMemberRecord) {
      showDialog({
        title: '温馨提示',
        message: '您的团练宝暂未激活，请激活后使用'
      }).then(() => {
        gotoMemberCenter()
      })
      return false
    }
    //学员没有会员
    if (!state.user.data.vipMember) {
      showDialog({
        title: '温馨提示',
        message: '您暂未开通团练宝，请开通后使用'
      }).then(() => {
        gotoMemberCenter()
      })
      return false
    }
  }
  return true
}
