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

  },
  onShow() {
      this.init()
  },
  init() {
    this.setData({
      page: 1,
      has_more: true
    })
    this.getVisitor(true);
  },
  async getVisitor(flag = false) {
    let {
      page,
      page_size,
      visitor_list
    } = this.data;
    const {
      list
    } = await HTTP.MyVisitor({
      page,
      page_size
    });
    this.setData({
      visitor_list: flag ? list : [...visitor_list, ...list],
      page: ++page,
      has_more: list.length === page_size
    })
  },
  onReachBottom() {
    if (!this.data.has_more) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return 
    }
    this.getVisitor()
  },
  onPullDownRefresh: function () {
    app.onRefresh();
    this.init();
  },
  deleteVisitor(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.DeleteVisitor({id})
    if(res.code==0){
      this.init();
    }
  },
  goVisitor(){
    wx.navigateTo({
      url: '../visitor/index',
    })
  },
  goShare(e){
    const {id,qcode} = e.currentTarget.dataset;
    wx.navigateTo({
      url: `../door/index?id=${id}&qcode=${qcode}&type=2`, 
    })
  }
})