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 = () => {
  postMessage({ api: 'back' });
  const browserInfo = browser();
  if (browserInfo.isApp) {
    const url = window.location.origin + window.location.pathname + `#/member`;
    postMessage({
      api: 'openWebView',
      content: {
        url: url,
        orientation: 1
      }
    });
    return;
  }
  location.href = window.location.origin + window.location.pathname + `#/member`;
};

/**验证是否是会员 */
export const hasVip = () => {
  // &&
  //   !state.user.data.isExistPendingMember
  return !dayjs().isBefore(dayjs(state.user.data.student.membershipEndTime))
    ? 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;
};
