// pages/userInfo/userInfo.js
const app = getApp()
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalUrl: app.globalData.globalUrl,
    userInfo: '', //缓存的用户信息
    user: '', //用户信息
    companyDefault: '', //默认用户企业(企业用户才有)
    sex: ['男', '女'], //性别列表
    sexIndex: '', //当前性别下标
    spreadFlag: false, 
    entTypeDict:['一般纳税人','小规模纳税人','起征点以下纳税人'], //企业类型
    entTypeDictIndex:'', //企业类型下标
    deviceTypeDict:['软证书','税控盘','金税盘','税控ukey','税控服务器'], // 开票默认设备
    deviceTypeDictIndex:'',// 开票默认设备下标
    rateList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: utils.getInfo()
    })
    //获取个人信息
    this.getUser()
    //如果是企业用户，获取默认公司信息
    if (this.data.userInfo.roles === 1) {
      this.getCompanyDefault()
    }
  },

  spreadBuyer() {
    this.setData({
      spreadFlag: !this.data.spreadFlag
    })
  },

  /* 公用函数—————————————————————————————————————————————————————————————— */
  //获取用户信息
  getUser() {
    utils.getUser({
      sendBefore: () => {
        wx.showLoading({
          title: '加载中...',
          mask: true
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    }).then(res => {
      if (res.data) {
        if (res.data.sex === 'M') {
          this.setData({
            sexIndex: 0
          })
        } else if (res.data.sex === 'F') {
          this.setData({
            sexIndex: 1
          })
        }
      }
      this.setData({
        user: res.data
      })
    })
  },
  //修改或者上传头像
  uploadAvatar() {
    utils.chooseImage({
      sendBefore: () => {
        wx.showLoading({
          title: '上传中...'
        })
      }
    }).then(res => {
      utils.uploadFile({
        name: 'file',
        url: '/auth/comm/user/avatar/update',
        filePath: res.tempFilePaths[0],
        formData: {
          mobile: this.data.userInfo.mobile,
          reqChannel: 5
        },
        complete: () => {
          wx.hideLoading()
        }
      }).then(res => {
        this.updateSuccess()
      })
    })
  },
  //修改其他字段
  request(params) {
    utils.axios({
      method: 'post',
      url: '/auth/comm/user/personalInfo/save',
      data: params,
      success: res => {
        this.updateSuccess()
      }
    })
  },
  //信息修改成功执行的字段
  updateSuccess() {
    utils.toast('信息修改成功')
    this.selectComponent('#nickName').hideModal()
    this.selectComponent('#intro').hideModal()
    this.getUser()
  },
  //昵称
  showNickName() {
    this.selectComponent('#nickName').showModal()
  },
  getNickName(e) {
    let params = {
      mobile: this.data.userInfo.mobile,
      reqChannel: 5,
      nickName: e.detail
    }
    this.request(params)
  },
  //性别
  setSex(e) {
    let sexList = ['M', 'F']
    let params = {
      mobile: this.data.userInfo.mobile,
      reqChannel: 5,
      sex: sexList[e.detail.value]
    }
    this.request(params)
  },
  //个人简介
  showIntro() {
    this.selectComponent('#intro').showModal()
  },
  getIntro(e) {
    let params = {
      mobile: this.data.userInfo.mobile,
      reqChannel: 5,
      personalProfile: e.detail
    }
    this.request(params)
  },

  /* 企业用户—————————————————————————————————————————————————————————————— */
  //企业用户获取默认企业
  getCompanyDefault() {
    let entInfo=utils.getCurrEntInfo()
    this.setData({
      companyDefault: entInfo,
      entTypeDictIndex: entInfo.entType?entInfo.entType-1:'',
      deviceTypeDictIndex: entInfo.defaultInvoiceDevice?entInfo.defaultInvoiceDevice-1:'',
    })
    // utils.getCompany().then(res => {
    //   this.setData({
    //     companyDefault: res.data,
    //     entTypeDictIndex: res.data.entType?res.data.entType-1:'',
    //     deviceTypeDictIndex: res.data.defaultInvoiceDevice?res.data.defaultInvoiceDevice-1:'',
    //   })
    // })
  },

  //企业类型
  setEntType(e) {
    let companyDefault = this.data.companyDefault
    companyDefault["entType"] = Number(e.detail.value)+1
    this.requestEntInfo(companyDefault)
  },
  //公司简称
  showEntShortName() {
    this.selectComponent('#entShortName').showModal()
  },
  getEntShortName(e) {
    let companyDefault = this.data.companyDefault
    companyDefault["entShortName"] = e.detail
    this.requestEntInfo(companyDefault)
  },
  //可用税率
  showAvailableTaxes() {
    utils.axios({
      method: 'get',
      url: '/auth/param/getRates',
      success: res => {
        this.setData({
          rateList:res.data
        })
        console.log(this.rateList)
        this.selectComponent('#availableTaxes').showModal()
      }
    })
  },
  getAvailableTaxes(e) {
    let companyDefault = this.data.companyDefault
    companyDefault["availableTaxes"] = e.detail
    this.requestEntInfo(companyDefault)
  },
  //默认开票设备
  setDefaultInvoiceDevice(e) {
    let companyDefault = this.data.companyDefault
    companyDefault["defaultInvoiceDevice"] = Number(e.detail.value)+1
    this.requestEntInfo(companyDefault)
  },

  //收款人
  showPayees() {
    this.selectComponent('#payees').showModal()
  },
  getPayees(e) {
    let companyDefault = this.data.companyDefault
    companyDefault["payees"] = e.detail
    this.requestEntInfo(companyDefault)
  },

  //审核人
  showReviewers() {
    this.selectComponent('#reviewers').showModal()
  },
  getReviewers(e) {
    let companyDefault = this.data.companyDefault
    companyDefault["reviewers"] = e.detail
    this.requestEntInfo(companyDefault)
  },

  //开户行
  showBankName() {
    this.selectComponent('#bankName').showModal()
  },
  getBankName(e) {
    let companyDefault = this.data.companyDefault
    companyDefault["bankName"] = e.detail
    this.requestEntInfo(companyDefault)
  },
  //联系人
  showEntContactPerson() {
    this.selectComponent('#entContactPerson').showModal()
  },
  getEntContactPerson(e) {
    let companyDefault = this.data.companyDefault
    companyDefault["entContactPerson"] = e.detail
    this.requestEntInfo(companyDefault)
  },

  //企业电话
  showEntPhone() {
    this.selectComponent('#entPhone').showModal()
  },
  getEntPhone(e) {
    let companyDefault = this.data.companyDefault
    companyDefault["entPhone"] = e.detail
    this.requestEntInfo(companyDefault)
  },

  //银行账号
  showBankAccountNumber() {
    this.selectComponent('#bankAccountNumber').showModal()
  },
  getbankAccountNumber(e) {
    let companyDefault = this.data.companyDefault
    companyDefault["bankAccountNumber"] = e.detail
    this.requestEntInfo(companyDefault)
  },

  //公司地址
  showEntAddress() {
    this.selectComponent('#entAddress').showModal()
  },
  getEntAddress(e) {
    let companyDefault =this.data.companyDefault
    companyDefault["entAddress"] = e.detail
    this.requestEntInfo(companyDefault)
  },
  //修改企业字段
  requestEntInfo(params) {
    params["userMobile"] = this.data.userInfo.mobile
    params["reqChannel"]=5
    utils.axios({
      method: 'post',
      url: '/sys/entInfo/updateBriefInfo',
      data: params,
      success: res => {
        this.setData({
          companyDefault: params
        })
        utils.toast('信息修改成功')
        this.selectComponent('#entShortName').hideModal()
        this.selectComponent('#availableTaxes').hideModal()
        this.selectComponent('#reviewers').hideModal()
        this.selectComponent('#bankName').hideModal()
        this.selectComponent('#bankAccountNumber').hideModal()
        this.selectComponent('#entContactPerson').hideModal()
        this.selectComponent('#entPhone').hideModal()
        this.selectComponent('#entAddress').hideModal()
        this.getCompanyDefault()
      }
    })
  },


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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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