// pages/searchCircle/searchCircle.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    circleList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let keyword = options.keyword;
    this.setData({
      keyword: keyword
    })

    this.searchCircle()
  },

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

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

  //获取输入内容
  changeInput(e){
    this.setData({
      keyword: e.detail.value
    })
  },

  //调取接口检索圈子
  searchCircle(){
    var that = this;
    var config = wx.getStorageSync('config');
    wx: wx.request({
      url: app.url + 'ajax_get_search_circle_list.php',
      data: {
        unionId: config.unionId,
        keyword: that.data.keyword
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log(res)
        if (res.statusCode == 200) {
           that.setData({
             circleList: res.data.result.circleList
           })
         }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  //进圈
  circleJoin(e) {
    //获取圈子的id
    let circleId = e.currentTarget.dataset['index']
    //调用接口申请加入
    var that = this;
    var config = wx.getStorageSync('config');
    wx: wx.request({
      url: app.url + 'ajax_user_join_circle.php',
      data: {
        unionId: config.unionId,
        circleId: circleId
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log(res)
        if (res.statusCode == 200) {
          //参数错误
          if (res.data.status == "NG") {
            wx.showToast({
              title: res.data.result.message,
              icon: 'success',
              duration: 1500
            });
            return;
          }
          
          //已经是会员 或者 加入成功
          if (res.data.status == "MEMBER") {
            //跳转到对应圈子首页
            wx.navigateTo({
              url: '../circleDetails/circleDetails?circleId=' + circleId
            })
            return;
          }

          //申请成功
          if (res.data.status == "OK") {
            //弹出消息，等待审核
            wx.showToast({
              title: res.data.result.message,
              icon: 'success',
              duration: 1500
            });
            return;
          }
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
})