// pages/myPrescript/myPrescript.js
const app = getApp()
import { prescription,delPrescription} from "../../../api/prescription"
import {throttle} from "../../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page:1,
    index:-1,
    show:false,
    isTriggered:false,
    order_prescription_id:'',
    isLock: false,
  },
  goBack(){
    wx.navigateBack({
      delta: 1,
    })
   },
   handleRefresher(){
     this.setData({
      list: [],
      isLock: false,
      page:1,
     });
    this.handleprescription()
   },
  handelDel(event){
    let order_prescription_id=event.currentTarget.dataset.id;
    let index=event.currentTarget.dataset.index;
    this.setData({
      order_prescription_id,
      index,
      show:true
    })
  },
  goconsult(event){
    let order_inquiry_id=event.currentTarget.dataset.id;
   app.method.navigateTo({
     url: '/patient/pages/orderDetail/orderDetail?order_inquiry_id='+order_inquiry_id
   })
  },
  goPayInfo(event){
    let id=event.currentTarget.dataset.id;
    if(getCurrentPages().length>=8){
      wx.reLaunch({
        url: '/patient/pages/payInfo/payInfo?order_prescription_id='+id,
      })
    }else{
      app.method.navigateTo({
        url: '/patient/pages/payInfo/payInfo?order_prescription_id='+id,
      })
    }
   
  },
  goExpert:throttle(function(event){
    let doctor_id=event.currentTarget.dataset.doctor_id;
   
    app.method.navigateTo({
      url: '/patient/pages/expertDetail/expertDetail?doctor_id='+doctor_id
    })
  }),
  goprescriptDetail:throttle(function(event) {
    let id=event.currentTarget.dataset.id;
    app.method.navigateTo({
      url: '/patient/pages/prescriptDetail/prescriptDetail?order_prescription_id='+id
    })
  }),
  confirm:throttle(function(event){
    if(event.detail){
      this.handeldelPrescription();
    }
  }),

  handeldelPrescription(){
    
     let {order_prescription_id,index}=this.data;
    delPrescription(order_prescription_id).then(data=>{
      this.data.list.splice(index,1);
      this.setData({
        list:this.data.list
      })
      wx.showToast({
        title: '成功删除该处方订单',
        icon:"none"
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  lower(){
      if(!this.data.isLock){
        this.setData({
          page: ++this.data.page
        })
        this.handleprescription();
      }
  },
  handleprescription() {
    let {page}=this.data;
    prescription({
      page,
      per_page:10,
    }).then(data => {
      let result = data.data;
      if (result.length == 0){
        this.setData({
          isLock: true,
          isTriggered:false
        })
        return false;
      }
      this.setData({
        isTriggered:false,
        list:this.data.list.concat(result)
      })
    })
  },
  onLoad(options) {
    
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      list: [],
      isLock: false,
      page:1,
     });
    this.handleprescription()
  },

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

  },

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

  },

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

  },

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

  },

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