import { API } from './../../../utils/network/api'
const api = new API()
const app = getApp()
Page({
  data: {
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '系统通知', //导航栏 中间的标题
    },
    height: app.globalData.height, 
    visible: false,
    list: [],
    current_page: 0,
    total: 0,
    per_page: 0,
    last_page: 0,
    static_host: api.getStaticHost()
  },
  go(e){
    this.putMessageReadNotice(e);
    app.go(e.currentTarget.dataset.url);
  },
  onShow(){
    this.setData({
      list: [],
      current_page: 0,
      total: 0,
      per_page: 0,
      last_page: 0,
    })
    let ap = this.getList();
    ap.then(res => {
      this.check()
    })
  },
  check(){
    // 不满一页自动加载下一页
    wx.createSelectorQuery().select('#list').boundingClientRect(res2 => {
      console.log(res2.height)
      console.log(wx.getSystemInfoSync().windowHeight)
      if (res2.height < wx.getSystemInfoSync().windowHeight) {
        this.getList().then(res => {
          if(this.data.current_page < this.data.last_page){
            this.check()
          }
        });
      }
    }).exec()
  },
  getList(){
    let current_page = this.data.current_page;
    current_page = current_page + 1;
    this.setData({
      current_page: current_page
    })
    let list = this.data.list;
    return api.getDoctorMessageSystem({page: current_page}).then(response => {
      console.log(response);
      this.setData({
        list: list.concat(response.data.data),
        current_page: response.data.current_page,
        total: response.data.total,
        per_page: response.data.per_page,
        last_page: response.data.last_page,
      })
    }).catch(errors => {console.error(errors);})
  },
  onReachBottom() {
    console.log('===触底了！！===');
    if(this.data.current_page < this.data.last_page){//最后一页时停止分页
      this.getList()
    }
  },
  putMessageReadNotice(e){
    let notice_id = e.currentTarget.dataset.notice_id;
    api.putMessageReadNotice({notice_id: notice_id}).then(response => {
      console.log(response);
      
    }).catch(errors => {console.error(errors);})
  },
  putMessageReadNoticeAll(){
    api.putMessageReadNoticeAll({notice_type: 2}).then(response => {
      console.log(response);
      let new_list = [];
      this.data.list.forEach(item => {
        item.read_status = 1;
        new_list.push(item)
      })
      this.setData({
        list: new_list
      })
      let usertype = wx.getStorageSync('usertype');
      let userID = wx.getStorageSync('user_id_'+usertype);
      wx.setStorageSync( userID+"_system_notice_unreadnnum", "");
      wx.showToast({
        title: '操作成功',
        icon: "success"
      })
    }).catch(errors => {console.error(errors);})
  }
})