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

  /**
   * 页面的初始数据
   */
  data: {
    receiptUrl: "",
    courierInput: "",
    expressageIdInput: "",
    formData: {
      orderNo: '',
      orderMoney: '',
      cusName: '',
      cusPhone: "",
      cusAddress: "",
      lotteryNum: [''],
      lotteryCount: '',
      anniversaryLotteryNum: '',
    },
    address: '',
    paramData: null,
    detailData: null,
    auditRemark: '',
    disabled: false,
    btnText: '提交',
    auditRemarkShow: false,
    disabledDetail: true,
    recordExtends: [''],
    rule: {
      "orderNo": [{
        required: true,
        message: "请填写订单编号"
      }],
      "orderMoney": [{
          required: true,
          message: "请填写订单编号"
        },
        {
          type: 'float',
          message: "请输入最多两位小数的数字"
        }
      ],
      "cusName": [{
        required: true,
        message: "请填写客户姓名"
      }],
      "cusPhone": [{
          required: true,
          message: "请填写手机号"
        },
        {
          type: 'phone',
          message: "请输入正确的手机号码"
        }
      ],
      "cusAddress": [{
        required: true,
        message: "请填写客户地址"
      }],
      // "lotteryCount": [{
      //   required: true,
      //   message: "请填写抽奖券数量"
      // }],
      // "anniversaryLotteryNum": [{
      //   required: true,
      //   message: "周年庆抽奖编码"
      // }
    // ],
      // "cusAddress":[
      //   {
      //     pattern:/^[A-Z]{1}[0-9]{9}/g,
      //     message:"请输入以大写字母开头的长度为10的字符"
      //   }
      // ]
    },
  },
  inputChange(e) {
    console.log(e, 9999)
    this.setData({
      formData: Object.assign(this.data.formData, {
        [e.target.dataset.key]: e.detail.value
      })
    })
    // this.setData({
    //     auditRemark: e.detail.value
    // })
  },
  inputLotteryNum(e) {
    let lotteryNum = this.data.formData.lotteryNum
    lotteryNum[e.target.dataset.index] = e.detail.value
    this.setData({
      formData: Object.assign(this.data.formData, {
        lotteryNum
      })
    })
  },
  addRemark() {
    let lotteryNum = this.data.formData.lotteryNum
    lotteryNum.push('')
    console.log(lotteryNum, 'lotteryNum')
    this.setData({
      formData: Object.assign(this.data.formData, {
        lotteryNum
      })
    })
  },
  delRemark(e) {
    let lotteryNum = this.data.formData.lotteryNum
    lotteryNum.splice(e.target.dataset.index, 1)
    this.setData({
      formData: Object.assign(this.data.formData, {
        lotteryNum
      })
    })
  },
  /**
   * 取消
   */
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  submit() {
    if (!format.validateForm(this.data.formData, this.data.rule)) {
      return
    }

    if (this.data.formData.auditRemark == "" && this.data.detailData?.auditStatus == 2) {
      wx.showToast({
        title: '请输入驳回意见',
        icon: "none"
      })
      return
    }
    Http.post({
      url: config.api.offlineSaveOrUpdate,
      data: {
        id: this.data.paramData.tag === 'add' ? '' : this.data.detailData.id,
        activityId: this.data.paramData.tag === 'add' ? '' : this.data.detailData.activityId,
        orderNo: this.data.formData.orderNo,
        orderMoney: this.data.formData.orderMoney,
        cusName: this.data.formData.cusName,
        cusPhone: this.data.formData.cusPhone,
        cusAddress: this.data.formData.cusAddress,
        lotteryNum: JSON.stringify(this.data.formData.lotteryNum),
        lotteryCount: this.data.formData.lotteryCount,
        anniversaryLotteryNum: this.data.formData.anniversaryLotteryNum,
        // 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 === 'add' ? false : true,
      auditRemarkShow:  options.auditStatus == '2' ? true : false,
      btnText: options.auditStatus == '0' || options.auditStatus == '1'|| options.auditStatus == '3' ? '' : options.auditStatus == '2' ? '修改' : '提交',
    })
    wx.setNavigationBarTitle({
      title: options.auditStatus == '0' || options.auditStatus === '1' ? '查看活动审批' : options.auditStatus == '2' ? '修改活动审批' : '提交活动审批',
    })
    console.log(options, 1111)
    options.tag !== 'add' && this.getData()
  },
  getData() {
    Http.get({
      url: config.api.offlineRecordGetInfoById,
      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
      })
      const formData = {
        orderNo: res.data.orderNo,
        orderMoney: res.data.orderMoney,
        cusName: res.data.cusName,
        cusPhone: res.data.cusPhone,
        cusAddress: res.data.cusAddress,
        lotteryNum: JSON.parse(res.data.lotteryNum),
        lotteryCount: res.data.lotteryCount,
        anniversaryLotteryNum: res.data.anniversaryLotteryNum,
      }
      if(res.data.auditStatus === 2){
        formData.auditRemark = res.data.auditRemark
      }
      console.log(res.data.auditStatus === 0 || res.data.auditStatus === 1 ? true : false, 888888)
      this.setData({
        formData,
        disabledDetail: res.data.auditStatus === 0 || res.data.auditStatus === 1 || res.data.auditStatus === 3? true : false
      })
      if (res.data.auditStatus === 0) {
        this.setData({
          disabled: false
        })
      }
    }).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 () {

  }
})