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

export const gotoMemberCenter = () => {
  const browserInfo = browser();
  let pathname = '/'
  if (/gym.lexiaoya.cn/.test(location.origin) || /test.gym.lexiaoya.cn/.test(location.origin)) {
    pathname = '/mdaya/'
  }
  if (browserInfo.isApp) {
    const url = window.location.origin + pathname + `#/member?id=${state.buyVipId}&type=${state.buyVipType}`;
    postMessage({
      api: 'openWebView',
      content: {
        url: url,
        orientation: 1
      }
    });
    return;
  }
  location.href = window.location.origin + pathname + `#/member?id=${state.buyVipId}&type=${state.buyVipType}`;
};

/**验证是否是会员 */
export const hasVip = () => {
  return !dayjs().isBefore(dayjs(state.user.data.student.membershipEndTime)) &&
    !state.user.data.isExistPendingMember
    ? false
    : true;
};
export const handleCheckVip = () => {
  // 学员端验证
  if (state.platformType == 'STUDENT') {
    //学员有待激活会员
    // if (
    //   !state.user.data.membershipDays &&
    //   state.user.data.purchaseMemberRecord
    // ) {
    //   showDialog({
    //     title: '温馨提示',
    //     message: '您的云练习暂未激活，请激活后使用'
    //   }).then(() => {
    //     gotoMemberCenter();
    //   });
    //   return false;
    // }

    //学员没有会员
    if (
      !dayjs().isBefore(dayjs(state.user.data.student.membershipEndTime)) &&
      !state.user.data.isExistPendingMember
    ) {
      showDialog({
        title: '温馨提示',
        message: '您暂未开通云练习，请开通后使用'
      }).then(() => {
        gotoMemberCenter();
      });
      return false;
    }
  }
  return true;
};
