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

  /**
   * 页面的初始数据
   */
  data: {
    type: 0, //0是新增，1是编辑
    list: [{
      selected: true,
      customerType: 2,
      text: '企业'
    }, {
      selected: false,
      customerType: 1,
      text: '个人及政府事业单位'
    }],
    currentIndex: 0,
    customerName: '',
    entTaxId: '',
    customerMobile: '',
    email: '',
    address: '',
    contactPhone: '',
    bankName: '',
    bankAccountNumber:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: Number(options.type)
    })
    if (this.data.type === 1) {
      this.getDetail()
      wx.setNavigationBarTitle({
        title: '编辑购方'
      })
    }
  },

  //如果是编辑操作，就获取回显信息
  getDetail() {
    let detail = wx.getStorageSync('configBuyer')
    this.setData({
      customerName: detail.customerName,
      entTaxId: detail.customerEntTaxId,
      address: detail.address,
      contactPhone: detail.contactPhone,
      bankName: detail.bankName,
      bankAccountNumber: detail.bankAccountNumber,
      customerMobile: detail.customerMobile,
      email: detail.email,
      customerId: detail.customerId
    })
  },

  //提交
  submitData() {
    let params = {
      mobile: utils.getInfo().mobile,
      entTaxId: wx.getStorageSync('entTaxId'),
      reqChannel: 5,
      customerType: this.data.list[this.data.currentIndex].customerType,
      customerName: utils.trimAll(this.data.customerName),
      customerMobile: utils.trimAll(this.data.customerMobile),
      email: utils.trimAll(this.data.email)
    }
    if (this.data.type === 1) {
      params.customerId = this.data.customerId
    }
    if (params.customerName === '') {
      utils.toast('购方名称不能为空')
      return
    }
    if (this.data.currentIndex === 0) {
      if (utils.trimAll(this.data.entTaxId) === '') {
        utils.toast('税号不能为空')
        return
      } else {
        params.customerEntTaxId = utils.trimAll(this.data.entTaxId)
      }
      if (utils.trimAll(this.data.address) !== '') {
        params.address = utils.trimAll(this.data.address)
      }
      if (utils.trimAll(this.data.contactPhone) !== '') {
        params.contactPhone = utils.trimAll(this.data.contactPhone)
      }
      if (utils.trimAll(this.data.bankName) !== '') {
        params.bankName = utils.trimAll(this.data.bankName)
      }
      if (utils.trimAll(this.data.bankAccountNumber) !== '') {
        params.bankAccountNumber = utils.trimAll(this.data.bankAccountNumber)
      }
    }
    /* if (params.customerMobile === '') {
      utils.toast('您的个人手机号不能为空')
      return
    }
    if (params.email === '') {
      utils.toast('您的个人邮箱不能为空')
      return
    } */
    if (params.customerMobile !== '') {
      if (!utils.testMobile(params.customerMobile)) {
        utils.toast('请填写正确的手机号')
        return
      }
    }
    if (params.email !== '') {
      if (!utils.testEmail(params.email)) {
        utils.toast('请填写正确的邮箱')
        return
      }
    }
    utils.axios({
      method: 'post',
      url: '/sys/customer/saveOrUpdate',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '购方保存中...',
          mask: true
        })
      },
      complete() {
        wx.hideLoading()
      },
      success: res => {
        utils.funPrev(prev => {
          prev.toFresh()
          utils.toast('购方保存成功')
          wx.navigateBack()
        })
      }
    })
  },

  //选择
  choose(e) {
    let index = Number(e.currentTarget.dataset.index)
    let list = this.data.list
    for (let i = 0; i < list.length; i++) {
      list[i].selected = false
    }
    list[index].selected = true
    this.setData({
      list,
      currentIndex: index
    })
  },

  //购方名
  getcustomerName(e) {
    let value = e.detail.value
    this.setData({
      customerName: value
    })
  },
  getentTaxId(e) {
    let value = e.detail.value
    this.setData({
      entTaxId: value
    })
  },
  getcustomerMobile(e) {
    let value = e.detail.value
    this.setData({
      customerMobile: value
    })
  },
  getemail(e) {
    let value = e.detail.value
    this.setData({
      email: value
    })
  },
  getaddress(e) {
    let value = e.detail.value
    this.setData({
      address: value
    })
  },
  getcontactPhone(e) {
    let value = e.detail.value
    this.setData({
      contactPhone: value
    })
  },
  getbankName(e) {
    let value = e.detail.value
    this.setData({
      bankName: value
    })
  },
  getbankAccountNumber(e) {
    let value = e.detail.value
    this.setData({
      bankAccountNumber: value
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})