const { api, imgName, getUserInfo, wxLogin, alertTip, authAgainByUser, userLocation } = require('../../lib/util.js')
const qrcode = require('../../lib/qrcode.js')
Page({
  data: {
    userList: [],
    addUserShow:false,
    mobile:'',
    promtFalse: {
      title: '还没有副账户！',
      buttons: [{
        text: '添加一个',
      }],
    },
    actions: [
      {
        name: '删除',
        color: '#fff',
        fontsize: '16',
        width: 60,
        icon: 'trash',
        background: '#ed3f14'
      },
      {
        name: '取消',
        width: 60,
        color: '#80848f',
        fontsize: '16',
        icon: 'undo'
      }
    ]
  },
  onLoad: function () {
    this.getUserList()
  },
  onShow: function () {
  },
  addUser(){
    this.setData({
      addUserShow:true
    })
  },
  handleClose2(){
    this.setData({
      addUserShow:false
    })
  },
  
  phoneInput(e){
    this.setData({
      mobile:e.detail.value
    })
  },
  
  apiAddUser(){
    api.post('user/addAssistantUser', { mobile:this.data.mobile})
      .then(res => {
        this.setData({
          addUserShow: false,
          mobile:''
        })
        this.getUserList()
      })
      .catch(err => {
        alertTip(err)
      })
  },
  
  actionsClick(e) {
    if (e.detail.index === 0) {
      const id = e.target.id
      this.removeUser(id)
    }
  },
  getUserList() {
    api.get('user/getAssistantUser')
      .then(res => {
        console.log('res:',res)
        this.setData({
          userList: res.items
        })
      })
      .catch(err => {
        alertTip(err)
      })
  },
  removeUser(mobile) {
    let that = this
    wx.showModal({
      title: '警告',
      content: '确定删除副账号？',
      success: function (res) {
        if (res.confirm) {
          api.post('user/delAssistantUser', { mobile })
            .then(res => {
              that.getUserList()
            })
            .catch(err => {
              alertTip(err, () => {
                that.getUserList()
              })
            })
        }
      }
    })
  }
})