// pages/customermanage/customermanage.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagesList: [],
    loading: true,
    dataLoadComplete: false,
    params: {
      pageNum: 1,
      pageSize: 15,
    },
  },
  addCustomer() {
    wx.navigateTo({
      url: '/pages/customermanage/addcustomer'
    })
  },
  getList(showSetTip) {
    const that = this;
    wx.showLoading({ mask: true });
    this.setData({
      title: '加载中',
      loading: true
    })
    const params = JSON.parse(JSON.stringify(that.data.params));
    app.$http.get('/user/getAccountInfoPage', params).then(({ status, data: { list = [], total = 0 }, msg }) => {
      if (0 === status) {
        let result = list,
          dataLoadComplete = false;
        // 判断数据是否全部加载完成
        if (15 != list.length) {
          dataLoadComplete = true;
        }
        // 不是第一页时，合并原有数据
        if (1 != params.pageNum) {
          result = this.data.pagesList.concat(result)
        }
        this.setData({
          pagesList: result,
          dataLoadComplete
        })
      }
      wx.hideLoading();
      this.setData({
        loading: false
      })
      if(showSetTip) {
        wx.showToast({
          title: '设置成功',
          icon: 'success'
        })
      }
    }).catch(() => {
      wx.hideLoading();
      this.setData({
        loading: false
      })
    })
  },
  // 滚动到处理
  scrollBottom() {
    if (this.data.dataLoadComplete) {
      return
    }
    this.setData({
      'params.pageNum': this.data.params.pageNum + 1
    })
    this.getList();
  },
  setDefault(e) {
    const { id, isDefault } = e.currentTarget.dataset.item;
    if (!isDefault) {
      wx.showLoading();
      app.$http.postForm('/user/setDefault', {
        id
      }).then(({ status, msg }) => {
        if (status === 0) {
          
          app.globalData = {};
          app.getUserInfo(app.token);
          this.getList(true);
        } else {
          wx.showToast({
            title: msg,
            icon: 'none'
          })
        }
        wx.hideLoading();
      }).catch(() => {
        wx.hideLoading();
      })
    }
  },
  unbindDevice(e) {
    const {id, username,  userNumber} = e.currentTarget.dataset.item;
    this.popup.setData({
      extClass: 'customer-popup',
      showIcon: false,
      title: '您确认解绑当前户号？',
      content: `户名：${username || '-'}`,
      content1: `户号：${userNumber || '-'}`,
      params: {
        id
      }
    });
    this.popup.show();

  },
  call(e){
    let mobile= e.currentTarget.dataset['index'];
    if(mobile!==""){
      wx.makePhoneCall({
        phoneNumber: mobile
      })
    }
  },
  cancelEvent: function () {
    this.popup.close();
  },
  confirmEvent: function () {
    const self = this;
    if (this.popup.data.type == 'success' && this.popup.data.params) {
      const { id } = this.popup.data.params;
      app.$http.deleteForm('/user/cancelBind', {
        id
      }).then(({ status, msg }) => {
        if (status === 0) {
          wx.showToast({
            title: '解绑成功',
          })
          setTimeout(() => {
            self.getList();
            app.globalData = {};
            app.getUserInfo(app.token);
            self.popup.close();
          }, 1500)
        } else {
          wx.showToast({
            title: msg,
            icon: 'none'
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获得popup组件
    this.popup = this.selectComponent("#popup");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList();
  },
})