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

  /**
   * 页面的初始数据
   */
  data: {
    email: '',
    invoiceId: '',
    imgUrl: '',
    loading: false,
    color: '#000',
    background: '#ffffff',
    show: true,
    animated: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      email: options.email,
      invoiceId: options.invoiceId,
      imgUrl: options.imgUrl
    })
  },
  goBack(){
    wx.navigateBack({
      delta: 2
    })
  },
  setEmail(e) {
    const {
      value
    } = e.target.dataset;
    this.setData({
      email: value || e.detail.value,
    })
  },
  imgPreview: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current,//当前显示图片的http链接，我这边是把图片转成了base64
      urls: [current] //需要预览的图片http链接列表
    })
  },
  submit:util.throttle(function(){
    const { email } = this.data;
    if (!email) {
      wx.showToast({
        title: '请输入电子邮箱',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(email))) {
      wx.showToast({
        title: '您输入的格式有误，请重新输入',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.InvoiceSend();
  },3000),
  InvoiceSend() {
    app.$http.get('/invoice/InvoiceSend', {
      email: this.data.email,
      invoiceId: this.data.invoiceId
    }).then(({ status, data = {} }) => {
      if (status == 0) {
        wx.navigateTo({
          url: '/pages/invoice/pages/operation/operation?operate=send&status=1&email='+this.data.email
        })
      } else {
        wx.navigateTo({
          url: '/pages/invoice/pages/operation/operation?operate=send&status=0'
        })
      }
    }).catch(() => {
      wx.navigateTo({
        url: '/pages/invoice/pages/operation/operation?operate=send&status=0'
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})