// pages/gongyiClass/gongyiClass.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    parentCategorys:[],
    tabIndex:0,
    erjiTab:0,
    subCategory1:[],
    subCategory2: [],

    parentId:1,
    subId:1,
    page:0,//当前页数
    pageCount:0,//总页数
    list:[],
    //数据加载loading
    showLoading: false,
    isMember:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(app.url)
    var isMember = wx.getStorageSync('isMember');
    if (isMember){
      this.setData({
        isMember:isMember
      })
    }
    var that=this;
    wx.request({
      url: app.url+'ajax_get_course_category.php',
      header: { 'content-type': 'application/json'},
      success: function(res) {
        console.log(res)
        if(res.statusCode==200){
          that.setData({
            parentCategorys:res.data.result,
            subCategory1: res.data.result[0].subCategorys,
            subCategory2: res.data.result[1].subCategorys,
            //默认为第1条
            parentId: res.data.result[0].id,
            subId: res.data.result[0].subCategorys[0].id
          })
        }
        that.getCourselist()
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

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

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

  },

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

  },

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

  //课程详细页面
  classDetails(e){
    //获取参数
    let mediaId = e.currentTarget.dataset['index']
    wx.navigateTo({
      url: '../classDetails/classDetails?mediaId='+mediaId,
    });
    wx.setNavigationBarTitle({
      title: '课程详情'
    })
  },

  //切换一级分类
  parentTab(e){
    let index = e.currentTarget.dataset['index']
    this.setData({
      tabIndex:index,
      parentId: this.data.parentCategorys[index].id
    })
    //检索课程数据
    this.getCourselist()
  },

  //切换二级分类
  subTab(e) {
    let index = e.currentTarget.dataset['index']
    if (this.data.tabIndex==0) {
      this.setData({
        erjiTab:index,
        subId: this.data.subCategory1[index].id
      })
    } else {
      this.setData({
        erjiTab: index,
        subId: this.data.subCategory2[index].id
      })
    }

    //检索课程数据
    this.getCourselist()

  },

  //下滑到底部以后加载更多
  loadMore() {
    if (this.data.page < this.data.pageCount) {
      var pageIndex = this.data.page + 1
      this.setData({
        page: pageIndex
      })
      this.getCourselist();
    } else {
      wx.showToast({
        title: '没有更多内容啦。',
        icon: 'success',
        duration: 2000
      });
    }
  },

  //观看历史
  history(){
    wx.navigateTo({
      url: '../history/history',
    });
    wx.setNavigationBarTitle({
      title: '历史'
    })
  },

  // 获取分类的对应的课程列表
  getCourselist(){
    var that=this;
    that.setData({
      showLoading: true,
    });
    wx: wx.request({
      url: app.url + 'ajax_get_course_by_category.php',
      data: {
        parentId:this.data.parentId,
        subId: this.data.subId,
        page: this.data.page,
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log(res)
        that.setData({
          list: that.data.list.concat(res.data.result.list),
          pageCount:res.data.result.pageCount
        })
      },
      fail: function (res) { },
      complete: function (res) {
        that.setData({
          showLoading: false,
        });
       },
    })
  },
  // 跳到会员
  memberChange() {
    wx.navigateTo({
      url: '../memberChange/memberChange',
    })
  }
})