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

  /**
   * 页面的初始数据
   */
  data: {
    receiptUrl: "",
    obj: {},
    courierInput: "",
    expressageIdInput: "",
  },
  setCourierInput(e) {
    console.log(e)
    this.setData({
      courierInput: e.detail.value
    })
  },
  setExpressageIdInput(e) {
    console.log(e)
    this.setData({
      expressageIdInput: e.detail.value
    })
  },
  submit() {
    if (this.data.courierInput == "") {
      wx.showToast({
        title: '请输入快递公司名称',
        icon: "none"
      })
      return
    }
    if (this.data.expressageIdInput == "") {
      wx.showToast({
        title: '请输入快递单号',
        icon: "none"
      })
      return
    }
    if (this.data.receiptUrl == "") {
      wx.showToast({
        title: '请上传快递照片',
        icon: "none"
      })
      return
    }
    debugger
    if (this.data.expressageIdInput.length>18) {
      wx.showToast({
        title: '快递单号不能超过18位',
        icon: "none"
      })
      return
    }
    Http.post({
      url: config.api.sendGoods,
      data: {
        id: this.data.id,
        deliveryInfo: JSON.stringify({
          courierInput: this.data.courierInput,
          expressageIdInput: this.data.expressageIdInput,
          receiptUrl: this.data.receiptUrl
        })
      }
    }).then(res=>{
      wx.showToast({
        title: '提交成功！',
        icon:"none"
      })
      wx.navigateBack({
        delta: 1
      })
    }).catch(err=>{
      wx.showToast({
        title: err.message?err.message:err.data,
        icon: "none"
      })
    })
  },
  uploadImg() {

    let that = this;
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: config.api.imgUpload, // 仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'token': app.globalData.token
          },
          success(res) {
            const data = res.data
            that.setData({
              receiptUrl: JSON.parse(res.data).data.url
            })
            // do something
          },
          fail(err) {
            wx.showToast({
              title: err.message ? err.message : err.data,
              icon: "none"
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id
    })
    this.getData()
  },
  getData() {
    Http.get({
      url: config.api.ouponOrderId,
      data: {
        couponOrderId: this.data.id
      }
    }).then(res => {
      let tempObj = res.data
      tempObj.createDate = util.formatTime(tempObj.createDate, 'yyyy-MM-dddd hh:mm:ss')
      tempObj.couponPriceStr = (tempObj.couponPrice / 100).toFixed(2)
      tempObj.shippingAddressObj = JSON.parse(tempObj.shippingAddress)
      let tempRegion = JSON.parse(tempObj.shippingAddressObj.region)
      if (tempObj.deliveryInfo) {
        tempObj.deliveryInfo = JSON.parse(tempObj.deliveryInfo)
        this.setData({
          receiptUrl: tempObj.deliveryInfo.receiptUrl,
          courierInput: tempObj.deliveryInfo.courierInput,
          expressageIdInput: tempObj.deliveryInfo.expressageIdInput
        })
      }
      
      let str = ""
      tempRegion.map(item => {
        str += item
      })
      tempObj.regionStr = str
      this.setData({
        obj: tempObj
      })
      console.log(this.data.obj)
    }).catch(err => {
      wx.showToast({
        title: err.message ? err.message : err.data,
      })
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})