import { connect } from 'herculex';
import { prefixPagePath } from '../../utils';

const app = getApp();
Component(connect({
  mapStateToProps: {
    scopes: state => state.$global.scopes,
    userInfo: state => state.$global.userInfo || {},
  },
})({
  props: {
    componentData: {},
  },
  data: {
    imgSrcPrefix: app.globalData.imgSrcPrefix,
    backgroundUrl: 'https://gw.alipayobjects.com/mdn/rms_a0c592/afts/img/A*gzsAS6beOyMAAAAAAAAAAABkARQnAQ',
    defaultAvatar: 'https://gw.alipayobjects.com/mdn/rms_8ed4a1/afts/img/A*XasgTZbA22UAAAAAAAAAAAAAARQnAQ',
    defaultNickName: '',
  },
  methods: {
    goToDetail() {
      if (this.data.userInfo.isLogin) {
        my.navigateTo({
          url: `${prefixPagePath}/user-detail/index`,
        });
      }
    },
    onGetAuthorize() {
      const { scopes, userInfo: { isLogin } } = this.data;
      if (isLogin || scopes === 'auth_user') return;
      this.dispatch('$global:doLogin', { scopes });
    },
    onLogin() {
      const { scopes, userInfo: { isLogin } } = this.data;
      if (isLogin || scopes !== 'auth_user') return;
      this.dispatch('$global:doLogin', { scopes });
    },
    onAuthError(e) {
      console.log(e);
    },
  },
}));
