// pages/switchAccount/switchAccount.js
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    entTaxId: '',
    userInfo: '',
    list: [],
    pageNum: 1,
    pageSize: 20,
    pages: '',
    finish: false,
    tabIndex: 0,
    deviceList: [],
    deviceNumber: '',
    deviceFinish: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      entTaxId: wx.getStorageSync('entTaxId'),
      userInfo: utils.getInfo()
    })
    this.getList()
  },
  //切换
  switchTab(e) {
    let tabIndex = e.currentTarget.dataset.index
    this.setData({
      tabIndex
    })
  },
  //列表
  getList(type) {
    let params = {
      userMobile: this.data.userInfo.mobile,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    utils.axios({
      method: 'get',
      url: '/sys/entInfo/findPageByMobile',
      data: params,
      sendBefore() {
        wx.showNavigationBarLoading()
      },
      complete() {
        wx.hideNavigationBarLoading()
        wx.stopPullDownRefresh()
      },
      success: res => {
        let result = res.data
        for (let i = 0; i < result.records.length; i++) {
          result.records[i].selected = false
          if (result.records[i].entTaxId === this.data.entTaxId.toString()) {
            result.records[i].selected = true
          }
        }
        if (type === 'toFresh') {
          this.setData({
            list: result.records,
            pages: result.pages
          })
          this.getDeviceList('toFresh')
        } else {
          let list = this.data.list
          list = list.concat(result.records)
          this.setData({
            list,
            pages: result.pages
          })
          this.getDeviceList()
        }
        if (result.total <= this.data.pageSize) {
          this.setData({
            finish: true
          })
        }
      }
    })
  },
  //列表
  getDeviceList(type) {
    let defaultCompany = utils.getCurrEntInfo()
    let company = {}
    this.data.list.forEach((item) => {
      if (item.selected === true) {
        company = item
      }
    })
    let params = {
      entTaxId: company.entTaxId
    }
    utils.axios({
      method: 'get',
      url: '/sys/entDevice/findDevices',
      data: params,
      sendBefore() {
        wx.showNavigationBarLoading()
      },
      complete() {
        wx.hideNavigationBarLoading()
        wx.stopPullDownRefresh()
      },
      success: res => {
        for (let i = 0; i < res.data.length; i++) {
          res.data[i].selected = false
          if (res.data[i].deviceKey === defaultCompany.defaultDeviceInfo.deviceKey) {
            res.data[i].selected = true
          }
        }
        if (type === 'toFresh') {
          this.setData({
            deviceList: res.data,
            deviceNumber: res.data.length,
            deviceFinish: true
          })
        } else {
          let list = this.data.deviceList
          list = list.concat(res.data)
          this.setData({
            deviceList: list,
            deviceNumber: res.data.length,
            deviceFinish: true
          })
        }
      }
    })
  },
  toFresh() {
    this.setData({
      pageNum: 1,
      pages: '',
      finish: false
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
    this.getList('toFresh')
  },

  //切换企业
  switchCompany(e) {
    let list = this.data.list
    let entTaxId = e.currentTarget.dataset.id
    let params = {
      mobile: this.data.userInfo.mobile,
      defaultChoose: entTaxId,
      reqChannel: 5
    }
    utils.axios({
      method: 'post',
      url: '/auth/comm/user/setDefaultChoose',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '企业切换中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: (res) => {
        utils.toast('企业切换成功')
        this.setData({
          entTaxId
        })
        for (let i = 0; i < list.length; i++) {
          list[i].selected = false
          if (list[i].entTaxId === this.data.entTaxId.toString()) {
            list[i].selected = true
          }
        }
        this.setData({
          list
        })
        let userInfo = wx.getStorageSync('userInfo')
        wx.setStorageSync('userInfo', userInfo)
        wx.setStorageSync('token', res.data.token)
        wx.setStorageSync('expirationTime', res.data.expirationTime)
        wx.setStorageSync('entTaxId', entTaxId)
        wx.setStorageSync('company', res.data.chooseEntity)
        
        // utils.getCompany().then(res => {
        //   wx.setStorageSync('company', res.data)
        // })
      }
    })
  },
//切换企业
switchDevice(e) {
  let defaultCompany = utils.getCurrEntInfo()
  let device = e.currentTarget.dataset
  defaultCompany.deviceKey = device.deviceKey;
  defaultCompany.deviceLabel = device.deviceLabel;
  defaultCompany.devicePos = device.devicePos;
  defaultCompany.deviceType = device.deviceType;
  defaultCompany.entTaxId = device.entTaxId;
  defaultCompany.initStatus = device.initStatus;
  defaultCompany.isvPlatform = device.isvPlatform;
  defaultCompany.statusChangeTime = device.statusChangeTime;
  defaultCompany.taxDiscId = device.taxDiscId;
  defaultCompany.taxMachineNo = device.taxMachineNo;
  wx.setStorageSync('company', defaultCompany)
  utils.toast('设备切换成功')
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.toFresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNum = this.data.pageNum
    let pages = this.data.pages
    pageNum++
    if (pageNum <= pages) {
      this.setData({
        pageNum
      })
      this.getList()
    } else {
      this.setData({
        finish: true
      })
      return
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '诺信云',
      path: '/pages/index/index'
    }
  }
})