const wx2my = require('../../wx2my');
const Behavior = require('../../Behavior');
const app = getApp();

const wxService = require('../../utils/wxService');

const utils = require('../../utils/util');

const envInfo = require('../../config/index').envInfo;

Component({
  /**
   * 组件的属性列表
   */
  props: {
    isAuthorizationActiveText: '',
    isAuthorizationType: 2
  },

  /**
   * 组件的初始数据
   */
  data: {
    couponNum: '2',
    userInfo: {},
    hasUserInfo: false,
    env: envInfo.env,
    checkAll: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    checkAll() {
      this.setData({
        checkAll: !this.data.checkAll
      });
    },

    handelToMemberRules() {
      wxService.router(`/pages/memberRules/memberRules?type=${this.data.isAuthorizationType}`);
    },

    //再想想
    close() {
      this.setData({
        isAuthorization: false
      });
    },

    //立即兑换
    query() { },
    _getUserInfo(res = {}) {
      this.setData({
        isAuthorization: false
      });
      const userInfo = res.detail || {};

      if (res.detail.userInfo) {
        utils.getUserInfoByBtn(userInfo).then((data = {}) => {
          const {
            token
          } = data;
          wx2my.setStorageSync('_accreditUserInfo', userInfo);
          wx2my.hideNavigationBarLoading();

          if (token) {
            wx2my.setStorageSync('token', token);
          } else {
            wx2my.setStorageSync('token', '');
          }

          wx2my.setStorageSync('_baseUserInfo', data); // 存储用户开卡状态
          // wx.setStorageSync('memberActivateStatus', data.memberActivateStatus)

          this.setUserInfo();
        }).catch(err => {
          if (err) {
            wx2my.showToast({
              title: `会员系统异常请稍后重试!`,
              icon: 'none'
            });
          }
        });
      } else {
        this.setData({
          isAuthorization: false
        });
      }
    },
    onGetAuthorize(res) {
      this.setData({
        isAuthorization: false
      });
      // 获取会员基础信息
      my.getOpenUserInfo({
        fail: (res) => {
        },
        success: (res) => {
          let userInfo = JSON.parse(res.response).response // 以下方的报文格式解析两层 response
          if (userInfo) {
            utils.getUserInfoByBtn(userInfo).then((data = {}) => {
              const {
                token
              } = data;
              wx2my.setStorageSync('_accreditUserInfo', userInfo);
              wx2my.hideNavigationBarLoading();
              if (token) {
                wx2my.setStorageSync('token', token);
              } else {
                wx2my.setStorageSync('token', '');
              }

              wx2my.setStorageSync('_baseUserInfo', data); // 存储用户开卡状态

              this.setUserInfo();
            }).catch(err => {
              if (err) {
                wx2my.showToast({
                  title: `会员系统异常请稍后重试!`,
                  icon: 'none'
                });
              }
            });

          } else {
            this.setData({
              isAuthorization: false
            });
          }
        }
      });
    },
    handleGetAuthCode() {
      this.setData({
        isAuthorization: false
      });

      my.getAuthCode({
        scopes: 'auth_user',
        success: (res) => {
          console.log('res', res, res.authCode)
          my.getAuthUserInfo({
            success: (userInfo) => {
              console.log('userInfo', userInfo)
              if (userInfo) {
                utils.getUserInfoByBtn(userInfo, res.authCode).then((data = {}) => {
                  console.log('data', data)

                })
              }
              // my.alert({
              //   content: userInfo.nickName
              // });
              // my.alert({
              //   content: userInfo.avatar
              // });
            }
          });
        },
      });
    },

    handleActiveAgree() {
      // 子组件触发父组件 去开卡
      wxService.nextTick(() => {
        this.props.onActiveTocard({
          detail: true
        });
      });
      this.setData({
        isAuthorization: false
      });
    },

    setUserInfo() {
      // 判断有无开卡
      // const {memberActivateStatus} = wx.getStorageSync('_baseUserInfo')
      const userInfo = wx2my.getStorageSync('_baseUserInfo'); // 新用户去激活领卡 

      if (userInfo && userInfo.data && userInfo.data.member && !userInfo.data.member.mobile) {
        wxService.openCard();
        return false;
      }
      const urls = getCurrentPages();
      setTimeout(() => {
        const item = urls[0];
        console.log('urls', urls, item, item.options)
        console.log('item', item)
        console.log('.route', item.route)
        if (item && item.route) {
          try {
            const options = JSON.parse(item.options);
            wxService.router(`/${item.route}`).search(options).replace();
          } catch (e) {
            wxService.router('/pages/userCenter/userCenter').replace();
          }
        } else {
          wxService.router('/pages/userCenter/userCenter').replace();
        }
      }, 200);
    }

  }
});