var util = require('../../utils/util.js');
var api = require('../../config/api.js');
let referCode = 0
// pages/graveDetail/graveDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    currentSize: 0,
    phoneTypes: ['853', '852', '86'],
    name: '',
    email: '',
    phone: '',
    desc: '',
    statusMap: ['現貨', '已預定', '已售', '現貨', '預售', '轉售', '已下架'],
    detail: {}
  },

  bindIndexChange(val) {
    this.setData({
      index: val.detail.value
    })
  },

  bindDescChange(val) {
    this.setData({
      currentSize: val.detail.value.length,
      desc: val.detail.value
    })
  },

  bindNameChange(val) {
    this.setData({
      currentSize: val.detail.value.length,
      name: val.detail.value
    })
  },
  bindPhoneChange(val) {
    this.setData({
      currentSize: val.detail.value.length,
      phone: val.detail.value
    })
  },
  bindEmailChange(val) {
    this.setData({
      currentSize: val.detail.value.length,
      email: val.detail.value
    })
  },

  gotoImg(ev) {
    if (this.data.detail.boxPositionPic) {
      wx.navigateTo({
        url: '/pages/graveImage/graveImage?img=' + this.data.detail.boxPositionPic,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let res = await util.request(api.BoxOrderDetail, {
      id: options.orderId
    }, 'GET')

    // let res = await util.request(api.BoxDetail, {
    //   id: options.orderId
    // }, 'GET')


    this.setData({
      detail: res.data
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },


  bindCanceHandle() {
    if (this.data.detail.isHandle) {
      return
    }

    wx.showActionSheet({
      alertText: '是否取消預訂',
      itemColor: '#FA5151',
      itemList: ['确定'],
      success: async _ => {
        let res = await util.request(api.BoxCancelOrder + '?orderId=' + this.options.orderId, {}, 'POST', 'application/json')
        if (res.code === 0) {
          wx.showToast({
            title: '取消預訂成功',
            icon: 'success'
          })
          getApp().callOrderBuyCbs({referCode: referCode++})
          wx.navigateBack()
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

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

  }
})