// pages/address/address.js
const app = getApp();
import {getAddress,delAddress} from "../../../api/address"
import {throttle} from "../../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList:[],
    address_id:'',
    isTriggered:false,
    order_prescription_id:'',
    message:'',
    currentId:'',
    show:false,
    img_host:'https://oss.prod.applets.igandanyiyuan.com/applet/patient/static'
  },
  addAddresss:throttle(function(){
   app.method.navigateTo({
     url: '/patient/pages/editAddress/editAddress?order_prescription_id='+this.data.order_prescription_id,
   })
  }),
  confirm:throttle(function(event){
    if(event.detail){
      this.handelDelAddress();
    }
  }),
  onCloseAddress(event) {
     let id=event.currentTarget.dataset.id;
    const {instance } = event.detail;
    instance.close();
    this.setData({
      show:true,
      currentId:id,
      message:"确定删除该地址？"
    })
  },
  handleRefresher(){
  this.handleGetAddress()
  },
  handleGetAddress(){
    getAddress().then(data=>{
      this.setData({
        isTriggered:false,
        addressList:data
      })
    })
  },
  goBack(){
    wx.navigateBack({
      delta: 1,
    })
   },
  handelDelAddress(){
    let id=this.data.currentId;
    delAddress(id).then(data=>{
        wx.showToast({
          title: '删除成功',
          icon:"none"
        });
        this.handleGetAddress();
    })
  },
  selectAddress(event){
    if(this.data.address_id){
      let{id,name,phone,province,city,country,address}=event.currentTarget.dataset
      this.setData({
        address_id: id,
      });
      let addressInfo={
        name,
        phone,
        province,
        city,
        country,
        address,
        address_id:id
      };
          let pages = getCurrentPages();
          let prevPage = pages[pages.length - 2]; //上上一页
          prevPage.setData({
            prevData:JSON.stringify(addressInfo)
          });
          wx.navigateBack({
            delta: 1,
          })
    }
   
  },
  goEdit(event){
    let id=event.currentTarget.dataset.id
    app.method.navigateTo({
      url: '/patient/pages/editAddress/editAddress?address_id='+id+"&order_prescription_id="+this.data.order_prescription_id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let address_id=options.address_id;
    let order_prescription_id=options.order_prescription_id;
    if(address_id){
      this.setData({
        address_id,
        order_prescription_id
      })
    }
  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

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