var app = getApp();
// pages/orderDetail/orderDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    venueUrl : app.venueUrl,
    imgRoute  : '/route/file/downloadfile/true/',
    token:'',
    groundBookingId: '',
    serial: '',
    venuesName: '',
    projectName: '',
    venuePanorama: '',
    gmtCreate: '',
    price: '',
    itemList:[],
    cancelType: false,
  },
  
  //查询订单详情
  getDetail: function(){
    var self = this;
    app.dialog.loading('请稍等');
    app.restAjax.get(app.restAjax.path('{venueUrl}/app/booking/getmyticketdetail/{groundBookingId}', [app.venueUrl,self.data.groundBookingId]), 
      {}, {headers:{token : self.data.token}}, 
      function (code, data) {
        self.setData({
          serial: data.data.serial,
          venuesName: data.data.venuesName,
          projectName: data.data.projectName,
          venuePanorama: data.data.venuePanorama.split(',')[0],
          gmtCreate: data.data.gmtCreate.substring(0,19),
          itemList: data.data.itemDTOList,
          cancelType: false
        })
        var totalPrice = 0;
        self.data.itemList.forEach(element => {
          if(element.orderType != '1'){
            totalPrice += Number(element.price);
          }
          if(element.orderType == '0'){
            self.setData({
              cancelType: true
            })
          }
        });
        self.setData({
          price: totalPrice
        })
      },
      function (code, data) {
        app.dialog.msg(data.msg);
      },function(){
        wx.hideLoading();
      })
  },

  //取消预订场次
  cancelItem: function(options){
    var self = this;
    var bookingItemId = options.currentTarget.dataset.id;
    var unCancelNum = 0;
    self.data.itemList.forEach(element => {
      if(element.orderType == '0'){
        unCancelNum++;
      }
    });
    //取消一个
    if(unCancelNum > 1){
      app.dialog.loading('请稍等');
      app.restAjax.delete(app.restAjax.path('{venueUrl}/app/booking/removemyticketitem/{groundBookingId}/{bookingItemId}', 
      [app.venueUrl,self.data.groundBookingId,bookingItemId]), {}, {headers:{token : self.data.token}}, 
      function (code, data) {
        self.getDetail();
      },
      function (code, data) {
        app.dialog.msg(data.msg);
      },function(){
        wx.hideLoading();
      })
    }
    //取消全部
    if(unCancelNum <= 1){
      self.cancelAll();
    }
  },

  //全部取消
  cancelAll: function(){
    var self = this;
    app.dialog.loading('请稍等');
      app.restAjax.delete(app.restAjax.path('{venueUrl}/app/booking/removemyticket/{groundBookingId}', 
      [app.venueUrl,self.data.groundBookingId]), {}, {headers:{token : self.data.token}}, 
      function (code, data) {
        self.getDetail();
      },
      function (code, data) {
        app.dialog.msg(data.msg);
      },function(){
        wx.hideLoading();
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getStorage({
      key: 'token',
      success: (result) => {
        this.setData({
          token : result.data,
          groundBookingId: options.groundBookingId
        })
        this.getDetail();
      }
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})