const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
Page({
  data: {
    
    wqdataArr: 0,
    jjdataArr: 0,
    isNeedlogin: false,
    // 课程轮播
    bnrUrl: [{
      clasImg: app.globalData.imgUrl+"/images/zbfm.png"
    }],
    gridArr: [{
      url: "/images/icon1.png",
      name: "在线直播",
      isread: true
    }, {
      url: "/images/icon2.png",
      name: "在线录播",
      isread: true
    }, {
      url: "/images/icon3.png",
      name: "互动答疑",
      isread: true
    }, {
      url: "/images/icon4.png",
      name: "测试答卷",
      isread: true
    }],
    xczsArr: [],
    goGridArr: [
      '/pages/onlineClass/onlineClass',
      '/pages/demandCourses/demandCourses',
      '/pages/myInteractions/myInteractions',
      '/pages/testAnswer/testAnswer'
    ],
    active: 0,
    xcpArr: [],
    wdArr:[],
    notice:"",
    noticeVis:false,
    vType:["理论阵地","我的学院","特色课程","特色管理"]
  },
  // 轮播点击去上课跳转到我的班级
  gomyClass(e) {
    if (e.currentTarget.dataset.item.isActive == 5) {
      wx.switchTab({
        url: '/pages/myClass/myClass',
      })
    }
  },
  // 即将开班
  gostartClass(e) {
    if (e.currentTarget.dataset.item !== 0) {
      wx.navigateTo({
        url: '/pages/startClass/startClass',
      })
    } else {
      wx.showModal({
        showCancel: false,
        content: "您还没有即将开班哦",
        success(res) {
          if (res.confirm) {
            wx.switchTab({
              url: '../index/index'
            })
          }
        }
      })
    }
  },
  // 往期开班
  goagoClass(e) {
    if (e.currentTarget.dataset.item !== 0) {
      wx.navigateTo({
        url: '/pages/agoClass/agoClass',
      })
    } else {
      wx.showModal({
        showCancel: false,
        content: "您还没有往期开班哦",
        success(res) {
          if (res.confirm) {
            wx.switchTab({
              url: '../index/index'
            })
          }
        }
      })
    }
  },
  // 宣传展示查看更多
  xczsMore() {
    wx.navigateTo({
      url: '/pages/exhibitionList/exhibitionList?id=0',
    })
  },
  // 每个宣传展示跳转详情页
  turnDetails(e) {
    let num = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/exhibitionList/exhibitionList?id=' + num,
    })
  },
  // 首页的四个点击
  goGrid(e) {
    wx.navigateTo({
      url: this.data.goGridArr[e.currentTarget.dataset.id]
    })
  },
  // banner图
  getBanner(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selAllClas',
      method: "post",
      data: {
        sessionKey
      },
      success: (res) => {
        if (res.data.data && res.data.data.claList.length != 0) {
          this.setData({
            bnrUrl: res.data.data.claList
          })
        }
      }
    })
  },
  // 即将开班有几个
  getjjLength(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selClasToo',
      method: "post",
      data: {
        sessionKey,
        isActive: "0"
      },
      success: (res) => {
        if (res.data.data)
          this.setData({
            jjdataArr: res.data.data.length
          })
      }
    })
  },
  // 往期开班有几个
  getwqLength(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selClasToo',
      method: "post",
      data: {
        sessionKey,
        isActive: "2"
      },
      success: (res) => {
        if (res.data.data)
          this.setData({
            wqdataArr: res.data.data.length
          })
      }
    })
  },
  // 四个宣传图
  getfour() {
    wx.request({
      url: app.globalData.publicUrl + '/wx/course/listCategory',
      method: "post",
      success: (res) => {
        this.setData({
          xczsArr: res.data.list
        })
      }
    })
  },
  // 四个宣传图onshow
  getfour1() {
    wx.request({
      url: app.globalData.publicUrl + '/wx/course/listCategory',
      method: "post",
      success: (res) => {
        if (this.data.xczsArr && this.data.xczsArr.length != 0) {
          if (this.data.xczsArr.sort().toString() != res.data.list.sort().toString()) {
            this.setData({
              xczsArr: res.data.list
            })
          }
        }
      }
    })
  },
  // 第一个小红点
  getRedPoint1(sessionKey) {
    let completeStatus1 = `gridArr[${0}].isread`;
    let completeStatus2 = `gridArr[${1}].isread`;
    wx.request({
      url: app.globalData.publicUrl + '/wx/course/undoneLogo',
      method: "post",
      data: {
        sessionKey
      },
      success: (res) => {
        if (res.data.code == 0 && res.data.isLive == true) {
          this.setData({
            [completeStatus1]: false
          })
        }
        if (res.data.code == 0 && res.data.isRecord == true) {
          this.setData({
            [completeStatus2]: false
          })
        }
      }
    })
  },
  // 第四个小红点
  getRedPoint4(sessionKey) {
    let completeStatus = `gridArr[${3}].isread`;
    wx.request({
      url: app.globalData.publicUrl + '/wx/exam/list',
      method: "post",
      data: {
        sessionKey,
        status: 0
      },
      success: (res) => {
        if (res.data.code == 0 && res.data.list.length > 0) {
          this.setData({
            [completeStatus]: false
          })
        } else {
          this.setData({
            [completeStatus]: true
          })
        }
      }
    })
  },
  goSeevideo(e) {
    let id = e.currentTarget.dataset.item.id
    wx.navigateTo({
      url: '/pages/dbVideo/dbVideo?id=' + id,
    })
  },
  onChange(event) {
    let tag = event.detail.name + 1;
    this.getVedioList(tag)
    
  },
  getVedioList(tag){
    var that=this;
    wx.request({
      method: "POST",
      url: app.globalData.publicUrl + '/wx/course/listByCourseTag?tag=' + tag,
      success: (e) => {
        that.setData({
          xcpArr: e.data.list,
        })
      }
    })
  },
  toNotice(){
    var that=this;
      wx.setStorage({
        data: that.data.noticeArr,
        key: 'noticeArr',
        success:()=>{
          wx.navigateTo({
            url: '../myNoticen/myNotice'
          })
        }
      })
  },
  getNotice(sessionKey){
    wx.request({
       
      url: app.globalData.publicUrl + '/wx/eduNoticeSee/afficheLists',
      method: "post",
      data: {
        sessionKey: sessionKey,
        //isSee: 1
        // isNotice: 0
      },
      success: (res) => {
        console.log(res);
     
        if (res.data.code == 0) {
          if (res.data.data.notReadAfficheList.length !== 0) {
            this.setData({
              noticeVis: true,
              noticeArr: res.data,
              notice: res.data.data.notReadAfficheList[0].noticeContent
            
            })
          } else {
            this.setData({
              noticeArr: res.data.data,
              noticeVis: false,
              
            })
          }
        } else {
          this.setData({
            noticeVis: false
          })
        }
      },
      fail: () => {
        this.setData({
          noticeVis: false
        })
      }
    })
  },
  async onShow() {
    const sessionKey = await tools.checkSessionAndLogin();
    // this.getBanner(sessionKey);
    this.getjjLength(sessionKey);
    this.getwqLength(sessionKey);
    this.getRedPoint1(sessionKey);
    this.getRedPoint4(sessionKey);
    this.getNotice(sessionKey);
    
   // this.getfour1();
  },
  async onLoad() {
    //this.getfour();
    
    this.getVedioList(this.data.active+1);
  },
})