const DEFAULT_PAGE = 0;
const baseUrl = getApp();
const api = require('../../config/api.js');
const util = require('../../config/util.js');
const { default: toast } = require('../../miniprogram_npm/@vant/weapp/toast/toast.js');
Page({

  data: {
    startPageX: 0,
    currentView: DEFAULT_PAGE,
    livesdata: [],
    baseurl: baseUrl.globalData.baseimgurl,
    movies: [],
    active: 1,
    challengeactive: 1,
    toView: `card_${DEFAULT_PAGE}`,
    list: [],
    itemdata: [],
    isdisplay: 1,
    onelist: [], //一级数据
    activeshow: 1,
    currentid: 1,
    classid:1  //班级ID
  },
  checkclassshow(event) {
    //新增开关功能
    console.log(event);
    console.log(event.currentTarget.id);
    const id = event.currentTarget.id;
    let arr1 = [];
    let arr = [];
    arr = this.data.onelist;

    // if (id == 1) {

    //   arr.forEach(function (item, index) {
    //     console.log(item);
    //     if (item.id == 2) {
    //       arr1[index] = item;
    //     }
    //   })
    //   this.setData({
    //     onelist: arr1
    //   })

    // } else {
    //   arr.forEach(function (item, index) {
    //     console.log(item);
    //     if (item.id == 1) {
    //       arr1[index] = item;
    //     }
    //   })
    //   this.setData({
    //     onelist: arr1
    //   })
    // }



    // arr.forEach(function (item, index) {

    //     if (index == e.currentTarget.dataset.itemid) {
    //         item['liveclickopen'] = 2;
    //         arr[index] = item;
    //     } else {
    //         item['liveclickopen'] = 2;
    //         arr[index] = item
    //     }
    // })
    // this.setData({
    //     listdata: arr
    // })


  },
  footeronChange(event) {
    this.setData({
      footeractive: event.detail
    });
  },
  onChangefooter(event) {
    console.log(event.detail);
    this.setData({
      footeractive: event.detail
    });
  },
  onChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.name}`,
      icon: 'none',
    });
  },
  touchStart(e) {
    this.startPageX = e.changedTouches[0].pageX;
  },
  touchEnd(e) {

    const moveX = e.changedTouches[0].pageX - this.startPageX;
    const maxPage = this.data.list.length - 1;
    if (Math.abs(moveX) >= 150) {
      if (moveX > 0) {
        this.currentView = this.currentView !== 0 ? this.currentView - 1 : 0;
      } else {
        this.currentView = this.currentView !== maxPage ? this.currentView + 1 : maxPage;
      }
    }
    this.setData({
      toView: `card_${this.currentView}`
    });
  },
  touchecard(e) {
    console.log(e.currentTarget.id);
    this.getindexcoursetwo(e.currentTarget.id);
    this.setData({
      active: e.currentTarget.id
    })
  },
  loaditem(id) {
    console.log(id);
  },
  cardaddclick(e) {

    console.log(e);

  },
  challengeclickleft(e) {
    console.log(e.currentTarget.id);
    this.setData({
      challengeactive: e.currentTarget.id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //baseUrl.islogin();         
    console.log("------------==============-------------");
    console.log(options.classid);     
    console.log("------------==============-------------");

    this.setData({
        classid:options.classid
    });

  },
  getpopindexcouse: function (params) {
    //顶级分类
    const that = this;
    let url = baseUrl.globalData.baseUrl + '/classindex/indexcoursetop';
    var param = {
      id: this.data.classid
    };
    //此处为使用封装的post请求
    util.get(url, param).then((res) => {
      console.log(res.data[0].id);
      that.setData({
        list: res.data,
        active:res.data[0].id
      });
      if(res.data[0].id>0){
        that.getindexcoursetwo(res.data[0].id);
      }
     

    }).catch((errMsg) => {
      console.log(errMsg);
    });
  },
  getindexcourse: function () {
    const that = this;
    let url = baseUrl.globalData.baseUrl + '/classindex/indexcourse';
    var param = {
      id: '123456789'
    };
    //此处为使用封装的post请求
    util.get(url, param).then((res) => {
      console.log(res);
      that.setData({
        list: res.data
      });

    }).catch((errMsg) => {
      console.log(errMsg);
    });
  },
  getindexcoursetwo: function (e) {

    //得到二级课程的数据
    const that = this;
    let url = baseUrl.globalData.baseUrl + '/classindex/indexcoursepara';
    var param = {
      pareid: e
    };
    //此处为使用封装的post请求
    util.post(url, param).then((res) => {

      console.log('===================');
      console.log(res.data);
      that.setData({
        onelist: res.data
      });

    }).catch((errMsg) => {
      console.log(errMsg);
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

    // this.setData({
    //   active: 1
    // });

    // if (wx.getStorageSync('userinfo') != null) {
    //     baseUrl.globalData.currentuserinfo = wx.getStorageSync('userinfo');
    //     wx.setStorageSync('wxopenid', wx.getStorageSync('userinfo').openid);
    // }
    this.isdisplay();
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    this.getpopindexcouse(); //顶级分类
    // this.getindexcourse(); //一级分类
    console.log("activeid"+this.data.active)
    this.getindexcoursetwo(this.data.active);
   // this.adsfun();
    //this.indexlives();
    this.isjumpblack();

  },
  isjumpblack: function () {
    console.log(wx.getStorageSync('userisblack'));
    if (wx.getStorageSync('userisblack') == 2) {

      wx.showToast({
        title: "不允许访问该程序",//提示文字
        duration: 5000,//显示时长
        mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
        icon: 'none', //图标，支持"success"、"loading"  
        success: function () {
          wx.reLaunch({
            url: '/pages/userlogin/userlogin',
          })
        },//接口调用成功
        fail: function () { },  //接口调用失败的回调函数  
        complete: function () { } //接口调用结束的回调函数  
      })
    }
  },
  indexlives: function () {
    //直播列表
    const that = this;
    let url = baseUrl.globalData.baseUrl + '/lives/indexlives';
    var param = {
      id: '123456789'
    };
    //此处为使用封装的post请求
    util.get(url, param).then((res) => {
      //  console.log(res);
      that.setData({
        livesdata: res.data
      });

    }).catch((errMsg) => {
      console.log(errMsg);
    });
  },
  adsfun: function () {
    //广告
    const that = this;
    let url = baseUrl.globalData.baseUrl + '/ads/index';
    var param = {
      id: '123456789'
    };
    //此处为使用封装的post请求
    util.get(url, param).then((res) => {
      // console.log(res);
      that.setData({
        movies: res.data
      });

    }).catch((errMsg) => {
      console.log(errMsg);
    });
  },
  livelistclick: function () {

    if (this.data.isdisplay == 1) {
      // 直播列表
      wx.navigateTo({
        url: '/pages/indexlives/indexlives',
      })
    } else {
      wx.showToast({
        title: `功能正在开发中`,
        icon: 'none',
      });
    }
  },
  messageclick: function () {

    wx.navigateTo({
      url: '/pages/messagelist/messagelist',
    })
  },
  resclick: function () {
    wx.navigateTo({
      url: '/pages/indexreslists/indexreslists',
    })
  },
  courselearningclick: function (e) {

    //课程学习
    console.log(e.currentTarget.id);
    wx.navigateTo({
      url: '/pages/classcommunityitemlist/classcommunityitemlist?typeid=' + e.currentTarget.id,
    })
  },
  picsclick: function (e) {
    console.log(e);
    if (this.data.isdisplay == 1) {
      console.log(e.currentTarget.id);
      wx.navigateTo({
        url: '/pages/messageencyclopedias/messageencyclopedias?id=' + e.currentTarget.id,
      })
    }

  },
  isdisplay: function (params) {

    const that = this;
    let url = baseUrl.globalData.baseUrl + '/index/isdisplay';
    var param = {
      pareid: params
    };
    //此处为使用封装的post请求
    util.get(url, param).then((res) => {
      console.log(res);
      that.setData({
        isdisplay: res.data
      });

    }).catch((errMsg) => {
      console.log(errMsg);
    });

  },
  onShareAppMessage: function (options) {//分享onShareAppMessage 
    return {
      title: '怼怼公考',
      path: `/pages/classindex/classindex`,
      success: function (res) {
        console.log(res, '成功')
        console.info(res + '成功');
        wx.showToast({
          title: '分享成功',
        })
        // 转发成功
      },
      fail: function (res) {
        console.log(res + '失败');
        // 转发失败
      },
      complete: function (res) {
        // 不管成功失败都会执行
        console.log(res, '成功或失败')
        wx.showToast({
          title: '成功或失败',
        })
      }
    }
  }

})