//index.js
//获取应用实例
const app = getApp();
var api = require("../../config/api.config.js").api;

Page({
  data: {
    item: {
      navigationBarTitle: "首页",
      statusBarHeight: app.globalData.statusBarHeight
    },
    home:true,
    hidden: true, //预约弹窗
    date: [], //日期列表
    up: [], //上午
    down: [], //下午
    Failed: true, //未通过
    review: true, //审核中
    firstDate: '', //默认时间
    appointment: [], //预约
    time: '', //预约的时间
    otherTime: 0, //其他时间
    type: 0, //选择其他小时
    id: '', //飞机id
    plane:'',//几号飞机
    userInfo:{},
   
  },
  //确认
  bindconfirm: function (e) {
    var that = this;
    var id = that.data.id;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        wx.request({
          url: api.Ding,
          method: "POST",
          header: {
            'token': res.data
          },
          data: {
            id: id,
            type: that.data.type
          },
          success: function (res) {
            if (res.data.code == 1) {
              wx.showToast({
                title: res.data.msg,
              })
              that.setData({
                hidden: true,
                otherTime: 0, //其他时间
                type:0
              })
              that.tableListOnload();
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: "none"
              })
            }
          }
        })
      }
    })

  },
  //取消
  bindcancel:function(e){
    this.setData({
      hidden:true,
      otherTime: 0, //其他时间
      type:0
    })
  },
  //选择时间
  binddate: function (e) {
    var date = e.currentTarget.dataset.date;
    var that = this;
    that.setData({
      firstDate: date,
      up: [], //上午
      down: [], //下午
    })
    that.tableListOnload();
  },
  //选择其他时间
  bindselectTime: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.id;
    console.log(e)
    that.setData({
      type: type,
      otherTime: type
    })
  },
  //取消预约
  bindreservation:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.getStorage({
      key: 'token',
      success:function(res){
        wx.request({
          url: api.IsKill,
          method:"POST",
          header:{
            "token":res.data
          },
          data:{
            id:id
          },
          success:function(res){
            if(res.data.code == 1){
              wx.showModal({
                title:"提示",
                content:'是否取消预约',
                success:function(res){
                  if (res.confirm) {
                    console.log('用户点击确定')
                    wx.request({
                      url: api.Kill,
                      method:"POST",
                      header:{
                        "token":wx.getStorageSync('token')
                      },
                      data:{
                        id:id
                      },
                      success:function(res){
                        console.log(res);
                        if(res.data.code == 1){
                          that.tableListOnload();
                        }else{
                          wx.showToast({
                            title: res.data.msg,
                            icon:"none"
                          })
                        }
                      
                      }
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
          }
        })
      }
    })
 
  },
  //飞机预约
  bindplane: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.id);
    var id = e.currentTarget.dataset.id;
    var time = e.currentTarget.dataset.time;
    var plane = e.currentTarget.dataset.plane;
   
    wx.getStorage({
      key: 'token',
      success(res) {
        wx.request({
          url: api.LocationWindow,
          method: "POST",
          header: {
            "token": res.data
          },
          data: {
            id: id
          },
          success: function (res) {
            console.log(res);
            if (res.data.code == 1) {
              that.setData({
                hidden: false,
                appointment: res.data.data,
                time: time,
                id: id,
                plane:plane,
               
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: "none"
              })
            }
          }
        })
      }
    })


  },
  //列表
  tableListOnload: function () {
    wx.showLoading({
      title: '加载中...',
    })
    var that = this;
    wx.request({
      url: api.TableList,
      method: "POST",
      data: {
        date: that.data.firstDate
      },
      success: function (res) {
        var fname = res.data.data.up[0].fname;
        var name = res.data.data.up[0].name;
        var bname = res.data.data.up[0].bname;
        var aname = res.data.data.up[0].aname;
        that.setData({
          up: res.data.data.up,
          down: res.data.data.down,
          fname:fname,
          name:name,
          aname:aname,
          bname:bname

        })
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
  },
  onLoad: function () {
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success:function(res){
        that.setData({
          userInfo:res.data
        })
        if(res.data.status == 0){
          that.setData({
            Failed:false,
            review:true,
            home:true
          })
        }else if(res.data.status == 1){
          that.setData({
            Failed:true,
            review:true,
            home:false
          })
        }else if(res.data.status == 2){
          that.setData({
            Failed:true,
            review:false,
            home:true
          })
        }
      }
    })
    //日期
    wx.request({
      url: api.WeekDay,
      method: "POST",
      success: function (res) {
        console.log(res)
        that.setData({
          date: res.data.data,
          firstDate: res.data.data[0].date
        });
        that.tableListOnload();

      }
    })
  },

})