// nova-tourism/pages/my/my-order/my-refund/refund-good/refund-express/index.js
let Parse = getApp().Parse;
const company = getApp().globalData.company
const uid = Parse.User.current()?.id
const dateF = require("../../../../../../../utils/date")
const qiniuUploader = require("../../../../../../../utils/qiniuUploader");
Page({

  /**
   * 页面的初始数据
   */
  data: {

    order: null, //订单
    expressNum: null, //快递单号

    isShowExpress: false, //快递公司弹框
    expVal: '', //快递公司搜索关键字
    timeout: null, //搜索框计时器
    expComList: [], //快递公司列表
    checkExp: null, //选中的公司
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let {
      oid
    } = options
    console.log(options)
    this.setData({
      oid
    })
    this.getOrder()
  },

  /**提交 */
  async submit() {
    let {
      expressNum,order,checkExp
    } = this.data
    console.log(expressNum)
    if(order.way!='店内退换') {
      if(!expressNum){
        wx.showToast({
          title: '请填写快递单号',
          icon:'none'
        })
        return
      }
      if(!checkExp?.objectId){
        wx.showToast({
          title: '请选择快递公司',
          icon:'none'
        })
        return
      }
    }else{
      wx.showToast({
        title: '请前往店内退换',
        icon:'none'
      })
      return
    }
    let content = `退换方式：${order.way}。原因：${order.cause}。描述：${order.text}。快递公司：${checkExp.name}。快递公司代码：${checkExp.code}。快递单号：${expressNum}。`
    let query = new Parse.Query('Order')
    let o = await query.get(order.objectId)
    o.set('content',content)
    await o.save()
    await wx.showToast({
      title: '提交成功，等待商家收货。',
      icon: 'none'
    })
    setTimeout(() => {
      wx.navigateBack({
        delta: 1,
      })
    }, 800);
  
  },
/**确认退款 */
async submit02(){
  let {
    order
  } = this.data
  let query = new Parse.Query('Order')
    let o = await query.get(order.objectId)
    o.set('status','700')
    await o.save()
    await wx.showToast({
      title: '操作成功',
      icon: 'success'
    })
    setTimeout(() => {
      wx.navigateBack({
        delta: 1,
      })
    }, 800);
},


  /**获取订单 */
  async getOrder() {
    let {
      oid,expressNum,checkExp
    } = this.data
    let query = new Parse.Query('Order')
    query.include('targetObject', 'store', 'store.user')
    let d = await query.get(oid)
    let order = d?.toJSON()
    console.log(order)

    let parts = order?.content?.split('。')
    order.way = parts[0]?.split('：')[1]
    order.cause = parts[1]?.split('：')[1]
    order.text = parts[2]?.split('：')[1]
    let exp_name = parts[3]?.split('：')[1]||''
    let exp_code = parts[4]?.split('：')[1]||''
    expressNum = parts[5]?.split('：')[1]||''
    if(exp_name&&exp_code){
      let exp_query = new Parse.Query('ExpressCompany')
      exp_query.equalTo('name',exp_name)
      exp_query.equalTo('code',exp_code)
      let e = await exp_query.first()
      if(e?.id) checkExp = e.toJSON()
    }
    this.setData({
      order,
      orderParse: d,expressNum,checkExp
    })
  },

  closeExpress() {
    this.setData({
      isShowExpress: false
    });
  },
  openExpress() {
    this.getPointerItem()
    this.setData({
      isShowExpress: true
    });
  },

  /**指针搜索框变化 */
  async valChange(e) {
    await this.setData({
      expVal: e.detail || ''
    })
    let {
      timeout
    } = this.data
    clearTimeout(timeout)
    timeout = setTimeout(async () => {
      await this.setData({
        expComList: [],
      })
      this.getPointerItem()
    }, 1000);
    this.setData({
      timeout
    })
  },
  /**选中快递公司 */
  checkPointer(e) {
    let {
      index
    } = e.currentTarget.dataset
    let {
      expComList
    } = this.data
    this.setData({
      checkExp: expComList[index]
    })
    this.closeExpress()
  },
  /** 查询快递公司*/
  async getPointerItem() {
    let {
      expVal,
      expComList
    } = this.data
    let query = new Parse.Query('ExpressCompany')
    query.contains('name', expVal)
    query.limit(30)
    query.skip(expComList?.length || 0)
    let d = await query.find()
    let list = d?.map(item => item.toJSON())
    this.setData({
      expComList: [...(expComList || []), ...(list || [])]
    })
  },

  /**查看图片 */
  previewImage(e) {
    let {
      src,
      urls
    } = e.currentTarget.dataset
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: urls || [] // 需要预览的图片http链接列表
    })
  },
  /** 复制*/
  copyStr(e) {
    let {
      str
    } = e.currentTarget.dataset
    if (str) {
      wx.setClipboardData({
        data: str,
        success: () => {
          wx.showToast({
            title: '复制成功',
            icon: 'success',
          });
        },
        fail: (err) => {
          console.error('复制失败:', err);
          wx.showToast({
            title: '复制失败',
            icon: 'none',
            duration: 2000
          });
        }
      });
    } else {
      wx.showToast({
        title: '复制失败',
        icon: 'none',
        duration: 2000
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})