let app = getApp();
//替换成开发者后台设置的安全域名
let domain = app.globalData.domain;
//获取用户信息的url
let url = domain + "/dingding/login";
let conferenceUrl = domain + "/dingding/conferenceList";
let myinfoUrl = domain +"/dingding/myinfo";

Page({
  data: {
    corpId: '',
    authCode: '',
    userId: '',
    conferenceList: [],
    imageUrl: "http://139.159.202.24:8888"
  },

  //请求授权登录
  setImageUrl: function(e) {
    console.log("图片加载错误");
    var index = e.currentTarget.dataset.index;
    // document.getElementById(id).src = "../../../resource/images/list-room.png";
    var conferenceList = this.data.conferenceList;
    conferenceList[index].photoUrl = "/images/conference/dd-defalut.png"
    this.setData({
      conferenceList
    });
  },

  goDetail:function(e){
  // var cnum=e.currentTarget.dataset.cnum;
  var cnum=e.currentTarget.dataset.cnum;
  let reserveCode = e.currentTarget.dataset.reserveCode;
  console.log("reserveCode" ,reserveCode);
    if(reserveCode != ""  && reserveCode!=null ){
           dd.navigateTo({
              url:'../details/details?cnum='+cnum
            })
     }else{
         dd.alert({
           content: '该会议室没有授权码',
           buttonText: '我知道了',
       })
      }
  
  //  dd.navigateTo({
  //  url:'../details/details?cnum='+cnum
  //  });
  },
  onLoad(query) {
    // 页面加载
    console.info(`Page onLoad with query: ${JSON.stringify(query)}`);

    console.log("页面参数",query);

    //请求授权登录
    let _this = this;
    
     
    this.setData({
      corpId: app.globalData.corpId
    })


    dd.getAuthCode({
      success: (res) => {
        _this.setData({
          authCode: res.authCode
        })
      console.log("authCode" ,res.authCode)

        dd.httpRequest({
          url: url,
          method: 'POST',
          data: {
            authCode: res.authCode,
            corpId: app.globalData.corpId,
            reserveCode: res.reserveCode,
          },
          dataType: 'json',
          success: function(res) {
           // console.log('success----', res);
         
            let userId = res.data.result.userId;
            _this.setData({
              userId: userId
            });
            app.globalData.userId = userId;
            console.log('userId----', app.globalData.userId);

            //查询用户是否绑定
            dd.httpRequest({
              url: myinfoUrl,
              method: 'POST',
              data: {

                userId: userId

              },
              dataType: 'json',
              success: function(res) {
                console.log('success----', res)
                if (res.data.result==null){

                  dd.reLaunch({
                    url: '../../login/login'
                  });
                }else {

                  dd.httpRequest({
                    url: conferenceUrl,
                    method: 'POST',
                    data: {
                      userId: app.globalData.userId,
                      corpId: app.globalData.corpId,
                      person: query.person

                    },
                    dataType: 'json',
                    success: function(res) {
                      console.log('success----', res)

                      _this.setData({
                        conferenceList: res.data.result
                      })
                    },
                    fail: function(res) {
                      console.log("httpRequestFail---", res)
                      dd.alert({ content: JSON.stringify(res) });
                    },
                    complete: function(res) {
                      dd.hideLoading();
                    }

                  });
                }
                // _this.setData({
                //   conferenceList: res.data.result
                // })
              },
              fail: function(res) {
                console.log("httpRequestFail---", res)
                dd.alert({ content: JSON.stringify(res) });
              },
              complete: function(res) {
                dd.hideLoading();
              }

            });

          },
          fail: function(res) {
            console.log("httpRequestFail---", res)
            // dd.alert({ content: JSON.stringify(res) });
          },
          complete: function(res) {
            dd.hideLoading();
          }

        });
      },
      
      fail: (err) => {
        dd.alert({
          content: JSON.stringify(err)
        })
      }
    });


  },

  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});
