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

  /**
   * 页面的初始数据
   */
  data: {
    bookTime: '',
    company: '',
    name: '',
    phone: '',
    plateSn: '',
    content: '',
    userId: 0,
    orgId: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var userId = wx.getStorageSync("visitorToUserId")?Number(wx.getStorageSync("visitorToUserId")):0;
    var orgId = wx.getStorageSync("visitorOrgId")?Number(wx.getStorageSync("visitorOrgId")):0;
    if(userId <= 0 || orgId <= 0){
      wx.redirectTo({
        url: '/pages/splash/splash?type=visitor'
      });
    }else{
      this.setData({
        userId: userId,
        orgId: orgId
      });
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.ajaxReadyCallback = res => {
      var apiname = res.data.apiname;
      if (apiname == 'book'){
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          duration: 2000
        })
        setTimeout(() =>{
          wx.navigateTo({
            url: '/pages/visitor/index'
          })
        },1000);
      }
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  companyChange(e){
    this.setData({
      company: e.detail.value
    });
  },

  nameChange(e){
    this.setData({
      name: e.detail.value
    });
  },

  phoneChange(e){
    this.setData({
      phone: e.detail.value
    });
  },

  plateChange(e){
    this.setData({
      plateSn: e.detail.value
    });
  },

  contentChange(e){
    this.setData({
      content: e.detail.value
    });
  },

  onPickerChange3: function (e) {
    console.log(e.detail);
    this.setData({
      bookTime: e.detail.dateString
    })
  },

  goYY(){
    wx.navigateTo({
      url: '/pages/visitor/index'
    });

  },

  submitBtn(){
    if(!this.data.company){
      wx.showToast({
        title: "请输入单位",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!this.data.name){
      wx.showToast({
        title: "请输入姓名",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!this.data.phone){
      wx.showToast({
        title: "请输入电话",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!this.data.bookTime || this.data.bookTime=='选择时间'){
      wx.showToast({
        title: "请选择到访时间",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if(!this.data.content){
      wx.showToast({
        title: "请输入到访事由",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    
    const param = {
      company: this.data.company,
      name: this.data.name,
      phone: this.data.phone,
      plateSn: this.data.plateSn,
      bookTime: this.data.bookTime,
      content: this.data.content,
      type: 3,
      fromUserId: this.data.userId,
      orgId: this.data.orgId
    }
    console.log(param);
    app.ajax2({
      url: app.globalData.visitorServerUrl + 'server/visitorOrder/order2',
      type: 'POST',
      apiname: 'book',
      data: param
    });
  }
})
