// pages/life/life.js

const app=getApp();
Component({
  /**
 * 继承父级样式
 */
  options: {
    addGlobalClass: true,
  },

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: true,
    baseUrl: 'https://yongma16.xyz/api/',
    path: 'get-word/',
    articlePath:'article/blog/',
    content: 'loading...',
    togetherDate: (() => {
      const start = new Date('2022-02-20')
      const now = new Date()
      const diff = now.valueOf() - start.valueOf();//
      const onDay = 24 * 60 * 60 * 1000;
      const day = Math.ceil(diff / onDay);//天数
      const dayStr = `${day}天`
      return dayStr
    })(),
    autoTimer: null,
    article: '',
    config:{
        title:'个人信息'
    }
  },


  lifetimes: {
    // 生命周期函数，可以为函数，或一个在 methods 段中定义的方法名
    attached: function () {
      this.initArticle()
      const aboutPageConfig=app.wxProgramConfig.aboutPageConfig

      if (aboutPageConfig) {
       const title=aboutPageConfig.title
       this.setData({
            config:{
                title:title
            }
        })
      }

      
      // this.updateContent();
    },
    moved: function () { this.clearTimerFunc() },
    detached: function () {
      this.clearTimerFunc()
    },
  },

  // 生命周期函数，可以为函数，或一个在 methods 段中定义的方法名
  // attached: function () { }, // 此处 attached 的声明会被 lifetimes 字段中的声明覆盖
  // ready: function() { },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      console.log('pageLifetimes')
    },
    hide: function () { },
    resize: function () { },
  },

  methods: {
    initArticle() {
      const articleId=app.wxProgramConfig.aboutPageConfig.articleId
      this.getRemoteArticle(articleId)
    },

    clearTimerFunc: function () {
      //清除定时器
      if (this.data.autoTimer) {
        clearTimeout(this.data.autoTimer)
      }
    },
    updateContent: function () {
      const baseUrl = this.data.baseUrl
      const path = this.data.path
      const headers = { 'Content-Type': 'application/json;charset=UTF-8' }
      const that = this
      wx.request({
        url: baseUrl + path,
        headers: headers,
        method: 'GET',
        success: (res) => {
          const data = res.data
          const word = data.data
          that.setData(
            {
              content: word
            }
          )
        }
      });
      // that.setData({
      //   autoTimer:setTimeout(()=>{
      //     return that.updateContent()
      //   },8000)
      // })
    },
    getRemoteArticle(id){
      this.setData({
        isLoading:true
      })
      const baseUrl=this.data.baseUrl
      const path=this.data.articlePath
      const headers = {'Content-Type':'application/json;charset=UTF-8'}
      const that=this
      const params={
        id:id
      }
      wx.request({
        url: baseUrl+path,
        headers:headers,
        data:params,
        method:'POST',
        success: (res) => {
          console.log('res',res)
          const data=res.data
          const articleStr=data&&data.article&&data.article
          const markdownText = app.changeMrkdownText(articleStr)
          that.setData({
            isLoading:false,
            article:markdownText
          })
        }
      });
    },
    onMyButtonTap: function () {
      this.setData({
        // 更新属性和数据的方法与更新页面数据的方法类似
      })
    },
    // 内部方法建议以下划线开头
    _myPrivateMethod: function () {
      // 这里将 data.A[0].B 设为 'myPrivateData'
      // this.setData({
      //   'A[0].B': 'myPrivateData'
      // })
    },
    _propertyChange: function (newVal, oldVal) {

    }
  }
})