import HTTP from "../../../../requestFn/Api"
const app = getApp();
Page({
  data: {
    garden_id: null,
    class_list: [],
    class_id: 0,
    class_name: "全部",
    phone_list: [],
    page: 1,
    page_size: 10,
    has_more: true,
  },

  onLoad(options) {
    const that = this;
    const {
      garden_id
    } = app.globalData;
    if (garden_id) {
      that.setData({
        garden_id
      })
    }
    that.getPhone_class(garden_id);
  },
  onShow() {
    this.init();
  },
  init() {
    this.setData({
      page: 1,
      has_more: true
    })
    this.getPhone(true)
  },
  getPhone_class(garden_id) {
    const that = this;
    HTTP.GetPhone_class({
      garden_id,
      page: 1,
      page_size: 9999,
    }).then(res => {
      const class_list = res.list.filter(item => item.enable);
      that.setData({
        class_list: [{
          id: 0,
          class_name: "全部"
        }, ...class_list]
      })
    })
  },
  getPhone(flag = false) {
    const that = this;
    let {
      class_id,
      garden_id,
      page,
      page_size,
      phone_list
    } = that.data;
    HTTP.GetPhone({
      garden_id,
      page,
      page_size,
      class_id
    }).then(res => {
      const newArray = res.list.filter(item => item.enable)
      that.setData({
        phone_list: flag ? newArray : [...newArray, ...phone_list],
        page: ++page,
        has_more: page_size === newArray.length
      })
    })
  },
  bindPickerChange(e) {
    const that = this;
    const {
      class_list,
    } = that.data
    const {
      value
    } = e.detail;
    const class_id = class_list[value].id
    that.setData({
      class_name: class_list[value].class_name,
      class_id
    });
    that.init()
  },
  callphone(e) {
    const {
      phone
    } = e.currentTarget.dataset;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  onReachBottom() {
    if (!this.data.has_more) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return console.log('没有更多数据了');
    }
    this.getPhone()
  },
  onPullDownRefresh: function () {
    app.onRefresh();
    this.init();
  },
})