b_addnew.js 12.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
// pages/book/b_addnew.js
import util from '../../../utils/util';
import App from '../../../utils/api';
import PubSub from '../../../utils/PubSub'
const formTpl = {
  rid: '',
  pnum: 6,
  tabid: 0,

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

  tid: [],

}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type:1, // type 1/2/3 新增/修改/桌位
    time:0,//组件最终构造的到店时间
    runTime:0,//当前时间戳,或修改到店,或桌位预订时间
    form: JSON.parse(JSON.stringify(formTpl)),
    tables: [],
    grand: [
      {
        sex: 1,
        grand_title: '先生',
        isselect: true
      },
      {
        sex: 2,
        grand_title: '女士',
        isselect: false
      },
      {
        sex: 3,
        grand_title: '公司',
        isselect: false
      }
    ],
    buttonhide: true,
    tablesLength: 0,
    pnumInputFocus: false,
    pageMarginBottom: 0,
    speciallist: [],
    CustomRemark:[],
    CustomRemarkMoreMask:false,
  },
  reserve_id: '',
  checkedTables:{},//已选中的桌子集合
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    PubSub.on('arrangeDesk',(res)=>{
      console.log('arrangeDesk',res)
      this.getTables();
    })
    this.options=options
    this.sellerid = util.cache('enter_params')['seller_id'];
    this.getCustomRemark();
    if(options.id>0){
      //修改
      this.reserve_id=options.id
      return this.initModify();
    }
    if(options.hasQuick==1){
      //桌位预订
      return this.initQuick();
    }
    this.setData({
      runTime:parseInt(new Date()/1000),
      type:1
    })
  },

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

  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    PubSub.off('arrangeDesk')
  },

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

  },

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

  },

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

  },
  //到店时间选择通知
  addTimeDurItemsEvent(e){
    //{time: 1577788200, ddate_solt: 2, timeDur: "1577786400-1577804400"}
    console.log('addTimeDurItemsEvent',e.detail,util.unixtoTime(e.detail.time))
    this.time=e.detail.time
    this.ddate_solt=e.detail.ddate_solt
    this.timeDur=e.detail.timeDur//预订所在时段区间时间戳
    this.setData({
      ...e.detail,
    })
    this.getTables();
  },
  getCustomRemark(){
    App.User.GetCustomRemark({
      uid:util.cache('enter_params')['manager_id'],
    }).then(res=>{
      let CustomRemark=[];
      res.data.forEach(r=>{
        if(r.often_use){
          CustomRemark.push(r);
        }
      })
      this.setData({
        CustomRemark:CustomRemark,
      })
    })
  },
  oftenUse(e){
    let id=util.attr(e,'id');
    let val=util.attr(e,'value');
    let CustomRemark=this.data.CustomRemark;
    let form=this.data.form;
    CustomRemark.forEach(r=>{
      if(id==r.id){
        if(r.active){
          form.cusdem=form.cusdem.replace(val+'、','')
        }
        r.active=!r.active
        if(r.active){
          form.cusdem+=val+'、'
        }
      }
    })
    this.setData({
      CustomRemark:CustomRemark,
      form:form,
    })
    this.conditionInfo()
  },
  CustomRemarkMore(){
    this.setData({
      CustomRemarkMoreMask:!this.data.CustomRemarkMoreMask
    })
  },
  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
    })
  },

  async getTables(){
    let form = this.data.form;
    let tableid = util.cache('tableid');
    console.log('getTables-tableid', tableid.length, this.reserve_id);
    //if (tableid.length < 1 && this.reserve_id) {
    if (tableid.length < 1) {
      tableid = this.data.form.tid;
    }
    console.log('getTables-tableid', tableid, tableid.length);
    try {
      App.Reserve.GetTableList({
        sid: util.cache('enter_params')['seller_id'],
        //pnum_str:form.pnum_str,
        pnum: form.pnum,
        rdate: util.unixtoTime(this.time,'YYYYMMDD'),
        tid: form.tabid,
        rid: this.reserve_id,
        ddate_solt:this.ddate_solt
      }).then(res => {
        let tables = [];
        let tablesLength = 0;
        if (tableid.length > 0) {
          let toastTables={...this.checkedTables}
          let resData=res.data.slice(1,res.data.length);
          console.log('resData',resData)
          resData.forEach(p => {
            //if(tables.length>=3) return true;
            p.list.forEach(t => {
              delete toastTables[t.id]
              tablesLength++;
              if (tables.length >= 3) return true;
              if (tableid._indexOf(t.id) > -1) {
                t.isselect = true;
                tables.push(t);
              }
            })
          })
          console.log('toastTables',toastTables,this.data.form.tid,this.checkedTables);
          //清理已选中的桌位
          let toastTablesInfo=[];
          let tid=this.data.form.tid
          Object.keys(this.checkedTables).forEach(ckid=>{
            if(toastTables[ckid]){
              delete this.checkedTables[ckid]
              tid.remove(ckid);
              toastTablesInfo.push(toastTables[ckid]);
            }
          })
          if(toastTablesInfo.length>0){
            this.setData({
              [`form.tid`]:tid,
            })
            util.toast(toastTablesInfo.toString()+'已被占用啦','none');
          }
          wx.removeStorage({key: 'tableid'})
        }
        console.log('tables-first',tables);
        if(tables.length<1){
          tablesLength=0;
          let fortids=[];
          res.data.forEach((p,ri) => {
            p.list.forEach(t => {
              if(fortids._indexOf(t.id)<0) {
                tablesLength++;
                tables.push(t);
                fortids.push(t.id);
              }
            })
          })
          if (tables.length >= 3) {
            tables = tables.slice(0, 3);
          }
        }
        this.tablesLength = tablesLength;
        this.setData({
          tables: tables,
          tablesLength: tablesLength
        })
        this.conditionInfo();
      })
    } catch (e) {
      this.setData({
        tables: []
      })
    }
  },
  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();
  },
  selectdesk(){
    wx.navigateTo({
      url: 'b_desk?rdate=' + this.time + '&tid=' + this.data.form.tabid
      //+'&pnum_str='+this.data.form.pnum_str
      + '&pnum=' + this.data.form.pnum
      + '&checked_tids=' + this.data.form.tid
      + '&rid=' + this.reserve_id
      + '&ddate_solt='+this.ddate_solt
    })
  },
  selectedValue(e){
    console.log(e);
    let key = util.attr(e, 'key');
    let val;
    let sobj = {};
    if (e.type == 'input' || e.type == 'confirm') {
      val = util.val(e);
      if (key == 'phone') {
        var that = this;
        if (e.detail.value != '' && e.detail.value.length >= 3) {
          App.Reserve.GetReserveUser(
            {
              keywords: e.detail.value,
              sid: this.sellerid
            }
          ).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
          })
        }
      }
      if (key == 'pnum') {
        sobj.pnumInputFocus = true;
      }
    } else {
      val = util.attr(e, 'value');
    }


    let form = this.data.form;

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

    if ('pnum,pnum_str,tabid'.indexOf(key) > -1) {
      if (!displayTimes) {
        this.getTables();
      }
    }

    this.setData(sobj)
  },

  async formsubmit(){
    if (this.data.buttonhide) return;
    try {
      this.setData({
        buttonhide: true,
      })
      let form = JSON.parse(JSON.stringify(this.data.form));
      console.log('typeof form.tid', typeof form.tid);
      if (typeof form.tid == 'object') {
        let tid = new Set(form.tid);//tid 去重
        form.tid = [...tid].toString();
      }
      if(this.ddate_solt<1){
        return util.toast('请选择时段', 'none');
      }
      if (!form.tid) {
        return util.toast('请选择桌子', 'none');
      }
      if (!form.rname) {
        return util.toast('请填写预订人', 'none');
      }
      form.sid = this.sellerid;
      form.rdate = this.timeDur

      try {
        let enter_params = util.cache('enter_params');
        let sc = (await App.Reserve.SellerInfo(enter_params)).data
        let note_openid = enter_params.seller_openid;
        let note_name = sc.seller_user.nick_name || sc.seller_user.nickname;
        form.note_openid = note_openid || '';
        if (form.rid) {//修改时传递uid
          form.note_openid = enter_params.manager_id || '';
        }
        form.note_name = note_name || '';
      } catch (e) {}

      form.ddate = this.time;
      form.ddate_solt=this.ddate_solt
      form.is_set_ddate=1
	  form.uid=util.cache('seller_user').id
      App.Reserve.SetReserveInfo(form).then(res => {
        if (res.ret == 200) {
          this.getCustomRemark()
          let _id = (res.data.id || res.data)
          this.setData({
            form: JSON.parse(JSON.stringify(formTpl)),
            tables: [],
            runTime:parseInt(new Date()/1000),
            type:1
          })
          this.checkedTables={};
          App.Reserve.Detail({rid: _id, openid: ''}, 'clear')
          wx.navigateTo({
            url: util.http_build_query({id:_id,tplid:1},'b_invitation'),
          });
        } else {
          util.toast(res.msg, 'none')
        }
      })
    } catch (e) {
    }
  },

  /**
   * 修改初始化
   */
  async initModify(){
    try {
      let res = await App.Reserve.Detail({rid: this.options.id, openid: ''});
      let form=this.data.form
      form.pnum = res.data.pnum || 6;
      form.rname = res.data.rname;
      form.phone = res.data.phone || "";
      form.sex = res.data.sex;
      form.cusdem = res.data.cusdem;
      form.rid = this.options.id;
      form.tid = [];
      let tablesed = [];
      res.data.tables.forEach((t, ti) => {
        form.tid.push(t.id)
        if (ti >= 3) return;
        t.isselect = true
        t["isselected"] = 1
        tablesed.push(t);
        this.checkedTables[t.id]=t.name;
      })
      this.setData({
        type:2,//修改
        runTime: res.data.ddate,//到店时间
        ddate_solt:res.data.ddate_solt,
        form: form,
      })
    } catch (e) {
      console.log('initModify', e)
    }
  },
  /**
   * 快速预订初始化
   * tid
   * tname
   * ddate
   * pnum
   */
  initQuick(){
    let options=this.options
    this.checkedTables[options.tid]=options.tname
    this.setData({
      [`form.tid`]: [options.tid],
      runTime:options.ddate.substr(0,10),
      type:3
    })
  },
})