// pages/reserve/index.js
import moment, { parseTwoDigitYear } from '../../utils/moment.min';
moment.locale('en', {
  longDateFormat: {
    l: "YYYY-MM-DD",
    L: "YYYY-MM-DD HH:mm:ss",
  },
})
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.height * 2 + 25,
    windowHeight: app.globalData.windowHeight,
    navbarData: {
      name: '报餐'
    },
    restaurant: '当前餐厅',
    today: '',
    // 点餐
    currentTab: 0,
    // 早餐，午餐，晚餐
    infoTab: 0,
    // 餐列表
    breakfastList: [],
    lunchList: [],
    dinnerList: [],
    // 餐数量
    oneStepper: 0,
    twoStepper: 0,
    thrStepper: 0,
    // 选餐表单
    form: {
      breakfast: { reserve: 0, list: [], },
      lunch: { reserve: 0, list: [], },
      dinner: { reserve: 0, list: [], },
    },
  },
  // 选择日期
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({ today: e.detail.value })
  },
  // 禁止左右滑动
  stopTab: function (e) {
    return false
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  // 点击选择餐食
  infoClickTab: function (e) {
    var that = this;
    let data = e.target.dataset.current;
    if (this.data.infoTab === data) return false;
    else that.setData({ infoTab: data });

  },
  // 选择餐数量
  // 早餐,午餐，晚餐
  oneChange: function (e) {
    if (this.data.infoTab == 0) this.setData({ oneStepper: e.detail })
    else if (this.data.infoTab == 1) this.setData({ twoStepper: e.detail })
    else if (this.data.infoTab == 2) this.setData({ thrStepper: e.detail })
  },
  // 增加
  onePlus: function (e) {
    console.log('增加');
    let data = e.target.dataset.data;
    let type = this.data.infoTab;
    if (type == 0) {//早餐
      var meal = this.data.form.breakfast;
      // 计算卡路里
      let reserve = meal.reserve + data.reserve;
      this.setData({ 'form.breakfast.reserve': reserve })
    } else if (type == 1) {//午餐
      var meal = this.data.form.lunch;
      // 计算卡路里
      let reserve = meal.reserve + data.reserve;
      this.setData({ 'form.lunch.reserve': reserve })
    } else if (type == 2) {//晚餐
      var meal = this.data.form.dinner;
      // 计算卡路里
      let reserve = meal.reserve + data.reserve;
      this.setData({ 'form.dinner.reserve': reserve })
    }
    // 查询下标
    let res = meal.list.findIndex(i => i.id === data.id);
    // 查询数据
    let arr = meal.list.find(i => i.id === data.id);
    if (arr) {//已有值
      let qwe = { ...arr, num: arr.num + 1 } //id: arr.id, title: arr.title
      meal.list.splice(res, 1, qwe)
    } else {//没有值
      let arr = { ...data, num: 1 } //id: data.id, title: data.title
      meal.list.push(arr)
    }
  },
  // 减少
  oneMinus: function (e) {
    console.log('减少');
    let data = e.target.dataset.data;
    let type = this.data.infoTab;
    if (type == 0) {//早餐
      var meal = this.data.form.breakfast;
      // 计算减少卡路里
      let reserve = meal.reserve - data.reserve;
      this.setData({ 'form.breakfast.reserve': reserve })
    } else if (type == 1) {//午餐
      var meal = this.data.form.lunch;
      // 计算减少卡路里
      let reserve = meal.reserve - data.reserve;
      this.setData({ 'form.lunch.reserve': reserve })
    } else if (type == 2) {//晚餐
      var meal = this.data.form.dinner;
      // 计算减少卡路里
      let reserve = meal.reserve - data.reserve;
      this.setData({ 'form.dinner.reserve': reserve })
    }
    // 查询下标
    let res = meal.list.findIndex(i => i.id === data.id);
    // 查询数据
    let arr = meal.list.find(i => i.id === data.id);
    if (arr) {
      let qwe = { id: arr.id, title: arr.title, num: arr.num - 1 }
      meal.list.splice(res, 1, qwe)
    }
  },
  // 提交
  onSubmit: function () {
    console.log(this.data.form);
    console.log(app.globalData.userInfo)
    const data = JSON.parse(JSON.stringify(this.data.form))
    if (app.globalData.userInfo.openid) data.openid =app.globalData.userInfo.openid;
    if (this.data.today) data.date = this.data.today
    console.log(data)
    // wx.request({
    //   url: `${app.globalData.publicUrl}/api/st/dining/order`,
    //   method: "post",
    //   header: {
    //     'x-tenant': 'test'
    //   },
    //   data,
    //   success: res => {
    //     console.log(res)
    //   },
    //   error: err => {
    //     wx.showToast({
    //       title: err.msg,
    //       icon: 'error'
    //     })
    //   }
    // })
    wx.showToast({
      title: '完成点餐',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.searchDate();
    let one = [{
      id: '1',
      url: '/image/home/image2.jpg',
      title: '早餐1',
      num: '1000',
      reserve: 15
    }, {
      id: '2',
      url: '/image/home/image2.jpg',
      title: '早餐2',
      num: '1000',
      reserve: 15
    },]
    if (one) this.setData({ breakfastList: one })
    let two = [{
      id: '3',
      url: '/image/home/image2.jpg',
      title: '午餐1',
      num: '1000',
      reserve: 15
    }, {
      id: '4',
      url: '/image/home/image2.jpg',
      title: '午餐2',
      num: '1000',
      reserve: 15
    },]
    if (two) this.setData({ lunchList: two })
    let thr = [{
      id: '5',
      url: '/image/home/image2.jpg',
      title: '晚餐1',
      num: '1000',
      reserve: 15
    }, {
      id: '6',
      url: '/image/home/image2.jpg',
      title: '晚餐2',
      num: '1000',
      reserve: 15
    },]
    if (thr) this.setData({ dinnerList: thr })
  },
  // 查询时间
  searchDate: function () {
    let today = moment().format('YYYY-MM-DD');
    if (today) this.setData({ today: today })
    wx.request({
      url: `${app.globalData.publicUrl}/api/st/dining/arrange/getByDate?date=` + today,
      method: "get",
      header: {
        'x-tenant': 'test'
      },
      success: res => {
        const { arrange } = res.data.data;
        if (!arrange) return;
        let { breakfast, lunch, dinner } = arrange
        breakfast = this.dealImg(breakfast)
        lunch = this.dealImg(lunch)
        dinner = this.dealImg(dinner)
        this.setData({
          breakfastList: breakfast,
          lunchList: lunch,
          dinnerList: dinner
        })
      },
      error: err => {
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      }
    })
  },
  dealImg(list) {
    for (let i of list) {
      if (i.img && i.img.length > 0 && i.img[0]) i.url = `${app.globalData.fileUrl}${i.img[0].url}`
    }
    return list;
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})