let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    personNum: {
      ManNum: 0,
      ChirdNum: 0,
      BabyNum: 0,
      OldPeopleNum: 0,
      GuestList: [],
      PreferPrice: 0,//应收总金额,
      ContactMobile: '',
      ContactEmail: '',
      ContactName: '',
      EnterID: 0,
      CustomerId: 0,
      PlaceOrderFrom: 1,
      OrderForm: 7,
      OrderSource: 2,
    },
    cr:0,
    et:0,
    ye:0,
    lr:0,
    allPP: 0,
    userInfo: {},
    isLogin: app.isLogin,
    editIndex: -1,
    details: {},
    price: 0,
    allPrice: 0,
    detailShow: false,
    priceShow: false,
  },
  // 价格详情
  priceBoxShow: function () {
    this.setData({
      priceShow: !this.data.priceShow,
    })
  },
  // 详情弹窗
  closeDetail: function (){
    this.setData({
      detailShow: !this.data.detailShow,
    })
  },
  //联系人信息填写
  lianxirenChange: function (e) {
    let name = e.currentTarget.dataset.type
    this.data.personNum[name] = e.detail.value
    this.setData({
      personNum: this.data.personNum
    })
  },
  // 增加完事后 变动价格数据
  setPrice: function (){
    // ageType 1 成年 2小孩 3婴儿 4老人
    let cr = 0, et = 0, ye = 0,lr = 0;
    let ppList = this.data.personNum.GuestList;
    for (let i = 0; i < ppList.length; i++) {
      if (ppList[i].ageType == 0) {
        cr = cr + 1
      } else if (ppList[i].ageType == 1) {
        et = et + 1
      } else if (ppList[i].ageType == 2) {
        ye = ye + 1
      } else if (ppList[i].ageType == 3) {
        lr = lr + 1
      }
    }
    this.setData({
      cr: cr,
      et: et,
      ye: ye,
      lr: lr,
      allPP: cr + et + lr,
      allPrice: (cr + et + lr) * this.data.price
    })
  },
  // 付款生成订单按钮
  StartYd: function () {
    let msg = this.data.personNum
    if (this.data.allPP < 1) {
      wx.showToast({
        title: '请添加乘机人',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (msg.ContactName == "") {
      wx.showToast({
        title: '请填写联系人',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (msg.ContactMobile == "") {
      wx.showToast({
        title: '请填写联系人手机号码',
        icon: 'none',
        duration: 1000
      })
      return
    }
    msg.ManNum = this.data.cr
    msg.ChirdNum = this.data.et
    msg.BabyNum = this.data.ye
    msg.OldPeopleNum = this.data.lr
    msg.GuestNum = this.data.allPP + this.data.ye
    msg.seatNum = this.data.allPP
    msg.PreferPrice = this.data.allPP * this.data.price
    for (let i = 0; i < msg.GuestList.length; i++){
      msg.GuestList[i].airOrderId = msg.airOrderId
      msg.GuestList[i].airProductId = msg.airProductId
    }
    console.log(msg)
    app.$api('sellorder_post_SetOrderInfoForAirTicket', msg).then(res => {
      console.log(res)
    }).catch(err => { }); 
  },
  // deletePP 删除乘机人
  deletePP: function (e){
    let index = Number(e.currentTarget.dataset.index);
    let pp = this.data.personNum.GuestList[index];
    let _this = this;
    wx.showModal({
      title: '是否删除“' + pp.SurName + pp.Name +'”',
      content: '删除后不可恢复',
      success(res) {
        if (res.confirm) {
          _this.data.personNum.GuestList.splice(index, 1)
          _this.setData({
            personNum: _this.data.personNum
          })
          _this.setPrice()
        } else if (res.cancel) {
          
        }
      }
    })
  },
  //addPP 添加乘机人
  addPP: function () {
    // if (this.data.allPP == this.data.details.residue) {
    //   wx.showToast({
    //     title: '位置不够了~',
    //     icon: 'none',
    //     duration: 1000
    //   })
    //   return
    // }
    wx.navigateTo({
      url: '/pages/Ticket/TicketSetpp/TicketSetpp?edit=' + 0 + '&app=' + this.data.allPP + '&residue=' + this.data.details.residue,
    })
    this.setData({
      editIndex: -1
    })
    wx.removeStorage({
      key: 'Addppdate',
      success: function (res) { },
    })
  },
  // 修改乘机人
  editPP: function (e){
    let index = Number(e.currentTarget.dataset.index)
    this.setData({
      editIndex: index
    })
    wx.removeStorage({
      key: 'Addppdate',
      success: function(res) {},
    })
    wx.setStorage({
      key: "ppdate",
      data: this.data.personNum.GuestList[index]
    })
    wx.navigateTo({
      url: '/pages/Ticket/TicketSetpp/TicketSetpp?edit=' + 1 + '&app=' + this.data.allPP + '&residue=' + this.data.details.residue,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.removeStorage({
      key: 'Addppdate',
      success: function (res) {},
    })
    console.log(options)
    let userInfo = wx.getStorageSync('admin');
    this.setData({
      userInfo: userInfo,
      "personNum.CustomerId": userInfo.id
    })
    app.$api('sell_get_GetCustomerToSellInfo', {}).then(res => {
      this.setData({
        "personNum.EnterID": res[0].EmployeeId
      })
    }).catch(err => { }); 
    this.getDetails(options.id)
  },
  // 获取机票详情
  getDetails: function (id) {
    app.$apiJavaNew('/api/dmc/airticket/getB2BAirticketInfo', { AirTicketId: id}).then(res => {
      let data = res
      this.setData({
        details: res,
        "personNum.airProductId": data.airProductId,
        "personNum.airTicketId": data.id,
        "personNum.departureCityId": data.departure_city,
        "personNum.Unit_Price": this.isLogin ? data.scatterB2BPrice : data.scatterB2CPrice,//	成交单价
        "personNum.TC_Price": this.isLogin ? data.scatterB2BPrice : data.scatterB2CPrice,//机票单价
        "personNum.returnArriveCityId": data.arrival_city,
        "personNum.TCID": data.tCID,
        "personNum.FreightSpace": data.freightSpace,
        "personNum.GoCityTime": data.goFlightTime,
        "personNum.BackCityTime": data.backFlightTime, 
        "perSonNum.PreferPrice": this.isLogin ? data.scatterB2BPrice : data.scatterB2CPrice,
        price: this.isLogin ? data.scatterB2BPrice : data.scatterB2CPrice,
      })
    }).catch(err => {})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { 
    let _this = this
    wx.getStorage({
      key: 'Addppdate',
      success(res) {
        console.log(res)
        let data = res.data
        if (_this.data.editIndex == -1) {  // 新增
          _this.data.personNum.GuestList.push(data)
          _this.setData({
            personNum: _this.data.personNum
          })
        } else { // 修改
          _this.data.personNum.GuestList[_this.data.editIndex] = data
          _this.setData({
            personNum: _this.data.personNum
          })
        }
        _this.setPrice()
      }
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})