import HTTP from "../../../../requestFn/Api";
const app = getApp();
Page({
  data: {
    page: 1,
    page_size: 1,
    has_more: true,
    appointment_list: [],
  },
  onLoad(options) {

  },
  onShow() {
    this.init();
  },
  init() {
    this.setData({
      page: 1,
      page_size:10,
      has_more: true,
    })
    this.getMyAppointment(true);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    app.onRefresh();
    this.init(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.has_more) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return 
    }
    this.getMyAppointment()
  },
  async getMyAppointment(flag=false) {
    let {
      page,
      page_size,
      appointment_list
    } = this.data;
    const {list} = await HTTP.Appointment({
      page,
      page_size
    });
    this.setData({
      appointment_list:flag?list:[...appointment_list,...list],
      page: ++page,
      has_more: list.length === page_size
    })
  },
  deleteAppointment(e){
    const that = this;
    const {id} = e.currentTarget;id;
    wx.showModal({
      title: "提示",
      content: "是否删除该条记录",
      success: function (res) {
        if (res.confirm) {
          that.deleteHTTP(id);
        } else if (res.cancel) {
          console.log("用户点击取消")
        }
      }
    })
  },
  async deleteHTTP(id){
    const res = await HTTP.DeleteAppointment({id})
    if(res.code==0){
      this.init();
    }
  }
})