// pages/user/my_comment/index.js
const { noCollectionImg } = require('../../../utils/images');
const {
  request,
  serverName
} = require('../../../utils/services');
const {
  defaultImg,
  noExhibitionImg
} = require('../../../utils/images');

const {
  Toast
} = require('../../../utils/util.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    testImg: '../../../imgs/testImg/fdkz.png',
    myBrowsed: [],
    noCollectionImg
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      serverName,
      noExhibitionImg,
      defaultImg
    })
    wx.getLocation({
      type:'wgs84',
      success: (res)=> {
        this.setData({
          latitude:res.latitude||0,
          longitude:res.longitude||0
        })
      },
      complete:()=>{
        this.getBrowsedExhibitions()
      }
    })

  },
  onPullDownRefresh: function() {
    this.setData({
      myBrowsed: [],
    });
    this.getBrowsedExhibitions()
  },
  saveBrowsedExhibitions() {
    let {
      cookieIDs
    } = this.globalData;
    let ids = undefined;
    console.log(this.globalData)
    if (cookieIDs) {
      ids = cookieIDs.join(",") || undefined;
    } else {
      return
    }
    let loginSessionKey = wx.getStorageSync("token") || "";

    if (loginSessionKey) {
      request["saveBrowsedExhibitions"]({
          ids: ids,
          loginSessionKey
        }, "", res => {
          if (res) {
            this.getBrowsedExhibitions()
          }
        }, err => {

        },
        complete => {

        })
    } else {
      return
    }

  },

  getBrowsedExhibitions: function() {
    let loginSessionKey = wx.getStorageSync("token") || "";
    let ids = undefined;
    console.log(app.globalData)
    if (app.globalData.cookieIDs) {
      if (app.globalData.cookieIDs.length > 9) {
        app.globalData.cookieIDs = app.globalData.cookieIDs.slice(0, 10)
        // console.log(cookieIDs)

      }
      console.log(app.globalData.cookieIDs.join(","))

      for (let i = 0; i < app.globalData.cookieIDs.length;i++){
        if (app.globalData.cookieIDs[i] =='undefined'){
          app.globalData.cookieIDs.splice(i,1);
        }
      }
      ids = app.globalData.cookieIDs.join(",") || "";
      
      // console.log("asdasdas",ids)
    } else {
      return
    }
    let {latitude:lat,longitude:lng} = this.data
    request["getExhibitionsByIds"]({
        ids,
        lat,
        lng,
        loginSessionKey
      }, "", res => {
        if (res.data.code > -1) {
          let {
            data: myBrowsed
          } = res.data;
          this.setData({
            myBrowsed
          })
          console.log(myBrowsed)
        }

      }, err => {

      },
      complete => {
        wx.stopPullDownRefresh();

      })
  },

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

  },

  _getExhibitionsByIds() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let {
      collectedArr,
      collectedChange
    } = app.globalData;
    let {
      myBrowsed
    } = this.data
    let isLogin = app.globalData.isLogin;


    // this.setData({
    //   myBrowsed: []
    // });
    // this.getBanner();
    // this.getList(1);
    this.setData({
      isLogin
    })

    if (collectedChange) {
      for (let i = 0; i < myBrowsed.length; i++) {
        for (let j = 0; j < collectedArr.length; j++) {
          if (collectedArr[j].collectedId == myBrowsed[i].id) {
            myBrowsed[i].hasCollect = collectedArr[j].status
            if (myBrowsed[i].hasCollect) {
              myBrowsed[i].collectionsCount += 1;
            } else {
              myBrowsed[i].collectionsCount -= 1;
            }
          }
        }
        // console.log(collectedArr.status, collectedArr.collectedId)

      }
      this.setData({
        myBrowsed,

      })
    }
    // app.globalData.clickToSelect = false;
    // app.globalData.collectedChange = false;

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },



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

  },

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

  }
})