// pages/operation/index.js
const config = require('../../config/config.js')
const Http = require('../../utils/HttpBasics.js')
const util = require('../../utils/util.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDetailIf: true,
    type: null,
    paramData: '',
    array: ['美国', '中国', '巴西', '日本'],
    merchentList: [],
    spendStr: null,
    creditNum: 0,
    submitAble: true,
    thoroughfare:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type,
      paramData: options
    })
    this.getMerchent();
  },
  /**
   * 保存积分操作数据
   */
  submitForm() {
    let _this = this;
    const reg = /^[1-9]\d*$/;
    if (!reg.test(_this.data.creditNum) || Number(_this.data.creditNum) > 9999999 || Number(_this.data.creditNum) <1) {
      tt.showToast({
        title: '消费金额限制1~9999999的正整数！',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return;
    }
    _this.setData({
      submitAble: false
    })
    Http.post({
      url: config.api.addCredit,
      data: {
        creditNum: _this.data.creditNum,
        creditType: 12,//消费积分
        cuserId: _this.data.paramData.id,
        operatorType: 3,
        merchantId: _this.data.paramData.merchant_id,
        changePurpose: _this.data.thoroughfare
      }
    })
      .then(res => {
        _this.setData({
          merchentList: res.data,
          submitAble: true
        })
        tt.showToast({
          title: '保存成功！',
          icon: 'none',
          duration: 2000,
          mask: false
        });
        tt.navigateTo({
          url: '/pages/editSuccess/index?type=2&creditNum=' + _this.data.creditNum + '&creditAmount=' + res.data.creditAmount,
        })
      })
      .catch(err => {
        _this.setData({
          submitAble: true
        })
        tt.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  getPrice(e) {
    this.setData({
      creditNum: e.detail.value
    })
  },
  getThoroughfare(e){
    this.setData({
      thoroughfare: e.detail.value
    })
  },
  getMerchent() {
    let _this = this;
    Http.get({
      url: config.api.getMerchentList
    })
      .then(res => {
        _this.setData({
          merchentList: res.data
        })
      })
      .catch(err => {
        tt.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  bindPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      submitAble:true
    })
  },

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

  },

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

  },

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

  },

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

  }
})