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

  /**
   * 页面的初始数据
   */
  data: {
    receiptUrl: "",
    courierInput: "",
    expressageIdInput: "",
        orderNo: '',
        orderMoney: '',
        cusName: '',
        phone: '',
        address: '',
        paramData: null,
        detailData: null,
        auditRemark: '',
        disabled: false,
        btnText: '提交',
        auditRemarkShow: false,
        disabledDetail: false,
        recordExtends: [{
          remark: ''
        }]
  },
  inputauditRemark(e) {
    this.setData({
        auditRemark: e.detail.value
    })
  },
  inputorderNo(e) {
    this.setData({
        orderNo: e.detail.value
    })
  },
  inputorderMoney(e) {
    this.setData({
        orderMoney: e.detail.value
    })
  },
  inputcusName(e) {
    this.setData({
        cusName: e.detail.value
    })
  },
  inputphone(e) {
    this.setData({
        phone: e.detail.value
    })
  },
  inputaddress(e) {
    this.setData({
        address: e.detail.value
    })
  },

  inputRemark(e) {
    let recordExtends = this.data.recordExtends
    recordExtends[e.target.dataset.index].remark = e.detail.value
    this.setData({
      recordExtends: recordExtends
    })
  },
  addRemark() {
    let recordExtends = this.data.recordExtends
    recordExtends.push({remark: ''})
    this.setData({
      recordExtends: recordExtends
    })
  },
  delRemark(e) {
    let recordExtends = this.data.recordExtends
    recordExtends.splice(e.target.dataset.index, 1)
    this.setData({
      recordExtends: recordExtends
    })
  },
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  submit() {
    if(this.data.paramData.tag == 'detail' && this.data.detailData?.auditStatus !== 0) {
        wx.navigateBack({
            delta: 1
        })
        return
    }
    if (this.data.auditRemark == "" && this.data.detailData?.auditStatus == 2) {
        wx.showToast({
          title: '请输入驳回意见',
          icon: "none"
        })
        return
      }
    if (this.data.orderNo == "") {
      wx.showToast({
        title: '请输入订单号',
        icon: "none"
      })
      return
    }
    if (this.data.orderMoney == "") {
      wx.showToast({
        title: '请输入订单金额',
        icon: "none"
      })
      return
    }
    if (this.data.cusName == "") {
      wx.showToast({
        title: '请输入客户姓名',
        icon: "none"
      })
      return
    }
    if (this.data.phone == "") {
      wx.showToast({
        title: '请输入客户电话',
        icon: "none"
      })
      return
    }
    if (this.data.address == "") {
      wx.showToast({
        title: '请输入客户地址',
        icon: "none"
      })
      return
    }
    Http.post({
      url: config.api.raffleSaveOrUpdateRecord,
      data: {
        id:  this.data.paramData.tag === 'add' ? '' : this.data.detailData.id,
        activityId: this.data.paramData.tag === 'add' ? this.data.paramData.activityId : this.data.detailData.activityId,
        orderNo: this.data.orderNo,
        orderMoney: this.data.orderMoney,
        cusName: this.data.cusName,
        phone: this.data.phone,
        address: this.data.address,
        auditStatus: this.data.paramData.tag !== 'add' && this.data.detailData.auditStatus === 2 ? 0 : undefined,
        recordExtends: this.data.recordExtends
      }
    }).then(res=>{
      wx.showToast({
        title: '提交成功！',
        icon:"none"
      })
      wx.navigateBack({
        delta: 1
      })
    }).catch(err=>{
      wx.showToast({
        title: err.message?err.message:err.data,
        icon: "none"
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id,
      paramData: options,
      disabled: false,// options.tag !== 'add' ? true : false,
      disabledDetail: options.tag == 'detail' ,
      btnText: options.tag == 'add' ? '提交' : options.tag == 'detail' ? '已完成' : '修改'
    })
    options.tag !== 'add' && this.getData()
  },
  getData() {
    Http.get({
      url: config.api.raffleRecordInfo,
      data: {
        id: this.data.id
      }
    }).then(res => {
        res.data.cashRate = res.data.cashRate + '%'
        res.data.mallRate = res.data.mallRate + '%'
        res.data.merchantRate = res.data.merchantRate + '%'
        this.setData({
            detailData: res.data
        })
        this.setData({
            orderNo: res.data.orderNo,
            orderMoney:res.data.orderMoney,
            cusName: res.data.cusName,
            phone: res.data.phone,
            address: res.data.address,
            auditRemark: res.data.auditRemark,
            auditRemarkShow: res.data.auditStatus === 2,
            recordExtends: res.data.recordExtends
        })
        if(res.data.auditStatus === 0) {
          this.setData({
            disabled: false,
            disabledDetail: false,
            btnText: '提交'
        })
      }
    }).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() {

  }
})