// pages/home/home.js
//获取应用实例
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    toView: '',
    title: 'header',
    userInfo: {},
    content: '我是父组件的content',
    footContent: "bottomContent",
    headBackground: [
    ],
    swiperConfig: {
      indicatorDots: true,
      headerContent: 'swiper',
      vertical: false,
      autoplay: false,
      interval: 5000,
      duration: 1000,
      indicatorColor: "rgba(255,255,255,.2)",
      indicatorActiveColor: "#dff9fb",
    },
    menuIsShow: false,
    bottomBtn: 'target',
    titleObj: [],
    baseUrl: 'https://yongma16.xyz/api/',
    path: 'article/index/',
    currentArticleId: 0,
    currentArticleTitle: '',
    isShowBottom: true,
    bottomStyleConfig: {
      tabMenuText: '记忆碎片',
      homePageText: '主页',
      blogText: '博客',
      apiText: 'openai',
      aboutText: '关于',
      imgsText:'图片',
      codeText:'code',
    }
  },
  // 监听
  watch: {
    bottomBtn: function (newVal, oldVal) {
      console.log('监听', newVal, oldVal)
    }
  },
  updateBottomNavigation(e) {
    console.log('val home', e)
    this.setData({
      isShowBottom: e.detail
    })
  },
  /**
   * 用户信息配置
   * @param {Object} userInfo
   */
  setCurrentUserInfo(userInfo) {
    console.log('事件触发userInfo', userInfo)
    this.setData({
      userInfo: userInfo
    })
  },
  /**
   * 获取文章
   */
  clickBlogTitleBtn(e) {
    const { id, title } = e.currentTarget.dataset
    this.setData({
      currentArticleId: id,
      currentArticleTitle: title
    })
    this.getRemoteArticleById(id)
  },
  initHomeComponent() {
    const articleObjId = this.data.headBackground[0].articleId
    this.updateHomeContent(articleObjId)
  },
  /**
   * 更新
   * @param {*} articleObjId
   */
  updateHomeContent(articleObjId) {
    this.triggerClearHomeContent()
    this.triggerGetHomeContent(articleObjId)
  },
  /**
   * 卡片变动修改内容
   * @param {*} e 
   */
  swiperChange(e) {
    const index = e.detail.current
    const articleObjId = this.data.headBackground[index].articleId
    this.updateHomeContent(articleObjId)
  },
  /**
   * 触发子组件跟新id
   */
  triggerGetHomeContent(id) {
    const homeComponent = this.selectComponent('#home-content-id')
    if (homeComponent && homeComponent.getRemoteArticle) {
      homeComponent.getRemoteArticle(id)
    }
  },

  /**
 * 触发子组件删除article
 */
  triggerClearHomeContent() {
    const homeComponent = this.selectComponent('#home-content-id')
    if (homeComponent && homeComponent.clearArticle) {
      homeComponent.clearArticle()
    }
  },

  /**
   * 获取文章
   * @param {*} id
   */
  getRemoteArticleById() {
    const paramId = this.data.currentArticleId;
    const blogComponent = this.selectComponent('#blog-id')
    blogComponent.setLoading(true)
    blogComponent.clearArticleNode()
    const baseUrl = this.data.baseUrl
    const path = this.data.path
    const that = this
    const headers = { 'Content-Type': 'application/json;charset=UTF-8' }
    const params = {
      value: paramId
    }
    wx.request({
      url: baseUrl + path,
      headers: headers,
      data: params,
      method: 'POST',
      success: (res) => {
        const data = res.data
        const articleStr = data && data.article[0]
        const title = that.data.currentArticleTitle
        blogComponent.setArticleNode(articleStr, title)
      }
    });
  },
  /**
   * 更新菜单
   * @param {Element} e
   */
  updateArticleMenu(e) {
    this.setData({
      titleObj: e.detail
    })
  },

  // 自定义函数
  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },

  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },



  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },

  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },

  // 关闭菜单
  menuClose() {
    this.data.menuIsShow && this.setData({
      menuIsShow: !this.data.menuIsShow
    })
  },
  menuSwitch() {
    this.setData({
      menuIsShow: !this.data.menuIsShow
    })
  },
  // 导航的按钮
  clickBottomBtn(e) {
    const label = e.currentTarget.dataset.label || this.data.bottomBtn
    this.setData({
      bottomBtn: label
    })
  },

  getRemoteData() {
    this.getRemoteArticleById()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const eventChannel = this.getOpenerEventChannel()
    const thisBack=this
    if (eventChannel.emit) {
      eventChannel.emit('acceptDataFromOpenedPage', { data: 'test' });
      eventChannel.emit('someEvent', { data: 'test' });
    }
    if (eventChannel.on) {
      // 监听 acceptDataFromOpenerPage 事件，获取上一页面通过 eventChannel 传送到当前页面的数据
      eventChannel.on('setHomePageData', function (data) {
        console.log('setHomePageData', data)
        thisBack.setData({
          isShowBottom:data.isShowBottom
        })
      })
    }
    this.setData({
      headBackground: app.wxProgramConfig.homePageConfig.headBackground,
      bottomBtn: app.wxProgramConfig.homePageConfig.bottomBtn,
      bottomStyleConfig: app.wxProgramConfig.homePageConfig.bottomStyleConfig
    })
    this.initHomeComponent();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})
