// pages/consultOrder/consultOrder.js
const app = getApp()
import {detectionList} from "../../../api/checkSugar"
 import{family} from "../../../api/familyDoc"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: 0,
    changeStatus:'',
    changeId:'',
    isLoading:true,
    selectstatus:false,
    selectfamily:false,
    delId:'',
    option1: [
      { text: '全部订单', value: 0 },
      { text: '待支付', value: 1 },
      { text: '待绑定', value: 2 },
      { text: '检测中', value: 3},
      { text: '检测完成', value: 4},
      {text:'已取消',value:5}
      
    ],
    page:1,
    isTriggered:false,
    pageNumber:10,
    option2: [],
    detection_status: 0,
    family_id:0,
    orderList:[],
    isLock:false,
    img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/patient/static',
    fromType:''
  },
  handleRefresher(){
    this.setData({
      isLock:false,
      page:1,
      orderList:[] 
    })
    this.getDetectionList();
  },
  goBack(flag=true){

   let {fromType}=this.data;
   if(app.globalData.origion==1){
    wx.reLaunch({
      url: '/pages/index/index',
    })
  }else if(app.globalData.origion==2){
    wx.reLaunch({
      url: '/pages/index/index',
    })
  }else{
    if(fromType){
      wx.reLaunch({
        url: '/pages/my/my',
      })
    }else{
      if(true){
        wx.navigateBack({
          delta: 1,
          fail:function(){
            wx.reLaunch({
              url: '/pages/index/index',
            })
          }
        })
      }
    
    }
  }
  },
  lower(e) {
   if(!this.data.isLock){
     this.setData({
       page: ++this.data.page
     })
      this.getDetectionList()
   }
  },
  goExpertList(){
   app.method.navigateTo({
     url: '/sugarCheck/pages/checkOrderDetail/checkOrderDetail'    //'/pages/expertConsult/expertConsult',
   })
  },
  changeStatus({ detail }){
    this.setData({
      isLock:false,
      page:1,
      selectstatus:true,
      detection_status:detail,
      orderList:[] 
    })
    this.getDetectionList()
  },
  changeFamily({ detail }){
    this.setData({
      family_id:detail,
      isLock:false,
      selectfamily:true,
      page:1,
      orderList:[] 
    })
    this.getDetectionList()
  },
  getDetectionList(){
    let {detection_status,family_id,page,pageNumber}=this.data;
    this.setData({
      isLoading:true
    })
    detectionList({
      detection_status,
      family_id,
      page,
      per_page:pageNumber
    }).then(data=>{
        let result=data.data;
        this.setData({
          isLoading:false
        })
        if(result.length==0){
           this.setData({
             isLock:true,
             isTriggered:false 
           })
           return false
        };
        this.setData({
          orderList:this.data.orderList.concat(result),
          isTriggered:false 
        })
    })
  },
  handelCancelOrder(id){
    cancelOrder(id).then(data=>{
      wx.showToast({
        title: '订单取消成功',
        icon:"none"
      })
    })
  },
  getfamily(){
    family().then(data=>{
     
      let arr=[];
      data.forEach((item,index)=>{
        arr[index]={};
         let sex="";
         switch (item.sex){
           case '0':sex="未知";
           break;
           case '1':sex="男";
           break;
           case '2':sex="女";
           break;
         }
         let age=item.age;
        arr[index].text=item.card_name+"（"+sex+"，"+age+"）";
        arr[index].value=item.family_id;
      })
      arr=[{text:"全部成员",value:0}].concat(arr);
      this.setData({
        option2:arr
      })
      this.getDetectionList();
    })
    
  },
  goDetail(event){
    let id=event.currentTarget.dataset.id;
    app.method.navigateTo({
      url: '/sugarCheck/pages/checkOrderDetail/checkOrderDetail?order_detection_id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.fromType){
      this.setData({
        fromType:options.fromType
      })
    };
    if(options.prevData){
      let json=JSON.parse(options.prevData);
      this.setData({
        changeStatus:json.changeStatus,
        changeId:json.changesId
      });
    }
    this.getfamily();
    
   
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      img_host:app.hostConfig().imghost
    });
    //刷新列表
    let {delId,orderList,changeStatus,changeId}=this.data;
    if(delId){
      let list=orderList.filter((item)=>{
        return item.order_detection_id!=delId
      })
      this.setData({
        orderList:list
      })
    }else if(changeStatus && changeId){
      for (let i = 0; i < orderList.length; i++) {
        const item =orderList[i];
        if(item.order_detection_id==changeId){
               let currentitem=`orderList[${i}].detection_status`
               this.setData({
                 [currentitem]:changeStatus
               })
               break;
             }
      }
    }else{
      // this.setData({
      //   isLock:false,
      //   page:1,
      //   orderList:[] 
      // })
      // this.getDetectionList();
      // this.getfamily();
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // this.goBack(false)
  },

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

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

  },

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