// pages/medinceOrder/medinceOrder.js
const app = getApp()
import{family} from "../../../api/familyDoc"
import {product} from "../../../api/medinceOrder"
import {throttle} from "../../../utils/util"
Page({
  data: {
    page:1,
    delId:'',
    isTriggered:false,
    changeStatus:'',
    changeId:'',
    orderList:[],
    selectstatus:false,
    selectfamily:false,
    fromType:'',
    option1: [
      { text: '全部', value: 0 },
      { text: '待支付', value: 1 },
      { text: '待发货', value: 2 },
      { text: '已发货', value: 3},
      { text: '已收货', value: 4},
      { text: '已取消', value: 5}    
    ],
    isLock:false,
    option2: [],
    order_product_status: 0,
    family_id:0,
    img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/patient/static'
  },
  getProduct(){
    let {order_product_status,family_id,page}=this.data;
    product({
      order_product_status,
      family_id,
      page,
      per_page:10
    }).then(data=>{
      let result=data.data;
      if(result.length==0){
         this.setData({
          isTriggered:false,
           isLock:true
         })
         return false
      }
      this.setData({
        isTriggered:false,
        orderList:this.data.orderList.concat(result)
      })
    })
  },
  handleRefresher(){
    this.setData({
      page:1,
      isLock:false,
      orderList:[],
    })
    this.getProduct();
  },
  goBack(){
    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{
        wx.navigateBack({
          delta: 1,
          fail:function(){
            wx.reLaunch({
              url: '/pages/index/index',
            })
          }
        })
      }
    }
   
  },
  goBuyandConsult(){
   app.method.navigateTo({
    url: '/patient/pages/quickConsult/quickConsult?inquiry_type=4'
   })
  },
  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.getProduct();
    })
 
  },
  changeStatus({ detail }){
    this.setData({
      isLock:false,
      page:1,
      selectstatus:true,
      order_product_status:detail,
      orderList:[] 
    })
    this.getProduct()
  },
  changeFamily({ detail }){
    this.setData({
      family_id:detail,
      isLock:false,
      selectfamily:true,
      page:1,
      orderList:[] 
    })
    this.getProduct()
  },
  goMedinceOrderDetail(event){
    let id=event.currentTarget.dataset.id
    app.method.navigateTo({
      url: '/patient/pages/medinceOrderDetail/medinceOrderDetail?order_product_id='+id,
    })
  },
  goExpress:throttle(function(event){
    let id=event.currentTarget.dataset.id;
    app.method.navigateTo({
      url: '/patient/pages/expressDetail/expressDetail?logistics_no='+id,
    })
  }),
  lower(){
    if(!this.data.isLock){
      this.setData({
        page: ++this.data.page
      })
       this.getProduct();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  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() {
    let {delId,orderList,changeStatus,changeId}=this.data;
    this.setData({
      img_host:app.hostConfig().imghost
    });
    if(delId){
      let list=orderList.filter((item)=>{
        return item.order_product_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_product_id==changeId){
               let currentitem=`orderList[${i}].order_product_status`
               this.setData({
                 [currentitem]:changeStatus
               })
               break;
             }
      }
    }else{
       //刷新页面
    //   this.setData({
    //     page:1,
    //     orderList:[],
    //     isLock:false,
    //   });
    // this.getfamily();
    // this.getProduct();
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

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

  },

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

  },

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