// packageG/cps/cpsPage/cpsPage.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    components: [],
    page_id: "",
    page_info: {},
    btnFlag: false,

    current_page: 1,
    total_page: 0,
    decorate_isLoadMore: true,
    bottomShowBol: false,
    bottomShowBolIndex: 0,

    showPage: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.page_id) {
      this.setData({
        page_id: options.page_id,
      });
    }
    if (options.mid) {
      app._setMid(options.mid);
    }
    this.getCPSdata();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getCPSdata();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      bottomShowBol: true,
    });

    if (this.data.decorate_isLoadMore) {
      this.getMoreDecorate();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var value = wx.getStorageSync('yz_uid');
    let mid = '';
    if (value) {
      mid = value;
    }
    return {
      path: `/packageG/cps/cpsPage/cpsPage?page_id=${this.data.page_id}&mid=${mid}`,
    };
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // 节流
    if (e.scrollTop > 80) {
      if (!this.data.btnFlag) {
        this.setData({
          btnFlag: true,
        });
      }
    } else {
      if (this.data.btnFlag) {
        this.setData({
          btnFlag: false,
        });
      }
    }

    if (this.data.bottomShowBol) {
      this.setData({
        bottomShowBol: false,
      });
    }
  },

  getCPSdata() {
    this.setData({
      showPage: false,
    });
    wx.showLoading({
      title: '加载中'
    });
    let jsons = {
      page_id: this.data.page_id,
    };

    let urlStr = app.getNetAddresss("home-page.index");
    app._getNetWork({
      url: urlStr,
      data: jsons,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.item) {
            this.setData({
              showPage: true,
            });
            wx.stopPullDownRefresh({
              complete: function (res) {
                console.log('关闭下拉刷新', res, new Date());
              }
            });
            this.setData({
              components: res.data.item.datas,
              page_name: res.data.item.page_info.page_title,
              decorate_isLoadMore: true,
            });

            if (res.data.item.page) {
              this.setData({
                total_page: res.data.item.page.total_page,
                current_page: res.data.item.page.current_page,
                bottomShowBolIndex: res.data.item.page.count - 1,
              });
            }


            // 小程序标题栏
            // if (res.data.item.page_info.preview_color) {
            // 	wx.setNavigationBarColor({
            // 		backgroundColor: res.data.item.page_info.preview_color,
            // 		frontColor:
            // 				res.data.item.page_info.font_color == 1
            // 						? "#ffffff"
            // 						: "#000000",
            // 		animation: {
            // 			duration: 400,
            // 			timingFunc: "easeIn",
            // 		},
            // 	});
            // }
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
        wx.hideLoading();
      },
    });
  },

  getMoreDecorate() {
    this.setData({
      decorate_isLoadMore: false,
    });

    if (this.data.current_page < this.data.total_page) {
      this.setData({
        current_page: this.data.current_page + 1,
      });
      let urlStr = app.getNetAddresss(
        "plugin.decorate.frotend.decorate-api.get-page"
      );
      app._postNetWork({
        url: urlStr,
        data: {
          decorate_page: this.data.current_page,
          decorate_id: this.data.page_id,
        },
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              decorate_isLoadMore: true,
              components: this.data.components.concat(res.data.datas),
            });
          } else {
            this.setData({
              decorate_isLoadMore: false,
              current_page: this.data.current_page - 1,
            });
            console.log(res.msg);
          }
        },
      });
    }
  },
});