import { API } from './../../../utils/network/api'
const api = new API()
const app = getApp()
Page({
  data: {
    title: '',
    height: app.globalData.height,
    order_inquiry_id: "",
    from:'',
    baseInfo: {}
  },
  onLoad(option) {
    // console.log("chat onload", option);
    let order_inquiry_id = option.order_inquiry_id;
    let from=option.from;
    this.setData({
      order_inquiry_id: order_inquiry_id,
      from:from?from:''
    })
    this.getInquiryMessageBasic();
  },
  onUnload() {
    // console.log("chat onUnload");
  },
  onShow(){
    // console.log("chat onShow");
    // console.log("app.globalData.scene from chat: ", app.globalData.scene);
  },
  $onMessageReceived(value) {
    const message = value.data[0];
    // console.log("message from chat: ",message)
  },
  initChat(){
    let _this = this;
    let count = 0;
    let interval = setInterval(() => {
      // console.log("chat init count: ", count);
      // console.log("app.globalData.chat_sdk_ready: ", app.globalData.chat_sdk_ready);
      if(app.globalData.chat_sdk_ready){
        const TUIChat = _this.selectComponent('#TUIChat');
        TUIChat.init();
        // wx.$TUIKit.on(wx.$TUIKitTIM.EVENT.MESSAGE_RECEIVED, _this.$onMessageReceived, _this);
        clearInterval(interval);
      }
      count = count + 1;
      if(count > 5){//如果尝试5次不成功就去主动调用IM初始化
        app.imInit();
        count = 0;
      }
    }, 1000);
  },
  //获取问诊订单消息内页基础数据
  getInquiryMessageBasic() {
    // console.log("order_inquiry_id: ", this.data.order_inquiry_id);
    api.getInquiryMessageBasic({order_inquiry_id: this.data.order_inquiry_id}).then(response => {
      // console.log(response);
      this.setData({
        baseInfo: response.data
      })
    }).then(res => {
      this.initChat();
    }).catch(errors => {console.error(errors);})
  },
})
