b_addnew.js 5.59 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,
    },
    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 () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    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',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){}
},
selectphone:function(e){
    this.data.form.phone=e.detail.phone;
    this.data.form.rname=e.detail.rname;
    this.data.form.cusdem=e.detail.cusdem;
    this.data.form.sex=e.detail.sex;
    this.setData({
        form:this.data.form,
        searchlist:null
    })
},
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
    })
}
})