const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
Page({
  data: {
    active: 0,
    wdArr: [],
    ydArr: [],
    showNull: false,
    showNull1: false
  },
  getNotice(){
    wx.showLoading();
    wx.getStorage({
      key: 'noticeArr',
      success:(res)=>{
        console.log(res);
        wx.hideLoading();
        this.setData({
          wdArr:res.data.data.notReadAfficheList,
          ydArr:res.data.data.readAfficheList,
          showNull:res.data.data.notReadAfficheList.length>0?false:true,
          showNull1:res.data.data.readAfficheList.length>0?false:true,
        })
       
      },
      fail:()=>{
        wx.hideLoading();
      }
    })
  },
  getNoticeArr(sessionKey,active){
    console.log("getNoticeArr");
    wx.showLoading();
    wx.request({
      url: app.globalData.publicUrl + '/wx/eduNoticeSee/afficheLists',
      method: "post",
      data: {
        sessionKey: sessionKey,
        // isSee: 1
        // // isNotice: 0
      },
      success: (res) => {
        console.log(res);
        wx.hideLoading();
        if (res.data.code == 0) {
          
          
          if(active){
            console.log("1");
            if(active==0){
              
              this.setData({
                wdArr:res.data.data.notReadAfficheList,
                ydArr:res.data.data.readAfficheList,
       
                showNull1:res.data.data.readAfficheList.length>0?false:true,
              })
            }else{
              this.setData({
                wdArr:res.data.data.notReadAfficheList,
                ydArr:res.data.data.readAfficheList,
                showNull:res.data.data.notReadAfficheList.length>0?false:true,
                
              })
            }
          }else{
            console.log("2");
            this.setData({
              wdArr:res.data.data.notReadAfficheList,
              ydArr:res.data.data.readAfficheList,
              showNull:res.data.data.notReadAfficheList.length>0?false:true,
              showNull1:res.data.data.readAfficheList.length>0?false:true,
            })
          }
        } else {
          if(active){
            if(active==0){
              this.setData({
                wdArr:res.data.data.notReadAfficheList,
                ydArr:res.data.data.readAfficheList,
       
                showNull1:res.data.data.readAfficheList.length>0?false:true,
              })
            }else{
              this.setData({
                wdArr:res.data.data.notReadAfficheList,
                ydArr:res.data.data.readAfficheList,
                showNull:res.data.data.notReadAfficheList.length>0?false:true,
                
              })
            }
          }
          


        }
      },
      fail: () => {
        console.log("fal");
        console.log("active",typeof active);
        wx.hideLoading();
        wx.showModal({
          showCancel: false,
          content: "网络错误"
        })
        // if(typeof active=="number"){
            
        //     if(active==0){
              
        //       console.log("weidu");
        //       this.setData({
        //         wdArr:[],
        //         showNull:true,
        //       })
        //     }else{
        //       this.setData({
        //         ydArr:[],
        //         showNull1:true,
                
        //       })
        //     }
        //   }
      }
    })
  },
  getArr(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/eduNoticeSee/list',
      method: "post",
      data: {
        sessionKey: sessionKey,
        isSee: 1
        // isNotice: 0
      },
      success: (res) => {
        if (res.data.code == 0) {
          if (res.data.list.length !== 0) {
            this.setData({
              showNull: false,
              wdArr: res.data.list
            })
          } else {
            this.setData({
              wdArr: res.data.list,
              showNull: true
            })
          }
        } else {
          this.setData({
            showNull: true
          })
        }
      },
      fail: () => {
        this.setData({
          showNull: true
        })
      }
    })
  },
  getArr1(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/eduNoticeSee/list',
      method: "post",
      data: {
        sessionKey: sessionKey,
        isSee: 0
        // isNotice: 0
      },
      success: (res) => {
        if (res.data.code == 0) {
          if (res.data.list.length !== 0) {
            this.setData({
              showNull1: false,
              ydArr: res.data.list
            })
          } else {
            this.setData({
              ydArr: res.data.list,
              showNull1: true
            })
          }
        } else {
          this.setData({
            showNull1: true
          })
        }
      },
      fail: () => {
        this.setData({
          showNull1: true
        })
      }
    })
  },
  async onChange(event) {
 console.log("onCHange");

      const sessionKey = await tools.checkSessionAndLogin();
      this.getNoticeArr(sessionKey,event.detail.index);
    
  },
  meRead(sessionKey, noticeId) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/eduNoticeSee/edit',
      method: "post",
      data: {
        sessionKey: sessionKey,
        isSee: 0,
        noticeId: noticeId
      },
      success: () => {
        console.log("meRead",noticeId);
        this.getNoticeArr(sessionKey)
      }
    })
  },
  async btn(e) {
    console.log(e.currentTarget.dataset.item.id);
    let noticeId = e.currentTarget.dataset.item.id;
    const sessionKey = await tools.checkSessionAndLogin();
    this.meRead(sessionKey, noticeId);
  },
  async onLoad() {
    const sessionKey = await tools.checkSessionAndLogin();
    this.getNotice();
    //this.getArr(sessionKey);
  }
})