let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{
      ageType: "0",//1 成年 2小孩 3婴儿 4老人
      airOrderId:  0,//机票订单id
      airProductId:  0,//机票产品id
      airProductId: '',//机票产品id
      Birthday: '',//出生日期
      MobilePhone: '',
      SurName: '',
      Name: "",
      Nationality: 2,
      PassportNo: "",//护照号码
      Sex: '1',
      PassportExpiry: "",
    },
    ppType: [
      {
        id: "1",
        name: "成人"
      },
      {
        id: "2",
        name: "小孩"
      },
      {
        id: "3",
        name: "婴儿"
      },
      {
        id: "4",
        name: "老人"
      },
    ],
    ppTypeIndex: 0,
    countroylist: [],
    countroylistIndex: 0,
    allpp: 0,
    residue: 0,
  },
  lianxirenChange: function (e){
    let name = e.currentTarget.dataset.type
    this.data.info[name] = e.detail.value
    this.setData({
      info: this.data.info
    })
  },
  //切换性别
  changeSex:function(e){
    let type = e.currentTarget.dataset.type
    this.setData({
      "info.Sex": type
    })
  },
  // 添加人
  addpp: function(e) {
    let msg = this.data.info
    console.log(msg)
    let allpp = this.data.allpp + 1;
    if (msg.ageType !== '2' && allpp > this.data.residue) {
      wx.showToast({
        title: '余票不够了',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (msg.Birthday == "") {
      wx.showToast({
        title: '请选择出生日期',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (msg.SurName == "" || msg.Name == "" ) {
      wx.showToast({
        title: '请填写姓或名',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (msg.PassportNo == "") {
      wx.showToast({
        title: '请填写护照信息',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (msg.PassportNo == "") {
      wx.showToast({
        title: '请选择证件有效期',
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (msg.MobilePhone == "") {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 1000
      })
      return
    }
    wx.setStorage({
      key: "Addppdate",
      data: msg
    })
    wx.navigateBack({
      delta: 1  // 返回上一级页面。 
    })
  },
  bindDateChange(e) { 
    let t = e.currentTarget.dataset.type
    if (t == 'b') {
      this.setData({
        'info.Birthday': e.detail.value
      })
    } else {
      this.setData({
        'info.PassportExpiry': e.detail.value
      })
    }
  },
  //类型切换
  bindPickerChangeType: function (e) {
    this.setData({
      "info.ageType": e.detail.value,
      ppTypeIndex: e.detail.value
    })
  },
  // 国籍切换
  bindPickerChangeCoun: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      "info.Nationality": this.data.countroylist[e.detail.value].ID,
      countroylistIndex: e.detail.value
    })
  },
  // 获取国籍
  getcountroylist: function (){
    app.$api('b2b_get_GetCountryForSMAirTick', {}).then(res => {
      this.setData({
        countroylist: res
      })
    }).catch(err => { }); 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let _this = this
    this.setData({
      allpp: Number(options.app),
      residue: Number(options.residue)
    })
    if (options.edit == "0"){ // 新增
      
    } else { // 修改
      wx.getStorage({
        key: 'ppdate',
        success(res) {
          console.log(res.data)
          _this.setData({
            info: res.data
          })
        }
      })
    }
    wx.removeStorage({
      key: 'ppdate',
      success: function(res) {},
    })
    this.getcountroylist()
  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

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

  }
})