b_addnew.js 9.82 KB
// pages/book/b_addnew.js
import util from '../../utils/util';
import comm from '../../utils/bookCommon';
import App from '../../utils/api';
const {regeneratorRuntime} = global
const formTpl={
    rid:'',
    date:0,
    time:0,
    pnum_str:'1-4',
    tabid:0,

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

    tid:[],

}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateStr:[],
    form:JSON.parse(JSON.stringify(formTpl)),
      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) {
    this.setDateStr();
    this.sellerid=util.cache('sellerid');
      let timePickerArray= await comm.getDayTimeSpace();
      this.setData({
          timePickerArray: timePickerArray,
      })
      if(options.id>0){
          this.initModify(options.id);
      }
  },
    /**
     *
      * @param d
     *
     */
  async setDateStr(d=new Date()/1000){
      let dateStr=[
          util.unixtoTime(d,'MM-DD `DAY/周WW`'),
          util.unixtoTime(parseInt(d)+86400,'MM-DD `DAY/周WW`'),
      ];
      let form=this.data.form;
      form.time=await comm.getTimeSpaceIndex(d);
      let obj={
          dateStr:dateStr,
          form:form,
      };
      this.setData(obj)
        return obj;

  },

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

  },

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

  /**
   * 生命周期函数--监听页面隐藏
   */
  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);
          if(e.currentTarget.dataset.key=='phone'){
              var that=this;
              if(e.detail.value!='' && e.detail.value.length>=3)  {
                  App.Reserve.GetReserveUser({keywords:e.detail.value}).then(function(json){
                      if(json.ret==200 && json.data.length>0){
                          that.setData({
                              searchlist:json.data
                          })
                      }else{
                          that.setData({
                              searchlist:null
                          })
                      }
                  })
              }else{
                  this.setData({
                      searchlist:null
                  })
              }
          }
      }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',this.data.dateStr[this.data.form.date],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 stimestr=this.data.timePickerArray[this.data.form.time][this.data.timePickerIndex[this.data.form.time]];
            console.log('stimestr', stimestr,this.data.form.time,this.data.timePickerIndex);
            let rdate = await comm.getTimeSpace(this.data.dateStr[this.data.form.date],
                this.data.form.time,stimestr);
            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){
                    let _id=(res.data.id || res.data)
                    this.setData({
                        form:JSON.parse(JSON.stringify(formTpl)),
                        tablesed:null,
                    })
                    App.Reserve.Detail({rid:_id,openid:''},true)
                    wx.navigateTo({
                        url:'b_detail?id='+res.data.id,
                    });
                }else{
                    util.toast(res.msg,'none')
                }
            })
        }catch (e){}
    },
    selectphone:function(e){
        this.data.form.phone=e.detail.phone;
        this.data.form.rname=e.detail.rname;
        this.data.form.cusdem=e.detail.cusdem;
        var form=this.data.form
        this.data.form.sex=e.detail.sex;
        this.setData({
            form:this.data.form,
            searchlist:null
        })
        this.conditionInfo();
    },
    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"
        })
        this.setDateStr(util.timetouUnix(event.detail.year+'/'+event.detail.month+'/'+event.detail.day))
        this.getTables()
    },
    calendarClose(){
        this.setData({
            calendarMask:false
        })
    },
    async  selectdesk(){
        let rdate=await comm.getTimeSpace(this.data.dateStr[this.data.form.date],this.data.form.time);
        console.log(this.data.form)
        wx.navigateTo({
            url:'b_desk?rdate='+rdate+'&tid='+this.data.form.tabid+'&pnum_str='+this.data.form.pnum_str
        })
    },
    conditionInfo:function(){
        var form=this.data.form
        if(form.tid.length>0 && form.rname.length>0){
            this.data.buttonhide=false;
        }
        this.setData({
            buttonhide:this.data.buttonhide
        })
    },
    /**
     * 选中时间区域
     */
    bindTimePickerChange(e){
        let timeIndex=util.attr(e,'timeIndex');
        console.log(e,timeIndex);
        let timePickerIndex=this.data.timePickerIndex;
        timePickerIndex[timeIndex]=util.val(e);
        this.setData({
            timePickerIndex:timePickerIndex,
        })
    },
    /**
     * 修改初始化
     */
    async initModify(id){
        try{
            let res= await App.Reserve.Detail({rid:id,openid:''});

            console.log('initModify',res);
            //设置日期
            let obj = await this.setDateStr(res.data.rdate);
            //设置时间
            let nd=util.unixtoTime(res.data.rdate,'HH:II');
            let timePickerIndex=this.data.timePickerIndex
            console.log('initModify-',this.data.timePickerArray,obj.form.time)
            this.data.timePickerArray[obj.form.time].forEach((dd,ii)=>{
                console.log('initModify-',nd,dd)
                if(nd==dd){
                    timePickerIndex[obj.form.time]=ii;
                    return true;
                }
            })

            console.log('initModify-timePickerIndex',timePickerIndex);
            obj.form.pnum_str=res.data.pnum_str;
            obj.form.rname=res.data.rname;
            obj.form.phone=res.data.phone;
            obj.form.sex=res.data.sex;
            obj.form.cusdem=res.data.cusdem;
            obj.form.rid=id;
            obj.form.tid=[];
            res.data.tables.forEach((t,ti)=>{
                t.isselect=true
                res.data.tables[ti]=t;
                obj.form.tid.push(t.id)
            })
            this.setData({
                timePickerIndex:timePickerIndex,
                form:obj.form,
                tablesed:res.data.tables,
            })
        }catch (e){
            console.log('initModify',e)
        }
    },
})