import { submit, getPhoneAuthReturnPage } from '../../utils/phone-auth';

Page({
  data: {
    show: false,
    avatar: '',
    avatarFile: null,
    phoneCode: '',
    phoneGot: false
  },
  onLoad() {
    setTimeout(() => this.setData({ show: true }), 100);
  },
  onAvatar(e) {
    const p = e.detail.avatarUrl;
    this.setData({ avatar: p, avatarFile: p });
  },
  onPhone(e) {
    if (e.detail.code) {
      this.setData({ phoneCode: e.detail.code, phoneGot: true });
    }
  },
  async submit() {
    const { phoneCode, avatarFile } = this.data;
    if (!avatarFile || !phoneCode) return;
    try {
      wx.showLoading({ title: '认证中...', mask: true });
      await submit(phoneCode, avatarFile);
      wx.hideLoading();
      wx.reLaunch({ url: getPhoneAuthReturnPage() });
    } catch (e) {
      wx.hideLoading();
      wx.showToast({ title: e.message, icon: 'none' });
    }
  },
  exit() {
    wx.exitMiniProgram();
  }
});
