b_addnew.js 5.15 KB
// pages/book/b_addnew.js
import util from '../../utils/util';
import comm from '../../utils/bookCommon';
import App from '../../utils/api';
const {regeneratorRuntime} = global
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateStr:[],
    form:{

      date:0,
      time:0,
        pnum_str:'1-4',
      tabid:0,

        rname:'',
        phone:'',
        sex:1,
        cusdem:'',

        tid:[],

    },
      tables:[],
      grand:[
          {
              sex:1,
              grand_title:'先生',
              isselect:true
          },
          {
              sex:2,
              grand_title:'女士',
              isselect:false
          }
      ],
      buttonhide:true,

      calendarMaskLoad:false,
      calendarMask:false,
      dayStyle:[
          { month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' },
          { month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' }
      ],
  },
    tablesAll:[],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let d=new Date()/1000;
    let dateStr=[
        util.unixtoTime(d,'MM-DD'),
        util.unixtoTime(d+86400,'MM-DD'),
    ];
    this.setData({
        dateStr:dateStr,
    })
    this.sellerid=util.cache('sellerid');
    this.getTables()
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      setTimeout(()=>{
          this.setData({
              calendarMaskLoad:true,
          })
      },1000);

  },

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

  },

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

  },

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

  },

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

  },

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

  },
    selectedValue(e){
      //console.log(e);
      let key=util.attr(e,'key');
        let val;
      if(e.type=='input'){
          val=util.val(e);
      }else{
          val=util.attr(e,'value');
      }

      let form=this.data.form;
      let sobj={};
      if(key=='tid'){
          let ts=this.data.tables;
          if(ts[val].isselect){
              ts[val].isselect=false;
              form.tid.remove(ts[val].id);
          }else{
              ts[val].isselect=true;
              form.tid.push(ts[val].id);
          }
          sobj.tables=ts;
      }else{
          form[key]=val;
      }
      //console.log(e.type,key,val,form);
        sobj.form=form;
        sobj.buttonhide=true;
      if(form.tid.length>0 && form.rname.length>0){
          sobj.buttonhide=false;
      }
      this.setData(sobj)

      if('date,time,pnum_str,tabid'.indexOf(key)>-1){
        this.getTables();
      }
    },
    async getTables(){
        let form=this.data.form;
        try{
            let rdate=await comm.getTimeSpace(this.data.dateStr[this.data.form.date],this.data.form.time);
            console.log('rdate',rdate);
            App.Reserve.GetTableList({
                sid:this.sellerid,
                pnum_str:form.pnum_str,
                rdate:rdate,
                tid:form.tabid,
            }).then(res=>{
                let tables=[];
                res.data.forEach(p=>{
                  if(tables.length>=3) return true;
                  p.list.forEach(t=>{
                      if(tables.length>=3) return true;
                      tables.push(t);
                  })
                })
                this.tablesAll=res.data;
                this.setData({
                    tables:tables
                })
            })
        }catch (e){
            this.setData({
                tables:[]
            })
        }
    },
    async formsubmit(){
        if(this.data.buttonhide) return;
        try {
            let rdate = await comm.getTimeSpace(this.data.dateStr[this.data.form.date], this.data.form.time);
            console.log('rdate', rdate);
            this.setData({
                buttonhide: true,
            })
            let form = this.data.form;
            form.tid = form.tid.toString();
            form.sid = this.sellerid;
            form.rdate =rdate
            App.Reserve.SetReserveInfo(form).then(res => {
                if(res.ret==200){
                    wx.navigateTo({
                        url:'b_detail?id='+res.data.id,
                    });
                }else{
                    util.toast(res.msg,'none')
                }
            })
        }catch (e){}
    },
    calendarShow(){
        this.setData({
            calendarMask:true
        })
    },
    dateChange(event){
        console.log(event);
        let clickDay = event.detail.day;
        let changeDay = `dayStyle[1].day`;
        let changeBg = `dayStyle[1].background`;
        this.setData({
            [changeDay]: clickDay,
            [changeBg]: "#84e7d0"
        })
    },
    calendarClose(){
        this.setData({
            calendarMask:false
        })
    },
})