// pages/inviteCircleMember/inviteCircleMember.js
var app = getApp();
import Notify from '../../dist/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    circleId: 0,
    inviteUserName:'',

    circleDat: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      circleId: options.circleId,
      inviteUserName: options.inviteUserName
    })
    this.circleinfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //参加
  accept() {
    var that = this;
    //判断有没有注册
    var config = wx.getStorageSync('config');
    if (config.registed == false || config.unionId == undefined) {
      Notify("请先注册");
      //跳转到社会人士注册,保存邀请地址
      var interval = setInterval(function () {
        let inviteUrl = '/pages/inviteCircleMember/inviteCircleMember?circleId=' + that.data.circleId + '&inviteUserName=' + that.data.inviteUserName
        wx.setStorageSync("inviteUrl", inviteUrl);
        wx.redirectTo({
          url: '../socialUserRegister/socialUserRegister',
        })
        clearInterval(interval)
      }, 1000);  
    } else {
      //调用接入圈子接口
      wx.request({
        url: app.url + 'ajax_user_join_circle.php',
        data: {
          unionId: config.unionId,
          circleId: that.data.circleId
        },
        header: { 'content-type': 'application/json' },
        method: 'GET',
        dataType: 'json',
        success: function (res) {
          console.log(res)
          if (res.statusCode == 200) {
            //已经是成员
            if (res.data.status == "MEMBER") {
              Notify("您已经是圈子成员");
              var interval = setInterval(function () {
                wx.redirectTo({
                  url: '../circleDetails/circleDetails?circleId=' + that.data.circleId,
                })
                clearInterval(interval)
              }, 1000);
            } else if (res.data.status == "OK") {
              //跳转到圈子模块首页
              wx.switchTab({
                url: '../circle/circle',
              })
            } else if (res.data.status == "NG") {
              //跳转到小程序首页
              wx.switchTab({
                url: '../index/index',
              })
            }

          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },

  //拒绝
  refuse() {
    var that = this;
    //判断有没有注册
    var config = wx.getStorageSync('config');
    if (config.registed == false || config.unionId == undefined) {
      Notify("请先注册");
      //跳转到社会人士注册,保存邀请地址
      var interval = setInterval(function () {
        wx.redirectTo({
          url: '../socialUserRegister/socialUserRegister',
        })
        clearInterval(interval)
      }, 1000);
    } else {
      //跳转到首页
      wx.switchTab({
        url: '../index/index',
      });

    }
  },

  //获取圈子信息
  circleinfo() {
    var that = this;
    wx.request({
      url: app.url + 'ajax_get_circle_info_invite.php',
      data: {
        circleId: that.data.circleId
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log(res)
        if (res.statusCode == 200) {
          if (res.data.status == "OK") {
            that.setData({
              circleDat: res.data.result.circleDat,
            })
          }
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
})