// pages/allotDoctor/allotDoctor.js
const app = getApp()
import {
  assignDoctor
} from "../../../api/consult"
import {
  doctorDetail,
  fllowDoctor,
  notfllowDoctor
} from "../../../api/consultExpert"
import {throttle} from "../../../utils/util"
import {cancelOrder} from "../../../api/consultOrder"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_inquiry_id: "",
    doctor_id: '',
    showDialog: false,
    order_type: '',
    fromType:'',
    chat_id: '',
    time: 0,
    timeData: {},
    allotCountDown:300000,
    countDown:3000,
    isAllot: 1, //1分配  //2 分配成功 //3分配超时
    isLock: false,
    timer: null,
    timer2: null,
    countTime: 0,
    doctor: {
      avatar: '',
      user_name: '',
      doctor_title_name: '',
      department_custom_name: '',
      hospital: {},
      multi_point_status: null,
      multi_point_enable:0,
      follow: false
    },
    img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/patient/static'
  },
  getDeatil(id) {
    doctorDetail(id).then((res) => {
      let doctor = this.data.doctor;
      for (const key in doctor) {
        let item=`doctor.${key}`;
        if (res[key]) {
          this.setData({
            [item]: res[key]
          })
        }
      };
    })
  },
  toggleFllow() {
    if (this.data.doctor.follow) {
      this.handenotfllowDoctor()
    } else {
      this.handelfllowDoctor()
    }
  },
  handelCancelOrder(){
    let { order_inquiry_id, fromType} = this.data;
    cancelOrder(order_inquiry_id).then(data=>{
      app.method.navigateTo({
        url: '/patient/pages/orderDetail/orderDetail?order_inquiry_id='+order_inquiry_id+"&fromType="+fromType,
      })
     
    })
  },
  handelfllowDoctor() {
    let id = this.data.doctor_id
    fllowDoctor(id).then(data => {
      this.setData({
        "doctor.follow": true
      })
      wx.showToast({
        title: '关注成功',
        icon: "none"
      })
    })
  },
  handenotfllowDoctor() {
    let id = this.data.doctor_id;
    notfllowDoctor(id).then(data => {
      this.setData({
        "doctor.follow": false
      })
      wx.showToast({
        title: '已取消关注',
        icon: "none"
      })
    })
  },
  close(event) {
    this.handelCancelOrder();
  },
  confirm(){
   this.setData({
    showDialog:false
   })
  },
  goDetail:throttle(function(){
    let {
      order_inquiry_id,
      fromType
    } = this.data;
    app.method.navigateTo({
      url: '/patient/pages/orderDetail/orderDetail?order_inquiry_id='+order_inquiry_id+"&fromType="+fromType
    })
  }),
  finished(){
   this.goChat();
  },
  finishedAllot(){
    clearTimeout(this.data.timer);
    this.setData({
      isAllot: 3
    })
   
  },
  onChangeCountDown(e){
   let time=e.detail;
   this.setData({
    timeData:time
   })
    if(time.minutes==3 && time.seconds==0)
       this.setData({
        showDialog: true
      })
   },
  //计算请求时间
  countReqTime() {
    var timer2 = setInterval(() => {
        let countTime = this.data.countTime;
        if (this.data.time >= 180000) {
          this.setData({
            isAllot: 3
          })
        };
        if (this.data.time >= 300000) {
          clearTimeout(this.data.timer);
          this.setData({
            showDialog: true
          })
        };
        let currentTime = this.data.time + 1000;
        this.setData({
          time: currentTime
        })
        this.setData({
          countTime: ++countTime
        });
      }, 1000);
      this.setData({
        timer2: timer2
      })
  },
  //定时发送请求
  reqCount(order_id){
    var timer = setInterval(() => {
      this.handelAssignDoctor(order_id);
    }, 10000);
    this.setData({
      timer: timer
    })
  },
  handelAssignDoctor(id) {
    assignDoctor(id).then(data => {
      if (data.user_id) {
        this.setData({
          chat_id: data.user_id,
          doctor_id: data.doctor_id,
          isAllot: 2
        })
        this.getDeatil(data.doctor_id);
        let {timer, timer2} = this.data;
        clearInterval(timer);
        clearInterval(timer2);
      }

    })
  },
  handleGochat(){
    const countDown = this.selectComponent('.control-count-down');
    countDown.pause();
    this.goChat();
  },
  goChat(){
    
    let {
      order_inquiry_id,
      inquiry_type,
      chat_id,
      fromType
    } = this.data;
    app.method.navigateTo({
      url: '/TUIService/pages/index?currentConversationID=' + chat_id + "&order_inquiry_id=" + order_inquiry_id + "&inquiry_type=" + inquiry_type+"&fromType="+fromType
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let order_inquiry_id = options.order_inquiry_id;
    let inquiry_type = options.inquiry_type;
    let order_id = options.order_id;
    if(options.fromType){
      this.setData({
        fromType:options.fromType
      })
    }
    if (order_inquiry_id) {
      this.setData({
        order_inquiry_id,
        order_id,
        inquiry_type
      })
      this.handelAssignDoctor(order_id);
      this.reqCount(order_id);
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      img_host:app.hostConfig().imghost
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.data.timer);
    clearInterval(this.data.timer2)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  
})