confirm-appointment-info.js 12.0 KB
// pages/index/confirm-appointment-info/confirm-appointment-info.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        modal_data: {
            is_showModal: false,
            currentBtn: null,
            title: '当前学员还没有购买/兑换畅玩卡,暂不能预约实验活动,是否马上购买/兑换?',
            tab_left: '否',
            tab_right: '是',
        },
        state_data: {
            is_showSuccessModal: false,
            icon: '../../../images/success.png',
            title:'恭喜您!',
            content: '您已经预约成功!'
        },
        student_list: [{id:1,name:'张三',checked: false},
            {id:2,name:'李思思', checked: false},
            {id:3,name:'王五', checked: false},
            {id:4,name:'赵柳', checked: false},
            {id:5,name:'迪丽热巴-迪力木拉提',checked: false}],
        is_choose: false,
        num_arr: [],
        session_info: {},
        student_num: 0,
        is_choose_student: false,
        is_buy: true,
        student_id:47,
    },
    chooseChildren(e) {
        console.log(e,e.detail);
        // const current = e.currentTarget.dataset.index;
        const that = this;
        var index_arr = e.detail.value;
        console.log(index_arr)
        //新建数组全部设置为没被选中
        var new_arr = [];
        console.log('选择时学员', that.data.student_list);
        that.data.student_list.forEach((item) => {
            new_arr.push({ ...item, checked: false })
        });//给每一项添加checked属性
        console.log('new_arr',new_arr);
        var new_itmes = new_arr;
        console.log('new_itmes', new_itmes);
        if(that.data.student_num >=2) {//两个学员预约时
            console.log('两个学员');
            if (index_arr.length > 2) {
                //取出倒数两个值
                var key1 = index_arr[index_arr.length - 1];
                var key2 = index_arr[index_arr.length - 2];
                // var key3 = skin[skin.length - 3];
                // console.log(key1, key2, key3);
                console.log(key1, key2);
                console.log(new_itmes[key1]['checked'],new_itmes[key1].id,new_itmes[key2].id);
                //设置最后两个值为选中状态
                new_itmes[key1]['checked'] = true
                new_itmes[key2]['checked'] = true
                // new_itmes[key3 - 1]['checked'] = 'true'
                //删除被选中的第一个值
                index_arr.splice(0, 1);
                console.log('skin',index_arr);
                const id_arr = [];
                id_arr.push(new_itmes[key1].id,new_itmes[key2].id);
                that.setData({student_id: id_arr.join(','),is_choose_student:true});
                console.log('student_id', that.data.student_id);
            } else {
                //被选中少于两个,直接设置被选中
                for (var i = 0; i < index_arr.length; i++) {
                    var key = index_arr[i]
                    console.log('key', key);
                    new_itmes[key]['checked'] = true;
                    that.setData({student_id: new_itmes[key].id,is_choose_student:true});
                }
                console.log('student_id', that.data.student_id);
            }
        }else if(that.data.student_num <2){//一个学员预约时
            console.log('一个学员');
            if (index_arr.length > 1) {
                //取出倒数一个值
                var key1 = index_arr[index_arr.length - 1];
                // var key2 = skin[skin.length - 2];
                // var key3 = skin[skin.length - 3];
                // console.log(key1, key2, key3);
                console.log(key1);
                console.log(new_itmes[key1]['checked'],new_itmes[key1].id);
                //设置最后一个值为选中状态
                new_itmes[key1]['checked'] = true
                // new_itmes[key2]['checked'] = true
                // new_itmes[key3 - 1]['checked'] = 'true'
                //删除被选中的第一个值
                index_arr.splice(0, 1);
                console.log('skin',index_arr);
                that.setData({student_id: new_itmes[key1].id,is_choose_student:true});
                console.log('student_id', that.data.student_id);
            } else {
                console.log('111');
                //被选中少于1个,直接设置被选中
                // new_itmes.forEach((item,index) => {
                //     if(item.id === that.data.student_id) {
                //         index_arr[0] = index
                //     }
                // });
                for (var i = 0; i < index_arr.length; i++) {
                    console.log('222',index_arr,i);
                    var key = index_arr[i]
                    console.log('key', key);
                    new_itmes[key]['checked'] = true
                    that.setData({student_id: new_itmes[key].id,is_choose_student:true});
                }
                index_arr = []
                console.log('333');
                console.log('student_id', that.data.student_id);
            }
        }

        //存入
        that.setData({
            index_arr: index_arr,
            // text: text,
            student_list: new_itmes,
        })

    },
    formSubmit(e) {
        console.log(e.detail);
        if(this.data.student_list.length === 0) {
            wx.showToast({title: '请添加学员!',icon:'none'})
        }else if(!this.data.is_choose_student) {
            wx.showToast({title: '请选择学员!',icon:'none'})
        }else {
            // this.setData({'modal_data.is_showModal': true})
            let url = '/portal/Experiment/confirm_session_submit';
            let header = {
                "XX-token": wx.getStorageSync('token')
            };
            let params = {
                _type: 2,//2,活动预约成功
                formId: e.detail.formId,
                experiment_id: this.data.experiment_id,
                session_id: this.data.session_id,
                reservation_id: this.data.reservation_id,
                students: this.data.student_id,
            };
            app.post(url,params,header).then((res) => {
                console.log('提交预约信息',res);
                if(res.data.card_modal === 1) {
                    this.setData({'modal_data.is_showModal': true});
                }else if(res.data.reservation_modal === 1) {
                    this.setData({'state_data.is_showSuccessModal': true});
                }
            })
        }
    },
    chooseNo(e) {
        this.setData({
            'modal_data.currentBtn': e.currentTarget.dataset.index,
            'modal_data.is_showModal': false
        });
        // wx.switchTab({url: '/pages/index/index'})
        wx.navigateBack({delta: 1})
    },
    chooseYes(e) {
        this.setData({
            'modal_data.currentBtn': e.currentTarget.dataset.index,
            'modal_data.is_showModal': false,
        });
        wx.navigateTo({url: '/pages/my/buyCard/buyCard?from_appointment=' + true})
    },
    modalConfirm() {
        this.setData({'state_data.is_showSuccessModal': false});
        wx.navigateTo({url: '/pages/my/all-appointment/all-appointment?currentTab=' + 2})
    },
    disableScroll() {},
    addStudent() {
        wx.navigateTo({url: './add-student/add-student?is_back=' + true})
    },
    //获取预约信息
    getAppointmentInfo() {
        let url = '/portal/Experiment/confirm_session';
        let header = {
            "XX-token": wx.getStorageSync('token')
        };
        let params = {
            experiment_id: this.data.experiment_id,
            session_id: this.data.session_id,
            reservation_id: this.data.reservation_id,
        };
        app.post(url,params,header).then((res) => {
            console.log('预约信息',res);
            var arr = [];
            for (let i in res.list) {
                arr.push(res.list[i]); //属性
                //arr.push(obj[i]); //值
            }//对象转数组
            console.log('arr',arr);
            var new_arr = [];
            arr.forEach((item) => {
                new_arr.push({ ...item, checked: false })
            });//给每一项添加checked属性
            console.log(new_arr);
            this.setData({
                student_list: new_arr,
                new_arr:new_arr,
                session_info: res.session_info,
            })
            console.log('student_list', this.data.student_list);
        })
    },
    //获取学员列表
    getStudentList() {
        const self = this;
        self.setData({is_choose_student: true})
        console.log('返回时的学生列表', self.data.student_list);
        let url = '/user/Student/student_list';
        let header = {
            "XX-token": wx.getStorageSync('token')
        };
        let params = {
            page: 1,
        };
        app.post(url,params,header).then((res) => {
            console.log('学员列表',res);

            var arr = [];
            for (let i in res.list) {
                arr.push(res.list[i]); //属性
                //arr.push(obj[i]); //值
            }//对象转数组
            console.log('arr',arr);
            var new_arr = [];
            arr.forEach((item) => {
                new_arr.push({ ...item, checked: false })
            });//给每一项添加checked属性
            console.log('new_arr',new_arr);
            this.setData({
                student_list: new_arr,
                new_arr:new_arr,
                // session_info: res.session_info,
            })
            console.log('student_list', this.data.student_list);
            this.refreshBuyState();

            // res.list.forEach((item) => {
            //     if(item.time !== '') {
                    // self.data.student_list.forEach((item) => {
                    //     new_arr.push({ ...item, checked: false })
                    // });//给每一项添加checked属性

                // }
            // })
            // this.setData({student_list: res.list})
        })
    },
    //更新购买状态
    refreshBuyState() {
        console.log('更新购买状态---学生id',this.data.is_buy,this.data.student_id);
        if(this.data.is_buy) {
            this.data.student_list.forEach((item) => {
                item.checked = false;
                if (item.id === this.data.student_id) {
                    console.log('item.id', item.id);
                    item.checked = true;
                }
            });
            this.setData({student_list:this.data.student_list})
            console.log('student_list',this.data.student_list);
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const self =this;
        console.log('load');
        console.log(options);
        self.setData({
            experiment_id: +options.experiment_id?+options.experiment_id:'',
            session_id: +options.session_id?+options.session_id:'',
            reservation_id: +options.reservation_id?+options.reservation_id:'',
            student_num: +options.student_num?+options.student_num:'',
        });
        // self.getAppointmentInfo();
    },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
      console.log('show');
      const self = this;
      self.getStudentList();
      self.setData({is_choose_student: false});
      // self.getAppointmentInfo();//需解注释
      setTimeout(function() {
      self.setData({
        show: true
      })
    }, 500)
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})