const util = require('../../../../utils/util.js');
const app = getApp();
Page({
  data: {
    id:'',
    studentInfo:{
      studentId:''
    },
    info:{},
    canBuy:0,
    leaderRecom:0,
    currentIndex:0,
    showCode:false,
    headImg:'',
    isShare:0
  },
  onLoad: function (options) {
    console.log(options)
    if (options) {
      if (!util.isEmpty(options.isShare) && options.isShare==1) {
        this.setData({
          id:options.id,
          isShare:options.isShare
        })
        // this.getChildList()
      }else{
        this.setData({
          id:options.id,
          'studentInfo.studentId':options.studentId,
          'studentInfo.age':options.age,
          'studentInfo.userNickname':app.globalData.userInfo.nickname,
          'studentInfo.childName':options.childName,
          'studentInfo.city':options.city,
          'studentInfo.sex':options.sex,
          'studentInfo.userAvatar':options.userAvatar,
          canBuy:options.canBuy,
          leaderRecom:options.leaderRecom,
          currentIndex:options.currentIndex,
        })
      }
    }
  },
  onShow:function(){
    if (this.data.isShare==1) {
      this.getChildList()
    }else{
      this.getInfo()
    }
  },
  // 获取儿童信息
  getChildList() {
    var self = this;
    util.ajax({
      func: "v2/gwcourse/student/list",
    }, function (res) {
      if(res.code == 0){
        if (res.data.length>0) {
          self.setData({
            'studentInfo.studentId':res.data[0].id,
            'studentInfo.age':res.data[0].age,
            'studentInfo.userNickname':app.globalData.userInfo.nickname,
            'studentInfo.childName':res.data[0].childName,
            'studentInfo.city':res.data[0].city,
            'studentInfo.sex':res.data[0].sex,
            'studentInfo.userAvatar':res.data[0].userAvatar,
            canBuy:1,
            // leaderRecom:options.leaderRecom,
            // currentIndex:options.currentIndex
          })
        }else{
          self.setData({canBuy:0})
        }
      }else{
        util.showTips(res.reason)
      }
      self.getInfo()
    });
  },
  
  // 获取课程详情
  getInfo(){
    let self = this;
    util.ajax({
      func: `v2/gwcourse/course/${self.data.id}`,
      data:{studentId:self.data.studentInfo.studentId}
    }, function (res) { 
      if (res.code==0) {
        self.setData({
          info:res.data,
          headImg:res.data.qrCode
        })
        self.getLeaderInfo()
      }else{
        util.showTips(res.reason)
      }
    })
  },
  // 跳转到领队简介页面
  jumpToLeaderDemeanor(e){
    let self = this;
    let teacherInfo = {
      uid:this.data.info.teacherUid,
      name:this.data.info.teacherName,
      city:this.data.info.teacherCity
    }
    wx.navigateTo({
      url: '/pages/account/growthCourse/leaderDemeanor/index?teacherUid='+teacherInfo.uid+'&studentId='+self.data.studentInfo.studentId+"&teacherName="+teacherInfo.name+'&teacherCity='+teacherInfo.city+'&canBuy='+this.data.canBuy+'&childName='+self.data.studentInfo.childName
    })
  },
  // 跳转到待反馈页面
  jumpToEvaluate(){
    let data = this.data.info;
    wx.navigateTo({
      url: '/pages/account/growthCourse/courseEvaluate/index?id='+data.id+'&studentId='+this.data.studentInfo.studentId+'&leaderName='+data.teacherName+'&courseTitle='+data.title+'&city='+data.teacherCity,
    })
  },
  // 预约
  appointment(){
    let self =this;
    if (parseInt(self.data.canBuy) ==1) {
      if (self.data.info.status==0) {
        wx.showModal({
          content: `确认为 ${self.data.studentInfo.childName} 小朋友预约这节成长课吗？`,
          success (res) {
            if (res.confirm) {
              self.appoint()
            } else if (res.cancel) {
              console.log('放弃预约')
            }
          }
        })
      } else {
        self.appoint()
      }
    }else {
      wx.showModal({
        content: '请先为儿童购买成长课，购买成功后即可预约课程。',
        success (res) {
          if (res.confirm) {
            wx.reLaunch({
              url: 'url',
            })({
              url:'/pages/home/index'
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  // 预约课程接口
  appoint(){
    let self =this;
    util.ajax({
      func: `v2/gwcourse/book`,
      data:{
        studentId:self.data.studentInfo.studentId,
        courseId:self.data.info.id,
        status:self.data.info.status==0?0:1
      }
    }, function (res) {
      if(res.code == 0){
        self.setData({'info.status':self.data.info.status == 0?3:0,})
        if (self.data.info.status== 3) {
          if (!util.isEmpty(self.data.headImg)) {
            self.setData({
              methodView:0,
              showCode:true,
            })
          } else {
            wx.showModal({
              content: '恭喜您预约成功！请耐心等待宝大大联系。',
              showCancel:false,
              success (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                }
              }
            })
          }
        }
        self.getInfo()
      }else{
        util.showTips(res.reason)
      }
    });
  },
  closeCode(e){
    this.setData({
      showCode:e.detail
    })
  },
  // 获取领队信息判断是否可以进入领队风采页面
  getLeaderInfo(){
    let that=this;
    util.ajax({
      func:'v2/leader/style/detail',
      data:{
        uid:that.data.info.teacherUid
      }
    },function (res) {
      if (res.code==0) {
        // if (util.isEmpty(res.data.avatar) && util.isEmpty(res.data.intro)) {
        //   that.setData({
        //   leaderRecom:0
        // })
        // }
        that.setData({leaderRecom:res.data.leaderRecom})
      }
    })
  },
  onShareAppMessage() {
    let self =this;
    return {
      title:'约吧，邀请你家宝贝一起参加成长课！',
      path:'/pages/account/growthCourse/courseDetail/index?isShare=1'+'&id='+self.data.id,
      imageUrl:'http://img.bbztx.com/image_test/upload/thumbs/20210223/logo/1614076819238032554.jpg'
    }
  },

})