Page({

  /**
   * 页面的初始数据
   */
  data: {
    peopleNum: 1,
    activeTab: 0,
    productInfo: {
      title: "葛优躺（北京躺）电影足疗",
      shop: "大华店",
    },
    tabs: [],
    orderTime: "",
    orderTimeHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    var tabs = await this.createTimeList(268);
    this.setData({
      tabs
    });
    this.getOrderHeight();
    this.checkedTime()
  },
  onSlideChangeEnd: function (e) {
    var that = this;
    that.setData({
      activeTab: e.detail.index
    })
  },
  //动态生成产品的父盒子高度
  getOrderHeight: function () {
    var activeTab = this.data.activeTab;
    var orderTimeHeight = (this.data.tabs[activeTab].list.length) / 5 * 130;
    this.setData({
      orderTimeHeight
    })
  },
  createTimeList: async function (price) {
    var titles = ["今天", "明天", "12-23", "12-24", "12-25", "12-26", "12-27"];
    var day = ["12-21", "12-22", "12-23", "12-24", "12-25", "12-26", "12-27"];
    const tabs = [];
    let list = [{
        id: "1",
        time: "8:00",
        price: price,

      },
      {
        id: "2",
        time: "8:30",
        price: price,

      },
      {
        id: "3",
        time: "9:00",
        price: price,

      },
      {
        id: "4",
        time: "9:30",
        price: price,

      },
      {
        id: "5",
        time: "10:00",
        price: price,

      },
      {
        id: "6",
        time: "10:30",
        price: price,

      },
      {
        id: "7",
        time: "11:00",
        price: price,

      },
      {
        id: "8",
        time: "11:30",
        price: price,

      }, {
        id: "9",
        time: "12:00",
        price: price,

      },
      {
        id: "10",
        time: "12:30",
        price: price,

      },
      {
        id: "11",
        time: "13:00",
        price: price,

      },
      {
        id: "12",
        time: "13:30",
        price: price,

      },
      {
        id: "13",
        time: "14:00",
        price: price,

      },
      {
        id: "14",
        time: "14:30",
        price: price,

      },
      {
        id: "15",
        time: "15:00",
        price: price,

      },
      {
        id: "16",
        time: "15:30",
        price: price,

      }, {
        id: "17",
        time: "16:00",
        price: price,

      },
      {
        id: "18",
        time: "16:30",
        price: price,

      },
      {
        id: "19",
        time: "17:00",
        price: price,

      },
      {
        id: "20",
        time: "17:30",
        price: price,

      },
      {
        id: "21",
        time: "18:00",
        price: price,

      },
      {
        id: "22",
        time: "18:30",
        price: price,

      },
      {
        id: "23",
        time: "19:00",
        price: price,

      },
      {
        id: "24",
        time: "19:30",
        price: price,

      },
      {
        id: "25",
        time: "20:00",
        price: price,

      }
    ];

    titles.forEach(function (item, i, array) {
      list.forEach(function (item, i, array) {
        array[i].timeId = i;
        array[i].inTime = true;
        array[i].checked = false;
      })

      tabs.push({
        title: titles[i],
        day: day[i],
        list
      })
    })
    // tabs[2].list.splice(7,7)
    return tabs;


  },

  //判断产品是否过时间
  checkedTime: function (e) {
    // 设置一小时内订单不能购买
    var timeLimit = 1;
    var nowTime = new Date(2020, 11, 21, 18, 29, 0);
    var hour = nowTime.getHours();
    var tabs = this.data.tabs[0];
    var list = firstTabs.list;
    // console.log(tabs[5].list)
    list.forEach(function (item, i, array) {
      var orderHour = item.time.split(":")[0];
      if (parseInt(orderHour) <= parseInt(hour)) {
        item.inTime = false;
      }
    })
    this.setData({
      tabs:nowTabs
    })
  },

  getOrderTime: function (e) {
    // var date = document.getElementById("orderDate");
    // console.log(date.target)
    // console.log(e.currentTarget)
    //选中日期的下标
    var i = this.data.activeTab;
    //选中时间的下标
    var index = e.currentTarget.dataset.i;
    //获取选中的日期
    var date = this.data.tabs[i].day;
    var time = e.currentTarget.dataset.time;
    var oTime = date + '/' + time;

    //设置选中的样式
    var tabs = this.data.tabs;
    var list = tabs[i].list;
    //先清空其他选中的样式
    list.forEach(function (item, i, array) {
      item.checked = false;
    })
    if (list[index].inTime) {
      list[index].checked = true;
      this.data.tabs[i].list = list;
    }
    this.setData({
      orderTime: oTime,
      tabs
    })
  },

  addPeopleNum: function (e) {
    var num = e.currentTarget.dataset.num;
    var maxNum = 9;
    if (num < maxNum) {
      num++;
    }
    this.setData({
      peopleNum: num
    })
  },

  reducePeopleNum: function (e) {
    var num = e.currentTarget.dataset.num;
    if (num > 1) {
      num--;
    }
    this.setData({
      peopleNum: num
    })
  },



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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})