//index.js
const utils = require('../../utils/util.js')
Page({
  data: {
    isLogin: false, //是否登陆
    roles: 1, //登录角色(默认企业用户)
    userInfo: '', //缓存的用户信息
    statistics: '', //统计信息
    noticeList: [], //公告消息
    bannerList: ['../../images/example-02.png', '../../images/example-02.png', '../../images/example-02.png'], //轮播图
    tabIndex: 0,
    companyDefault: '', //企业用户默认
    remain: '', //获取剩余发票数量
    iconList: [{ //企业列表
      icon: '../../images/ico_stay-open@3x.png',
      text: '待开单据',
      path: '/pages/ticketWaiting/ticketWaiting'
    },
    {
      icon: '../../images/ico_query@3x.png',
      text: '发票查询',
      path: '/pages/ticketQuery/ticketQuery'
    },
    {
      icon: '../../images/ico_red-rush@3x.png',
      text: '空白作废',
      path: '/pages/blankInvoice/blankInvoice'
    },
    {
      icon: '../../images/ico_invoice-purchase@3x.png',
      text: '发票领购',
      path: '/pages/ticketGet/ticketGet'
    },
    {
      icon: '../../images/ico_clear-card@3x.png',
      text: '发票库存',
      path: '/pages/invoiceStock/invoiceStock'
    },
    {
      icon: '../../images/ico_network-copy@3x.png',
      text: '发票汇总',
      path: '/pages/invoiceSummary/invoiceSummary'
    },
    {
      icon: '../../images/ico_deploy@3x.png',
      text: '配置管理',
      path: '/pages/ticketConfig/ticketConfig'
    }
      // {
      //   icon: '../../images/ico_audit@3x.png',
      //   text: '操作审计',
      //   path: '/pages/audit/audit'
      // },
    ],
    ispName: '', //服务商名称
    ispList: [{ //服务商列表
      icon: '../../images/ico_enterprise-shelves@3x.png',
      text: '企业上架',
      path: '/pages/ispPutaway/ispPutaway'
    },
      // {
      //   icon: '../../images/ico_invoice-purchase@3x.png',
      //   text: '人员管理',
      //   path: '/pages/ispPeople/ispPeople'
      // },
      // {
      //   icon: '../../images/ico_audit@3x.png',
      //   text: '操作审计',
      //   path: '/pages/audit/audit'
      // }
    ],
    operName: '', //运营商名称
    operList: [ //运营商列表
      {
        icon: '../../images/ico_facilitator@3x.png',
        text: '服务商列表',
        path: '/pages/operIsp/operIsp'
      },
      // {
      //   icon: '../../images/ico_invoice-purchase@3x.png',
      //   text: '人员管理',
      //   path: '/pages/operPeople/operPeople'
      // },
      // {
      //   icon: '../../images/ico_audit@3x.png',
      //   text: '操作审计',
      //   path: '/pages/audit/audit'
      // }
    ],
  },

  onLoad: function () { },

  /* 以下是三端公用函数=================================================== */
  //公告(公用)
  getNotice() {
    let params = {
      mobile: this.data.userInfo.mobile,
      pageNum: 1,
      pageSize: 10
    }
    utils.axios({
      method: 'get',
      url: '/auth/user/msg/findGlobalNotice',
      data: params,
      noLogin: () => { },
      complete: () => { },
      success: res => {
        this.setData({
          noticeList: res.data.records
        })
      }
    })
  },
  //统计信息(公用)
  getStatis() {
    let params = {
      mobile: this.data.userInfo.mobile
    }
    if (this.data.userInfo.roles === 1) {
      params.entTaxId = this.data.companyDefault.entTaxId
    }
    utils.axios({
      method: 'get',
      url: '/sys/stat/home',
      data: params,
      complete: () => { },
      success: res => {
        this.setData({
          statistics: res.data
        })
      }
    })
  },
  //菜单跳转
  toSkip(e) {
    let path = e.currentTarget.dataset.path
    // if(path==='/pages/ticketGet/ticketGet'){
    //   utils.toast('功能完善中')
    //   return
    // }
    if (path) {
      wx.navigateTo({
        url: path
      })
    }
  },
  //点击公告列表
  toNotice() {
    if (this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/notice/notice'
      })
    } else {
      utils.toLogin()
    }
  },
  //点击公告详情
  toDetail(e) {
    if (this.data.isLogin) {
      let msgId = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '/pages/notice/noticeDetail?msgId=' + msgId
      })
    } else {
      utils.toLogin()
    }
  },

  /* 以下是企业端=================================================== */
  //登录用户获取默认企业(企业端)
  getCompanyDefault() {
    let params = {
      mobile: this.data.userInfo.mobile
    }
    utils.axios({
      method: 'get',
      url: '/auth/comm/user/findDefaultChoose',
      data: params,
      success: res => {
        this.setData({
          companyDefault: res.data
        })
        wx.setStorageSync('company', res.data)
        wx.setStorageSync('entTaxId', this.data.companyDefault.entTaxId)
        this.getStatis()
      },
      sendBefore: () => {
        if (params.sendBefore) {
          params.sendBefore()
        }
      },
      error: res => {
        if (params.error) {
          params.error(res)
        }
      },
      complete: () => {
        if (params.complete) {
          params.complete()
        }
      },
      noLogin: res => {
        if (params.noLogin) {
          params.noLogin()
        } else {
          toLogin()
        }
      }
    })
  },
  //获取剩余发票数量(企业端)
  getRemain() {
    let params = {
      reqChannel: 5,
      entTaxId: this.data.companyDefault.entTaxId
    }
    utils.axios({
      method: 'post',
      url: '/sys/invoiceInfo/remain',
      data: params,
      complete: () => { },
      success: res => {
        this.setData({
          remain: res.data
        })
      }
    })
  },
  //手工开票
  toHand() {
    if (this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/ticketHand/ticketHand'
      })
    } else {
      utils.toLogin()
    }
  },
  //扫码开票
  toScan() {
    if (this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/ticketScan/ticketScan'
      })
    } else {
      utils.toLogin()
    }
  },
  //购方开票
  toBuy() {
    if (this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/ticketBuy/ticketBuy'
      })
    } else {
      utils.toLogin()
    }
  },
  //待开抬头
  toWait() {
    if (this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/ticketWait/ticketWait'
      })
    } else {
      utils.toLogin()
    }
  },
  //切换企业
  toSwitch() {
    if (this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/switchAccount/switchAccount?entTaxId=' + this.data.companyDefault.entTaxId
      })
    } else {
      utils.toLogin()
    }
  },
  //切换(总计/本月)
  switchTab(e) {
    let tabIndex = e.currentTarget.dataset.index
    this.setData({
      tabIndex
    })
  },

  /* 以下是服务端=================================================== */
  //企业列表（服务商）
  toBusiness() {
    wx.navigateTo({
      url: '/pages/ispBusiness/ispBusiness'
    })
  },
  //服务器列表（服务商）
  toServers() {
    wx.navigateTo({
      url: '/pages/ispServers/ispServers'
    })
  },
  //开票列表（服务商）
  toTicket() {
    wx.navigateTo({
      url: '/pages/ispTicket/ispTicket'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    //验证是否登陆
    utils.getUser({
      noLogin: () => {
        this.setData({
          isLogin: false
        })
      }
    }).then(res => {
      this.setData({
        isLogin: true,
        roles: utils.getInfo().roles,
        userInfo: utils.getInfo()
      })
      // this.getNotice()
      //角色为企业时发送
      if (this.data.userInfo.roles === 1) {
        this.getCompanyDefault()
      }
      //角色为服务商时发送
      if (this.data.userInfo.roles === 2) {
        utils.getIsp({
          noLogin: () => { }
        }).then(res => {
          this.setData({
            ispName: res.data.name
          })
        })
        this.getStatis()
      }
      //角色为运营商时发送
      if (this.data.userInfo.roles === 3) {
        this.setData({
          operName: this.data.userInfo.userName
        })
        this.getStatis()
      }
    })
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

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