//index.js
const { request, serverName, imgServer } = require('../../utils/services');
const { Toast } = require('../../utils/util.js');
const { defaultImg, noExhibitionImg } = require('../../utils/images');


const WxParse = require('../../common/component/wxParse/wxParse.js');
const app = getApp();


Page({
  data: {
    animationData: {},
    loading: false,
    isLike: true,
    imgServer,
    commodityImgs: [],
    testImg: '../../imgs/testImg/fdkz.png',
    currentPage: 1,
    exhibitionList: [],
    likes: {},
    tag: 1
  },

  onLoad: function () {

    this.setData({
      serverName,
      defaultImg,
      noExhibitionImg,
    })

    this.getList(1);
  },

  loadMore: function () {
    if (!this.data.lastPage) {
      this.getList(this.data.currentPage + 1);
    } else {
      return;
    }
  },

  onReachBottom: function () {
    if (!this.data.loading) {
      this.loadMore();
    }

  },
  onShareAppMessage: function () {

  },

  onShow: function () {
    let { collectedArr, collectedChange } = app.globalData;
    let { exhibitionList } = this.data

    // this.setData({
    //   exhibitionList: []
    // });
    // this.getBanner();
    // this.getList(1);
    if (collectedChange) {
      for (let i = 0; i < exhibitionList.length; i++) {
        for (let j = 0; j < collectedArr.length; j++) {
          if (collectedArr[j].collectedId == exhibitionList[i].id) {
            exhibitionList[i].hasCollect = collectedArr[j].status
            if (exhibitionList[i].hasCollect) {
              exhibitionList[i].collectionsCount += 1;
            }
            else {
              exhibitionList[i].collectionsCount -= 1;
            }
            if (exhibitionList[i].collectionsCount < 0) {
              exhibitionList[i].collectionsCount = 0
            }
          }
        }

      }
      this.setData({
        exhibitionList
      })
    }
    app.globalData.clickToSelect = false;
    app.globalData.collectedChange = false;
  },
  enablePullDownRefresh: function () {
    this.setData({
      exhibitionList: [],
      currentPage: 1
    });
    this.getList(1);
    this.getBanner();

  },
  onPullDownRefresh: function () {
    this.setData({
      exhibitionList: [],
      currentPage: 1
    });
    this.getList(1);
  },

  getList: function (page) {
    let type = this.data.tag;
    this.setData({
      loading: true
    })
    request["getExhibitionList"]({
      page: page,
      type: type
    }, '', res => {

      let tempContent = this.data.exhibitionList
        ? this.data.exhibitionList
        : [];
      let { last: lastPage, totalPages, content: exhibitionList } = res.data.data;

      let { openTime } = exhibitionList;
      exhibitionList.forEach((currentValue) => {
        currentValue.product ? currentValue.product.link = escape(currentValue.product.link) : '';
        currentValue.product ? currentValue.product.imageUrl = escape(currentValue.product.imageUrl) : '';
      })
      this.setData({
        currentPage: res.data.data.number + 1,
        lastPage,
        loading: false,
        exhibitionList: tempContent.concat(exhibitionList),
      });
      wx.stopPullDownRefresh();

      // WxParse.wxParseTemArray("replyTemArray", 'reply', replyArr.length, that)

    }, err => {

    }, complete => {

    })
  },

  addLike: function (e) {
    let { type, id, idx } = e.currentTarget.dataset;

    let likes = this.data.likes;
    likes[id] = !likes[id];
    let exhibitionList = this.data.exhibitionList;
    let { collectedArr, collectedChange } = app.globalData, hasItem = true;

    this.setData({
      likes: likes
    })
    Toast.showToast2('loading');

    let loginSessionKey = wx.getStorageSync('token') || "";
    // if (loginSessionKey){
    request['isCollect']({
      loginSessionKey,
      exhibitionId: id,
      type: Number(type),
    }, "post", res => {
      if (res.data.code > -1) {
        for (let i = 0; i < collectedArr.length; i++) {
          if (collectedArr[i].collectedId && id == collectedArr[i].collectedId) {
            collectedArr[i] = {
              collectedId: id,
              status: res.data.data.hasCollect,
            }
            hasItem = false;
          }
        }
        if (hasItem) {
          collectedArr.push({
            collectedId: id,
            status: res.data.data.hasCollect,
          })
        }

        app.globalData.collectedArr = collectedArr;
        app.globalData.collectedChange = true;
        
        exhibitionList[idx].hasCollect = res.data.data.hasCollect
        this.setData({
          exhibitionList: exhibitionList
        })
      }

    }, err => {

    }, complete => {
      
      Toast.hideLoading();
    })
    // }

  },

  to_search: function () {
    wx.navigateTo({
      url: './search/index',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

})
