let Parse = getApp().Parse
const company = getApp().globalData.company
let navigationBarHeight = getApp().globalData.statusBarHeight + 44;
const rechText = require('../../../../../utils/rech-text')
const dateF = require('../../../../../utils/date')
const dateServ = require('../../../../../utils/date')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusMap: {
      100: '未入住',
      200: '已入住',
      400: '已退房',
      500: '申请退款',
      601: '退款审核通过',
      602: '退款已驳回',
      700: '退款成功',
      800: '已完成',
      101: '已取消支付',
      102: '申请退房',
      103: '退房成功',
      104: '退房被驳回',
      105: '未支付',
    },
    id: "",
    order: null,
  },
  async getRoomOrder() {
    let id = this.data.id
    let RoomOrder = new Parse.Query('RoomOrder')
    RoomOrder.include('room')
    RoomOrder.include('shopStore')
    let roomOrder = await RoomOrder.get(id)
    let currentStore = wx.getStorageSync('store')
    if(roomOrder?.get('shopStore')?.id!=currentStore?.objectId){
      this.setData({
        isHide:true
      })
      return
    }
    console.log(roomOrder, 666666);
    let Order = roomOrder.toJSON()
    Order.createdAt = dateF.formatTime("YYYY-mm-dd HH:MM:SS", Order.createdAt)
    // Order.room.note = rechText.formatRichText(Order.room.note)
    Order['day'] = parseInt((new Date(Order.endTime.iso).getTime() - new Date(Order.startTime.iso).getTime()) / (1000 * 60 * 60 * 24));
    Order.startTime = dateServ.formatTime('YYYY-mm-dd HH:MM', Order.startTime.iso)
    Order.endTime = dateServ.formatTime('mm-dd HH:MM', Order.endTime.iso)
    this.setData({
      order: Order
    })
    console.log(this.data.order);
  },

  async submit() {
    try {
      let id = this.data.id
      let {order,statusMap}=this.data
      let RoomOrder = new Parse.Query("RoomOrder")
      let roomOrder = await RoomOrder.get(id)
      if((order.status == 100 || order.status == 500 || order.status == 602)&&roomOrder?.get('status') == 200){
        wx.showToast({
          title: '已执行入住操作，无需重复',
          icon:'none'
        })
        return
      }
      if (roomOrder?.get('status') == 100 || roomOrder?.get('status') == 500 || roomOrder?.get('status') == 602) {
        roomOrder.set("status", 200)
        await roomOrder.save()
        wx.showToast({
          title: '已入住',
          icon: 'success'
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000);
      } else if (roomOrder?.get('status') == 200 || roomOrder?.get('status') == 102 || roomOrder?.get('status') == 104) {
        roomOrder.set("status", 400)
        await roomOrder.save()
        wx.showToast({
          title: '退房成功',
          icon: 'success'
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000);
      }else{
        wx.showToast({
          title: `当前订单 ${statusMap[roomOrder?.get('status')]} 无法操作。`,
          icon: 'none'
        })
      }
    } catch (error) {
      wx.showToast({
        title: '核销失败',
        icon: 'none'
      })
    }
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id
    this.setData({
      id: id
    })
    this.getRoomOrder()
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})